search.ko.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. // A widget is generic. It has an id that refer to another object (e.g. facet) with the same id.
  67. var Widget = function (size, id, name, widgetType, properties, offset) {
  68. var self = this;
  69. self.size = ko.observable(size).extend({ numeric: 0 });
  70. self.name = ko.observable(name);
  71. self.id = ko.observable(id);
  72. self.widgetType = ko.observable(typeof widgetType != "undefined" && widgetType != null ? widgetType : "empty-widget");
  73. self.properties = ko.observable(typeof properties != "undefined" && properties != null ? properties : {});
  74. self.offset = ko.observable(typeof offset != "undefined" && offset != null ? offset : 0).extend({ numeric: 0 });
  75. self.klass = ko.computed(function () {
  76. return "card card-widget span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  77. });
  78. self.expand = function () {
  79. self.size(self.size() + 1);
  80. }
  81. self.compress = function () {
  82. self.size(self.size() - 1);
  83. }
  84. self.moveLeft = function () {
  85. self.offset(self.offset() - 1);
  86. }
  87. self.moveRight = function () {
  88. self.offset(self.offset() + 1);
  89. }
  90. self.remove = function (row, widget) {
  91. viewModel.removeWidget(widget);
  92. row.widgets.remove(widget);
  93. }
  94. };
  95. Widget.prototype.clone = function () {
  96. return new Widget(this.size(), UUID(), this.name(), this.widgetType());
  97. };
  98. function fullLayout() {
  99. setLayout([12]);
  100. }
  101. function oneThirdLeftLayout() {
  102. setLayout([3, 9]);
  103. }
  104. function oneThirdRightLayout() { // instead --> full with 1 row = timeline, 2 = 3 pies, 3 = grid result
  105. setLayout([9, 3]);
  106. }
  107. function setLayout(colSizes) {
  108. // Save previous widgets
  109. var _allRows = [];
  110. $(viewModel.columns()).each(function (cnt, col) {
  111. var _tRows = [];
  112. $(col.rows()).each(function (icnt, row) {
  113. if (row.widgets().length > 0) {
  114. _tRows.push(row);
  115. }
  116. });
  117. _allRows = _allRows.concat(_tRows);
  118. });
  119. var _cols = [];
  120. var _highestCol = {
  121. idx: -1,
  122. size: -1
  123. };
  124. $(colSizes).each(function (cnt, size) {
  125. _cols.push(new Column(size, []));
  126. if (size > _highestCol.size) {
  127. _highestCol.idx = cnt;
  128. _highestCol.size = size;
  129. }
  130. });
  131. if (_allRows.length > 0 && _highestCol.idx > -1) {
  132. _cols[_highestCol.idx].rows(_allRows);
  133. }
  134. $(_cols).each(function (cnt, col) {
  135. if (col.rows().length == 0) {
  136. col.rows([new Row([])]);
  137. }
  138. });
  139. viewModel.columns(_cols);
  140. }
  141. function loadLayout(viewModel, json_layout) {
  142. var _columns = [];
  143. $(json_layout).each(function (cnt, json_col) {
  144. var _rows = [];
  145. $(json_col.rows).each(function (rcnt, json_row) {
  146. var row = new Row();
  147. $(json_row.widgets).each(function (wcnt, widget) {
  148. row.addWidget(new Widget(widget.size, widget.id, widget.name, widget.widgetType, widget.properties, widget.offset));
  149. });
  150. _rows.push(row);
  151. });
  152. var column = new Column(json_col.size, _rows);
  153. _columns = _columns.concat(column);
  154. });
  155. viewModel.columns(_columns);
  156. }
  157. // End dashboard lib
  158. var Query = function (vm, query) {
  159. var self = this;
  160. self.q = ko.observable(query.q);
  161. self.fq = query.fq
  162. self.selectFacet = function (facet_json) {
  163. self.fq[facet_json.cat] = facet_json.value;
  164. vm.search();
  165. }
  166. self.unselectFacet = function (facet_json) {
  167. delete self.fq[facet_json.cat];
  168. vm.search();
  169. }
  170. };
  171. var Collection = function (vm, collection) {
  172. var self = this;
  173. self.id = collection.id;
  174. self.name = collection.name;
  175. self.label = collection.label;
  176. self.idField = collection.idField;
  177. self.template = ko.mapping.fromJS(collection.template);
  178. self.template.fieldsSelected.subscribe(function () {
  179. vm.search();
  180. });
  181. self.template.template.subscribe(function () {
  182. vm.search();
  183. });
  184. self.template.isGridLayout.subscribe(function () {
  185. vm.search();
  186. });
  187. self.facets = ko.mapping.fromJS(collection.facets);
  188. self.fields = ko.mapping.fromJS(collection.fields);
  189. self.addFacet = function (facet_json) {
  190. self.facets.push(ko.mapping.fromJS({
  191. "id": facet_json.widget_id,
  192. "label": facet_json.name,
  193. "field": facet_json.name,
  194. "type": "field"
  195. }));
  196. }
  197. self.removeFacet = function (widget_id) {
  198. $.each(self.facets(), function (index, item) {
  199. if (item.id() == widget_id()) {
  200. self.facets.remove(item);
  201. }
  202. });
  203. }
  204. self.getFacetById = function (facet_id) {
  205. var _facet = null;
  206. $.each(self.facets(), function (index, facet) {
  207. if (facet.id() == facet_id) {
  208. _facet = facet;
  209. }
  210. });
  211. return _facet;
  212. }
  213. self.template.fields = ko.computed(function () {
  214. var _fields = [];
  215. $.each(self.template.fieldsAttributes(), function (index, field) {
  216. var position = self.template.fieldsSelected.indexOf(field.name());
  217. if (position != -1) {
  218. _fields[position] = field;
  219. }
  220. });
  221. return _fields;
  222. });
  223. self.addDynamicFields = function () {
  224. $.post("/search/index/" + self.id + "/fields/dynamic", {
  225. }, function (data) {
  226. if (data.status == 0) {
  227. $.each(data.dynamic_fields, function (index, field) {
  228. if (self.fields.indexOf(field) == -1) {
  229. self.fields.push(field);
  230. }
  231. });
  232. }
  233. }).fail(function (xhr, textStatus, errorThrown) {});
  234. }
  235. self.toggleSortColumnGridLayout = function (template_field) {
  236. if (! template_field.sort.direction()) {
  237. template_field.sort.direction('desc');
  238. } else if (template_field.sort.direction() == 'desc') {
  239. template_field.sort.direction('asc');
  240. } else {
  241. template_field.sort.direction(null);
  242. }
  243. vm.search();
  244. };
  245. };
  246. var SearchViewModel = function (collection_json, query_json) {
  247. var self = this;
  248. // Models
  249. self.collection = new Collection(self, collection_json.collection);
  250. self.query = new Query(self, query_json);
  251. // UI
  252. self.response = ko.observable({});
  253. self.results = ko.observableArray([]);
  254. self.norm_facets = ko.computed(function () {
  255. return self.response().normalized_facets;
  256. });
  257. self.getFacetFromQuery = function (facet_id) {
  258. var _facet = null;
  259. if (self.norm_facets() !== undefined) {
  260. $.each(self.norm_facets(), function (index, norm_facet) {
  261. if (norm_facet.id == facet_id()) {
  262. _facet = norm_facet;
  263. }
  264. });
  265. }
  266. return _facet;
  267. };
  268. self.previewColumns = ko.observable("");
  269. self.columns = ko.observable({});
  270. loadLayout(self, collection_json.layout);
  271. self.isEditing = ko.observable(false);
  272. self.toggleEditing = function () {
  273. self.isEditing(!self.isEditing());
  274. };
  275. self.isRetrievingResults = ko.observable(false);
  276. self.draggableFacet = ko.observable(new Widget(12, UUID(), "Facet", "facet-widget"));
  277. self.draggableResultset = ko.observable(new Widget(12, UUID(), "Results", "resultset-widget"));
  278. self.draggableBar = ko.observable(new Widget(12, UUID(), "Bar Chart", "bar-widget"));
  279. self.draggableArea = ko.observable(new Widget(12, UUID(), "Area Chart", "area-widget"));
  280. self.draggableMap = ko.observable(new Widget(12, UUID(), "Map", "map-widget"));
  281. self.draggableLine = ko.observable(new Widget(12, UUID(), "Line Chart", "line-widget"));
  282. self.draggablePie = ko.observable(new Widget(12, UUID(), "Pie Chart", "pie-widget"));
  283. self.init = function () {
  284. //self.collection.addDynamicFields();
  285. self.isEditing(true);
  286. self.search();
  287. }
  288. self.search = function () {
  289. self.isRetrievingResults(true);
  290. $(".jHueNotify").hide();
  291. $.post("/search/search", {
  292. collection: ko.mapping.toJSON(self.collection),
  293. query: ko.mapping.toJSON(self.query),
  294. layout: ko.mapping.toJSON(self.columns)
  295. }, function (data) {
  296. self.response(data); // Content not observable for performance
  297. self.results.removeAll();
  298. if (data.error) {
  299. $(document).trigger("error", data.error);
  300. }
  301. else {
  302. if (self.collection.template.isGridLayout()) {
  303. // Table view
  304. $.each(data.response.docs, function (index, item) {
  305. var row = [];
  306. var fields = self.collection.template.fieldsSelected();
  307. // Field selection or whole record
  308. if (fields.length != 0) {
  309. $.each(self.collection.template.fieldsSelected(), function (index, field) {//alert(ko.mapping.toJSON(field));
  310. row.push(item[field]);
  311. });
  312. } else {
  313. row.push(ko.mapping.toJSON(item));
  314. }
  315. self.results.push(row);
  316. });
  317. }
  318. else {
  319. // Template view
  320. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  321. $.each(data.response.docs, function (index, item) {
  322. addTemplateFunctions(item);
  323. self.results.push(Mustache.render(_mustacheTmpl, item));
  324. });
  325. }
  326. self.isRetrievingResults(false);
  327. }
  328. }).fail(function (xhr, textStatus, errorThrown) {
  329. $(document).trigger("error", xhr.responseText);
  330. });
  331. };
  332. self.removeWidget = function (widget_json) {
  333. self.collection.removeFacet(widget_json.id);
  334. self.search();
  335. }
  336. self.save = function () {
  337. $.post("/search/save", {
  338. collection: ko.mapping.toJSON(self.collection),
  339. layout: ko.mapping.toJSON(self.columns)
  340. },function (data) {
  341. if (data.status == 0) {
  342. $(document).trigger("info", data.message);
  343. }
  344. else {
  345. $(document).trigger("error", data.message);
  346. }
  347. }).fail(function (xhr, textStatus, errorThrown) {
  348. $(document).trigger("error", xhr.responseText);
  349. });
  350. };
  351. };