search.ko.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. // Start dashboard lib to move out
  17. var Column = function (size, rows) {
  18. var self = this;
  19. self.size = ko.observable(size);
  20. self.rows = ko.observableArray(rows);
  21. self.klass = ko.computed(function () {
  22. return "card card-home card-column span" + self.size();
  23. });
  24. self.addEmptyRow = function () {
  25. self.addRow();
  26. };
  27. self.addRow = function (row) {
  28. if (typeof row == "undefined" || row == null) {
  29. row = new Row([]);
  30. }
  31. self.rows.push(row);
  32. };
  33. }
  34. var Row = function (widgets) {
  35. var self = this;
  36. self.widgets = ko.observableArray(widgets);
  37. self.addWidget = function (widget) {
  38. self.widgets.push(widget);
  39. };
  40. self.move = function (from, to) {
  41. try {
  42. viewModel.columns()[to].addRow(self);
  43. viewModel.columns()[from].rows.remove(self);
  44. }
  45. catch (exception) {
  46. }
  47. }
  48. self.moveDown = function (col, row) {
  49. var _i = col.rows().indexOf(row);
  50. if (_i < col.rows().length - 1) {
  51. var _arr = col.rows();
  52. col.rows.splice(_i, 2, _arr[_i + 1], _arr[_i]);
  53. }
  54. }
  55. self.moveUp = function (col, row) {
  56. var _i = col.rows().indexOf(row);
  57. if (_i >= 1) {
  58. var _arr = col.rows();
  59. col.rows.splice(_i - 1, 2, _arr[_i], _arr[_i - 1]);
  60. }
  61. }
  62. self.remove = function (col, row) {
  63. col.rows.remove(row);
  64. }
  65. }
  66. var Widget = function (size, name, widgetType, properties, offset) {
  67. var self = this;
  68. self.size = ko.observable(size).extend({ numeric: 0 });
  69. self.name = ko.observable(name);
  70. self.widgetType = ko.observable(typeof widgetType != "undefined" && widgetType != null ? widgetType : "empty-widget");
  71. self.properties = ko.observable(typeof properties != "undefined" && properties != null ? properties : {});
  72. self.offset = ko.observable(typeof offset != "undefined" && offset != null ? offset : 0).extend({ numeric: 0 });
  73. self.klass = ko.computed(function () {
  74. return "card card-widget span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  75. });
  76. self.expand = function () {
  77. self.size(self.size() + 1);
  78. }
  79. self.compress = function () {
  80. self.size(self.size() - 1);
  81. }
  82. self.moveLeft = function () {
  83. self.offset(self.offset() - 1);
  84. }
  85. self.moveRight = function () {
  86. self.offset(self.offset() + 1);
  87. }
  88. self.remove = function (row, widget) {
  89. // widget.remove() --> callback to removeFacet() or other depending on type of widget
  90. row.widgets.remove(widget);
  91. }
  92. };
  93. Widget.prototype.clone = function () {
  94. return new Widget(this.size(), this.name(), this.widgetType());
  95. };
  96. function fullLayout() {
  97. setLayout([12]);
  98. }
  99. function oneThirdLeftLayout() {
  100. setLayout([3, 9]);
  101. }
  102. function oneThirdRightLayout() {
  103. setLayout([9, 3]);
  104. }
  105. function setLayout(colSizes) {
  106. // save previous widgets
  107. var _allRows = [];
  108. $(viewModel.columns()).each(function (cnt, col) {
  109. var _tRows = [];
  110. $(col.rows()).each(function (icnt, row) {
  111. if (row.widgets().length > 0) {
  112. _tRows.push(row);
  113. }
  114. });
  115. _allRows = _allRows.concat(_tRows);
  116. });
  117. var _cols = [];
  118. var _highestCol = {
  119. idx: -1,
  120. size: -1
  121. };
  122. $(colSizes).each(function (cnt, size) {
  123. _cols.push(new Column(size, []));
  124. if (size > _highestCol.size) {
  125. _highestCol.idx = cnt;
  126. _highestCol.size = size;
  127. }
  128. });
  129. if (_allRows.length > 0 && _highestCol.idx > -1) {
  130. _cols[_highestCol.idx].rows(_allRows);
  131. }
  132. $(_cols).each(function (cnt, col) {
  133. if (col.rows().length == 0) {
  134. col.rows([new Row([])]);
  135. }
  136. });
  137. viewModel.columns(_cols);
  138. }
  139. // End dashboard lib
  140. var Query = function (vm, query) {
  141. var self = this;
  142. self.q = ko.observable(query.q);
  143. self.fq = query.fq
  144. self.selectFacet = function (facet_json) {
  145. self.fq[facet_json.cat] = facet_json.value;
  146. vm.search();
  147. }
  148. self.unselectFacet = function (facet_json) {
  149. delete self.fq[facet_json.cat];
  150. vm.search();
  151. }
  152. };
  153. var Collection = function (vm, collection) {
  154. var self = this;
  155. self.id = collection.id;
  156. self.name = collection.name;
  157. self.idField = collection.idField;
  158. self.template = ko.mapping.fromJS(collection.template);
  159. self.template.fields.subscribe(function () {
  160. vm.search();
  161. });
  162. self.template.template.subscribe(function () {
  163. vm.search();
  164. });
  165. self.template.isGridLayout.subscribe(function () {
  166. vm.search();
  167. });
  168. self.facets = ko.mapping.fromJS(collection.facets);
  169. self.fields = ko.observableArray(collection.fields);
  170. self.addFacet = function (facet_json) {
  171. self.facets.push(ko.mapping.fromJS({
  172. "uuid": "f6618a5c-bbba-2886-1886-bbcaf01409ca",
  173. "verbatim": "", "isVerbatim": false, "label": facet_json.name,
  174. "field": facet_json.name, "type": "field"
  175. }));
  176. }
  177. self.addDynamicFields = function () {
  178. $.post("/search/index/" + self.id + "/fields/dynamic", {
  179. },function (data) {
  180. if (data.status == 0) {
  181. $.each(data.dynamic_fields, function (index, field) {
  182. self.fields.push(field);
  183. });
  184. }
  185. }).fail(function (xhr, textStatus, errorThrown) {
  186. }
  187. );
  188. }
  189. self.addDynamicFields();
  190. };
  191. var SearchViewModel = function (collection_json, query_json) {
  192. var self = this;
  193. // Models
  194. self.collection = new Collection(self, collection_json);
  195. self.query = new Query(self, query_json);
  196. // UI
  197. self.response = ko.observable({});
  198. self.results = ko.observableArray([]);
  199. self.norm_facets = ko.computed(function () {
  200. return self.response().normalized_facets;
  201. });
  202. self.selectedFacet = ko.observable();
  203. self.previewColumns = ko.observable("");
  204. self.columns = ko.observableArray([]); // aka 'layout'
  205. self.isEditing = ko.observable(false);
  206. self.draggableFacet = ko.observable(new Widget(12, "Facet", "facet-widget"));
  207. self.draggableResultset = ko.observable(new Widget(12, "Results", "resultset-widget"));
  208. self.draggableBar = ko.observable(new Widget(12, "Bar Chart", "bar-widget"));
  209. self.draggableArea = ko.observable(new Widget(12, "Area Chart", "area-widget"));
  210. self.draggableMap = ko.observable(new Widget(12, "Map", "map-widget"));
  211. self.draggableLine = ko.observable(new Widget(12, "Line Chart", "line-widget"));
  212. self.draggablePie = ko.observable(new Widget(12, "Pie Chart", "pie-widget"));
  213. self.toggleEditing = function () {
  214. self.isEditing(!self.isEditing());
  215. };
  216. self.isRetrievingResults = ko.observable(false);
  217. self.search = function () {
  218. self.isRetrievingResults(true);
  219. $(".jHueNotify").hide();
  220. $.post("/search/search", {
  221. collection: ko.mapping.toJSON(self.collection),
  222. query: ko.mapping.toJSON(self.query),
  223. layout: ko.mapping.toJSON(self.columns)
  224. },function (data) {
  225. self.response(data); // If error we should probably update only the facets
  226. self.results.removeAll();
  227. if (data.error) {
  228. $(document).trigger("error", data.error);
  229. }
  230. else {
  231. if (self.collection.template.isGridLayout()) {
  232. // Table view
  233. $.each(data.response.docs, function (index, item) {
  234. var row = [];
  235. var fields = self.collection.template.fields();
  236. // Field selection or whole record
  237. if (fields.length != 0) {
  238. $.each(self.collection.template.fields(), function (index, column) {
  239. row.push(item[column]);
  240. });
  241. } else {
  242. row.push(ko.mapping.toJSON(item));
  243. }
  244. self.results.push(row);
  245. });
  246. }
  247. else {
  248. // Template view
  249. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  250. $.each(data.response.docs, function (index, item) {
  251. addTemplateFunctions(item);
  252. self.results.push(Mustache.render(_mustacheTmpl, item));
  253. });
  254. }
  255. self.isRetrievingResults(false);
  256. }
  257. }).fail(function (xhr, textStatus, errorThrown) {
  258. $(document).trigger("error", xhr.responseText);
  259. });
  260. };
  261. // To move to collection
  262. self.selectSingleFacet = function (normalized_facet_json) {
  263. $.each(self.collection.facets(), function (index, facet) {
  264. if (facet.field() == normalized_facet_json.field) {
  265. self.selectedFacet(facet);
  266. }
  267. });
  268. }
  269. self.removeFacet = function (facet_json) {
  270. $.each(self.collection.facets(), function (index, item) {
  271. if (item.field() == facet_json.field) {
  272. self.collection.facets.remove(item);
  273. }
  274. });
  275. self.search();
  276. }
  277. self.save = function () {
  278. $.post("/search/save", {
  279. collection: ko.mapping.toJSON(self.collection),
  280. layout: ko.mapping.toJSON(self.columns)
  281. },function (data) {
  282. if (data.status == 0) {
  283. $(document).trigger("info", data.message);
  284. }
  285. else {
  286. $(document).trigger("error", data.message);
  287. }
  288. }).fail(function (xhr, textStatus, errorThrown) {
  289. $(document).trigger("error", xhr.responseText);
  290. });
  291. };
  292. };