Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

541 lines
17 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>The source code</title>
  5. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  6. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  7. </head>
  8. <body onload="prettyPrint();">
  9. <pre class="prettyprint lang-js">/*!
  10. * Ext JS Library 3.3.1
  11. * Copyright(c) 2006-2010 Sencha Inc.
  12. * licensing@sencha.com
  13. * http://www.sencha.com/license
  14. */
  15. Ext.ns('Ext.ux.grid');
  16. /**
  17. * @class Ext.ux.grid.RowEditor
  18. * @extends Ext.Panel
  19. * Plugin (ptype = 'roweditor') that adds the ability to rapidly edit full rows in a grid.
  20. * A validation mode may be enabled which uses AnchorTips to notify the user of all
  21. * validation errors at once.
  22. *
  23. * @ptype roweditor
  24. */
  25. Ext.ux.grid.RowEditor = Ext.extend(Ext.Panel, {
  26. floating: true,
  27. shadow: false,
  28. layout: 'hbox',
  29. cls: 'x-small-editor',
  30. buttonAlign: 'center',
  31. baseCls: 'x-row-editor',
  32. elements: 'header,footer,body',
  33. frameWidth: 5,
  34. buttonPad: 3,
  35. clicksToEdit: 'auto',
  36. monitorValid: true,
  37. focusDelay: 250,
  38. errorSummary: true,
  39. saveText: 'Save',
  40. cancelText: 'Cancel',
  41. commitChangesText: 'You need to commit or cancel your changes',
  42. errorText: 'Errors',
  43. defaults: {
  44. normalWidth: true
  45. },
  46. initComponent: function(){
  47. Ext.ux.grid.RowEditor.superclass.initComponent.call(this);
  48. this.addEvents(
  49. <div id="event-Ext.ux.grid.RowEditor-beforeedit"></div>/**
  50. * @event beforeedit
  51. * Fired before the row editor is activated.
  52. * If the listener returns <tt>false</tt> the editor will not be activated.
  53. * @param {Ext.ux.grid.RowEditor} roweditor This object
  54. * @param {Number} rowIndex The rowIndex of the row just edited
  55. */
  56. 'beforeedit',
  57. <div id="event-Ext.ux.grid.RowEditor-canceledit"></div>/**
  58. * @event canceledit
  59. * Fired when the editor is cancelled.
  60. * @param {Ext.ux.grid.RowEditor} roweditor This object
  61. * @param {Boolean} forced True if the cancel button is pressed, false is the editor was invalid.
  62. */
  63. 'canceledit',
  64. <div id="event-Ext.ux.grid.RowEditor-validateedit"></div>/**
  65. * @event validateedit
  66. * Fired after a row is edited and passes validation.
  67. * If the listener returns <tt>false</tt> changes to the record will not be set.
  68. * @param {Ext.ux.grid.RowEditor} roweditor This object
  69. * @param {Object} changes Object with changes made to the record.
  70. * @param {Ext.data.Record} r The Record that was edited.
  71. * @param {Number} rowIndex The rowIndex of the row just edited
  72. */
  73. 'validateedit',
  74. <div id="event-Ext.ux.grid.RowEditor-afteredit"></div>/**
  75. * @event afteredit
  76. * Fired after a row is edited and passes validation. This event is fired
  77. * after the store's update event is fired with this edit.
  78. * @param {Ext.ux.grid.RowEditor} roweditor This object
  79. * @param {Object} changes Object with changes made to the record.
  80. * @param {Ext.data.Record} r The Record that was edited.
  81. * @param {Number} rowIndex The rowIndex of the row just edited
  82. */
  83. 'afteredit'
  84. );
  85. },
  86. init: function(grid){
  87. this.grid = grid;
  88. this.ownerCt = grid;
  89. if(this.clicksToEdit === 2){
  90. grid.on('rowdblclick', this.onRowDblClick, this);
  91. }else{
  92. grid.on('rowclick', this.onRowClick, this);
  93. if(Ext.isIE){
  94. grid.on('rowdblclick', this.onRowDblClick, this);
  95. }
  96. }
  97. // stopEditing without saving when a record is removed from Store.
  98. grid.getStore().on('remove', function() {
  99. this.stopEditing(false);
  100. },this);
  101. grid.on({
  102. scope: this,
  103. keydown: this.onGridKey,
  104. columnresize: this.verifyLayout,
  105. columnmove: this.refreshFields,
  106. reconfigure: this.refreshFields,
  107. beforedestroy : this.beforedestroy,
  108. destroy : this.destroy,
  109. bodyscroll: {
  110. buffer: 250,
  111. fn: this.positionButtons
  112. }
  113. });
  114. grid.getColumnModel().on('hiddenchange', this.verifyLayout, this, {delay:1});
  115. grid.getView().on('refresh', this.stopEditing.createDelegate(this, []));
  116. },
  117. beforedestroy: function() {
  118. this.stopMonitoring();
  119. this.grid.getStore().un('remove', this.onStoreRemove, this);
  120. this.stopEditing(false);
  121. Ext.destroy(this.btns, this.tooltip);
  122. },
  123. refreshFields: function(){
  124. this.initFields();
  125. this.verifyLayout();
  126. },
  127. isDirty: function(){
  128. var dirty;
  129. this.items.each(function(f){
  130. if(String(this.values[f.id]) !== String(f.getValue())){
  131. dirty = true;
  132. return false;
  133. }
  134. }, this);
  135. return dirty;
  136. },
  137. startEditing: function(rowIndex, doFocus){
  138. if(this.editing && this.isDirty()){
  139. this.showTooltip(this.commitChangesText);
  140. return;
  141. }
  142. if(Ext.isObject(rowIndex)){
  143. rowIndex = this.grid.getStore().indexOf(rowIndex);
  144. }
  145. if(this.fireEvent('beforeedit', this, rowIndex) !== false){
  146. this.editing = true;
  147. var g = this.grid, view = g.getView(),
  148. row = view.getRow(rowIndex),
  149. record = g.store.getAt(rowIndex);
  150. this.record = record;
  151. this.rowIndex = rowIndex;
  152. this.values = {};
  153. if(!this.rendered){
  154. this.render(view.getEditorParent());
  155. }
  156. var w = Ext.fly(row).getWidth();
  157. this.setSize(w);
  158. if(!this.initialized){
  159. this.initFields();
  160. }
  161. var cm = g.getColumnModel(), fields = this.items.items, f, val;
  162. for(var i = 0, len = cm.getColumnCount(); i < len; i++){
  163. val = this.preEditValue(record, cm.getDataIndex(i));
  164. f = fields[i];
  165. f.setValue(val);
  166. this.values[f.id] = Ext.isEmpty(val) ? '' : val;
  167. }
  168. this.verifyLayout(true);
  169. if(!this.isVisible()){
  170. this.setPagePosition(Ext.fly(row).getXY());
  171. } else{
  172. this.el.setXY(Ext.fly(row).getXY(), {duration:0.15});
  173. }
  174. if(!this.isVisible()){
  175. this.show().doLayout();
  176. }
  177. if(doFocus !== false){
  178. this.doFocus.defer(this.focusDelay, this);
  179. }
  180. }
  181. },
  182. stopEditing : function(saveChanges){
  183. this.editing = false;
  184. if(!this.isVisible()){
  185. return;
  186. }
  187. if(saveChanges === false || !this.isValid()){
  188. this.hide();
  189. this.fireEvent('canceledit', this, saveChanges === false);
  190. return;
  191. }
  192. var changes = {},
  193. r = this.record,
  194. hasChange = false,
  195. cm = this.grid.colModel,
  196. fields = this.items.items;
  197. for(var i = 0, len = cm.getColumnCount(); i < len; i++){
  198. if(!cm.isHidden(i)){
  199. var dindex = cm.getDataIndex(i);
  200. if(!Ext.isEmpty(dindex)){
  201. var oldValue = r.data[dindex],
  202. value = this.postEditValue(fields[i].getValue(), oldValue, r, dindex);
  203. if(String(oldValue) !== String(value)){
  204. changes[dindex] = value;
  205. hasChange = true;
  206. }
  207. }
  208. }
  209. }
  210. if(hasChange && this.fireEvent('validateedit', this, changes, r, this.rowIndex) !== false){
  211. r.beginEdit();
  212. Ext.iterate(changes, function(name, value){
  213. r.set(name, value);
  214. });
  215. r.endEdit();
  216. this.fireEvent('afteredit', this, changes, r, this.rowIndex);
  217. }
  218. this.hide();
  219. },
  220. verifyLayout: function(force){
  221. if(this.el && (this.isVisible() || force === true)){
  222. var row = this.grid.getView().getRow(this.rowIndex);
  223. this.setSize(Ext.fly(row).getWidth(), Ext.isIE ? Ext.fly(row).getHeight() + 9 : undefined);
  224. var cm = this.grid.colModel, fields = this.items.items;
  225. for(var i = 0, len = cm.getColumnCount(); i < len; i++){
  226. if(!cm.isHidden(i)){
  227. var adjust = 0;
  228. if(i === (len - 1)){
  229. adjust += 3; // outer padding
  230. } else{
  231. adjust += 1;
  232. }
  233. fields[i].show();
  234. fields[i].setWidth(cm.getColumnWidth(i) - adjust);
  235. } else{
  236. fields[i].hide();
  237. }
  238. }
  239. this.doLayout();
  240. this.positionButtons();
  241. }
  242. },
  243. slideHide : function(){
  244. this.hide();
  245. },
  246. initFields: function(){
  247. var cm = this.grid.getColumnModel(), pm = Ext.layout.ContainerLayout.prototype.parseMargins;
  248. this.removeAll(false);
  249. for(var i = 0, len = cm.getColumnCount(); i < len; i++){
  250. var c = cm.getColumnAt(i),
  251. ed = c.getEditor();
  252. if(!ed){
  253. ed = c.displayEditor || new Ext.form.DisplayField();
  254. }
  255. if(i == 0){
  256. ed.margins = pm('0 1 2 1');
  257. } else if(i == len - 1){
  258. ed.margins = pm('0 0 2 1');
  259. } else{
  260. if (Ext.isIE) {
  261. ed.margins = pm('0 0 2 0');
  262. }
  263. else {
  264. ed.margins = pm('0 1 2 0');
  265. }
  266. }
  267. ed.setWidth(cm.getColumnWidth(i));
  268. ed.column = c;
  269. if(ed.ownerCt !== this){
  270. ed.on('focus', this.ensureVisible, this);
  271. ed.on('specialkey', this.onKey, this);
  272. }
  273. this.insert(i, ed);
  274. }
  275. this.initialized = true;
  276. },
  277. onKey: function(f, e){
  278. if(e.getKey() === e.ENTER){
  279. this.stopEditing(true);
  280. e.stopPropagation();
  281. }
  282. },
  283. onGridKey: function(e){
  284. if(e.getKey() === e.ENTER && !this.isVisible()){
  285. var r = this.grid.getSelectionModel().getSelected();
  286. if(r){
  287. var index = this.grid.store.indexOf(r);
  288. this.startEditing(index);
  289. e.stopPropagation();
  290. }
  291. }
  292. },
  293. ensureVisible: function(editor){
  294. if(this.isVisible()){
  295. this.grid.getView().ensureVisible(this.rowIndex, this.grid.colModel.getIndexById(editor.column.id), true);
  296. }
  297. },
  298. onRowClick: function(g, rowIndex, e){
  299. if(this.clicksToEdit == 'auto'){
  300. var li = this.lastClickIndex;
  301. this.lastClickIndex = rowIndex;
  302. if(li != rowIndex && !this.isVisible()){
  303. return;
  304. }
  305. }
  306. this.startEditing(rowIndex, false);
  307. this.doFocus.defer(this.focusDelay, this, [e.getPoint()]);
  308. },
  309. onRowDblClick: function(g, rowIndex, e){
  310. this.startEditing(rowIndex, false);
  311. this.doFocus.defer(this.focusDelay, this, [e.getPoint()]);
  312. },
  313. onRender: function(){
  314. Ext.ux.grid.RowEditor.superclass.onRender.apply(this, arguments);
  315. this.el.swallowEvent(['keydown', 'keyup', 'keypress']);
  316. this.btns = new Ext.Panel({
  317. baseCls: 'x-plain',
  318. cls: 'x-btns',
  319. elements:'body',
  320. layout: 'table',
  321. width: (this.minButtonWidth * 2) + (this.frameWidth * 2) + (this.buttonPad * 4), // width must be specified for IE
  322. items: [{
  323. ref: 'saveBtn',
  324. itemId: 'saveBtn',
  325. xtype: 'button',
  326. text: this.saveText,
  327. width: this.minButtonWidth,
  328. handler: this.stopEditing.createDelegate(this, [true])
  329. }, {
  330. xtype: 'button',
  331. text: this.cancelText,
  332. width: this.minButtonWidth,
  333. handler: this.stopEditing.createDelegate(this, [false])
  334. }]
  335. });
  336. this.btns.render(this.bwrap);
  337. },
  338. afterRender: function(){
  339. Ext.ux.grid.RowEditor.superclass.afterRender.apply(this, arguments);
  340. this.positionButtons();
  341. if(this.monitorValid){
  342. this.startMonitoring();
  343. }
  344. },
  345. onShow: function(){
  346. if(this.monitorValid){
  347. this.startMonitoring();
  348. }
  349. Ext.ux.grid.RowEditor.superclass.onShow.apply(this, arguments);
  350. },
  351. onHide: function(){
  352. Ext.ux.grid.RowEditor.superclass.onHide.apply(this, arguments);
  353. this.stopMonitoring();
  354. this.grid.getView().focusRow(this.rowIndex);
  355. },
  356. positionButtons: function(){
  357. if(this.btns){
  358. var g = this.grid,
  359. h = this.el.dom.clientHeight,
  360. view = g.getView(),
  361. scroll = view.scroller.dom.scrollLeft,
  362. bw = this.btns.getWidth(),
  363. width = Math.min(g.getWidth(), g.getColumnModel().getTotalWidth());
  364. this.btns.el.shift({left: (width/2)-(bw/2)+scroll, top: h - 2, stopFx: true, duration:0.2});
  365. }
  366. },
  367. // private
  368. preEditValue : function(r, field){
  369. var value = r.data[field];
  370. return this.autoEncode && typeof value === 'string' ? Ext.util.Format.htmlDecode(value) : value;
  371. },
  372. // private
  373. postEditValue : function(value, originalValue, r, field){
  374. return this.autoEncode && typeof value == 'string' ? Ext.util.Format.htmlEncode(value) : value;
  375. },
  376. doFocus: function(pt){
  377. if(this.isVisible()){
  378. var index = 0,
  379. cm = this.grid.getColumnModel(),
  380. c;
  381. if(pt){
  382. index = this.getTargetColumnIndex(pt);
  383. }
  384. for(var i = index||0, len = cm.getColumnCount(); i < len; i++){
  385. c = cm.getColumnAt(i);
  386. if(!c.hidden && c.getEditor()){
  387. c.getEditor().focus();
  388. break;
  389. }
  390. }
  391. }
  392. },
  393. getTargetColumnIndex: function(pt){
  394. var grid = this.grid,
  395. v = grid.view,
  396. x = pt.left,
  397. cms = grid.colModel.config,
  398. i = 0,
  399. match = false;
  400. for(var len = cms.length, c; c = cms[i]; i++){
  401. if(!c.hidden){
  402. if(Ext.fly(v.getHeaderCell(i)).getRegion().right >= x){
  403. match = i;
  404. break;
  405. }
  406. }
  407. }
  408. return match;
  409. },
  410. startMonitoring : function(){
  411. if(!this.bound && this.monitorValid){
  412. this.bound = true;
  413. Ext.TaskMgr.start({
  414. run : this.bindHandler,
  415. interval : this.monitorPoll || 200,
  416. scope: this
  417. });
  418. }
  419. },
  420. stopMonitoring : function(){
  421. this.bound = false;
  422. if(this.tooltip){
  423. this.tooltip.hide();
  424. }
  425. },
  426. isValid: function(){
  427. var valid = true;
  428. this.items.each(function(f){
  429. if(!f.isValid(true)){
  430. valid = false;
  431. return false;
  432. }
  433. });
  434. return valid;
  435. },
  436. // private
  437. bindHandler : function(){
  438. if(!this.bound){
  439. return false; // stops binding
  440. }
  441. var valid = this.isValid();
  442. if(!valid && this.errorSummary){
  443. this.showTooltip(this.getErrorText().join(''));
  444. }
  445. this.btns.saveBtn.setDisabled(!valid);
  446. this.fireEvent('validation', this, valid);
  447. },
  448. lastVisibleColumn : function() {
  449. var i = this.items.getCount() - 1,
  450. c;
  451. for(; i >= 0; i--) {
  452. c = this.items.items[i];
  453. if (!c.hidden) {
  454. return c;
  455. }
  456. }
  457. },
  458. showTooltip: function(msg){
  459. var t = this.tooltip;
  460. if(!t){
  461. t = this.tooltip = new Ext.ToolTip({
  462. maxWidth: 600,
  463. cls: 'errorTip',
  464. width: 300,
  465. title: this.errorText,
  466. autoHide: false,
  467. anchor: 'left',
  468. anchorToTarget: true,
  469. mouseOffset: [40,0]
  470. });
  471. }
  472. var v = this.grid.getView(),
  473. top = parseInt(this.el.dom.style.top, 10),
  474. scroll = v.scroller.dom.scrollTop,
  475. h = this.el.getHeight();
  476. if(top + h >= scroll){
  477. t.initTarget(this.lastVisibleColumn().getEl());
  478. if(!t.rendered){
  479. t.show();
  480. t.hide();
  481. }
  482. t.body.update(msg);
  483. t.doAutoWidth(20);
  484. t.show();
  485. }else if(t.rendered){
  486. t.hide();
  487. }
  488. },
  489. getErrorText: function(){
  490. var data = ['<ul>'];
  491. this.items.each(function(f){
  492. if(!f.isValid(true)){
  493. data.push('<li>', f.getActiveError(), '</li>');
  494. }
  495. });
  496. data.push('</ul>');
  497. return data;
  498. }
  499. });
  500. Ext.preg('roweditor', Ext.ux.grid.RowEditor);
  501. </pre>
  502. </body>
  503. </html>