controls.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. var searchRenderers = {
  17. 'rowkey': { //class to tag selection
  18. select: /[^\,\{\[]+(([\{\[][^\}\]]+[\}\]])+|)([^\,]+|)/g, //select the substring to process, useful as JS has no lookbehinds old: ([^,]+\[([^,]+(,|)+)+\]|[^,]+)
  19. tag: /.+/g, //select the matches to wrap with tags
  20. nested: {
  21. 'scan': { select: /\+[0-9 ]+/g, tag: /.+/g},
  22. 'columns': { select: /\[.+\]/g, tag: /[^:,\[\]]+:([^,\[\]]+|)/g}, //forced to do this select due to lack of lookbehinds /[\[\]]/g
  23. 'prefix': { select: /[^\*]+\*/g, tag: /\*/g},
  24. 'filter': {
  25. select: /\{[^\{\}]+\}/,
  26. tag:/.+/g,
  27. nested: {
  28. 'linker': {
  29. select: /AND|OR|SKIP|WHILE/g,
  30. tag: /.+/g
  31. }/*,
  32. 'compare_op': {
  33. select: /[\<\=\!\>]{1,2}/g,
  34. tag: /.+/g
  35. }*/ //will be added eventually after html bug is figured out
  36. }
  37. }
  38. }
  39. }
  40. };
  41. var DataTableViewModel = function(options)
  42. {
  43. var self = this, _defaults = {
  44. name: '',
  45. columns: [],
  46. items: [],
  47. reload: function()
  48. {
  49. },
  50. el:''
  51. };
  52. options = ko.utils.extend(_defaults,options);
  53. ListViewModel.apply(this, [options]);
  54. self.name = ko.observable(options.name);
  55. self.searchQuery.subscribe(function(value)
  56. {
  57. self._table.fnFilter(value);
  58. });
  59. self.columns = ko.observableArray(options.columns);
  60. self._el = $('table[data-datasource="' + options.el + '"]');
  61. self._table = null;
  62. self._initTable = function()
  63. {
  64. if(!self._table)
  65. {
  66. self._table = self._el.dataTable({
  67. "aoColumnDefs": [
  68. { "bSortable": false, "aTargets": [ 0 ] }
  69. ],
  70. "sDom": 'tr',//this has to be in, change to sDom so you can call filter()
  71. 'bAutoWidth':false,
  72. "iDisplayLength": -1});
  73. return self._table;
  74. }
  75. };
  76. self.sort = function(viewModel, event) {
  77. var el = $(event.currentTarget);
  78. };
  79. var _reload = self.reload;
  80. self.reload = function(callback)
  81. {
  82. logGA('view_cluster');
  83. if(self._table)
  84. {
  85. self._table.fnClearTable();
  86. self._table.fnDestroy();
  87. self._table = null;
  88. }
  89. _reload(function()
  90. {
  91. self._initTable();
  92. if(callback!=null)
  93. callback();
  94. });
  95. };
  96. };
  97. //a Listview of Listviews
  98. var SmartViewModel = function(options)
  99. {
  100. var self = this;
  101. options = ko.utils.extend({
  102. name: '',
  103. items: [],
  104. reload: function()
  105. {
  106. },
  107. el:'',
  108. sortFields: {
  109. 'Row Key': function(a, b)
  110. {
  111. return a.row.localeCompare(b.row);
  112. },
  113. 'Column Count': function(a, b)
  114. {
  115. a = a.items().length;
  116. b = b.items().length;
  117. if(a > b)
  118. return 1;
  119. if(a < b)
  120. return -1;
  121. return 0;
  122. },
  123. 'Row Key Length': function(a, b)
  124. {
  125. a = a.row.length;
  126. b = b.row.length;
  127. if(a > b)
  128. return 1;
  129. if(a < b)
  130. return -1;
  131. return 0;
  132. }
  133. }
  134. }, options);
  135. ListViewModel.apply(this, [options]); //items: [ListView.items[],ListView.items[]]
  136. self.columnFamilies = ko.observableArray();
  137. self.name = ko.observable(options.name);
  138. self.name.subscribe(function(){
  139. self.querySet.removeAll();
  140. self.querySet.push(new QuerySetPeice({
  141. 'row_key': 'null',
  142. 'scan_length': 10,
  143. 'prefix': 'false'
  144. }));
  145. self._reloadcfs();
  146. }); //fix and decouple
  147. self.lastReloadTime = ko.observable(1);
  148. //self.columnFamilies.subscribe(function(){self.reload();});
  149. self.searchQuery.subscribe(function goToRow(value) //make this as nice as the renderfucnction and split into two, also fire not down on keyup events
  150. {
  151. var inputs = value.match(searchRenderers['rowkey']['select']);
  152. self.querySet.removeAll();
  153. for(var i=0;i<inputs.length;i++)
  154. {
  155. if(inputs[i].trim() != "" && inputs[i].trim() != ',')
  156. {
  157. var p = inputs[i].split('+');
  158. var scan = p.length > 1 ? parseInt(p[1].trim()) : 0;
  159. var extract = inputs[i].match(searchRenderers['rowkey']['nested']['columns']['select']);
  160. var columns = extract != null ? extract[0].match(searchRenderers['rowkey']['nested']['columns']['tag']) : [];
  161. var filter = inputs[i].match(searchRenderers['rowkey']['nested']['filter']['select']);
  162. self.querySet.push(new QuerySetPeice({
  163. 'row_key': p[0].replace(/[\[\{].+[\]\}]|\*/g,'').trim(), //clean up with column regex selectors instead
  164. 'scan_length': scan ? scan + 1 : 1,
  165. 'columns': columns,
  166. 'prefix': inputs[i].match(searchRenderers['rowkey']['nested']['prefix']['select']) != null,
  167. 'filter': filter != null && filter.length > 0 ? escape(filter[0].slice(1, -1)) : null
  168. }));
  169. }
  170. }
  171. routie(app.cluster() + '/' + app.views.tabledata.name() +'/query/' + value);
  172. self.evaluateQuery();
  173. });
  174. self._reloadcfs = function()
  175. {
  176. return API.queryTable("getColumnDescriptors").done(function(data)
  177. {
  178. self.columnFamilies.removeAll();
  179. var keys = Object.keys(data);
  180. for(var i=0;i<keys.length;i++)
  181. {
  182. self.columnFamilies.push(new ColumnFamily({name:keys[i], enabled:false}));
  183. }
  184. self.reload();
  185. });
  186. };
  187. self.columnQuery = ko.observable("");
  188. self.columnQuery.subscribe(function(query)
  189. {
  190. $(self.items()).each(function()
  191. {
  192. this.searchQuery(query);
  193. });
  194. });
  195. self.rows = ko.computed(function()
  196. {
  197. var a = [];
  198. var items = this.items();
  199. for(var i=0; i<items.length; i++)
  200. {
  201. a.push(items[i].row);
  202. }
  203. return a;
  204. }, self);
  205. self.querySet = ko.observableArray();
  206. self.validateQuery = function()
  207. {
  208. $(self.querySet()).each(function()
  209. {
  210. this.validate();
  211. this.editing(false);
  212. });
  213. };
  214. self.addQuery = function()
  215. {
  216. self.validateQuery();
  217. self.querySet.push(new QuerySetPeice({onValidate: function()
  218. {
  219. //self.reload();
  220. }}))
  221. };
  222. self.evaluateQuery = function(callback)
  223. {
  224. self.validateQuery();
  225. self.reload(callback);
  226. };
  227. var _reload = self.reload;
  228. self.reload = function(callback)
  229. {
  230. logGA('view_table');
  231. self.truncated = ko.observable(false);
  232. var queryStart = new Date();
  233. _reload(function()
  234. {
  235. self.lastReloadTime((new Date() - queryStart)/1000);
  236. if(callback!=null)
  237. callback();
  238. self.isLoading(false);
  239. });
  240. };
  241. self.truncated = ko.observable(false);
  242. self.truncateLimit = ko.observable(1500);
  243. self.truncateCount = ko.observable(0);
  244. };
  245. var SmartViewDataRow = function(options)
  246. {
  247. var self = this;
  248. options = ko.utils.extend({
  249. sortFields: {
  250. 'Column Family': function(a, b)
  251. {
  252. return a.name.localeCompare(b.name);
  253. },
  254. 'Column Name': function(a, b)
  255. {
  256. return a.name.split(':')[1].localeCompare(b.name.split(':')[1]);
  257. },
  258. 'Cell Value': function(a, b)
  259. {
  260. a = a.value.length;
  261. b = b.value.length;
  262. if(a > b)
  263. return 1;
  264. if(a < b)
  265. return -1;
  266. return 0;
  267. },
  268. 'Timestamp': function(a, b)
  269. {
  270. a = parseInt(a.timestamp);
  271. b = parseInt(b.timestamp);
  272. if(a > b)
  273. return 1;
  274. if(a < b)
  275. return -1;
  276. return 0;
  277. },
  278. 'MIME Type': function()
  279. {
  280. },
  281. 'Column Name Length': function(a, b)
  282. {
  283. a = a.name.split(':')[1].length;
  284. b = b.name.split(':')[1].length;
  285. if(a > b)
  286. return 1;
  287. if(a < b)
  288. return -1;
  289. return 0;
  290. }
  291. }
  292. }, options);
  293. DataRow.apply(self,[options]);
  294. ListViewModel.apply(self,[options]);
  295. self.displayedItems = ko.observableArray();
  296. self.displayRangeStart = 0;
  297. self.displayRangeLength = 20;
  298. self.items.subscribe(function()
  299. {
  300. self.displayedItems([]);
  301. self.updateDisplayedItems();
  302. });
  303. self.searchQuery.subscribe(function(searchValue)
  304. {
  305. self.scrollLoadSource = ko.computed(function(){
  306. return self.items().filter(function(column)
  307. {
  308. return column.name.toLowerCase().indexOf(searchValue.toLowerCase()) != -1;
  309. });
  310. });
  311. self.displayRangeLength = 20;
  312. self.updateDisplayedItems();
  313. });
  314. self.scrollLoadSource = self.items;
  315. self.updateDisplayedItems = function()
  316. {
  317. var x = self.displayRangeStart;
  318. self.displayedItems(self.scrollLoadSource().slice(x, x + self.displayRangeLength));
  319. };
  320. self.resetScrollLoad = function()
  321. {
  322. self.scrollLoadSource = self.items;
  323. self.updateDisplayedItems();
  324. };
  325. self.toggleSelectedCollapse = function()
  326. {
  327. if(self.displayedItems().length == self.displayRangeStart + self.displayRangeLength)
  328. {
  329. self.displayedItems(self.displayedItems().filter(function(item)
  330. {
  331. return item.isSelected();
  332. }));
  333. self.scrollLoadSource = self.displayedItems;
  334. }
  335. else
  336. {
  337. self.resetScrollLoad();
  338. }
  339. };
  340. self.onScroll = function(target, ev)
  341. {
  342. if($(ev.target).scrollLeft() == ev.target.scrollWidth - ev.target.clientWidth && self.displayedItems().length < self.scrollLoadSource().length)
  343. {
  344. self.displayRangeLength += 15;
  345. self.updateDisplayedItems();
  346. }
  347. };
  348. self.drop = function(cont)
  349. {
  350. function doDrop()
  351. {
  352. self.isLoading(true);
  353. return API.queryTable('deleteAllRow', self.row, "{}").complete(function()
  354. {
  355. app.views.tabledata.items.remove(self); //decouple later
  356. self.isLoading(false);
  357. });
  358. }
  359. (cont === true) ? doDrop() : confirm("Confirm Delete", 'Delete row ' + self.row + '? (This cannot be undone)', doDrop);
  360. };
  361. self.setItems = function(cols)
  362. {
  363. var colKeys = Object.keys(cols);
  364. var items = [];
  365. for(var q=0;q<colKeys.length;q++)
  366. {
  367. items.push(new ColumnRow({name: colKeys[q],
  368. timestamp: cols[colKeys[q]].timestamp,
  369. value: cols[colKeys[q]].value,
  370. parent: self}));
  371. }
  372. self.items(items);
  373. return self.items();
  374. };
  375. self.selectAllVisible = function(){
  376. for(t=0;t<self.displayedItems().length;t++)
  377. self.displayedItems()[t].isSelected(true);
  378. return self;
  379. };
  380. self.toggleSelectAllVisible = function()
  381. {
  382. if(self.selected().length != self.displayedItems().length)
  383. return self.selectAllVisible();
  384. return self.deselectAll();
  385. };
  386. self.push = function(item)
  387. {
  388. var column = new ColumnRow(item);
  389. self.items.push(column);
  390. };
  391. var _reload = self.reload;
  392. self.reload = function(callback)
  393. {
  394. logGA('get_row');
  395. _reload(function()
  396. {
  397. if(callback!=null)
  398. callback();
  399. self.isLoading(false);
  400. });
  401. };
  402. };
  403. var ColumnRow = function(options)
  404. {
  405. var self = this;
  406. ko.utils.extend(self,options);
  407. DataRow.apply(self,[options]);
  408. self.value = ko.observable(self.value);
  409. self.history = new CellHistoryPage({row: self.parent.row, column: self.name, timestamp: self.timestamp, items: []});
  410. self.drop = function(cont)
  411. {
  412. function doDrop()
  413. {
  414. logGA('filter_columns');
  415. self.parent.isLoading(true);
  416. return API.queryTable('deleteColumn', self.parent.row, self.name).done(function(data)
  417. {
  418. self.parent.items.remove(self);
  419. if(self.parent.items().length > 0)
  420. self.parent.reload(); //change later
  421. self.parent.isLoading(false);
  422. });
  423. }
  424. (cont === true) ? doDrop() : confirm("Confirm Delete", "Are you sure you want to drop this column?", doDrop);
  425. };
  426. self.reload = function(callback, skipPut)
  427. {
  428. self.isLoading(true);
  429. API.queryTable('get', self.parent.row, self.name, 'null').done(function(data)
  430. {
  431. if(data.length > 0 && !skipPut)
  432. self.value(data[0].value);
  433. if(typeof callback !== "undefined" && callback != null)
  434. callback();
  435. self.isLoading(false);
  436. });
  437. };
  438. self.value.subscribe(function(value)
  439. {
  440. //change transport prep to object wrapper
  441. logGA('put_column');
  442. API.queryTable('putColumn', self.parent.row, self.name, "hbase-post-key-" + JSON.stringify(value)).done(function(data)
  443. {
  444. self.reload(function(){}, true);
  445. });
  446. self.editing(false);
  447. });
  448. self.editing = ko.observable(false);
  449. self.isLoading = ko.observable(false); //move to baseclass
  450. };
  451. var SortDropDownView = function(options)
  452. {
  453. var self = this;
  454. options = ko.utils.extend({
  455. sortFields: {},
  456. target: null
  457. }, options);
  458. BaseModel.apply(self,[options]);
  459. self.target = options.target;
  460. self.sortAsc = ko.observable(true);
  461. self.sortAsc.subscribe(function(){self.sort()});
  462. self.sortField = ko.observable("");
  463. self.sortField.subscribe(function(){self.sort()});
  464. self.sortFields = ko.observableArray(Object.keys(options.sortFields)); // change to ko.computed?
  465. self.sortFunctionHash = ko.observable(options.sortFields);
  466. self.toggleSortMode = function()
  467. {
  468. self.sortAsc(!self.sortAsc());
  469. };
  470. self.sort = function()
  471. {
  472. if (!self.target || !(self.sortFields().length > 0)) return;
  473. self.target.sort(function(a, b)
  474. {
  475. return (self.sortAsc() ? 1 : -1) * self.sortFunctionHash()[self.sortField() ? self.sortField() : self.sortFields()[0]](a,b); //all sort functions must sort by ASC for default
  476. });
  477. };
  478. };
  479. var TableDataRow = function(options)
  480. {
  481. var self = this;
  482. options = ko.utils.extend({
  483. name:"",
  484. enabled:true
  485. }, options);
  486. DataRow.apply(self,[options]);
  487. self.name = options['name'];
  488. self.enabled = ko.observable(options['enabled']);
  489. self.toggle = function(viewModel,event){
  490. var action = ['enable','disable'][self.enabled() << 0], el = $(event.currentTarget);
  491. confirm("Confirm "+action, "Are you sure you want to " + action + " this table?", function() //gotta i18n this!
  492. {
  493. el.showIndicator();
  494. return self[action](el).complete(function()
  495. {
  496. el.hideIndicator();
  497. });
  498. });
  499. };
  500. self.enable = function(el)
  501. {
  502. return API.queryCluster('enableTable',self.name).complete(function()
  503. {
  504. self.enabled(true);
  505. });
  506. };
  507. self.disable = function(el)
  508. {
  509. return API.queryCluster('disableTable',self.name).complete(function()
  510. {
  511. self.enabled(false);
  512. });
  513. };
  514. self.drop = function(el)
  515. {
  516. return API.queryCluster('deleteTable',self.name);
  517. };
  518. };
  519. var QuerySetPeice = function(options)
  520. {
  521. var self = this;
  522. options = ko.utils.extend({
  523. row_key: "null",
  524. scan_length: 1,
  525. prefix: false,
  526. columns: [],
  527. filter: null,
  528. onValidate: function(){}
  529. }, options);
  530. BaseModel.apply(self,[options]);
  531. self.row_key = ko.observable(options.row_key);
  532. self.scan_length = ko.observable(options.scan_length);
  533. self.columns = ko.observableArray(options.columns);
  534. self.prefix = ko.observable(options.prefix);
  535. self.filter = ko.observable(options.filter);
  536. self.editing = ko.observable(true);
  537. self.validate = function()
  538. {
  539. if(self.scan_length() <= 0 || self.row_key() == "")
  540. return app.views.tabledata.querySet.remove(self); //change later
  541. return options.onValidate();
  542. };
  543. self.row_key.subscribe(self.validate.bind());
  544. self.scan_length.subscribe(self.validate.bind());
  545. };
  546. var ColumnFamily = function(options)
  547. {
  548. this.name = options.name;
  549. this.enabled = ko.observable(options.enabled);
  550. this.toggle = function()
  551. {
  552. this.enabled(!this.enabled());
  553. app.views.tabledata.reload();
  554. };
  555. }
  556. //tagsearch
  557. var tagsearch = function()
  558. {
  559. var self = this;
  560. self.tags = ko.observableArray();
  561. self.mode = ko.observable('idle');
  562. self.cur_input = ko.observable('');
  563. self.hints = ko.observableArray([
  564. {
  565. hint: 'End Query',
  566. shortcut: ',',
  567. mode: ['rowkey', 'prefix', 'scan'],
  568. selected: false
  569. },
  570. {
  571. hint: 'Mark Row Prefix',
  572. shortcut: '*',
  573. mode: ['rowkey'],
  574. selected: false
  575. },
  576. {
  577. hint: 'Start Scan',
  578. shortcut: '+',
  579. mode: ['rowkey', 'prefix'],
  580. selected: false
  581. },
  582. {
  583. hint: 'Start Select Columns',
  584. shortcut: '[',
  585. mode: ['rowkey', 'prefix'],
  586. selected: false
  587. },
  588. {
  589. hint: 'End Column/Family',
  590. shortcut: ',',
  591. mode: ['columns'],
  592. selected: false
  593. },
  594. {
  595. hint: 'End Select Columns',
  596. shortcut: ']',
  597. mode: ['columns'],
  598. selected: false
  599. },
  600. {
  601. hint: 'Start FilterString',
  602. shortcut: '{',
  603. mode: ['rowkey'],
  604. selected: false
  605. },
  606. {
  607. hint: 'End FilterString',
  608. shortcut: '}',
  609. mode: ['filter'],
  610. selected: false
  611. }
  612. ]);
  613. self.activeHints = ko.computed(function()
  614. {
  615. var ret = [];
  616. $(self.hints()).each(function(i, hint)
  617. {
  618. if (hint.mode.indexOf(self.mode()) > -1)
  619. ret.push(hint);
  620. });
  621. return ret;
  622. });
  623. self.activeHint = ko.observable(-1);
  624. self.modes =
  625. {
  626. 'rowkey': {
  627. hint: 'Row Key Value',
  628. type: 'String'
  629. },
  630. 'scan': {
  631. hint: 'Length of Scan or Row Key',
  632. type: 'Integer'
  633. },
  634. 'columns': {
  635. hint: 'Column Family: Column Name',
  636. type: 'String'
  637. },
  638. 'prefix': {
  639. hint: 'Rows starting with',
  640. type: 'String'
  641. },
  642. 'filter': {
  643. hint: 'Thrift FilterString',
  644. type: 'String'
  645. }
  646. }
  647. self.modeQueue = ['idle'];
  648. self.focused = ko.observable(false);
  649. self.insertTag = function(tag)
  650. {
  651. var mode = tag.indexOf('+') != -1 ? 'scan' : 'rowkey';
  652. var tag = {value: tag, tag: mode} //parse mode
  653. self.tags.push(tag);
  654. }
  655. self.render = function(input, renderers)
  656. {
  657. var keys = Object.keys(renderers);
  658. for(var i=0; i<keys.length; i++)
  659. {
  660. input = input.replace(renderers[keys[i]].select, function(selected)
  661. {
  662. var hasMatched = false;
  663. var processed = selected.replace(renderers[keys[i]].tag, function(tagged)
  664. {
  665. hasMatched = true;
  666. return "<span class='" + keys[i] + " tagsearchTag' title='" + keys[i] + "' data-toggle='tooltip'>" + ('nested' in renderers[keys[i]] ? self.render(tagged, renderers[keys[i]].nested) : tagged) + "</span>";
  667. });
  668. if(hasMatched && renderers[keys[i]]['strip'])
  669. processed = processed.replace(renderers[keys[i]].strip, '');
  670. return processed;
  671. });
  672. }
  673. return input;
  674. };
  675. self.updateMode = function(value)
  676. {
  677. var selection = value.slice(0, self.selectionEnd());
  678. var endindex = selection.slice(selection.lastIndexOf(',')).indexOf(',');
  679. if(endindex == -1) endindex = selection.length;
  680. var lastbit = value.substring(selection.lastIndexOf(','), endindex).trim();
  681. if(lastbit == "," || lastbit == "")
  682. {
  683. self.mode('idle');
  684. return;
  685. }
  686. var tokens = "[]+,*{}";
  687. var m = 'rowkey';
  688. for(var i=selection.length - 1; i>=0; i--)
  689. {
  690. if(tokens.indexOf(selection[i]) != -1)
  691. {
  692. if(selection[i] == '{')
  693. m = 'filter';
  694. else if(selection[i] == '[')
  695. m = 'columns';
  696. else if(selection[i] == ']' || selection[i] == '}')
  697. m = 'rowkey';
  698. else if(selection[i] == '+')
  699. m = 'scan';
  700. else if(selection[i] == '-')
  701. m = 'prefix';
  702. break;
  703. }
  704. }
  705. self.mode(m.trim());
  706. };
  707. self.selectionStart = ko.observable(0);
  708. self.selectionEnd = ko.observable(0);
  709. self.hintText = ko.computed(function()
  710. {
  711. var value = self.cur_input();
  712. var selection = value.slice(0, self.selectionEnd());
  713. var index = selection.lastIndexOf(',') + 1;
  714. var endindex = value.slice(index).indexOf(',');
  715. endindex = endindex == -1 ? value.length : endindex;
  716. var pre = value.substring(index, index + endindex);
  717. var s = self.selectionStart() - index, e = self.selectionEnd() - index;
  718. if(s == e)
  719. e += 1;
  720. s = s < 0 ? 0 : s;
  721. e = e > pre.length ? pre.length : e;
  722. return pre.slice(0, s) + "<span class='selection'>" + pre.slice(s, e) + "</span>" + pre.slice(e);
  723. });
  724. self.onKeyDown = function(target, ev)
  725. {
  726. if(ev.keyCode == 13 && self.cur_input().slice(self.cur_input().lastIndexOf(',')).trim() != ",")
  727. {
  728. self.evaluate();
  729. return false;
  730. }
  731. setTimeout(self.updateMenu, 1);
  732. return true;
  733. };
  734. self.updateMenu = function() {
  735. try{
  736. var pos = getEditablePosition(document.getElementById('search-tags'));
  737. self.selectionStart(pos);
  738. self.selectionEnd(pos);
  739. } catch (err) {}
  740. self.updateMode(self.cur_input());
  741. };
  742. self.evaluate = function()
  743. {
  744. app.views.tabledata.searchQuery(self.cur_input());
  745. };
  746. $('#search-tags').blur(function(){
  747. self.focused(false);
  748. });
  749. self.doBlur = function() {
  750. if(self.cur_input().trim() == "") {
  751. function doClick() {
  752. $('#search-tags').html('');
  753. setTimeout(function() {
  754. $('#search-tags').focus();
  755. }, 1);
  756. }
  757. $('#search-tags').html('<small>' + $('#search-tags').data("placeholder") + '</small>').one('click', doClick).find('small').on('mousedown', doClick);
  758. }
  759. }
  760. $('#search-tags').focus(function(){
  761. self.focused(true);
  762. });
  763. };
  764. var CellHistoryPage = function(options)
  765. {
  766. var self = this;
  767. self.items = ko.observableArray(options.items);
  768. self.loading = ko.observable(false);
  769. self.reload = function(timestamp, append)
  770. {
  771. if(!timestamp)
  772. timestamp = options.timestamp
  773. API.queryTable("getVerTs", options.row, options.column, timestamp, 10, 'null').done(function(res)
  774. {
  775. self.loading = ko.observable(true);
  776. if(!append)
  777. self.items(res);
  778. else
  779. self.items(self.items() + res);
  780. self.loading = ko.observable(false);
  781. });
  782. };
  783. };