arkiwi.js 6.6 KB

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