/app/assets/javascripts/models/search_result.js

https://bitbucket.org/schatt/chorus · JavaScript · 200 lines · 168 code · 32 blank · 0 comment · 31 complexity · 360fef0d167945cbf695231d3b53880a MD5 · raw file

  1. (function() {
  2. var collectionMap = {
  3. hdfs_entries: "HdfsEntrySet",
  4. datasets: "DatasetSet",
  5. workfiles: "WorkfileSet",
  6. workspaces: "WorkspaceSet",
  7. workspaceItems: "WorkspaceItemSet",
  8. instances: "InstanceSet",
  9. users: "UserSet",
  10. attachments: "AttachmentSet"
  11. };
  12. function makeCollectionMethod(methodName) {
  13. var constructorName = collectionMap[methodName];
  14. var collection, memoizedName = "_" + methodName;
  15. return function() {
  16. var ctor = chorus.collections.Search[constructorName];
  17. var searchKey = ctor.prototype.searchKey;
  18. if (!this[memoizedName]) {
  19. collection = this[memoizedName] = new ctor([], { search: this });
  20. collection.loaded = true;
  21. if (this.get(searchKey)){
  22. collection.refreshFromSearch();
  23. }
  24. }
  25. return this[memoizedName];
  26. };
  27. }
  28. chorus.models.SearchResult = chorus.models.Base.extend({
  29. constructorName: "SearchResult",
  30. numResultsPerPage: 50,
  31. initialize: function() {
  32. this.bind('invalidated', function() {
  33. this.selectedItem.trigger('invalidated');
  34. });
  35. },
  36. urlTemplate: function() {
  37. if (this.isScopedToSingleWorkspace()) {
  38. return "workspaces/{{workspaceId}}/search/";
  39. } else if (this.isScopedToUserWorkspaces()) {
  40. return "search/workspaces/";
  41. } else {
  42. return "search/";
  43. }
  44. },
  45. currentPageNumber: function() {
  46. return this.get("page") || 1;
  47. },
  48. showUrlTemplate: function() {
  49. var prefix = "",
  50. workspaceId = this.get("workspaceId");
  51. if (workspaceId) {
  52. prefix = "workspaces/" + workspaceId + "/";
  53. }
  54. if (this.get("isTag")) {
  55. prefix += "tags/";
  56. } else {
  57. prefix += "search/";
  58. }
  59. if (this.isConstrained()) {
  60. return prefix + this.searchIn() + "/" + this.entityType() + "/" + encodeURIComponent(this.get("query"));
  61. } else {
  62. return prefix + encodeURIComponent(this.get("query"));
  63. }
  64. },
  65. download: function(options) {
  66. this.selectedItem.download(options);
  67. },
  68. name: function() {
  69. return this.selectedItem.name();
  70. },
  71. isScoped: function() {
  72. return this.isScopedToSingleWorkspace() || this.isScopedToUserWorkspaces();
  73. },
  74. isConstrained: function() {
  75. return this.isScoped() || this.hasSpecificEntityType();
  76. },
  77. isScopedToSingleWorkspace: function() {
  78. return this.searchIn() === "this_workspace";
  79. },
  80. isScopedToUserWorkspaces: function() {
  81. return this.searchIn() === "my_workspaces";
  82. },
  83. isPaginated: function() {
  84. return this.hasSpecificEntityType() || this.isScopedToSingleWorkspace();
  85. },
  86. hasSpecificEntityType: function() {
  87. return this.entityType() && (this.entityType() !== "all");
  88. },
  89. entityType: function() {
  90. return this.get("entityType") || "all";
  91. },
  92. searchIn: function() {
  93. return this.get("searchIn") || "all";
  94. },
  95. urlParams: function() {
  96. var params = { query: this.get("query") };
  97. if(this.get("isTag")){
  98. params.tag = true;
  99. }
  100. if (this.hasSpecificEntityType()) {
  101. params.entityType = this.entityType();
  102. }
  103. if (this.has("workspaceId")) {
  104. params.workspaceId = this.get("workspaceId");
  105. }
  106. if (this.isPaginated()) {
  107. params.per_page = this.numResultsPerPage;
  108. params.page = this.currentPageNumber();
  109. } else {
  110. params.per_type = 3;
  111. }
  112. return params;
  113. },
  114. displayShortName: function(length) {
  115. length = length || 20;
  116. var name = this.get("query") || "";
  117. return (name.length < length) ? name : name.slice(0, length) + "...";
  118. },
  119. workspace: function() {
  120. var workspaceId = this.get("workspaceId");
  121. if (!this._workspace && workspaceId) {
  122. this._workspace = new chorus.models.Workspace({ id: workspaceId });
  123. }
  124. return this._workspace;
  125. },
  126. workfiles: makeCollectionMethod("workfiles"),
  127. datasets: makeCollectionMethod("datasets"),
  128. workspaces: makeCollectionMethod("workspaces"),
  129. instances: makeCollectionMethod("instances"),
  130. users: makeCollectionMethod("users"),
  131. hdfs_entries: makeCollectionMethod("hdfs_entries"),
  132. workspaceItems: makeCollectionMethod("workspaceItems"),
  133. attachments: makeCollectionMethod("attachments"),
  134. getResults: function() {
  135. if (this.isScopedToSingleWorkspace()) {
  136. return this.workspaceItems();
  137. }
  138. switch(this.entityType()) {
  139. case "user":
  140. return this.users();
  141. case "workspace":
  142. return this.workspaces();
  143. case "workfile":
  144. return this.workfiles();
  145. case "dataset":
  146. return this.datasets();
  147. case "instance":
  148. return this.instances();
  149. case "hdfs_entry":
  150. return this.hdfs_entries();
  151. case "attachment":
  152. return this.attachments();
  153. }
  154. },
  155. numPages: function(totalFound) {
  156. return Math.ceil(totalFound / this.numResultsPerPage);
  157. },
  158. total: function() {
  159. return _.reduce(_.values(this.attributes), function(sum, results) {
  160. if (results && results.numFound) {
  161. return sum + results.numFound;
  162. } else {
  163. return sum;
  164. }
  165. }, 0);
  166. }
  167. });
  168. })();