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.

531 lines
17 KiB

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