search.ko.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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(false);
  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. $.getJSON(self.LIST_COLLECTIONS_URL, function (data) {
  113. self.collections(ko.utils.arrayMap(data, function (coll) {
  114. return new Collection(coll);
  115. }));
  116. self.filteredCollections(self.collections());
  117. $(document).trigger("collectionsRefreshed");
  118. });
  119. };
  120. self.updateImportables = function () {
  121. self.isLoadingImportables(true);
  122. $.getJSON(self.LIST_IMPORTABLES_URL, function (data) {
  123. self.importableCollections(ko.utils.arrayMap(data.newSolrCollections, function (coll) {
  124. return new Importable(coll);
  125. }));
  126. self.importableCores(ko.utils.arrayMap(data.newSolrCores, function (core) {
  127. return new Importable(core);
  128. }));
  129. self.isLoadingImportables(false);
  130. });
  131. };
  132. self.importCollectionsAndCores = function () {
  133. $(document).trigger("importing");
  134. var selected = [];
  135. ko.utils.arrayForEach(self.selectedImportableCollections(), function (imp) {
  136. selected.push({
  137. type: imp.type(),
  138. name: imp.name()
  139. });
  140. });
  141. ko.utils.arrayForEach(self.selectedImportableCores(), function (imp) {
  142. selected.push({
  143. type: imp.type(),
  144. name: imp.name()
  145. });
  146. });
  147. $.post(self.IMPORT_URL,
  148. {
  149. selected: ko.toJSON(selected)
  150. },
  151. function (data) {
  152. $(document).trigger("imported");
  153. self.updateCollections();
  154. }, "json");
  155. };
  156. };