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.

547 lines
17 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  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. /**
  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. /**
  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. /**
  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. /**
  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. } else {
  218. this.fireEvent('canceledit', this, false);
  219. }
  220. this.hide();
  221. },
  222. verifyLayout: function(force){
  223. if(this.el && (this.isVisible() || force === true)){
  224. var row = this.grid.getView().getRow(this.rowIndex);
  225. this.setSize(Ext.fly(row).getWidth(), Ext.isIE ? Ext.fly(row).getHeight() + 9 : undefined);
  226. var cm = this.grid.colModel, fields = this.items.items;
  227. for(var i = 0, len = cm.getColumnCount(); i < len; i++){
  228. if(!cm.isHidden(i)){
  229. var adjust = 0;
  230. if(i === (len - 1)){
  231. adjust += 3; // outer padding
  232. } else{
  233. adjust += 1;
  234. }
  235. fields[i].show();
  236. fields[i].setWidth(cm.getColumnWidth(i) - adjust);
  237. } else{
  238. fields[i].hide();
  239. }
  240. }
  241. this.doLayout();
  242. this.positionButtons();
  243. }
  244. },
  245. slideHide : function(){
  246. this.hide();
  247. },
  248. initFields: function(){
  249. var cm = this.grid.getColumnModel(), pm = Ext.layout.ContainerLayout.prototype.parseMargins;
  250. this.removeAll(false);
  251. for(var i = 0, len = cm.getColumnCount(); i < len; i++){
  252. var c = cm.getColumnAt(i),
  253. ed = c.getEditor();
  254. if(!ed){
  255. ed = c.displayEditor || new Ext.form.DisplayField();
  256. }
  257. if(i == 0){
  258. ed.margins = pm('0 1 2 1');
  259. } else if(i == len - 1){
  260. ed.margins = pm('0 0 2 1');
  261. } else{
  262. if (Ext.isIE) {
  263. ed.margins = pm('0 0 2 0');
  264. }
  265. else {
  266. ed.margins = pm('0 1 2 0');
  267. }
  268. }
  269. ed.setWidth(cm.getColumnWidth(i));
  270. ed.column = c;
  271. if(ed.ownerCt !== this){
  272. ed.on('focus', this.ensureVisible, this);
  273. ed.on('specialkey', this.onKey, this);
  274. }
  275. this.insert(i, ed);
  276. }
  277. this.initialized = true;
  278. },
  279. onKey: function(f, e){
  280. if(e.getKey() === e.ENTER){
  281. this.stopEditing(true);
  282. e.stopPropagation();
  283. }
  284. },
  285. onGridKey: function(e){
  286. if(e.getKey() === e.ENTER && !this.isVisible()){
  287. var r = this.grid.getSelectionModel().getSelected();
  288. if(r){
  289. var index = this.grid.store.indexOf(r);
  290. this.startEditing(index);
  291. e.stopPropagation();
  292. }
  293. }
  294. },
  295. ensureVisible: function(editor){
  296. if(this.isVisible()){
  297. this.grid.getView().ensureVisible(this.rowIndex, this.grid.colModel.getIndexById(editor.column.id), true);
  298. }
  299. },
  300. onRowClick: function(g, rowIndex, e){
  301. if(this.clicksToEdit == 'auto'){
  302. var li = this.lastClickIndex;
  303. this.lastClickIndex = rowIndex;
  304. if(li != rowIndex && !this.isVisible()){
  305. return;
  306. }
  307. }
  308. this.startEditing(rowIndex, false);
  309. this.doFocus.defer(this.focusDelay, this, [e.getPoint()]);
  310. },
  311. onRowDblClick: function(g, rowIndex, e){
  312. this.startEditing(rowIndex, false);
  313. this.doFocus.defer(this.focusDelay, this, [e.getPoint()]);
  314. },
  315. onRender: function(){
  316. Ext.ux.grid.RowEditor.superclass.onRender.apply(this, arguments);
  317. this.el.swallowEvent(['keydown', 'keyup', 'keypress']);
  318. this.btns = new Ext.Panel({
  319. baseCls: 'x-plain',
  320. cls: 'x-btns',
  321. elements:'body',
  322. layout: 'table',
  323. width: (this.minButtonWidth * 2) + (this.frameWidth * 2) + (this.buttonPad * 4), // width must be specified for IE
  324. items: [{
  325. ref: 'saveBtn',
  326. itemId: 'saveBtn',
  327. xtype: 'button',
  328. text: this.saveText,
  329. width: this.minButtonWidth,
  330. handler: this.stopEditing.createDelegate(this, [true])
  331. }, {
  332. xtype: 'button',
  333. text: this.cancelText,
  334. width: this.minButtonWidth,
  335. handler: this.stopEditing.createDelegate(this, [false])
  336. }]
  337. });
  338. this.btns.render(this.bwrap);
  339. },
  340. afterRender: function(){
  341. Ext.ux.grid.RowEditor.superclass.afterRender.apply(this, arguments);
  342. this.positionButtons();
  343. if(this.monitorValid){
  344. this.startMonitoring();
  345. }
  346. },
  347. onShow: function(){
  348. if(this.monitorValid){
  349. this.startMonitoring();
  350. }
  351. Ext.ux.grid.RowEditor.superclass.onShow.apply(this, arguments);
  352. },
  353. onHide: function(){
  354. Ext.ux.grid.RowEditor.superclass.onHide.apply(this, arguments);
  355. this.stopMonitoring();
  356. this.grid.getView().focusRow(this.rowIndex);
  357. },
  358. positionButtons: function(){
  359. if(this.btns){
  360. var g = this.grid,
  361. h = this.el.dom.clientHeight,
  362. view = g.getView(),
  363. scroll = view.scroller.dom.scrollLeft,
  364. bw = this.btns.getWidth(),
  365. width = Math.min(g.getWidth(), g.getColumnModel().getTotalWidth());
  366. this.btns.el.shift({left: (width/2)-(bw/2)+scroll, top: h - 2, stopFx: true, duration:0.2});
  367. }
  368. },
  369. // private
  370. preEditValue : function(r, field){
  371. var value = r.data[field];
  372. return this.autoEncode && typeof value === 'string' ? Ext.util.Format.htmlDecode(value) : value;
  373. },
  374. // private
  375. postEditValue : function(value, originalValue, r, field){
  376. return this.autoEncode && typeof value == 'string' ? Ext.util.Format.htmlEncode(value) : value;
  377. },
  378. doFocus: function(pt){
  379. if(this.isVisible()){
  380. var index = 0,
  381. cm = this.grid.getColumnModel(),
  382. c;
  383. if(pt){
  384. index = this.getTargetColumnIndex(pt);
  385. }
  386. for(var i = index||0, len = cm.getColumnCount(); i < len; i++){
  387. c = cm.getColumnAt(i);
  388. if(!c.hidden && c.getEditor()){
  389. c.getEditor().focus();
  390. break;
  391. }
  392. }
  393. }
  394. },
  395. getTargetColumnIndex: function(pt){
  396. var grid = this.grid,
  397. v = grid.view,
  398. x = pt.left,
  399. cms = grid.colModel.config,
  400. i = 0,
  401. match = false;
  402. for(var len = cms.length, c; c = cms[i]; i++){
  403. if(!c.hidden){
  404. if(Ext.fly(v.getHeaderCell(i)).getRegion().right >= x){
  405. match = i;
  406. break;
  407. }
  408. }
  409. }
  410. return match;
  411. },
  412. startMonitoring : function(){
  413. if(!this.bound && this.monitorValid){
  414. this.bound = true;
  415. Ext.TaskMgr.start({
  416. run : this.bindHandler,
  417. interval : this.monitorPoll || 200,
  418. scope: this
  419. });
  420. }
  421. },
  422. stopMonitoring : function(){
  423. this.bound = false;
  424. if(this.tooltip){
  425. this.tooltip.hide();
  426. }
  427. },
  428. isValid: function(){
  429. var valid = true;
  430. this.items.each(function(f){
  431. if(!f.isValid(true)){
  432. valid = false;
  433. return false;
  434. }
  435. });
  436. return valid;
  437. },
  438. // private
  439. bindHandler : function(){
  440. if(!this.bound){
  441. return false; // stops binding
  442. }
  443. var valid = this.isValid();
  444. if(!valid && this.errorSummary){
  445. this.showTooltip(this.getErrorText().join(''));
  446. }
  447. this.btns.saveBtn.setDisabled(!valid);
  448. this.fireEvent('validation', this, valid);
  449. },
  450. lastVisibleColumn : function() {
  451. var i = this.items.getCount() - 1,
  452. c;
  453. for(; i >= 0; i--) {
  454. c = this.items.items[i];
  455. if (!c.hidden) {
  456. return c;
  457. }
  458. }
  459. },
  460. showTooltip: function(msg){
  461. var t = this.tooltip;
  462. if(!t){
  463. t = this.tooltip = new Ext.ToolTip({
  464. maxWidth: 600,
  465. cls: 'errorTip',
  466. width: 300,
  467. title: this.errorText,
  468. autoHide: false,
  469. anchor: 'left',
  470. anchorToTarget: true,
  471. mouseOffset: [40,0]
  472. });
  473. }
  474. var v = this.grid.getView(),
  475. top = parseInt(this.el.dom.style.top, 10),
  476. scroll = v.scroller.dom.scrollTop,
  477. h = this.el.getHeight();
  478. if(top + h >= scroll){
  479. t.initTarget(this.lastVisibleColumn().getEl());
  480. if(!t.rendered){
  481. t.show();
  482. t.hide();
  483. }
  484. t.body.update(msg);
  485. t.doAutoWidth(20);
  486. t.show();
  487. }else if(t.rendered){
  488. t.hide();
  489. }
  490. },
  491. getErrorText: function(){
  492. var data = ['<ul>'];
  493. this.items.each(function(f){
  494. if(!f.isValid(true)){
  495. data.push('<li>', f.getActiveError(), '</li>');
  496. }
  497. });
  498. data.push('</ul>');
  499. return data;
  500. }
  501. });
  502. Ext.preg('roweditor', Ext.ux.grid.RowEditor);