collections.ko.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.selectedImportableCollections = ko.computed(function () {
  70. return ko.utils.arrayFilter(self.importableCollections(), function (imp) {
  71. return imp.selected();
  72. });
  73. }, self);
  74. self.selectedImportableCores = ko.computed(function () {
  75. return ko.utils.arrayFilter(self.importableCores(), function (imp) {
  76. return imp.selected();
  77. });
  78. }, self);
  79. self.selectAll = function () {
  80. self.allSelected(!self.allSelected());
  81. ko.utils.arrayForEach(self.collections(), function (coll) {
  82. coll.selected(self.allSelected());
  83. });
  84. return true;
  85. };
  86. self.filterCollections = function (filter) {
  87. self.filteredCollections(ko.utils.arrayFilter(self.collections(), function (coll) {
  88. return coll.name().toLowerCase().indexOf(filter.toLowerCase()) > -1
  89. }));
  90. };
  91. self.editCollection = function (collection) {
  92. cleanCollections();
  93. location.href = collection.absoluteUrl();
  94. };
  95. self.editIndex = function (collection) {
  96. location.href = self.INDEXER_URL + collection.name();
  97. };
  98. self.markManyForDeletion = function (collections) {
  99. self.collectionToDelete = collections;
  100. $(document).trigger("confirmDelete")
  101. };
  102. self.deleteCollections = function () {
  103. self.isLoading(true);
  104. $(document).trigger("deleting");
  105. $.post(self.DELETE_URL,
  106. {
  107. collections: ko.mapping.toJSON(self.selectedCollections())
  108. },
  109. function (data) {
  110. self.updateCollections();
  111. }, "json"
  112. ).fail(function (xhr, textStatus, errorThrown) {});
  113. $(document).trigger("collectionDeleted");
  114. };
  115. self.copyCollections = function (collections) {
  116. $(document).trigger("copying");
  117. $.post(self.COPY_URL,
  118. {
  119. collections: ko.mapping.toJSON(self.selectedCollections())
  120. }, function (data) {
  121. self.updateCollections();
  122. }, "json"
  123. ).fail(function (xhr, textStatus, errorThrown) {});
  124. $(document).trigger("collectionCopied");
  125. };
  126. self.updateCollections = function () {
  127. self.isLoading(true);
  128. $.getJSON(self.LIST_COLLECTIONS_URL, function (data) {
  129. self.collections(ko.utils.arrayMap(data, function (coll) {
  130. return new Collection(coll);
  131. }));
  132. self.filteredCollections(self.collections());
  133. $(document).trigger("collectionsRefreshed");
  134. self.isLoading(false);
  135. });
  136. };
  137. self.updateImportables = function () {
  138. self.isLoadingImportables(true);
  139. $.getJSON(self.LIST_IMPORTABLES_URL, function (data) {
  140. self.importableCollections(ko.utils.arrayMap(data.newSolrCollections, function (coll) {
  141. return new Importable(coll);
  142. }));
  143. self.importableCores(ko.utils.arrayMap(data.newSolrCores, function (core) {
  144. return new Importable(core);
  145. }));
  146. self.isLoadingImportables(false);
  147. });
  148. };
  149. self.importCollectionsAndCores = function () {
  150. $(document).trigger("importing");
  151. var selected = [];
  152. ko.utils.arrayForEach(self.selectedImportableCollections(), function (imp) {
  153. selected.push({
  154. type: imp.type(),
  155. name: imp.name()
  156. });
  157. });
  158. ko.utils.arrayForEach(self.selectedImportableCores(), function (imp) {
  159. selected.push({
  160. type: imp.type(),
  161. name: imp.name()
  162. });
  163. });
  164. $.post(self.IMPORT_URL,
  165. {
  166. selected: ko.toJSON(selected)
  167. },
  168. function (data) {
  169. $(document).trigger("imported", data);
  170. self.updateCollections();
  171. }, "json");
  172. };
  173. self.toggleSelectAll = function() { // duplicated from hue/desktop/libs/indexer/static/js/collections.js
  174. var direction = ! self.selectedCollections().length;
  175. ko.utils.arrayForEach(self.filteredCollections(), function(collection) {
  176. collection.selected(direction);
  177. });
  178. };
  179. self.toggleCollectionSelect = function(collection, e) { // duplicated from hue/desktop/libs/indexer/static/js/collections.js
  180. ko.utils.arrayForEach(self.collections(), function(other_collection) {
  181. if(ko.unwrap(other_collection).id() == collection.id()) {
  182. other_collection.selected(! other_collection.selected());
  183. }
  184. });
  185. };
  186. };