search.ko.js 25 KB

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