arkiwi.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*------------------------------------------------------------------------------------------------------------------------
  2. @package: arkiwiJsBoilerplate
  3. @author: cek
  4. @www: arkiwi.oeg
  5. @copyright: COPYRIGHT 18 cek
  6. @license: MIT
  7. =============================================================================
  8. Filename: arkiwi.js
  9. =============================================================================
  10. This file is the main entry point for js on the arkiwiJsBoilerplate app.
  11. --------------------------------------------------------------------------------------------------------------------- */
  12. var ARKIWI = ARKIWI || {};
  13. ARKIWI.Arkiwi = function (endpoint, defaultParameters, formName, sessionUploadProgressName) {
  14. this.endpoint = endpoint;
  15. this.defaultParameters = defaultParameters || '';
  16. this.formName = formName || 'arkiwiUploadForm';
  17. this.sessionUploadProgressName = sessionUploadProgressName || 'PHP_SESSION_UPLOAD_PROGRESS';
  18. this.sessionId = '';
  19. if ($('#arkiwi_hidden_iframe').length == 0)
  20. $(document.body).append('<iframe id="arkiwi_hidden_iframe" name="arkiwi_hidden_iframe" src="about:blank" style="display: none;"></iframe>');
  21. };
  22. ARKIWI.Arkiwi.constructor = ARKIWI.Arkiwi;
  23. ARKIWI.Arkiwi.prototype.create = function (callbacks) {
  24. $.ajax({
  25. url: this.endpoint + '/create/?' + this.defaultParameters,
  26. type: 'GET',
  27. async: false,
  28. cache: false,
  29. context: this,
  30. data: '',
  31. dataType: 'json',
  32. error: function (xhr, status, error) {
  33. if (error != 'Unauthorized')
  34. throw 'Arkiwi.create(): error ' + error;
  35. else {
  36. if (callbacks != undefined && callbacks.unauthorized != undefined)
  37. callbacks.unauthorized();
  38. }
  39. },
  40. success: function (result, status, xhr) {
  41. this.sessionId = result.id;
  42. if (callbacks != undefined && callbacks.success != undefined)
  43. callbacks.success(this.sessionId);
  44. },
  45. complete: function (xhr, status) {},
  46. });
  47. };
  48. ARKIWI.Arkiwi.prototype.upload = function (form, destinationFolderBase64, callbacks) {
  49. form = $(form);
  50. form.attr('method', 'POST');
  51. form.attr('action', this.endpoint + '/upload/' + this.sessionId + '/' + destinationFolderBase64 + '/?' + this.defaultParameters);
  52. form.attr('id', this.formName);
  53. form.attr('enctype', 'multipart/form-data');
  54. form.attr('target', 'arkiwi_hidden_iframe');
  55. form.append('<input type="hidden" value="' + this.formName + '" name="' + this.sessionUploadProgressName + '" />');
  56. $(':file', form).attr('name', this.sessionId);
  57. form.ajaxForm({
  58. beforeSend: function () {
  59. callbacks.beforeSend();
  60. },
  61. uploadProgress: function (event, position, total, percentComplete) {
  62. callbacks.uploadProgress(event, position, total, percentComplete);
  63. },
  64. complete: function (xhr) {
  65. callbacks.complete(xhr);
  66. },
  67. error: function (xhr, status, error) {
  68. throw 'Arkiwi.upload(): error ' + error;
  69. }
  70. });
  71. form.submit();
  72. };
  73. ARKIWI.Arkiwi.prototype.metadata = function (jsonKVString, callback) {
  74. $.ajax({
  75. url: this.endpoint + '/metadata/' + this.sessionId + '/?' + this.defaultParameters,
  76. type: 'POST',
  77. async: true,
  78. cache: false,
  79. context: this,
  80. data: jsonKVString,
  81. dataType: 'json',
  82. error: function (xhr, status, error) {
  83. throw 'Arkiwi.metadata(): status ' + status + ' error ' + error;
  84. },
  85. success: function (result, status, xhr) {
  86. if (callback != undefined)
  87. callback(result);
  88. },
  89. complete: function (xhr, status) {}
  90. });
  91. };
  92. ARKIWI.Arkiwi.prototype.removeMetadata = function (jsonKVString, callback) {
  93. $.ajax({
  94. url: this.endpoint + '/removemetadata/' + this.sessionId + '/?' + this.defaultParameters,
  95. type: 'POST',
  96. async: true,
  97. cache: false,
  98. context: this,
  99. data: jsonKVString,
  100. dataType: 'json',
  101. error: function (xhr, status, error) {
  102. throw 'Arkiwi.removeMetadata(): status ' + status + ' error ' + error;
  103. },
  104. success: function (result, status, xhr) {
  105. if (callback != undefined)
  106. callback(result);
  107. },
  108. complete: function (xhr, status) {}
  109. });
  110. };
  111. ARKIWI.Arkiwi.prototype.close = function (callback) {
  112. $.ajax({
  113. url: this.endpoint + '/close/' + this.sessionId + '/?' + this.defaultParameters,
  114. type: 'GET',
  115. async: true,
  116. cache: false,
  117. context: this,
  118. data: '',
  119. dataType: 'json',
  120. error: function (xhr, status, error) {
  121. throw 'Arkiwi.close(): status ' + status + ' error ' + error;
  122. },
  123. success: function (result, status, xhr) {
  124. if (callback != undefined)
  125. callback(result);
  126. },
  127. complete: function (xhr, status) {}
  128. });
  129. };
  130. ARKIWI.Arkiwi.prototype.path = function (path, callback, notBase64) {
  131. var pathType = notBase64 ? 'path' : 'path64';
  132. $.getJSON(this.endpoint + '/' + pathType + '/' + path + '/jsonml/?' + this.defaultParameters, function (result) {
  133. if (callback != undefined)
  134. callback(result);
  135. }).fail(function (error) {
  136. throw 'Arkiwi.path(): status ' + status + ' error ' + error.responseText;
  137. });
  138. };
  139. ARKIWI.Arkiwi.prototype.search = function (query, callback, jailFolder) {
  140. var sanitizedQuery = query;
  141. if (jailFolder) {
  142. sanitizedQuery = '+file:' + jailFolder + '* AND ' + query;
  143. }
  144. sanitizedQuery = window.btoa(sanitizedQuery); //convert to Base64
  145. $.getJSON(this.endpoint + '/search64/' + sanitizedQuery + '/jsonml/?' + this.defaultParameters, function (result) {
  146. if (callback != undefined)
  147. callback(result);
  148. }).fail(function (error) {
  149. throw 'Arkiwi.search(): status ' + status + ' error ' + error.responseText;
  150. });
  151. };