search.ko.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Query = function (vm, query) {
  17. var self = this;
  18. self.q = ko.observable(query.q);
  19. self.fq = query.fq
  20. self.selectFacet = function(facet_json) {
  21. self.fq[facet_json.cat] = facet_json.value;
  22. vm.search();
  23. }
  24. self.unselectFacet = function(facet_json) {
  25. delete self.fq[facet_json.cat];
  26. vm.search();
  27. }
  28. };
  29. var FieldFacet = function(vm, props) {
  30. self.id = props.id;
  31. self.label = props.name;
  32. self.field = props.name;
  33. self.type = "field";
  34. }
  35. // FieldListFacet
  36. // RangeFacet
  37. var Collection = function (vm, collection) {
  38. var self = this;
  39. self.id = collection.id;
  40. self.name = collection.name;
  41. self.template = ko.mapping.fromJS(collection.template);
  42. self.template.fields.subscribe(function() {
  43. vm.search();
  44. });
  45. self.template.template.subscribe(function() {
  46. vm.search();
  47. });
  48. self.facets = ko.mapping.fromJS(collection.facets);
  49. self.fields = ko.observableArray(collection.fields);
  50. self.addFacet = function(facet_json) {
  51. self.facets.push(ko.mapping.fromJS({
  52. "uuid": "f6618a5c-bbba-2886-1886-bbcaf01409ca",
  53. "verbatim": "", "isVerbatim": false, "label": facet_json.name,
  54. "field": facet_json.name, "type": "field"
  55. }));
  56. }
  57. self.addDynamicFields = function() {
  58. $.post("/search/index/" + self.id + "/fields/dynamic", {
  59. }, function (data){
  60. if (data.status == 0) {
  61. $.each(data.dynamic_fields, function(index, field) {
  62. self.fields.push(field);
  63. });
  64. }
  65. }).fail(function(xhr, textStatus, errorThrown) {}
  66. );
  67. }
  68. // Init
  69. self.addDynamicFields();
  70. };
  71. var SearchViewModel = function (collection_json, query_json) {
  72. var self = this;
  73. // Models
  74. self.collection = new Collection(self, collection_json);
  75. self.query = new Query(self, query_json);
  76. // UI
  77. self.response = ko.observable({});
  78. self.results = ko.observableArray([]);
  79. self.norm_facets = ko.computed(function () {
  80. return self.response().normalized_facets;
  81. });
  82. self.selectedFacet = ko.observable();
  83. self.search = function () {
  84. $(".jHueNotify").hide();
  85. $.post("/search/search", {
  86. collection: ko.mapping.toJSON(self.collection),
  87. query: ko.mapping.toJSON(self.query),
  88. }, function (data) {
  89. self.response(data); // If error we should probably update only the facets
  90. self.results.removeAll();
  91. if (data.error) {
  92. $(document).trigger("error", data.error);
  93. } else {
  94. if (self.collection.template.isGridLayout()) {
  95. // Table view
  96. $.each(data.response.docs, function (index, item) {
  97. var row = [];
  98. $.each(self.collection.template.fields(), function (index, column) {
  99. row.push(item[column]); // TODO: if null + some escaping
  100. });
  101. self.results.push(row);
  102. });
  103. } else {
  104. // Template view
  105. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  106. $.each(data.response.docs, function (index, item) {
  107. addTemplateFunctions(item);
  108. self.results.push(Mustache.render(_mustacheTmpl, item));
  109. });
  110. }
  111. }
  112. }).fail(function(xhr, textStatus, errorThrown) {
  113. $(document).trigger("error", xhr.responseText);
  114. });
  115. };
  116. self.selectSingleFacet = function(normalized_facet_json) {
  117. $.each(self.collection.facets(), function(index, facet) {
  118. if (facet.field() == normalized_facet_json.field) {
  119. self.selectedFacet(facet);
  120. }
  121. });
  122. }
  123. self.removeFacet = function(facet_json) {
  124. $.each(self.collection.facets(), function(index, item) {
  125. if (item.field() == facet_json.field) {
  126. self.collection.facets.remove(item);
  127. }
  128. });
  129. self.search();
  130. }
  131. };