collections.ko.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. var Importable = function (importable) {
  17. var self = this;
  18. self.type = ko.observable(importable.type);
  19. self.name = ko.observable(importable.name);
  20. self.selected = ko.observable(false);
  21. self.handleSelect = function (row, e) {
  22. self.selected(! self.selected());
  23. };
  24. };
  25. var Collection = function (coll) {
  26. var self = this;
  27. self.id = ko.observable(coll.id);
  28. self.name = ko.observable(coll.name);
  29. self.label = ko.observable(coll.label);
  30. self.enabled = ko.observable(coll.enabled);
  31. self.isCoreOnly = ko.observable(coll.isCoreOnly);
  32. self.absoluteUrl = ko.observable(coll.absoluteUrl);
  33. self.selected = ko.observable(false);
  34. self.hovered = ko.observable(false);
  35. self.handleSelect = function (row, e) {
  36. self.selected(! self.selected());
  37. };
  38. self.toggleHover = function (row, e) {
  39. self.hovered(! self.hovered());
  40. };
  41. }
  42. var SearchCollectionsModel = function (props) {
  43. var cleanCollections,
  44. self = this;
  45. cleanCollections = function () {
  46. self.isLoading(true);
  47. self.collections.removeAll();
  48. self.filteredCollections.removeAll();
  49. };
  50. self.LABELS = props.labels;
  51. self.LIST_COLLECTIONS_URL = props.listCollectionsUrl;
  52. self.LIST_IMPORTABLES_URL = props.listImportablesUrl;
  53. self.IMPORT_URL = props.importUrl;
  54. self.DELETE_URL = props.deleteUrl;
  55. self.COPY_URL = props.copyUrl;
  56. self.INDEXER_URL = props.indexerUrl;
  57. self.isLoading = ko.observable(true);
  58. self.isLoadingImportables = ko.observable(false);
  59. self.allSelected = ko.observable(false);
  60. self.collections = ko.observableArray([]);
  61. self.filteredCollections = ko.observableArray(self.collections());
  62. self.importableCollections = ko.observableArray([]);
  63. self.importableCores = ko.observableArray([]);
  64. self.selectedCollections = ko.computed(function () {
  65. return ko.utils.arrayFilter(self.collections(), function (coll) {
  66. return coll.selected();
  67. });
  68. }, self);
  69. self.atLeastOneSelected = ko.computed(function() {
  70. return self.selectedCollections().length >= 1;
  71. });
  72. self.selectedImportableCollections = ko.computed(function () {
  73. return ko.utils.arrayFilter(self.importableCollections(), function (imp) {
  74. return imp.selected();
  75. });
  76. }, self);
  77. self.selectedImportableCores = ko.computed(function () {
  78. return ko.utils.arrayFilter(self.importableCores(), function (imp) {
  79. return imp.selected();
  80. });
  81. }, self);
  82. self.selectAll = function () {
  83. self.allSelected(!self.allSelected());
  84. ko.utils.arrayForEach(self.collections(), function (coll) {
  85. coll.selected(self.allSelected());
  86. });
  87. return true;
  88. };
  89. self.filterCollections = function (filter) {
  90. self.filteredCollections(ko.utils.arrayFilter(self.collections(), function (coll) {
  91. return coll.name().toLowerCase().indexOf(filter.toLowerCase()) > -1
  92. }));
  93. };
  94. self.editCollection = function (collection) {
  95. cleanCollections();
  96. location.href = collection.absoluteUrl();
  97. };
  98. self.editIndex = function (collection) {
  99. location.href = self.INDEXER_URL + collection.name();
  100. };
  101. self.markManyForDeletion = function (collections) {
  102. if (self.atLeastOneSelected()){
  103. self.collectionToDelete = collections;
  104. $(document).trigger("confirmDelete")
  105. }
  106. };
  107. self.deleteCollections = function () {
  108. if (self.atLeastOneSelected()){
  109. self.isLoading(true);
  110. $(document).trigger("deleting");
  111. $.post(self.DELETE_URL,
  112. {
  113. collections: ko.mapping.toJSON(self.selectedCollections())
  114. },
  115. function (data) {
  116. self.updateCollections();
  117. }, "json"
  118. ).fail(function (xhr, textStatus, errorThrown) {});
  119. $(document).trigger("collectionDeleted");
  120. }
  121. };
  122. self.copyCollections = function (collections) {
  123. if (self.atLeastOneSelected()){
  124. $(document).trigger("copying");
  125. $.post(self.COPY_URL,
  126. {
  127. collections: ko.mapping.toJSON(self.selectedCollections())
  128. }, function (data) {
  129. self.updateCollections();
  130. }, "json"
  131. ).fail(function (xhr, textStatus, errorThrown) {});
  132. $(document).trigger("collectionCopied");
  133. }
  134. };
  135. self.updateCollections = function () {
  136. self.isLoading(true);
  137. $.getJSON(self.LIST_COLLECTIONS_URL, function (data) {
  138. self.collections(ko.utils.arrayMap(data, function (coll) {
  139. return new Collection(coll);
  140. }));
  141. self.filteredCollections(self.collections());
  142. $(document).trigger("collectionsRefreshed");
  143. self.isLoading(false);
  144. });
  145. };
  146. self.updateImportables = function () {
  147. self.isLoadingImportables(true);
  148. $.getJSON(self.LIST_IMPORTABLES_URL, function (data) {
  149. self.importableCollections(ko.utils.arrayMap(data.newSolrCollections, function (coll) {
  150. return new Importable(coll);
  151. }));
  152. self.importableCores(ko.utils.arrayMap(data.newSolrCores, function (core) {
  153. return new Importable(core);
  154. }));
  155. self.isLoadingImportables(false);
  156. });
  157. };
  158. self.importCollectionsAndCores = function () {
  159. $(document).trigger("importing");
  160. var selected = [];
  161. ko.utils.arrayForEach(self.selectedImportableCollections(), function (imp) {
  162. selected.push({
  163. type: imp.type(),
  164. name: imp.name()
  165. });
  166. });
  167. ko.utils.arrayForEach(self.selectedImportableCores(), function (imp) {
  168. selected.push({
  169. type: imp.type(),
  170. name: imp.name()
  171. });
  172. });
  173. $.post(self.IMPORT_URL,
  174. {
  175. selected: ko.toJSON(selected)
  176. },
  177. function (data) {
  178. $(document).trigger("imported", data);
  179. self.updateCollections();
  180. }, "json");
  181. };
  182. self.toggleSelectAll = function() { // duplicated from hue/desktop/libs/indexer/static/js/collections.js
  183. var direction = ! self.selectedCollections().length;
  184. ko.utils.arrayForEach(self.filteredCollections(), function(collection) {
  185. collection.selected(direction);
  186. });
  187. };
  188. self.toggleCollectionSelect = function(collection, e) { // duplicated from hue/desktop/libs/indexer/static/js/collections.js
  189. ko.utils.arrayForEach(self.collections(), function(other_collection) {
  190. if(ko.unwrap(other_collection).id() == collection.id()) {
  191. other_collection.selected(! other_collection.selected());
  192. }
  193. });
  194. };
  195. };