0
0
Ответвление 0
arkiwi_boilerplate/app/scripts/arkiwi.js
2015-08-11 16:48:05 +02:00

174 строки
6 КиБ
JavaScript

/*------------------------------------------------------------------------------------------------------------------------
@package: arkiwiJsBoilerplate
@author: cek
@www: arkiwi.oeg
@copyright: COPYRIGHT 18 cek
@license: MIT
=============================================================================
Filename: arkiwi.js
=============================================================================
This file is the main entry point for js on the arkiwiJsBoilerplate app.
--------------------------------------------------------------------------------------------------------------------- */
var ARKIWI = ARKIWI || {};
ARKIWI.Arkiwi = function (endpoint, defaultParameters, formName, sessionUploadProgressName) {
this.endpoint = endpoint;
this.defaultParameters = defaultParameters || '';
this.formName = formName || 'arkiwiUploadForm';
this.sessionUploadProgressName = sessionUploadProgressName || 'PHP_SESSION_UPLOAD_PROGRESS';
this.sessionId = '';
if ($('#arkiwi_hidden_iframe').length == 0)
$(document.body).append('<iframe id="arkiwi_hidden_iframe" name="arkiwi_hidden_iframe" src="about:blank" style="display: none;"></iframe>');
};
ARKIWI.Arkiwi.constructor = ARKIWI.Arkiwi;
ARKIWI.Arkiwi.prototype.create = function (callbacks) {
$.ajax({
url: this.endpoint + '/create/?' + this.defaultParameters,
type: 'GET',
async: false,
cache: false,
context: this,
data: '',
dataType: 'json',
error: function (xhr, status, error) {
if (error != 'Unauthorized')
throw 'Arkiwi.create(): error ' + error;
else {
if (callbacks != undefined && callbacks.unauthorized != undefined)
callbacks.unauthorized();
}
},
success: function (result, status, xhr) {
this.sessionId = result.id;
if (callbacks != undefined && callbacks.success != undefined)
callbacks.success(this.sessionId);
},
complete: function (xhr, status) {},
});
};
ARKIWI.Arkiwi.prototype.upload = function (form, destinationFolderBase64, callbacks) {
form = $(form);
form.attr('method', 'POST');
form.attr('action', this.endpoint + '/upload/' + this.sessionId + '/' + destinationFolderBase64 + '/?' + this.defaultParameters);
form.attr('id', this.formName);
form.attr('enctype', 'multipart/form-data');
form.attr('target', 'arkiwi_hidden_iframe');
form.append('<input type="hidden" value="' + this.formName + '" name="' + this.sessionUploadProgressName + '" />');
$(':file', form).attr('name', this.sessionId);
form.ajaxForm({
beforeSend: function () {
callbacks.beforeSend();
},
uploadProgress: function (event, position, total, percentComplete) {
callbacks.uploadProgress(event, position, total, percentComplete);
},
complete: function (xhr) {
callbacks.complete(xhr);
},
error: function (xhr, status, error) {
throw 'Arkiwi.upload(): error ' + error;
}
});
form.submit();
};
ARKIWI.Arkiwi.prototype.metadata = function (jsonKVString, callback) {
$.ajax({
url: this.endpoint + '/metadata/' + this.sessionId + '/?' + this.defaultParameters,
type: 'POST',
async: true,
cache: false,
context: this,
data: jsonKVString,
dataType: 'json',
error: function (xhr, status, error) {
throw 'Arkiwi.metadata(): status ' + status + ' error ' + error;
},
success: function (result, status, xhr) {
if (callback != undefined)
callback(result);
},
complete: function (xhr, status) {}
});
};
ARKIWI.Arkiwi.prototype.removeMetadata = function (jsonKVString, callback) {
$.ajax({
url: this.endpoint + '/removemetadata/' + this.sessionId + '/?' + this.defaultParameters,
type: 'POST',
async: true,
cache: false,
context: this,
data: jsonKVString,
dataType: 'json',
error: function (xhr, status, error) {
throw 'Arkiwi.removeMetadata(): status ' + status + ' error ' + error;
},
success: function (result, status, xhr) {
if (callback != undefined)
callback(result);
},
complete: function (xhr, status) {}
});
};
ARKIWI.Arkiwi.prototype.close = function (callback) {
$.ajax({
url: this.endpoint + '/close/' + this.sessionId + '/?' + this.defaultParameters,
type: 'GET',
async: true,
cache: false,
context: this,
data: '',
dataType: 'json',
error: function (xhr, status, error) {
throw 'Arkiwi.close(): status ' + status + ' error ' + error;
},
success: function (result, status, xhr) {
if (callback != undefined)
callback(result);
},
complete: function (xhr, status) {}
});
};
ARKIWI.Arkiwi.prototype.path = function (path, callback, notBase64) {
var pathType = notBase64 ? 'path' : 'path64';
$.getJSON(this.endpoint + '/' + pathType + '/' + path + '/jsonml/?' + this.defaultParameters, function (result) {
if (callback != undefined)
callback(result);
}).fail(function (error) {
throw 'Arkiwi.path(): status ' + status + ' error ' + error.responseText;
});
};
ARKIWI.Arkiwi.prototype.search = function (query, callback, jailFolder) {
var sanitizedQuery = query;
if (jailFolder) {
sanitizedQuery = '+file:' + jailFolder + '* AND ' + query;
}
sanitizedQuery = window.btoa(sanitizedQuery); //convert to Base64
$.getJSON(this.endpoint + '/search64/' + sanitizedQuery + '/jsonml/?' + this.defaultParameters, function (result) {
if (callback != undefined)
callback(result);
}).fail(function (error) {
throw 'Arkiwi.search(): status ' + status + ' error ' + error.responseText;
});
};