search.ko.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. $.each(self.widgets(), function(i, widget) {
  64. viewModel.removeWidget(widget);
  65. });
  66. col.rows.remove(row);
  67. }
  68. }
  69. // A widget is generic. It has an id that refer to another object (e.g. facet) with the same id.
  70. var Widget = function (size, id, name, widgetType, properties, offset, loading) {
  71. var self = this;
  72. self.size = ko.observable(size).extend({ numeric: 0 });
  73. self.name = ko.observable(name);
  74. self.id = ko.observable(id);
  75. self.widgetType = ko.observable(typeof widgetType != "undefined" && widgetType != null ? widgetType : "empty-widget");
  76. self.properties = ko.observable(typeof properties != "undefined" && properties != null ? properties : {});
  77. self.offset = ko.observable(typeof offset != "undefined" && offset != null ? offset : 0).extend({ numeric: 0 });
  78. self.isLoading = ko.observable(typeof loading != "undefined" && loading != null ? loading : false);
  79. self.klass = ko.computed(function () {
  80. return "card card-widget span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  81. });
  82. self.expand = function () {
  83. self.size(self.size() + 1);
  84. }
  85. self.compress = function () {
  86. self.size(self.size() - 1);
  87. }
  88. self.moveLeft = function () {
  89. self.offset(self.offset() - 1);
  90. }
  91. self.moveRight = function () {
  92. self.offset(self.offset() + 1);
  93. }
  94. self.remove = function (row, widget) {
  95. viewModel.removeWidget(widget);
  96. row.widgets.remove(widget);
  97. }
  98. };
  99. Widget.prototype.clone = function () {
  100. return new Widget(this.size(), UUID(), this.name(), this.widgetType());
  101. };
  102. function fullLayout() {
  103. setLayout([12]);
  104. }
  105. function oneThirdLeftLayout() {
  106. setLayout([2, 10]);
  107. }
  108. function oneThirdRightLayout() {
  109. setLayout([9, 3]);
  110. }
  111. function magicLayout(vm) {
  112. loadLayout(vm, vm.initial.layout);
  113. }
  114. function setLayout(colSizes) {
  115. // Save previous widgets
  116. var _allRows = [];
  117. $(viewModel.columns()).each(function (cnt, col) {
  118. var _tRows = [];
  119. $(col.rows()).each(function (icnt, row) {
  120. if (row.widgets().length > 0) {
  121. _tRows.push(row);
  122. }
  123. });
  124. _allRows = _allRows.concat(_tRows);
  125. });
  126. var _cols = [];
  127. var _highestCol = {
  128. idx: -1,
  129. size: -1
  130. };
  131. $(colSizes).each(function (cnt, size) {
  132. _cols.push(new Column(size, []));
  133. if (size > _highestCol.size) {
  134. _highestCol.idx = cnt;
  135. _highestCol.size = size;
  136. }
  137. });
  138. if (_allRows.length > 0 && _highestCol.idx > -1) {
  139. _cols[_highestCol.idx].rows(_allRows);
  140. }
  141. $(_cols).each(function (cnt, col) {
  142. if (col.rows().length == 0) {
  143. col.rows([new Row([])]);
  144. }
  145. });
  146. viewModel.columns(_cols);
  147. }
  148. function loadLayout(viewModel, json_layout) {
  149. var _columns = [];
  150. $(json_layout).each(function (cnt, json_col) {
  151. var _rows = [];
  152. $(json_col.rows).each(function (rcnt, json_row) {
  153. var row = new Row();
  154. $(json_row.widgets).each(function (wcnt, widget) {
  155. row.addWidget(new Widget(widget.size, widget.id, widget.name, widget.widgetType, widget.properties, widget.offset, true));
  156. });
  157. _rows.push(row);
  158. });
  159. var column = new Column(json_col.size, _rows);
  160. _columns = _columns.concat(column);
  161. });
  162. viewModel.columns(_columns);
  163. }
  164. // End dashboard lib
  165. var Query = function (vm, query) {
  166. var self = this;
  167. self.qs = ko.mapping.fromJS(query.qs);
  168. self.fqs = ko.mapping.fromJS(query.fqs);
  169. var defaultMultiqGroup = {'id': 'query', 'label': 'query'};
  170. self.multiqs = ko.computed(function () { // List of widgets supporting multiqs
  171. var histogram_id = vm.collection.getHistogramFacet();
  172. return [defaultMultiqGroup].concat(
  173. $.map($.grep(self.fqs(), function(fq, i) {
  174. return (fq.type() == 'field' || fq.type() == 'range') && (histogram_id == null || histogram_id.id() != fq.id());
  175. }), function(fq) { return {'id': fq.id(), 'label': fq.field()} })
  176. );
  177. });
  178. self.selectedMultiq = ko.observable(defaultMultiqGroup);
  179. self.getFacetFilter = function (widget_id) {
  180. var _fq = null;
  181. $.each(self.fqs(), function (index, fq) {
  182. if (fq.id() == widget_id) {
  183. _fq = fq;
  184. return false;
  185. }
  186. });
  187. return _fq;
  188. };
  189. self.getMultiq = ko.computed(function () {
  190. if (self.selectedMultiq()) {
  191. if (self.selectedMultiq() == 'query') {
  192. if (self.qs().length >= 2) {
  193. return 'query';
  194. }
  195. } else {
  196. var facet = self.getFacetFilter(self.selectedMultiq());
  197. if (facet && facet.filter().length > 0) { // todo + histogram there?
  198. return 'facet';
  199. }
  200. }
  201. }
  202. return null;
  203. });
  204. self.selectedMultiq.subscribe(function () { // To keep below the computed
  205. vm.search();
  206. });
  207. self.addQ = function (data) {
  208. self.qs.push(ko.mapping.fromJS({'q': ''}));
  209. };
  210. self.removeQ = function (query) {
  211. self.qs.remove(query);
  212. };
  213. self.toggleFacet = function (data) {
  214. var fq = self.getFacetFilter(data.widget_id);
  215. if (fq == null) {
  216. self.fqs.push(ko.mapping.fromJS({
  217. 'id': data.widget_id,
  218. 'field': data.facet.cat,
  219. 'filter': [data.facet.value],
  220. 'type': 'field'
  221. }));
  222. } else {
  223. $.each(self.fqs(), function (index, fq) {
  224. if (fq.id() == data.widget_id) {
  225. if (fq.filter.indexOf(data.facet.value) > -1) {
  226. fq.filter.remove(data.facet.value);
  227. if (fq.filter().length == 0) {
  228. self.fqs.remove(fq);
  229. }
  230. } else {
  231. fq.filter.push(data.facet.value);
  232. }
  233. }
  234. });
  235. }
  236. vm.search();
  237. }
  238. self.selectRangeFacet = function (data) {
  239. if (data.force != undefined) {
  240. self.removeFilter(ko.mapping.fromJS({'id': data.widget_id}));
  241. }
  242. var fq = self.getFacetFilter(data.widget_id);
  243. if (fq == null) {
  244. self.fqs.push(ko.mapping.fromJS({
  245. 'id': data.widget_id,
  246. 'field': data.cat,
  247. 'filter': [data.from],
  248. 'properties': [{'from': data.from, 'to': data.to}],
  249. 'type': 'range'
  250. }));
  251. } else {
  252. if (fq.filter().indexOf(data.from) > -1) { // Unselect
  253. fq.filter.remove(data.from);
  254. $.each(fq.properties(), function (index, prop) {
  255. if (prop && prop.from() == data.from) {
  256. fq.properties.remove(prop);
  257. }
  258. });
  259. if (fq.filter().length == 0) {
  260. self.removeFilter(ko.mapping.fromJS({'id': data.widget_id}));
  261. }
  262. } else {
  263. fq.filter.push(data.from);
  264. fq.properties.push(ko.mapping.fromJS({'from': data.from, 'to': data.to}));
  265. }
  266. }
  267. if (data.no_refresh == undefined) {
  268. vm.search();
  269. }
  270. };
  271. self.removeFilter = function (data) {
  272. $.each(self.fqs(), function (index, fq) {
  273. if (fq.id() == data.id()) {
  274. self.fqs.remove(fq);
  275. return false;
  276. }
  277. });
  278. };
  279. };
  280. var Collection = function (vm, collection) {
  281. var self = this;
  282. self.id = collection.id;
  283. self.name = ko.mapping.fromJS(collection.name);
  284. self.label = ko.mapping.fromJS(collection.label);
  285. self.idField = collection.idField;
  286. self.template = ko.mapping.fromJS(collection.template);
  287. self.template.fieldsSelected.subscribe(function () {
  288. vm.search();
  289. });
  290. self.template.template.extend({ rateLimit: { method: "notifyWhenChangesStop", timeout: 400 } }); // hum?
  291. self.template.template.subscribe(function () {
  292. vm.search();
  293. });
  294. self.template.isGridLayout.subscribe(function () {
  295. vm.search();
  296. });
  297. self.facets = ko.mapping.fromJS(collection.facets);
  298. $.each(self.facets(), function (index, facet) {
  299. facet.field.subscribe(function () {
  300. vm.search();
  301. });
  302. });
  303. self.fields = ko.mapping.fromJS(collection.fields);
  304. self.availableFacetFields = ko.computed(function() {
  305. var facetFieldNames = $.map(self.facets(), function(facet) {
  306. return facet.field();
  307. });
  308. return $.grep(self.fields(), function(field) {
  309. return facetFieldNames.indexOf(field.name()) == -1;
  310. });
  311. });
  312. self.selectedDocument = ko.observable({});
  313. self.addFacet = function (facet_json) {
  314. $.post("/search/template/new_facet", {
  315. "collection": ko.mapping.toJSON(self),
  316. "id": facet_json.widget_id,
  317. "label": facet_json.name,
  318. "field": facet_json.name,
  319. "widget_type": facet_json.widgetType
  320. }, function (data) {
  321. if (data.status == 0) {
  322. var facet = ko.mapping.fromJS(data.facet);
  323. facet.field.subscribe(function () {
  324. vm.search();
  325. });
  326. self.facets.push(facet);
  327. vm.search();
  328. } else {
  329. $(document).trigger("error", data.message);
  330. }
  331. }).fail(function (xhr, textStatus, errorThrown) {});
  332. };
  333. self.removeFacet = function (widget_id) {
  334. $.each(self.facets(), function (index, facet) {
  335. if (facet.id() == widget_id()) {
  336. self.facets.remove(facet);
  337. return false;
  338. }
  339. });
  340. }
  341. self.getFacetById = function (facet_id) {
  342. var _facet = null;
  343. $.each(self.facets(), function (index, facet) {
  344. if (facet.id() == facet_id) {
  345. _facet = facet;
  346. return false;
  347. }
  348. });
  349. return _facet;
  350. }
  351. self.getFacetByType = function (facetType) {
  352. var _facet = null;
  353. $.each(self.facets(), function (index, facet) {
  354. if (facet.widgetType() == facetType) {
  355. _facet = facet;
  356. return false;
  357. }
  358. });
  359. return _facet;
  360. }
  361. self.getHistogramFacet = function () { // might remove when list of available widgets
  362. return self.getFacetByType('histogram-widget');
  363. }
  364. self.template.fields = ko.computed(function () {
  365. var _fields = [];
  366. $.each(self.template.fieldsAttributes(), function (index, field) {
  367. var position = self.template.fieldsSelected.indexOf(field.name());
  368. if (position != -1) {
  369. _fields[position] = field;
  370. }
  371. });
  372. return _fields;
  373. });
  374. self.getTemplateField = function (name) {
  375. var _field = null;
  376. $.each(self.template.fields(), function (index, field) {
  377. if (field.name() == name) {
  378. _field = field;
  379. return false;
  380. }
  381. });
  382. return _field;
  383. };
  384. self.template.fieldsModalFilter = ko.observable(); // For UI
  385. self.template.fieldsModalType = ko.observable("");
  386. self.template.filteredFieldsAttributes = ko.observableArray();
  387. self.template.availableWidgetFields = ko.computed(function() {
  388. return self.template.fieldsModalType() == 'histogram-widget'? vm.availableDateFields() : self.availableFacetFields();
  389. });
  390. self.template.availableWidgetFieldsNames = ko.computed(function() {
  391. return $.map(self.template.availableWidgetFields(), function(field){
  392. return field.name();
  393. });
  394. });
  395. self.template.fieldsModalFilter.subscribe(function(value) {
  396. var _fields = [];
  397. var _availableFields = self.template.availableWidgetFields();
  398. $.each(_availableFields, function (index, field) {
  399. if (self.template.fieldsModalFilter() == "" || field.name().toLowerCase().indexOf(self.template.fieldsModalFilter().toLowerCase()) > -1){
  400. _fields.push(field);
  401. }
  402. });
  403. self.template.filteredFieldsAttributes(_fields);
  404. });
  405. self.addDynamicFields = function () { // + Adding merge smartly if schema updated
  406. $.post("/search/index/" + self.id + "/fields/dynamic", {
  407. }, function (data) {
  408. if (data.status == 0) {
  409. $.each(data.dynamic_fields, function (index, field) {
  410. if (self.fields.indexOf(field) == -1) {
  411. self.fields.push(field);
  412. }
  413. });
  414. }
  415. }).fail(function (xhr, textStatus, errorThrown) {});
  416. }
  417. self.toggleSortColumnGridLayout = function (template_field) {
  418. if (! template_field.sort.direction()) {
  419. template_field.sort.direction('desc');
  420. } else if (template_field.sort.direction() == 'desc') {
  421. template_field.sort.direction('asc');
  422. } else {
  423. template_field.sort.direction(null);
  424. }
  425. vm.search();
  426. };
  427. self.toggleSortFacet = function (facet_field, event) {
  428. if (facet_field.properties.sort() == 'desc') {
  429. facet_field.properties.sort('asc');
  430. } else {
  431. facet_field.properties.sort('desc');
  432. }
  433. $(event.target).button('loading');
  434. vm.search();
  435. };
  436. self.toggleRangeFacet = function (facet_field, event) {
  437. vm.query.removeFilter(ko.mapping.fromJS({'id': facet_field.id})); // Reset filter query
  438. if (facet_field.type() == 'field') {
  439. facet_field.type('range');
  440. } else if (facet_field.type() == 'range') {
  441. facet_field.type('field')
  442. }
  443. $(event.target).button('loading');
  444. vm.search();
  445. };
  446. self.selectTimelineFacet = function (data) { // alert(ko.mapping.toJSON(facet));
  447. var facet = self.getFacetById(data.widget_id);
  448. facet.properties.start(data.from);
  449. facet.properties.end(data.to);
  450. vm.query.selectRangeFacet({widget_id: data.widget_id, from: data.from, to: data.to, cat: data.cat, no_refresh: true, force: true});
  451. $.ajax({
  452. type: "POST",
  453. url: "/search/get_range_facet",
  454. data: {
  455. collection: ko.mapping.toJSON(self),
  456. facet: ko.mapping.toJSON(facet),
  457. action: 'select'
  458. },
  459. success: function (data) {
  460. if (data.status == 0) {
  461. facet.properties.gap(data.properties.gap);
  462. }
  463. },
  464. async: false
  465. });
  466. vm.search();
  467. }
  468. self.timeLineZoom = function (facet_json) {
  469. var facet = self.getFacetById(facet_json.id);
  470. facet.properties.start(facet.from);
  471. facet.properties.end(facet.to);
  472. $.ajax({
  473. type: "POST",
  474. url: "/search/get_range_facet",
  475. data: {
  476. collection: ko.mapping.toJSON(self),
  477. facet: ko.mapping.toJSON(facet),
  478. action: "zoom_out"
  479. },
  480. success: function (data) {
  481. if (data.status == 0) {
  482. facet.properties.start(data.properties.start);
  483. facet.properties.end(data.properties.end);
  484. facet.properties.gap(data.properties.gap);
  485. }
  486. },
  487. async: false
  488. });
  489. vm.search();
  490. }
  491. self.translateSelectedField = function (index, direction) {
  492. var array = self.template.fieldsSelected();
  493. if (direction == 'left') {
  494. self.template.fieldsSelected.splice(index - 1, 2, array[index], array[index - 1]);
  495. } else {
  496. self.template.fieldsSelected.splice(index, 2, array[index + 1], array[index]);
  497. }
  498. vm.search();
  499. };
  500. };
  501. var NewTemplate = function (vm, initial) {
  502. var self = this;
  503. self.collections = ko.mapping.fromJS(initial.collections);
  504. self.collections.subscribe(function (newValue) {
  505. vm.collection.name(newValue);
  506. });
  507. self.layout = initial.layout;
  508. };
  509. var DATE_TYPE = ['date', 'tdate'];
  510. var SearchViewModel = function (collection_json, query_json, initial_json) {
  511. var self = this;
  512. // Models
  513. self.collection = new Collection(self, collection_json.collection);
  514. self.query = new Query(self, query_json);
  515. self.initial = new NewTemplate(self, initial_json);
  516. if (initial_json.collections) {
  517. self.collection.name.subscribe(function (newValue) {
  518. self.collection.label(newValue);
  519. });
  520. }
  521. // UI
  522. self.response = ko.observable({});
  523. self.results = ko.observableArray([]);
  524. self.norm_facets = ko.computed(function () {
  525. return self.response().normalized_facets;
  526. });
  527. self.getFacetFromQuery = function (facet_id) {
  528. var _facet = null;
  529. if (self.norm_facets() !== undefined) {
  530. $.each(self.norm_facets(), function (index, norm_facet) {
  531. if (norm_facet.id == facet_id) {
  532. _facet = norm_facet;
  533. }
  534. });
  535. }
  536. return _facet;
  537. };
  538. self.previewColumns = ko.observable("");
  539. self.columns = ko.observable([]);
  540. loadLayout(self, collection_json.layout);
  541. self.isEditing = ko.observable(false);
  542. self.toggleEditing = function () {
  543. self.isEditing(!self.isEditing());
  544. };
  545. self.isRetrievingResults = ko.observable(false);
  546. self.draggableHit = ko.observable(new Widget(12, UUID(), "Hit Count", "hit-widget"));
  547. self.draggableFacet = ko.observable(new Widget(12, UUID(), "Facet", "facet-widget"));
  548. self.draggableResultset = ko.observable(new Widget(12, UUID(), "Grid Results", "resultset-widget"));
  549. self.draggableHtmlResultset = ko.observable(new Widget(12, UUID(), "HTML Results", "html-resultset-widget"));
  550. self.draggableHistogram = ko.observable(new Widget(12, UUID(), "Histogram", "histogram-widget"));
  551. self.draggableBar = ko.observable(new Widget(12, UUID(), "Bar Chart", "bar-widget"));
  552. self.draggableMap = ko.observable(new Widget(12, UUID(), "Map", "map-widget"));
  553. self.draggableLine = ko.observable(new Widget(12, UUID(), "Line Chart", "line-widget"));
  554. self.draggablePie = ko.observable(new Widget(12, UUID(), "Pie Chart", "pie-widget"));
  555. self.draggableFilter = ko.observable(new Widget(12, UUID(), "Filter Bar", "filter-widget"));
  556. self.availableDateFields = ko.computed(function() {
  557. return $.grep(self.collection.availableFacetFields(), function(field) { return DATE_TYPE.indexOf(field.type()) != -1; });
  558. });
  559. function getWidgets(equalsTo) {
  560. return $.map(self.columns(), function (col){return $.map(col.rows(), function(row){ return $.grep(row.widgets(), function(widget){ return equalsTo(widget); });}) ;})
  561. };
  562. self.availableDraggableResultset = ko.computed(function() {
  563. return getWidgets(function(widget){ return ['resultset-widget', 'html-resultset-widget'].indexOf(widget.widgetType()) != -1; }).length == 0;
  564. });
  565. self.availableDraggableFilter = ko.computed(function() {
  566. return getWidgets(function(widget){ return widget.widgetType() == 'filter-widget'; }).length == 0;
  567. });
  568. self.availableDraggableHistogram = ko.computed(function() {
  569. return getWidgets(function(widget){ return widget.widgetType() == 'histogram-widget'; }).length == 0 &&
  570. self.availableDateFields().length > 0;
  571. });
  572. self.availableDraggableChart = ko.computed(function() {
  573. return self.collection.availableFacetFields().length > 0;
  574. });
  575. self.init = function () {
  576. //self.collection.addDynamicFields();
  577. self.isEditing(true);
  578. self.search();
  579. }
  580. self.search = function () {
  581. self.isRetrievingResults(true);
  582. $(".jHueNotify").hide();
  583. // Multi queries
  584. var multiQs = [];
  585. var multiQ = self.query.getMultiq();
  586. if (multiQ != null) {
  587. var facet = {};
  588. var queries = [];
  589. if (multiQ == 'query') {
  590. queries = self.query.qs();
  591. } else {
  592. facet = self.query.getFacetFilter(self.query.selectedMultiq());
  593. queries = facet.filter();
  594. }
  595. multiQs = $.map(queries, function(qdata) {
  596. return $.post("/search/get_timeline", {
  597. collection: ko.mapping.toJSON(self.collection),
  598. query: ko.mapping.toJSON(self.query),
  599. facet: ko.mapping.toJSON(facet),
  600. qdata: ko.mapping.toJSON(qdata),
  601. multiQ: multiQ
  602. }, function (data) {return data});
  603. });
  604. }
  605. $.when.apply($, [
  606. $.post("/search/search", {
  607. collection: ko.mapping.toJSON(self.collection),
  608. query: ko.mapping.toJSON(self.query),
  609. layout: ko.mapping.toJSON(self.columns)
  610. }, function (data) {
  611. self.response(data);
  612. self.results.removeAll();
  613. if (data.error) {
  614. $(document).trigger("error", data.error);
  615. }
  616. else {
  617. if (self.collection.template.isGridLayout()) {
  618. // Table view
  619. $.each(data.response.docs, function (index, item) {
  620. var row = [];
  621. var fields = self.collection.template.fieldsSelected();
  622. // Display selected fields or whole json document
  623. if (fields.length != 0) {
  624. $.each(self.collection.template.fieldsSelected(), function (index, field) {
  625. row.push(item[field]);
  626. });
  627. } else {
  628. row.push(ko.mapping.toJSON(item));
  629. }
  630. var doc = {'id': item[self.collection.idField], 'row': row};
  631. self.results.push(doc);
  632. });
  633. }
  634. else {
  635. // Template view
  636. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  637. $.each(data.response.docs, function (index, item) {
  638. addTemplateFunctions(item);
  639. self.results.push(Mustache.render(_mustacheTmpl, item));
  640. });
  641. }
  642. self.isRetrievingResults(false);
  643. }
  644. })
  645. ].concat(multiQs)
  646. )
  647. .done(function() {
  648. if (arguments[0] instanceof Array) { // If multi queries
  649. var histoFacetId = self.collection.getHistogramFacet().id();
  650. var histoFacet = self.getFacetFromQuery(histoFacetId);
  651. for (var i = 1; i < arguments.length; i++) {
  652. histoFacet.extraSeries.push(arguments[i][0]['series']);
  653. }
  654. self.response.valueHasMutated();
  655. }
  656. })
  657. .fail(function (xhr, textStatus, errorThrown) {
  658. $(document).trigger("error", xhr.responseText);
  659. })
  660. .always(function () {
  661. $('.btn-loading').button('reset');
  662. });
  663. };
  664. self.removeWidget = function (widget_json) {
  665. self.collection.removeFacet(widget_json.id);
  666. self.query.removeFilter(widget_json);
  667. self.search();
  668. }
  669. self.getWidgetById = function (widget_id) {
  670. var _widget = null;
  671. $.each(self.columns(), function (i, col) {
  672. $.each(col.rows(), function (j, row) {
  673. $.each(row.widgets(), function (z, widget) {
  674. if (widget.id() == widget_id){
  675. _widget = widget;
  676. }
  677. });
  678. });
  679. });
  680. return _widget;
  681. }
  682. self.getDocument = function (doc) {
  683. $.post("/search/get_document", {
  684. collection: ko.mapping.toJSON(self.collection),
  685. id: doc.id
  686. },function (data) {
  687. if (data.status == 0) {
  688. $(document).trigger("showDoc", data.doc.doc);
  689. }
  690. else if (data.status == 1) {
  691. $(document).trigger("info", data.message);
  692. }
  693. else {
  694. $(document).trigger("error", data.message);
  695. }
  696. }).fail(function (xhr, textStatus, errorThrown) {
  697. $(document).trigger("error", xhr.responseText);
  698. });
  699. };
  700. self.save = function () {
  701. $.post("/search/save", {
  702. collection: ko.mapping.toJSON(self.collection),
  703. layout: ko.mapping.toJSON(self.columns)
  704. },function (data) {
  705. if (data.status == 0) {
  706. self.collection.id = data.id;
  707. $(document).trigger("info", data.message);
  708. }
  709. else {
  710. $(document).trigger("error", data.message);
  711. }
  712. }).fail(function (xhr, textStatus, errorThrown) {
  713. $(document).trigger("error", xhr.responseText);
  714. });
  715. };
  716. };