search.ko.js 22 KB

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