search.ko.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.isLoading = ko.observable(false);
  48. self.isLoadingImportables = ko.observable(false);
  49. self.allSelected = ko.observable(false);
  50. self.collections = ko.observableArray([]);
  51. self.filteredCollections = ko.observableArray(self.collections());
  52. self.importableCollections = ko.observableArray([]);
  53. self.importableCores = ko.observableArray([]);
  54. self.selectedCollections = ko.computed(function () {
  55. return ko.utils.arrayFilter(self.collections(), function (coll) {
  56. return coll.selected();
  57. });
  58. }, self);
  59. self.selectedImportableCollections = ko.computed(function () {
  60. return ko.utils.arrayFilter(self.importableCollections(), function (imp) {
  61. return imp.selected();
  62. });
  63. }, self);
  64. self.selectedImportableCores = ko.computed(function () {
  65. return ko.utils.arrayFilter(self.importableCores(), function (imp) {
  66. return imp.selected();
  67. });
  68. }, self);
  69. self.selectAll = function () {
  70. self.allSelected(!self.allSelected());
  71. ko.utils.arrayForEach(self.collections(), function (coll) {
  72. coll.selected(self.allSelected());
  73. });
  74. return true;
  75. };
  76. self.filterCollections = function (filter) {
  77. self.filteredCollections(ko.utils.arrayFilter(self.collections(), function (coll) {
  78. return coll.name().toLowerCase().indexOf(filter.toLowerCase()) > -1
  79. }));
  80. };
  81. self.deleteCollections = function () {
  82. var ids = [];
  83. $(self.selectedCollections()).each(function (index, coll) {
  84. ids.push(coll.id());
  85. });
  86. //callDelete(ids);
  87. };
  88. self.updateCollections = function () {
  89. $.getJSON(self.LIST_COLLECTIONS_URL, function (data) {
  90. self.collections(ko.utils.arrayMap(data, function (coll) {
  91. return new Collection(coll);
  92. }));
  93. self.filteredCollections(self.collections());
  94. $(document).trigger("collectionsRefreshed");
  95. });
  96. };
  97. self.updateImportables = function () {
  98. self.isLoadingImportables(true);
  99. $.getJSON(self.LIST_IMPORTABLES_URL, function (data) {
  100. self.importableCollections(ko.utils.arrayMap(data.newSolrCollections, function (coll) {
  101. return new Importable(coll);
  102. }));
  103. self.importableCores(ko.utils.arrayMap(data.newSolrCores, function (core) {
  104. return new Importable(core);
  105. }));
  106. self.isLoadingImportables(false);
  107. });
  108. };
  109. self.importCollectionsAndCores = function () {
  110. $(document).trigger("importing");
  111. var selected = [];
  112. ko.utils.arrayForEach(self.selectedImportableCollections(), function (imp) {
  113. selected.push({
  114. type: imp.type(),
  115. name: imp.name()
  116. });
  117. });
  118. ko.utils.arrayForEach(self.selectedImportableCores(), function (imp) {
  119. selected.push({
  120. type: imp.type(),
  121. name: imp.name()
  122. });
  123. });
  124. $.post(self.IMPORT_URL,
  125. {
  126. selected: ko.toJSON(selected)
  127. },
  128. function (data) {
  129. $(document).trigger("imported");
  130. self.updateCollections();
  131. }, "json");
  132. };
  133. };