search.ko.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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: {timeout: 3000, method: "notifyWhenChangesStop"}});
  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.template.rows.extend({rateLimit: {timeout: 1500, method: "notifyWhenChangesStop"}});
  316. self.fields = ko.mapping.fromJS(collection.fields);
  317. self.availableFacetFields = ko.computed(function() {
  318. var facetFieldNames = $.map(self.facets(), function(facet) {
  319. return facet.field(); //filter out text_general
  320. });
  321. return $.grep(self.fields(), function(field) {
  322. return facetFieldNames.indexOf(field.name()) == -1;
  323. });
  324. });
  325. self.selectedDocument = ko.observable({});
  326. self.addFacet = function (facet_json) {
  327. self.removeFacet(function(){return facet_json.widget_id});
  328. $.post("/search/template/new_facet", {
  329. "collection": ko.mapping.toJSON(self),
  330. "id": facet_json.widget_id,
  331. "label": facet_json.name,
  332. "field": facet_json.name,
  333. "widget_type": facet_json.widgetType
  334. }, function (data) {
  335. if (data.status == 0) {
  336. var facet = ko.mapping.fromJS(data.facet);
  337. facet.field.subscribe(function () {
  338. vm.search();
  339. });
  340. self.facets.push(facet);
  341. vm.search();
  342. } else {
  343. $(document).trigger("error", data.message);
  344. }
  345. }).fail(function (xhr, textStatus, errorThrown) {});
  346. };
  347. self.removeFacet = function (widget_id) {
  348. $.each(self.facets(), function (index, facet) {
  349. if (facet.id() == widget_id()) {
  350. self.facets.remove(facet);
  351. return false;
  352. }
  353. });
  354. }
  355. self.getFacetById = function (facet_id) {
  356. var _facet = null;
  357. $.each(self.facets(), function (index, facet) {
  358. if (facet.id() == facet_id) {
  359. _facet = facet;
  360. return false;
  361. }
  362. });
  363. return _facet;
  364. }
  365. self.getFacetByType = function (facetType) {
  366. var _facet = null;
  367. $.each(self.facets(), function (index, facet) {
  368. if (facet.widgetType() == facetType) {
  369. _facet = facet;
  370. return false;
  371. }
  372. });
  373. return _facet;
  374. }
  375. self.getHistogramFacet = function () { // might remove when list of available widgets
  376. return self.getFacetByType('histogram-widget');
  377. }
  378. self.template.fields = ko.computed(function () {
  379. var _fields = [];
  380. $.each(self.template.fieldsAttributes(), function (index, field) {
  381. var position = self.template.fieldsSelected.indexOf(field.name());
  382. if (position != -1) {
  383. _fields[position] = field;
  384. }
  385. });
  386. return _fields;
  387. });
  388. self.getTemplateField = function (name) {
  389. var _field = null;
  390. $.each(self.template.fields(), function (index, field) {
  391. if (field.name() == name) {
  392. _field = field;
  393. return false;
  394. }
  395. });
  396. return _field;
  397. };
  398. self.template.fieldsModalFilter = ko.observable(); // For UI
  399. self.template.fieldsModalType = ko.observable("");
  400. self.template.filteredFieldsAttributes = ko.observableArray();
  401. self.template.availableWidgetFields = ko.computed(function() {
  402. return self.template.fieldsModalType() == 'histogram-widget'? vm.availableDateFields() : self.availableFacetFields();
  403. });
  404. self.template.availableWidgetFieldsNames = ko.computed(function() {
  405. return $.map(self.template.availableWidgetFields(), function(field){
  406. return field.name();
  407. });
  408. });
  409. self.template.fieldsModalFilter.subscribe(function(value) {
  410. var _fields = [];
  411. var _availableFields = self.template.availableWidgetFields();
  412. $.each(_availableFields, function (index, field) {
  413. if (self.template.fieldsModalFilter() == "" || field.name().toLowerCase().indexOf(self.template.fieldsModalFilter().toLowerCase()) > -1){
  414. _fields.push(field);
  415. }
  416. });
  417. self.template.filteredFieldsAttributes(_fields);
  418. });
  419. self.addDynamicFields = function () { // + Adding merge smartly if schema updated
  420. $.post("/search/index/" + self.id + "/fields/dynamic", {
  421. }, function (data) {
  422. if (data.status == 0) {
  423. $.each(data.dynamic_fields, function (index, field) {
  424. if (self.fields.indexOf(field) == -1) {
  425. self.fields.push(field);
  426. }
  427. });
  428. }
  429. }).fail(function (xhr, textStatus, errorThrown) {});
  430. }
  431. self.toggleSortColumnGridLayout = function (template_field) {
  432. if (! template_field.sort.direction()) {
  433. template_field.sort.direction('desc');
  434. } else if (template_field.sort.direction() == 'desc') {
  435. template_field.sort.direction('asc');
  436. } else {
  437. template_field.sort.direction(null);
  438. }
  439. vm.search();
  440. };
  441. self.toggleSortFacet = function (facet_field, event) {
  442. if (facet_field.properties.sort() == 'desc') {
  443. facet_field.properties.sort('asc');
  444. } else {
  445. facet_field.properties.sort('desc');
  446. }
  447. $(event.target).button('loading');
  448. vm.search();
  449. };
  450. self.toggleRangeFacet = function (facet_field, event) {
  451. vm.query.removeFilter(ko.mapping.fromJS({'id': facet_field.id})); // Reset filter query
  452. if (facet_field.type() == 'field') {
  453. facet_field.type('range');
  454. } else if (facet_field.type() == 'range') {
  455. facet_field.type('field')
  456. }
  457. $(event.target).button('loading');
  458. vm.search();
  459. };
  460. self.selectTimelineFacet = function (data) { // alert(ko.mapping.toJSON(facet));
  461. var facet = self.getFacetById(data.widget_id);
  462. facet.properties.start(data.from);
  463. facet.properties.end(data.to);
  464. vm.query.selectRangeFacet({widget_id: data.widget_id, from: data.from, to: data.to, cat: data.cat, no_refresh: true, force: true});
  465. $.ajax({
  466. type: "POST",
  467. url: "/search/get_range_facet",
  468. data: {
  469. collection: ko.mapping.toJSON(self),
  470. facet: ko.mapping.toJSON(facet),
  471. action: 'select'
  472. },
  473. success: function (data) {
  474. if (data.status == 0) {
  475. facet.properties.gap(data.properties.gap);
  476. }
  477. },
  478. async: false
  479. });
  480. vm.search();
  481. }
  482. self.timeLineZoom = function (facet_json) {
  483. var facet = self.getFacetById(facet_json.id);
  484. facet.properties.start(facet.from);
  485. facet.properties.end(facet.to);
  486. $.ajax({
  487. type: "POST",
  488. url: "/search/get_range_facet",
  489. data: {
  490. collection: ko.mapping.toJSON(self),
  491. facet: ko.mapping.toJSON(facet),
  492. action: "zoom_out"
  493. },
  494. success: function (data) {
  495. if (data.status == 0) {
  496. facet.properties.start(data.properties.start);
  497. facet.properties.end(data.properties.end);
  498. facet.properties.gap(data.properties.gap);
  499. }
  500. },
  501. async: false
  502. });
  503. vm.search();
  504. }
  505. self.translateSelectedField = function (index, direction) {
  506. var array = self.template.fieldsSelected();
  507. if (direction == 'left') {
  508. self.template.fieldsSelected.splice(index - 1, 2, array[index], array[index - 1]);
  509. } else {
  510. self.template.fieldsSelected.splice(index, 2, array[index + 1], array[index]);
  511. }
  512. vm.search();
  513. };
  514. self.upDownFacetLimit = function (facet_id, direction) {
  515. var facet = self.getFacetById(facet_id);
  516. if (facet.properties.prevLimit == undefined) {
  517. facet.properties.prevLimit = facet.properties.limit();
  518. }
  519. if (direction == 'up') {
  520. facet.properties.limit(facet.properties.limit() + 10);
  521. } else {
  522. facet.properties.limit(facet.properties.limit() - 10);
  523. }
  524. vm.search();
  525. };
  526. };
  527. var NewTemplate = function (vm, initial) {
  528. var self = this;
  529. self.collections = ko.mapping.fromJS(initial.collections);
  530. self.collections.subscribe(function (newValue) {
  531. vm.collection.name(newValue);
  532. });
  533. self.layout = initial.layout;
  534. };
  535. var DATE_TYPE = ['date', 'tdate'];
  536. var SearchViewModel = function (collection_json, query_json, initial_json) {
  537. var self = this;
  538. // Models
  539. self.collection = new Collection(self, collection_json.collection);
  540. self.query = new Query(self, query_json);
  541. self.initial = new NewTemplate(self, initial_json);
  542. if (initial_json.collections) {
  543. self.collection.name.subscribe(function (newValue) {
  544. self.collection.label(newValue);
  545. });
  546. }
  547. // UI
  548. self.response = ko.observable({});
  549. self.results = ko.observableArray([]);
  550. self.norm_facets = ko.computed(function () {
  551. return self.response().normalized_facets;
  552. });
  553. self.getFacetFromQuery = function (facet_id) {
  554. var _facet = null;
  555. if (self.norm_facets() !== undefined) {
  556. $.each(self.norm_facets(), function (index, norm_facet) {
  557. if (norm_facet.id == facet_id) {
  558. _facet = norm_facet;
  559. }
  560. });
  561. }
  562. return _facet;
  563. };
  564. self.previewColumns = ko.observable("");
  565. self.columns = ko.observable([]);
  566. loadLayout(self, collection_json.layout);
  567. self.isEditing = ko.observable(false);
  568. self.toggleEditing = function () {
  569. self.isEditing(!self.isEditing());
  570. };
  571. self.isRetrievingResults = ko.observable(false);
  572. self.draggableHit = ko.observable(new Widget(12, UUID(), "Hit Count", "hit-widget"));
  573. self.draggableFacet = ko.observable(new Widget(12, UUID(), "Facet", "facet-widget"));
  574. self.draggableResultset = ko.observable(new Widget(12, UUID(), "Grid Results", "resultset-widget"));
  575. self.draggableHtmlResultset = ko.observable(new Widget(12, UUID(), "HTML Results", "html-resultset-widget"));
  576. self.draggableHistogram = ko.observable(new Widget(12, UUID(), "Histogram", "histogram-widget"));
  577. self.draggableBar = ko.observable(new Widget(12, UUID(), "Bar Chart", "bar-widget"));
  578. self.draggableMap = ko.observable(new Widget(12, UUID(), "Map", "map-widget"));
  579. self.draggableLine = ko.observable(new Widget(12, UUID(), "Line Chart", "line-widget"));
  580. self.draggablePie = ko.observable(new Widget(12, UUID(), "Pie Chart", "pie-widget"));
  581. self.draggableFilter = ko.observable(new Widget(12, UUID(), "Filter Bar", "filter-widget"));
  582. self.availableDateFields = ko.computed(function() {
  583. return $.grep(self.collection.availableFacetFields(), function(field) { return DATE_TYPE.indexOf(field.type()) != -1; });
  584. });
  585. function getWidgets(equalsTo) {
  586. return $.map(self.columns(), function (col){return $.map(col.rows(), function(row){ return $.grep(row.widgets(), function(widget){ return equalsTo(widget); });}) ;})
  587. };
  588. self.availableDraggableResultset = ko.computed(function() {
  589. return getWidgets(function(widget){ return ['resultset-widget', 'html-resultset-widget'].indexOf(widget.widgetType()) != -1; }).length == 0;
  590. });
  591. self.availableDraggableFilter = ko.computed(function() {
  592. return getWidgets(function(widget){ return widget.widgetType() == 'filter-widget'; }).length == 0;
  593. });
  594. self.availableDraggableHistogram = ko.computed(function() {
  595. return getWidgets(function(widget){ return widget.widgetType() == 'histogram-widget'; }).length == 0 &&
  596. self.availableDateFields().length > 0;
  597. });
  598. self.availableDraggableChart = ko.computed(function() {
  599. return self.collection.availableFacetFields().length > 0;
  600. });
  601. self.init = function () {
  602. //self.collection.addDynamicFields();
  603. self.isEditing(true);
  604. self.search();
  605. }
  606. self.searchBtn = function () {
  607. self.query.start(0);
  608. self.search();
  609. };
  610. self.search = function () {
  611. self.isRetrievingResults(true);
  612. $(".jHueNotify").hide();
  613. // Multi queries
  614. var multiQs = [];
  615. var multiQ = self.query.getMultiq();
  616. if (multiQ != null) {
  617. var facet = {};
  618. var queries = [];
  619. if (multiQ == 'query') {
  620. queries = self.query.qs();
  621. } else {
  622. facet = self.query.getFacetFilter(self.query.selectedMultiq());
  623. queries = facet.filter();
  624. }
  625. multiQs = $.map(queries, function(qdata) {
  626. return $.post("/search/get_timeline", {
  627. collection: ko.mapping.toJSON(self.collection),
  628. query: ko.mapping.toJSON(self.query),
  629. facet: ko.mapping.toJSON(facet),
  630. qdata: ko.mapping.toJSON(qdata),
  631. multiQ: multiQ
  632. }, function (data) {return data});
  633. });
  634. }
  635. $.when.apply($, [
  636. $.post("/search/search", {
  637. collection: ko.mapping.toJSON(self.collection),
  638. query: ko.mapping.toJSON(self.query),
  639. layout: ko.mapping.toJSON(self.columns)
  640. }, function (data) {
  641. self.response(data);
  642. self.results.removeAll();
  643. if (data.error) {
  644. $(document).trigger("error", data.error);
  645. }
  646. else {
  647. if (self.collection.template.isGridLayout()) {
  648. // Table view
  649. $.each(data.response.docs, function (index, item) {
  650. var row = [];
  651. var fields = self.collection.template.fieldsSelected();
  652. // Display selected fields or whole json document
  653. if (fields.length != 0) {
  654. $.each(self.collection.template.fieldsSelected(), function (index, field) {
  655. row.push(item[field]);
  656. });
  657. } else {
  658. row.push(ko.mapping.toJSON(item));
  659. }
  660. var doc = {'id': item[self.collection.idField], 'row': row};
  661. self.results.push(doc);
  662. });
  663. }
  664. else {
  665. // Template view
  666. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  667. $.each(data.response.docs, function (index, item) {
  668. addTemplateFunctions(item);
  669. self.results.push(Mustache.render(_mustacheTmpl, item));
  670. });
  671. }
  672. self.isRetrievingResults(false);
  673. }
  674. })
  675. ].concat(multiQs)
  676. )
  677. .done(function() {
  678. if (arguments[0] instanceof Array) { // If multi queries
  679. var histoFacetId = self.collection.getHistogramFacet().id();
  680. var histoFacet = self.getFacetFromQuery(histoFacetId);
  681. for (var i = 1; i < arguments.length; i++) {
  682. histoFacet.extraSeries.push(arguments[i][0]['series']);
  683. }
  684. self.response.valueHasMutated();
  685. }
  686. })
  687. .fail(function (xhr, textStatus, errorThrown) {
  688. $(document).trigger("error", xhr.responseText);
  689. })
  690. .always(function () {
  691. $('.btn-loading').button('reset');
  692. });
  693. };
  694. self.removeWidget = function (widget_json) {
  695. self.collection.removeFacet(widget_json.id);
  696. self.query.removeFilter(widget_json);
  697. self.removeWidgetById(widget_json.id());
  698. self.search();
  699. }
  700. self.getWidgetById = function (widget_id) {
  701. var _widget = null;
  702. $.each(self.columns(), function (i, col) {
  703. $.each(col.rows(), function (j, row) {
  704. $.each(row.widgets(), function (z, widget) {
  705. if (widget.id() == widget_id){
  706. _widget = widget;
  707. }
  708. });
  709. });
  710. });
  711. return _widget;
  712. }
  713. self.removeWidgetById = function (widget_id) {
  714. $.each(self.columns(), function (i, col) {
  715. $.each(col.rows(), function (j, row) {
  716. $.each(row.widgets(), function (z, widget) {
  717. if (widget.id() == widget_id){
  718. row.widgets.remove(widget);
  719. }
  720. });
  721. });
  722. });
  723. }
  724. self.getDocument = function (doc) {
  725. $.post("/search/get_document", {
  726. collection: ko.mapping.toJSON(self.collection),
  727. id: doc.id
  728. },function (data) {
  729. if (data.status == 0) {
  730. $(document).trigger("showDoc", data.doc.doc);
  731. }
  732. else if (data.status == 1) {
  733. $(document).trigger("info", data.message);
  734. }
  735. else {
  736. $(document).trigger("error", data.message);
  737. }
  738. }).fail(function (xhr, textStatus, errorThrown) {
  739. $(document).trigger("error", xhr.responseText);
  740. });
  741. };
  742. self.save = function () {
  743. $.post("/search/save", {
  744. collection: ko.mapping.toJSON(self.collection),
  745. layout: ko.mapping.toJSON(self.columns)
  746. },function (data) {
  747. if (data.status == 0) {
  748. self.collection.id = data.id;
  749. $(document).trigger("info", data.message);
  750. }
  751. else {
  752. $(document).trigger("error", data.message);
  753. }
  754. }).fail(function (xhr, textStatus, errorThrown) {
  755. $(document).trigger("error", xhr.responseText);
  756. });
  757. };
  758. };