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

https://github.com/triAGENS/ArangoDB · JavaScript · 190 lines · 157 code · 21 blank · 12 comment · 25 complexity · 92aadac41ade0ebf0084773ed51518a1 MD5 · raw file

  1. /* jshint browser: true */
  2. /* jshint unused: false */
  3. /* global Backbone, window, arangoCollectionModel, $, arangoHelper, _ */
  4. (function () {
  5. 'use strict';
  6. window.ArangoCollections = Backbone.Collection.extend({
  7. url: arangoHelper.databaseUrl('/_api/collection'),
  8. model: arangoCollectionModel,
  9. searchOptions: {
  10. searchPhrase: null,
  11. includeSystem: false,
  12. includeDocument: true,
  13. includeEdge: true,
  14. sortBy: 'name',
  15. sortOrder: 1
  16. },
  17. translateStatus: function (status) {
  18. switch (status) {
  19. case 0:
  20. return 'corrupted';
  21. case 3:
  22. return 'loaded';
  23. case 5:
  24. return 'deleted';
  25. default:
  26. return 'unknown';
  27. }
  28. },
  29. translateTypePicture: function (type) {
  30. var returnString = '';
  31. switch (type) {
  32. case 'document':
  33. returnString += 'fa-file-text-o';
  34. break;
  35. case 'edge':
  36. returnString += 'fa-share-alt';
  37. break;
  38. case 'unknown':
  39. returnString += 'fa-question';
  40. break;
  41. default:
  42. returnString += 'fa-cogs';
  43. }
  44. return returnString;
  45. },
  46. parse: function (response) {
  47. var self = this;
  48. _.each(response.result, function (val) {
  49. val.isSystem = arangoHelper.isSystemCollection(val);
  50. val.type = arangoHelper.collectionType(val);
  51. val.status = self.translateStatus(val.status);
  52. val.picture = self.translateTypePicture(val.type);
  53. });
  54. return response.result;
  55. },
  56. getPosition: function (name) {
  57. var list = this.getFiltered(this.searchOptions); var i;
  58. var prev = null;
  59. var next = null;
  60. for (i = 0; i < list.length; ++i) {
  61. if (list[i].get('name') === name) {
  62. if (i > 0) {
  63. prev = list[i - 1];
  64. }
  65. if (i < list.length - 1) {
  66. next = list[i + 1];
  67. }
  68. }
  69. }
  70. return { prev: prev, next: next };
  71. },
  72. getFiltered: function (options) {
  73. var result = [];
  74. var searchPhrases = [];
  75. if (options.searchPhrase !== null) {
  76. var searchPhrase = options.searchPhrase.toLowerCase();
  77. // kick out whitespace
  78. searchPhrase = searchPhrase.replace(/\s+/g, ' ').replace(/(^\s+|\s+$)/g, '');
  79. searchPhrases = searchPhrase.split(' ');
  80. }
  81. this.models.forEach(function (model) {
  82. // search for name(s) entered
  83. if (searchPhrases.length > 0) {
  84. var lowerName = model.get('name').toLowerCase(); var i;
  85. // all phrases must match!
  86. for (i = 0; i < searchPhrases.length; ++i) {
  87. if (lowerName.indexOf(searchPhrases[i]) === -1) {
  88. // search phrase entered but current collection does not match?
  89. return;
  90. }
  91. }
  92. }
  93. if (options.includeSystem === false && model.get('isSystem')) {
  94. // system collection?
  95. return;
  96. }
  97. if (options.includeEdge === false && model.get('type') === 'edge') {
  98. return;
  99. }
  100. if (options.includeDocument === false && model.get('type') === 'document') {
  101. return;
  102. }
  103. result.push(model);
  104. });
  105. result.sort(function (l, r) {
  106. var lValue, rValue;
  107. if (options.sortBy === 'type') {
  108. // we'll use first type, then name as the sort criteria
  109. // this is because when sorting by type, we need a 2nd criterion (type is not unique)
  110. lValue = l.get('type') + ' ' + l.get('name').toLowerCase();
  111. rValue = r.get('type') + ' ' + r.get('name').toLowerCase();
  112. } else {
  113. lValue = l.get('name').toLowerCase();
  114. rValue = r.get('name').toLowerCase();
  115. }
  116. if (lValue !== rValue) {
  117. return options.sortOrder * (lValue < rValue ? -1 : 1);
  118. }
  119. return 0;
  120. });
  121. return result;
  122. },
  123. newCollection: function (object, callback) {
  124. var data = {};
  125. data.name = object.collName;
  126. data.waitForSync = object.wfs;
  127. data.isSystem = object.isSystem;
  128. data.type = parseInt(object.collType, 10);
  129. if (object.shards) {
  130. data.numberOfShards = object.shards;
  131. }
  132. data.shardKeys = object.shardKeys;
  133. if (object.smartJoinAttribute &&
  134. object.smartJoinAttribute !== '') {
  135. data.smartJoinAttribute = object.smartJoinAttribute;
  136. }
  137. data.distributeShardsLike = object.distributeShardsLike;
  138. var pattern = new RegExp(/^[0-9]+$/);
  139. if (object.replicationFactor) {
  140. data.replicationFactor = object.replicationFactor;
  141. if (pattern.test(object.replicationFactor)) {
  142. // looks like a number
  143. data.replicationFactor = JSON.parse(object.replicationFactor);
  144. }
  145. }
  146. if (object.writeConcern) {
  147. data.writeConcern = object.writeConcern;
  148. if (pattern.test(object.writeConcern)) {
  149. // looks like a number
  150. data.writeConcern = JSON.parse(object.writeConcern);
  151. }
  152. }
  153. $.ajax({
  154. cache: false,
  155. type: 'POST',
  156. url: arangoHelper.databaseUrl('/_api/collection'),
  157. data: JSON.stringify(data),
  158. contentType: 'application/json',
  159. processData: false,
  160. success: function (data) {
  161. callback(false, data);
  162. },
  163. error: function (data) {
  164. callback(true, data);
  165. }
  166. });
  167. }
  168. });
  169. }());