search.ko.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. this.selected(!this.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.isCoreOnly = ko.observable(coll.isCoreOnly);
  31. self.absoluteUrl = ko.observable(coll.absoluteUrl);
  32. self.selected = ko.observable(false);
  33. self.hovered = ko.observable(false);
  34. self.handleSelect = function (row, e) {
  35. this.selected(!this.selected());
  36. };
  37. self.toggleHover = function (row, e) {
  38. this.hovered(!this.hovered());
  39. };
  40. }
  41. var SearchCollectionsModel = function (props) {
  42. var self = this;
  43. self.LABELS = props.labels;
  44. self.LIST_COLLECTIONS_URL = props.listCollectionsUrl;
  45. self.LIST_IMPORTABLES_URL = props.listImportablesUrl;
  46. self.IMPORT_URL = props.importUrl;
  47. self.DELETE_URL = props.deleteUrl;
  48. self.COPY_URL = props.copyUrl;
  49. self.isLoading = ko.observable(true);
  50. self.isLoadingImportables = ko.observable(false);
  51. self.allSelected = ko.observable(false);
  52. self.collections = ko.observableArray([]);
  53. self.filteredCollections = ko.observableArray(self.collections());
  54. self.importableCollections = ko.observableArray([]);
  55. self.importableCores = ko.observableArray([]);
  56. self.collectionToDelete = null;
  57. self.selectedCollections = ko.computed(function () {
  58. return ko.utils.arrayFilter(self.collections(), function (coll) {
  59. return coll.selected();
  60. });
  61. }, self);
  62. self.selectedImportableCollections = ko.computed(function () {
  63. return ko.utils.arrayFilter(self.importableCollections(), function (imp) {
  64. return imp.selected();
  65. });
  66. }, self);
  67. self.selectedImportableCores = ko.computed(function () {
  68. return ko.utils.arrayFilter(self.importableCores(), function (imp) {
  69. return imp.selected();
  70. });
  71. }, self);
  72. self.selectAll = function () {
  73. self.allSelected(!self.allSelected());
  74. ko.utils.arrayForEach(self.collections(), function (coll) {
  75. coll.selected(self.allSelected());
  76. });
  77. return true;
  78. };
  79. self.filterCollections = function (filter) {
  80. self.filteredCollections(ko.utils.arrayFilter(self.collections(), function (coll) {
  81. return coll.name().toLowerCase().indexOf(filter.toLowerCase()) > -1
  82. }));
  83. };
  84. self.markForDeletion = function (collection) {
  85. self.collectionToDelete = collection;
  86. $(document).trigger("confirmDelete");
  87. };
  88. self.deleteCollection = function () {
  89. $(document).trigger("deleting");
  90. $.post(self.DELETE_URL,
  91. {
  92. id: self.collectionToDelete.id()
  93. },
  94. function (data) {
  95. self.updateCollections();
  96. $(document).trigger("collectionDeleted");
  97. }, "json");
  98. };
  99. self.copyCollection = function (collection) {
  100. $(document).trigger("copying");
  101. $.post(self.COPY_URL,
  102. {
  103. id: collection.id(),
  104. type: collection.isCoreOnly()?"core":"collection"
  105. },
  106. function (data) {
  107. self.updateCollections();
  108. $(document).trigger("collectionCopied");
  109. }, "json");
  110. };
  111. self.updateCollections = function () {
  112. self.isLoading(true);
  113. $.getJSON(self.LIST_COLLECTIONS_URL, function (data) {
  114. self.collections(ko.utils.arrayMap(data, function (coll) {
  115. return new Collection(coll);
  116. }));
  117. self.filteredCollections(self.collections());
  118. $(document).trigger("collectionsRefreshed");
  119. self.isLoading(false);
  120. });
  121. };
  122. self.updateImportables = function () {
  123. self.isLoadingImportables(true);
  124. $.getJSON(self.LIST_IMPORTABLES_URL, function (data) {
  125. self.importableCollections(ko.utils.arrayMap(data.newSolrCollections, function (coll) {
  126. return new Importable(coll);
  127. }));
  128. self.importableCores(ko.utils.arrayMap(data.newSolrCores, function (core) {
  129. return new Importable(core);
  130. }));
  131. self.isLoadingImportables(false);
  132. });
  133. };
  134. self.importCollectionsAndCores = function () {
  135. $(document).trigger("importing");
  136. var selected = [];
  137. ko.utils.arrayForEach(self.selectedImportableCollections(), function (imp) {
  138. selected.push({
  139. type: imp.type(),
  140. name: imp.name()
  141. });
  142. });
  143. ko.utils.arrayForEach(self.selectedImportableCores(), function (imp) {
  144. selected.push({
  145. type: imp.type(),
  146. name: imp.name()
  147. });
  148. });
  149. $.post(self.IMPORT_URL,
  150. {
  151. selected: ko.toJSON(selected)
  152. },
  153. function (data) {
  154. $(document).trigger("imported");
  155. self.updateCollections();
  156. }, "json");
  157. };
  158. };