/js/apps/system/_admin/aardvark/APP/frontend/js/collections/arangoQueries.js

https://github.com/triAGENS/ArangoDB · JavaScript · 204 lines · 173 code · 24 blank · 7 comment · 30 complexity · 10f3d5f2a194ee6e6a277cade91b3dcf MD5 · raw file

  1. /* jshint browser: true */
  2. /* jshint unused: false */
  3. /* global Backbone, _, FileReader, sessionStorage, frontendConfig, window, ArangoQuery, $, arangoHelper */
  4. (function () {
  5. 'use strict';
  6. window.ArangoQueries = Backbone.Collection.extend({
  7. getQueryPath: function () {
  8. return frontendConfig.db + '-' + this.username + '-queries';
  9. },
  10. initialize: function (models, options) {
  11. var self = this;
  12. $.ajax('whoAmI?_=' + Date.now(), {async: true}).done(
  13. function (data) {
  14. if (this.activeUser === false || this.activeUser === null) {
  15. self.activeUser = 'root';
  16. } else {
  17. self.activeUser = data.user;
  18. }
  19. }
  20. );
  21. },
  22. fetch: function (options) {
  23. options = _.extend({parse: true}, options);
  24. var model = this;
  25. var success = options.success;
  26. if (frontendConfig.ldapEnabled) {
  27. this.fetchLocalQueries();
  28. options.success = (function (resp) {
  29. // if success function available, call it
  30. if (success) {
  31. success.call(options.context, model, resp, options);
  32. }
  33. })();
  34. } else {
  35. if (frontendConfig.authenticationEnabled && window.App.currentUser) {
  36. this.url = arangoHelper.databaseUrl('/_api/user/' + encodeURIComponent(window.App.currentUser));
  37. } else {
  38. this.url = arangoHelper.databaseUrl('/_api/user/');
  39. }
  40. return Backbone.Collection.prototype.fetch.call(this, options);
  41. }
  42. },
  43. fetchLocalQueries: function () {
  44. // remove local available queries
  45. this.reset();
  46. var self = this;
  47. // fetch and add queries
  48. var item = sessionStorage.getItem(this.getQueryPath());
  49. try {
  50. item = JSON.parse(item);
  51. _.each(item, function (val, key) {
  52. self.add(val);
  53. });
  54. } catch (ignore) {
  55. }
  56. },
  57. url: arangoHelper.databaseUrl('/_api/user/'),
  58. model: ArangoQuery,
  59. activeUser: null,
  60. parse: function (response) {
  61. var self = this; var toReturn;
  62. if (this.activeUser === false || this.activeUser === null) {
  63. this.activeUser = 'root';
  64. }
  65. if (response.user === self.activeUser) {
  66. try {
  67. if (response.extra.queries) {
  68. toReturn = response.extra.queries;
  69. }
  70. } catch (e) {}
  71. } else {
  72. if (response.result && response.result instanceof Array) {
  73. _.each(response.result, function (userobj) {
  74. if (userobj.user === self.activeUser) {
  75. toReturn = userobj.extra.queries;
  76. }
  77. });
  78. }
  79. }
  80. return toReturn;
  81. },
  82. saveLocalCollectionQueries: function (data, callbackFunc) {
  83. sessionStorage.setItem(this.getQueryPath(), JSON.stringify(data));
  84. if (callbackFunc) {
  85. callbackFunc(false, data);
  86. }
  87. },
  88. saveCollectionQueries: function (callbackFunc) {
  89. var queries = [];
  90. this.each(function (query) {
  91. queries.push({
  92. value: query.attributes.value,
  93. parameter: query.attributes.parameter,
  94. name: query.attributes.name
  95. });
  96. });
  97. if (frontendConfig.ldapEnabled) {
  98. this.saveLocalCollectionQueries(queries, callbackFunc);
  99. } else {
  100. if (this.activeUser === false || this.activeUser === null) {
  101. this.activeUser = 'root';
  102. }
  103. // save current collection
  104. $.ajax({
  105. cache: false,
  106. type: 'PATCH',
  107. url: arangoHelper.databaseUrl('/_api/user/' + encodeURIComponent(this.activeUser)),
  108. data: JSON.stringify({
  109. extra: {
  110. queries: queries
  111. }
  112. }),
  113. contentType: 'application/json',
  114. processData: false,
  115. success: function (data) {
  116. callbackFunc(false, data);
  117. },
  118. error: function () {
  119. callbackFunc(true);
  120. }
  121. });
  122. }
  123. },
  124. downloadLocalQueries: function () {
  125. arangoHelper.downloadLocalBlob(JSON.stringify(this.toJSON()), 'json');
  126. },
  127. saveImportQueries: function (file, callback) {
  128. var self = this;
  129. if (this.activeUser === 0) {
  130. return false;
  131. }
  132. if (frontendConfig.ldapEnabled) {
  133. if (file) {
  134. var reader = new FileReader();
  135. reader.readAsText(file, 'UTF-8');
  136. reader.onload = function (evt) {
  137. try {
  138. var obj = JSON.parse(evt.target.result);
  139. _.each(obj, function (val, key) {
  140. if (val.name && val.value && val.parameter) {
  141. self.add(val);
  142. }
  143. });
  144. self.saveCollectionQueries();
  145. if (callback) {
  146. callback();
  147. }
  148. } catch (e) {
  149. arangoHelper.arangoError('Query error', 'Queries could not be imported.');
  150. window.modalView.hide();
  151. }
  152. };
  153. reader.onerror = function (evt) {
  154. window.modalView.hide();
  155. arangoHelper.arangoError('Query error', 'Queries could not be imported.');
  156. };
  157. }
  158. } else {
  159. window.progressView.show('Fetching documents...');
  160. $.ajax({
  161. cache: false,
  162. type: 'POST',
  163. url: 'query/upload/' + encodeURIComponent(this.activeUser),
  164. data: file,
  165. contentType: 'application/json',
  166. processData: false,
  167. success: function () {
  168. window.progressView.hide();
  169. arangoHelper.arangoNotification('Queries successfully imported.');
  170. callback();
  171. },
  172. error: function () {
  173. window.progressView.hide();
  174. arangoHelper.arangoError('Query error', 'queries could not be imported');
  175. }
  176. });
  177. }
  178. }
  179. });
  180. }());