search.ko.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. // Start dashboard lib to move out
  17. var Column = function (size, rows) {
  18. var self = this;
  19. self.size = ko.observable(size);
  20. self.rows = ko.observableArray(rows);
  21. self.klass = ko.computed(function () {
  22. return "card card-home card-column span" + self.size();
  23. });
  24. self.addEmptyRow = function () {
  25. self.addRow();
  26. };
  27. self.addRow = function (row) {
  28. if (typeof row == "undefined" || row == null) {
  29. row = new Row([]);
  30. }
  31. self.rows.push(row);
  32. };
  33. }
  34. var Row = function (widgets) {
  35. var self = this;
  36. self.widgets = ko.observableArray(widgets);
  37. self.addWidget = function (widget) {
  38. self.widgets.push(widget);
  39. };
  40. self.move = function (from, to) {
  41. try {
  42. viewModel.columns()[to].addRow(self);
  43. viewModel.columns()[from].rows.remove(self);
  44. }
  45. catch (exception) {
  46. }
  47. }
  48. self.moveDown = function (col, row) {
  49. var _i = col.rows().indexOf(row);
  50. if (_i < col.rows().length - 1) {
  51. var _arr = col.rows();
  52. col.rows.splice(_i, 2, _arr[_i + 1], _arr[_i]);
  53. }
  54. }
  55. self.moveUp = function (col, row) {
  56. var _i = col.rows().indexOf(row);
  57. if (_i >= 1) {
  58. var _arr = col.rows();
  59. col.rows.splice(_i - 1, 2, _arr[_i], _arr[_i - 1]);
  60. }
  61. }
  62. self.remove = function (col, row) {
  63. col.rows.remove(row);
  64. }
  65. }
  66. // A widget is generic. It has an id that refer to another object (e.g. facet) with the same id.
  67. var Widget = function (size, id, name, widgetType, properties, offset) {
  68. var self = this;
  69. self.size = ko.observable(size).extend({ numeric: 0 });
  70. self.name = ko.observable(name);
  71. self.id = ko.observable(id);
  72. self.widgetType = ko.observable(typeof widgetType != "undefined" && widgetType != null ? widgetType : "empty-widget");
  73. self.properties = ko.observable(typeof properties != "undefined" && properties != null ? properties : {});
  74. self.offset = ko.observable(typeof offset != "undefined" && offset != null ? offset : 0).extend({ numeric: 0 });
  75. self.isLoading = ko.observable(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([3, 9]);
  104. }
  105. function oneThirdRightLayout() {
  106. setLayout([9, 3]);
  107. }
  108. function magicLayout() {
  109. setLayout([3, 9]);
  110. alert('Hue picked a timeline, filter, result, pie bar, widgets...');
  111. }
  112. function setLayout(colSizes) {
  113. // Save previous widgets
  114. var _allRows = [];
  115. $(viewModel.columns()).each(function (cnt, col) {
  116. var _tRows = [];
  117. $(col.rows()).each(function (icnt, row) {
  118. if (row.widgets().length > 0) {
  119. _tRows.push(row);
  120. }
  121. });
  122. _allRows = _allRows.concat(_tRows);
  123. });
  124. var _cols = [];
  125. var _highestCol = {
  126. idx: -1,
  127. size: -1
  128. };
  129. $(colSizes).each(function (cnt, size) {
  130. _cols.push(new Column(size, []));
  131. if (size > _highestCol.size) {
  132. _highestCol.idx = cnt;
  133. _highestCol.size = size;
  134. }
  135. });
  136. if (_allRows.length > 0 && _highestCol.idx > -1) {
  137. _cols[_highestCol.idx].rows(_allRows);
  138. }
  139. $(_cols).each(function (cnt, col) {
  140. if (col.rows().length == 0) {
  141. col.rows([new Row([])]);
  142. }
  143. });
  144. viewModel.columns(_cols);
  145. }
  146. function loadLayout(viewModel, json_layout) {
  147. var _columns = [];
  148. $(json_layout).each(function (cnt, json_col) {
  149. var _rows = [];
  150. $(json_col.rows).each(function (rcnt, json_row) {
  151. var row = new Row();
  152. $(json_row.widgets).each(function (wcnt, widget) {
  153. row.addWidget(new Widget(widget.size, widget.id, widget.name, widget.widgetType, widget.properties, widget.offset));
  154. });
  155. _rows.push(row);
  156. });
  157. var column = new Column(json_col.size, _rows);
  158. _columns = _columns.concat(column);
  159. });
  160. viewModel.columns(_columns);
  161. }
  162. // End dashboard lib
  163. var Query = function (vm, query) {
  164. var self = this;
  165. self.qs = ko.mapping.fromJS(query.qs);
  166. self.fqs = ko.mapping.fromJS(query.fqs);
  167. var defaultMultiqGroup = {'id': 'query', 'label': 'query'};
  168. self.multiqs = ko.computed(function () { // List of widgets supporting multiqs
  169. return [defaultMultiqGroup].concat(
  170. $.map($.grep(self.fqs(), function(fq, i) {
  171. return fq.type() == 'field';
  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. var fq = self.getFacetFilter(data.widget_id);
  237. var unselect = fq != null && fq.id() == data.widget_id && data.force == undefined;
  238. // if range already in --> unselect
  239. // if not: add
  240. //var filter = self.getFacetFilter(data.widget_id);
  241. //if (filter != null && filter.filter().indexOf(data.from) > -1) {
  242. //
  243. //}
  244. // if empty now or force
  245. self.removeFilter(ko.mapping.fromJS({'id': data.widget_id}));
  246. if (! unselect) {
  247. self.fqs.push(ko.mapping.fromJS({
  248. 'id': data.widget_id,
  249. 'field': data.cat,
  250. 'filter': [data.from],
  251. 'properties': {'from': data.from, 'to': data.to},
  252. 'type': 'range'
  253. }));
  254. }
  255. if (data.no_refresh == undefined) {
  256. vm.search();
  257. }
  258. };
  259. self.removeFilter = function (data) {
  260. $.each(self.fqs(), function (index, fq) {
  261. if (fq.id() == data.id()) {
  262. self.fqs.remove(fq);
  263. return false;
  264. }
  265. });
  266. };
  267. };
  268. var Collection = function (vm, collection) {
  269. var self = this;
  270. self.id = collection.id;
  271. self.name = collection.name;
  272. self.label = collection.label;
  273. self.idField = collection.idField;
  274. self.template = ko.mapping.fromJS(collection.template);
  275. self.template.fieldsSelected.subscribe(function () {
  276. vm.search();
  277. });
  278. self.template.template.subscribe(function () {
  279. vm.search();
  280. });
  281. self.template.isGridLayout.subscribe(function () {
  282. vm.search();
  283. });
  284. self.facets = ko.mapping.fromJS(collection.facets);
  285. $.each(self.facets(), function (index, facet) {
  286. facet.field.subscribe(function () {
  287. vm.search();
  288. });
  289. });
  290. self.fields = ko.mapping.fromJS(collection.fields);
  291. self.addFacet = function (facet_json) {
  292. $.post("/search/template/new_facet", {
  293. "collection": ko.mapping.toJSON(self),
  294. "id": facet_json.widget_id,
  295. "label": facet_json.name,
  296. "field": facet_json.name,
  297. "widget_type": facet_json.widgetType
  298. }, function (data) {
  299. if (data.status == 0) {
  300. var facet = ko.mapping.fromJS(data.facet);
  301. facet.field.subscribe(function () {
  302. vm.search();
  303. });
  304. self.facets.push(facet);
  305. vm.search();
  306. } else {
  307. $(document).trigger("error", data.message);
  308. }
  309. }).fail(function (xhr, textStatus, errorThrown) {});
  310. };
  311. self.removeFacet = function (widget_id) {
  312. $.each(self.facets(), function (index, facet) {
  313. if (facet.id() == widget_id()) {
  314. self.facets.remove(facet);
  315. return false;
  316. }
  317. });
  318. }
  319. self.getFacetById = function (facet_id) {
  320. var _facet = null;
  321. $.each(self.facets(), function (index, facet) {
  322. if (facet.id() == facet_id) {
  323. _facet = facet;
  324. return false;
  325. }
  326. });
  327. return _facet;
  328. }
  329. self.getHistogramFacet = function () { // might remove when list of available widgets
  330. var _facet = null;
  331. $.each(self.facets(), function (index, facet) {
  332. if (facet.widgetType() == 'histogram-widget') {
  333. _facet = facet;
  334. return false;
  335. }
  336. });
  337. return _facet;
  338. }
  339. self.template.fields = ko.computed(function () {
  340. var _fields = [];
  341. $.each(self.template.fieldsAttributes(), function (index, field) {
  342. var position = self.template.fieldsSelected.indexOf(field.name());
  343. if (position != -1) {
  344. _fields[position] = field;
  345. }
  346. });
  347. return _fields;
  348. });
  349. self.getTemplateField = function (name) {
  350. var _field = null;
  351. $.each(self.template.fields(), function (index, field) {
  352. if (field.name() == name) {
  353. _field = field;
  354. return false;
  355. }
  356. });
  357. return _field;
  358. };
  359. self.template.fieldsModalFilter = ko.observable("");
  360. self.template.filteredFieldsAttributes = ko.observableArray(self.template.fieldsAttributes());
  361. self.template.fieldsModalFilter.subscribe(function(value){
  362. var _fields = [];
  363. $.each(self.template.fieldsAttributes(), function (index, field) {
  364. if (field.name().toLowerCase().indexOf(self.template.fieldsModalFilter().toLowerCase()) > -1){
  365. _fields.push(field);
  366. }
  367. });
  368. self.template.filteredFieldsAttributes(_fields);
  369. });
  370. self.addDynamicFields = function () { // + Adding merge smartly if schema updated
  371. $.post("/search/index/" + self.id + "/fields/dynamic", {
  372. }, function (data) {
  373. if (data.status == 0) {
  374. $.each(data.dynamic_fields, function (index, field) {
  375. if (self.fields.indexOf(field) == -1) {
  376. self.fields.push(field);
  377. }
  378. });
  379. }
  380. }).fail(function (xhr, textStatus, errorThrown) {});
  381. }
  382. self.toggleSortColumnGridLayout = function (template_field) {
  383. if (! template_field.sort.direction()) {
  384. template_field.sort.direction('desc');
  385. } else if (template_field.sort.direction() == 'desc') {
  386. template_field.sort.direction('asc');
  387. } else {
  388. template_field.sort.direction(null);
  389. }
  390. vm.search();
  391. };
  392. self.toggleSortFacet = function (facet_field, event) {
  393. if (facet_field.properties.sort() == 'desc') {
  394. facet_field.properties.sort('asc');
  395. } else {
  396. facet_field.properties.sort('desc');
  397. }
  398. $(event.target).button('loading');
  399. vm.search();
  400. };
  401. self.toggleRangeFacet = function (facet_field, event) {
  402. vm.query.removeFilter(ko.mapping.fromJS({'id': facet_field.id})); // Reset filter query
  403. if (facet_field.type() == 'field') {
  404. facet_field.type('range');
  405. } else if (facet_field.type() == 'range') {
  406. facet_field.type('field')
  407. }
  408. $(event.target).button('loading');
  409. vm.search();
  410. };
  411. self.selectTimelineFacet = function (data) { // alert(ko.mapping.toJSON(facet));
  412. var facet = self.getFacetById(data.widget_id);
  413. facet.properties.start(data.from);
  414. facet.properties.end(data.to);
  415. vm.query.selectRangeFacet({widget_id: data.widget_id, from: data.from, to: data.to, cat: data.cat, no_refresh: true, force: true});
  416. $.ajax({
  417. type: "POST",
  418. url: "/search/get_range_facet",
  419. data: {
  420. collection: ko.mapping.toJSON(self),
  421. facet: ko.mapping.toJSON(facet),
  422. action: 'select'
  423. },
  424. success: function (data) {
  425. if (data.status == 0) {
  426. facet.properties.gap(data.properties.gap);
  427. }
  428. },
  429. async: false
  430. });
  431. vm.search();
  432. }
  433. self.timeLineZoom = function (facet_json) {
  434. var facet = self.getFacetById(facet_json.id);
  435. facet.properties.start(facet.from);
  436. facet.properties.end(facet.to);
  437. $.ajax({
  438. type: "POST",
  439. url: "/search/get_range_facet",
  440. data: {
  441. collection: ko.mapping.toJSON(self),
  442. facet: ko.mapping.toJSON(facet),
  443. action: "zoom_out"
  444. },
  445. success: function (data) {
  446. if (data.status == 0) {
  447. facet.properties.start(data.properties.start);
  448. facet.properties.end(data.properties.end);
  449. facet.properties.gap(data.properties.gap);
  450. }
  451. },
  452. async: false
  453. });
  454. vm.search();
  455. }
  456. self.translateSelectedField = function (index, direction) {
  457. var array = self.template.fieldsSelected();
  458. if (direction == 'left') {
  459. self.template.fieldsSelected.splice(index - 1, 2, array[index], array[index - 1]);
  460. } else {
  461. self.template.fieldsSelected.splice(index, 2, array[index + 1], array[index]);
  462. }
  463. vm.search();
  464. };
  465. };
  466. var SearchViewModel = function (collection_json, query_json) {
  467. var self = this;
  468. // Models
  469. self.collection = new Collection(self, collection_json.collection);
  470. self.query = new Query(self, query_json);
  471. // UI
  472. self.response = ko.observable({});
  473. self.results = ko.observableArray([]);
  474. self.norm_facets = ko.computed(function () {
  475. return self.response().normalized_facets;
  476. });
  477. self.getFacetFromQuery = function (facet_id) {
  478. var _facet = null;
  479. if (self.norm_facets() !== undefined) {
  480. $.each(self.norm_facets(), function (index, norm_facet) {
  481. if (norm_facet.id == facet_id) {
  482. _facet = norm_facet;
  483. }
  484. });
  485. }
  486. return _facet;
  487. };
  488. self.previewColumns = ko.observable("");
  489. self.columns = ko.observable({});
  490. loadLayout(self, collection_json.layout);
  491. self.isEditing = ko.observable(false);
  492. self.toggleEditing = function () {
  493. self.isEditing(!self.isEditing());
  494. };
  495. self.isRetrievingResults = ko.observable(false);
  496. self.draggableHit = ko.observable(new Widget(12, UUID(), "Hit Count", "hit-widget"));
  497. self.draggableFacet = ko.observable(new Widget(12, UUID(), "Facet", "facet-widget"));
  498. self.draggableResultset = ko.observable(new Widget(12, UUID(), "Results", "resultset-widget"));
  499. self.draggableHistogram = ko.observable(new Widget(12, UUID(), "Histogram", "histogram-widget"));
  500. self.draggableBar = ko.observable(new Widget(12, UUID(), "Bar Chart", "bar-widget"));
  501. self.draggableMap = ko.observable(new Widget(12, UUID(), "Map", "map-widget"));
  502. self.draggableLine = ko.observable(new Widget(12, UUID(), "Line Chart", "line-widget"));
  503. self.draggablePie = ko.observable(new Widget(12, UUID(), "Pie Chart", "pie-widget"));
  504. self.draggableFilter = ko.observable(new Widget(12, UUID(), "Filter Bar", "filter-widget"));
  505. self.init = function () {
  506. //self.collection.addDynamicFields();
  507. self.isEditing(true);
  508. self.search();
  509. }
  510. self.search = function () {
  511. self.isRetrievingResults(true);
  512. $(".jHueNotify").hide();
  513. // Multi queries
  514. var multiQs = [];
  515. var multiQ = self.query.getMultiq();
  516. if (multiQ != null) {
  517. var facet = {};
  518. var queries = [];
  519. if (multiQ == 'query') {
  520. queries = self.query.qs();
  521. } else {
  522. facet = facet = self.query.getFacetFilter(self.query.selectedMultiq());
  523. queries = facet.filter();
  524. }
  525. multiQs = $.map(queries, function(qdata) {
  526. return $.post("/search/get_timeline", {
  527. collection: ko.mapping.toJSON(self.collection),
  528. query: ko.mapping.toJSON(self.query),
  529. facet: ko.mapping.toJSON(facet),
  530. qdata: ko.mapping.toJSON(qdata),
  531. multiQ: multiQ
  532. }, function (data) {return data});
  533. });
  534. }
  535. $.when.apply($, [
  536. $.post("/search/search", {
  537. collection: ko.mapping.toJSON(self.collection),
  538. query: ko.mapping.toJSON(self.query),
  539. layout: ko.mapping.toJSON(self.columns)
  540. }, function (data) {
  541. self.response(data);
  542. self.results.removeAll();
  543. if (data.error) {
  544. $(document).trigger("error", data.error);
  545. }
  546. else {
  547. if (self.collection.template.isGridLayout()) {
  548. // Table view
  549. $.each(data.response.docs, function (index, item) {
  550. var row = [];
  551. var fields = self.collection.template.fieldsSelected();
  552. // Display selected fields or whole json document
  553. if (fields.length != 0) {
  554. $.each(self.collection.template.fieldsSelected(), function (index, field) {
  555. row.push(item[field]);
  556. });
  557. } else {
  558. row.push(ko.mapping.toJSON(item));
  559. }
  560. var doc = {'id': item[self.collection.idField], 'row': row};
  561. self.results.push(doc);
  562. });
  563. }
  564. else {
  565. // Template view
  566. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  567. $.each(data.response.docs, function (index, item) {
  568. addTemplateFunctions(item);
  569. self.results.push(Mustache.render(_mustacheTmpl, item));
  570. });
  571. }
  572. self.isRetrievingResults(false);
  573. }
  574. })
  575. ].concat(multiQs)
  576. )
  577. .done(function() {
  578. if (arguments[0] instanceof Array) { // If multi queries
  579. var histoFacetId = self.collection.getHistogramFacet().id();
  580. var histoFacet = self.getFacetFromQuery(histoFacetId);
  581. for (var i = 1; i < arguments.length; i++) {
  582. histoFacet.extraSeries.push(arguments[i][0]['series']);
  583. }
  584. self.response.valueHasMutated();
  585. }
  586. })
  587. .fail(function (xhr, textStatus, errorThrown) {
  588. $(document).trigger("error", xhr.responseText);
  589. })
  590. .always(function () {
  591. $('.btn-loading').button('reset');
  592. });
  593. };
  594. self.removeWidget = function (widget_json) {
  595. self.collection.removeFacet(widget_json.id);
  596. self.query.removeFilter(widget_json);
  597. self.search();
  598. }
  599. self.getWidgetById = function (widget_id) {
  600. var _widget = null;
  601. $.each(self.columns(), function (i, col) {
  602. $.each(col.rows(), function (j, row) {
  603. $.each(row.widgets(), function (z, widget) {
  604. if (widget.id() == widget_id){
  605. _widget = widget;
  606. }
  607. });
  608. });
  609. });
  610. return _widget;
  611. }
  612. self.getDocument = function (doc) {
  613. $.post("/search/get_document", {
  614. collection: ko.mapping.toJSON(self.collection),
  615. id: doc.id
  616. },function (data) {
  617. if (data.status == 0) {
  618. alert(ko.mapping.toJSON(data.doc.doc));
  619. }
  620. else if (data.status == 1) {
  621. $(document).trigger("info", data.message);
  622. }
  623. else {
  624. $(document).trigger("error", data.message);
  625. }
  626. }).fail(function (xhr, textStatus, errorThrown) {
  627. $(document).trigger("error", xhr.responseText);
  628. });
  629. };
  630. self.save = function () {
  631. $.post("/search/save", {
  632. collection: ko.mapping.toJSON(self.collection),
  633. layout: ko.mapping.toJSON(self.columns)
  634. },function (data) {
  635. if (data.status == 0) {
  636. self.collection.id = data.id;
  637. $(document).trigger("info", data.message);
  638. }
  639. else {
  640. $(document).trigger("error", data.message);
  641. }
  642. }).fail(function (xhr, textStatus, errorThrown) {
  643. $(document).trigger("error", xhr.responseText);
  644. });
  645. };
  646. };