arkiwi.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 (aggregator, assetStore, defaultParameters, formName, sessionUploadProgressName) {
  14. this.assetStore = assetStore;
  15. this.aggregator = aggregator;
  16. this.defaultParameters = defaultParameters || '';
  17. this.formName = formName || 'arkiwiUploadForm';
  18. this.sessionUploadProgressName = sessionUploadProgressName || 'PHP_SESSION_UPLOAD_PROGRESS';
  19. this.sessionId = '';
  20. if ($('#arkiwi_hidden_iframe').length == 0)
  21. $(document.body).append('<iframe id="arkiwi_hidden_iframe" name="arkiwi_hidden_iframe" src="about:blank" style="visibility: hidden;"></iframe>');
  22. };
  23. ARKIWI.Arkiwi.constructor = ARKIWI.Arkiwi;
  24. /* Inizializza la sessione di dialogo con l'asset store. */
  25. ARKIWI.Arkiwi.prototype.create = function (callbacks) {
  26. $.ajax({
  27. url: this.assetStore + '/create/?' + this.defaultParameters,
  28. type: 'GET',
  29. async: false,
  30. cache: false,
  31. context: this,
  32. data: '',
  33. dataType: 'json',
  34. error: function (xhr, status, error) {
  35. if (error != 'Unauthorized')
  36. throw 'Arkiwi.create(): error ' + error;
  37. else {
  38. if (callbacks != undefined && callbacks.unauthorized != undefined)
  39. callbacks.unauthorized();
  40. }
  41. },
  42. success: function (result, status, xhr) {
  43. this.sessionId = result.id;
  44. if (callbacks != undefined && callbacks.success != undefined)
  45. callbacks.success(this.sessionId);
  46. },
  47. complete: function (xhr, status) {},
  48. });
  49. };
  50. /* Invia un file all'asset store. */
  51. ARKIWI.Arkiwi.prototype.upload = function (form, destinationFolderBase64, callbacks) {
  52. form = $(form);
  53. form.attr('method', 'POST');
  54. form.attr('action', this.assetStore + '/upload/' + this.sessionId + '/' + destinationFolderBase64 + '/?' + this.defaultParameters);
  55. form.attr('id', this.formName);
  56. form.attr('enctype', 'multipart/form-data');
  57. form.attr('target', 'arkiwi_hidden_iframe');
  58. form.append('<input type="hidden" value="' + this.formName + '" name="' + this.sessionUploadProgressName + '" />');
  59. $(':file', form).attr('name', this.sessionId);
  60. form.ajaxForm({
  61. beforeSend: function () {
  62. callbacks.beforeSend();
  63. },
  64. uploadProgress: function (event, position, total, percentComplete) {
  65. callbacks.uploadProgress(event, position, total, percentComplete);
  66. },
  67. complete: function (xhr) {
  68. callbacks.complete(xhr);
  69. },
  70. error: function (xhr, status, error) {
  71. throw 'Arkiwi.upload(): error ' + error;
  72. }
  73. });
  74. form.submit();
  75. };
  76. /* Aggiunge o sovrascrive i metadati di un file. */
  77. ARKIWI.Arkiwi.prototype.metadata = function (jsonKVString, callback) {
  78. $.ajax({
  79. url: this.assetStore + '/metadata/' + this.sessionId + '/?' + this.defaultParameters,
  80. type: 'POST',
  81. async: true,
  82. cache: false,
  83. context: this,
  84. data: jsonKVString,
  85. dataType: 'json',
  86. error: function (xhr, status, error) {
  87. throw 'Arkiwi.metadata(): status ' + status + ' error ' + error;
  88. },
  89. success: function (result, status, xhr) {
  90. if (callback != undefined)
  91. callback(result);
  92. },
  93. complete: function (xhr, status) {}
  94. });
  95. };
  96. /* Cancella un metadato da un file */
  97. ARKIWI.Arkiwi.prototype.removeMetadata = function (jsonKVString, callback) {
  98. $.ajax({
  99. url: this.assetStore + '/removemetadata/' + this.sessionId + '/?' + this.defaultParameters,
  100. type: 'POST',
  101. async: true,
  102. cache: false,
  103. context: this,
  104. data: jsonKVString,
  105. dataType: 'json',
  106. error: function (xhr, status, error) {
  107. throw 'Arkiwi.removeMetadata(): status ' + status + ' error ' + error;
  108. },
  109. success: function (result, status, xhr) {
  110. if (callback != undefined)
  111. callback(result);
  112. },
  113. complete: function (xhr, status) {}
  114. });
  115. };
  116. /* Chiude la sessione con l'asset store */
  117. ARKIWI.Arkiwi.prototype.close = function (callback) {
  118. $.ajax({
  119. url: this.assetStore + '/close/' + this.sessionId + '/?' + this.defaultParameters,
  120. type: 'GET',
  121. async: true,
  122. cache: false,
  123. context: this,
  124. data: '',
  125. dataType: 'json',
  126. error: function (xhr, status, error) {
  127. throw 'Arkiwi.close(): status ' + status + ' error ' + error;
  128. },
  129. success: function (result, status, xhr) {
  130. if (callback != undefined)
  131. callback(result);
  132. },
  133. complete: function (xhr, status) {}
  134. });
  135. };
  136. /* Chiede all'aggregatore i metadati su una cartella o un file. */
  137. ARKIWI.Arkiwi.prototype.path = function (path, callback, notBase64) {
  138. var pathType = notBase64 ? 'path' : 'path64';
  139. $.getJSON(this.aggregator + '/' + pathType + '/' + path + '/json/?' + this.defaultParameters, function (result) {
  140. if (callback != undefined)
  141. callback(result);
  142. }).fail(function (error) {
  143. throw 'Arkiwi.path(): status ' + status + ' error ' + error.responseText;
  144. });
  145. };
  146. /* Esegue un ricerca sull'aggregatore */
  147. ARKIWI.Arkiwi.prototype.search = function (query, callback, jailFolder) {
  148. var sanitizedQuery = query;
  149. if (jailFolder) {
  150. sanitizedQuery = '+file:' + jailFolder + '* AND ' + query;
  151. }
  152. sanitizedQuery = window.btoa(sanitizedQuery); //convert to Base64
  153. $.getJSON(this.aggregator + '/search64/' + sanitizedQuery + '/json/?' + this.defaultParameters, function (result) {
  154. if (callback != undefined)
  155. callback(result);
  156. }).fail(function (error) {
  157. throw 'Arkiwi.search(): status ' + status + ' error ' + error.responseText;
  158. });
  159. };