cc5e9bc282b06da9be9091381f0062f5abc83a78.svn-base 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.grid.EditorGridPanel
  8. * @extends Ext.grid.GridPanel Class for creating and editable grid.
  9. *
  10. * @constructor
  11. */
  12. Ext.grid.EditorGridPanel = Ext.extend(Ext.grid.GridPanel, {
  13. /**
  14. * @cfg {Number} clicksToEdit The number of clicks on a cell
  15. * required to display the cell's editor (defaults to 2)
  16. */
  17. clicksToEdit : 2,
  18. // private
  19. isEditor : true,
  20. // private
  21. detectEdit : false,
  22. /**
  23. * @cfg {Boolean} trackMouseOver
  24. * @hide
  25. */
  26. // private
  27. trackMouseOver : false, // causes very odd FF errors
  28. // private
  29. initComponent : function() {
  30. Ext.grid.EditorGridPanel.superclass.initComponent.call(this);
  31. if (!this.selModel) {
  32. this.selModel = new Ext.grid.CellSelectionModel();
  33. }
  34. this.activeEditor = null;
  35. this.addEvents(
  36. /**
  37. * @event beforeedit Fires before cell editing is
  38. * triggered. The edit event object has the
  39. * following properties <br />
  40. * <ul style="padding:5px;padding-left:16px;">
  41. * <li>grid - This grid</li>
  42. * <li>record - The record being edited</li>
  43. * <li>field - The field name being edited</li>
  44. * <li>value - The value for the field being
  45. * edited.</li>
  46. * <li>row - The grid row index</li>
  47. * <li>column - The grid column index</li>
  48. * <li>cancel - Set this to true to cancel the
  49. * edit or return false from your handler.</li>
  50. * </ul>
  51. * @param {Object}
  52. * e An edit event (see above for
  53. * description)
  54. */
  55. "beforeedit",
  56. /**
  57. * @event afteredit Fires after a cell is edited. <br />
  58. * <ul style="padding:5px;padding-left:16px;">
  59. * <li>grid - This grid</li>
  60. * <li>record - The record being edited</li>
  61. * <li>field - The field name being edited</li>
  62. * <li>value - The value being set</li>
  63. * <li>originalValue - The original value for
  64. * the field, before the edit.</li>
  65. * <li>row - The grid row index</li>
  66. * <li>column - The grid column index</li>
  67. * </ul>
  68. * @param {Object}
  69. * e An edit event (see above for
  70. * description)
  71. */
  72. "afteredit",
  73. /**
  74. * @event validateedit Fires after a cell is edited, but
  75. * before the value is set in the record. Return
  76. * false to cancel the change. The edit event
  77. * object has the following properties <br />
  78. * <ul style="padding:5px;padding-left:16px;">
  79. * <li>grid - This grid</li>
  80. * <li>record - The record being edited</li>
  81. * <li>field - The field name being edited</li>
  82. * <li>value - The value being set</li>
  83. * <li>originalValue - The original value for
  84. * the field, before the edit.</li>
  85. * <li>row - The grid row index</li>
  86. * <li>column - The grid column index</li>
  87. * <li>cancel - Set this to true to cancel the
  88. * edit or return false from your handler.</li>
  89. * </ul>
  90. * @param {Object}
  91. * e An edit event (see above for
  92. * description)
  93. */
  94. "validateedit");
  95. },
  96. // private
  97. initEvents : function() {
  98. Ext.grid.EditorGridPanel.superclass.initEvents.call(this);
  99. this.on("bodyscroll", this.stopEditing, this);
  100. if (this.clicksToEdit == 1) {
  101. this.on("cellclick", this.onCellDblClick, this);
  102. } else {
  103. if (this.clicksToEdit == 'auto' && this.view.mainBody) {
  104. this.view.mainBody.on("mousedown",
  105. this.onAutoEditClick, this);
  106. }
  107. this.on("celldblclick", this.onCellDblClick, this);
  108. }
  109. this.getGridEl().addClass("xedit-grid");
  110. },
  111. // private
  112. onCellDblClick : function(g, row, col) {
  113. this.startEditing(row, col);
  114. },
  115. // private
  116. onAutoEditClick : function(e, t) {
  117. var row = this.view.findRowIndex(t);
  118. var col = this.view.findCellIndex(t);
  119. if (row !== false && col !== false) {
  120. if (this.selModel.getSelectedCell) { // cell sm
  121. var sc = this.selModel.getSelectedCell();
  122. if (sc && sc.cell[0] === row && sc.cell[1] === col) {
  123. this.startEditing(row, col);
  124. }
  125. } else {
  126. if (this.selModel.isSelected(row)) {
  127. this.startEditing(row, col);
  128. }
  129. }
  130. }
  131. },
  132. // private
  133. onEditComplete : function(ed, value, startValue) {
  134. this.editing = false;
  135. this.activeEditor = null;
  136. ed.un("specialkey", this.selModel.onEditorKey, this.selModel);
  137. if (String(value) !== String(startValue)) {
  138. var r = ed.record;
  139. var field = this.colModel.getDataIndex(ed.col);
  140. var e = {
  141. grid : this,
  142. record : r,
  143. field : field,
  144. originalValue : startValue,
  145. value : value,
  146. row : ed.row,
  147. column : ed.col,
  148. cancel : false
  149. };
  150. if (this.fireEvent("validateedit", e) !== false
  151. && !e.cancel) {
  152. r.set(field, e.value);
  153. delete e.cancel;
  154. this.fireEvent("afteredit", e);
  155. }
  156. }
  157. this.view.focusCell(ed.row, ed.col);
  158. },
  159. /**
  160. * Starts editing the specified for the specified row/column
  161. *
  162. * @param {Number}
  163. * rowIndex
  164. * @param {Number}
  165. * colIndex
  166. */
  167. startEditing : function(row, col) {
  168. this.stopEditing();
  169. if (this.colModel.isCellEditable(col, row)) {
  170. this.view.ensureVisible(row, col, true);
  171. var r = this.store.getAt(row);
  172. var field = this.colModel.getDataIndex(col);
  173. var e = {
  174. grid : this,
  175. record : r,
  176. field : field,
  177. value : r.data[field],
  178. row : row,
  179. column : col,
  180. cancel : false
  181. };
  182. if (this.fireEvent("beforeedit", e) !== false && !e.cancel) {
  183. this.editing = true;
  184. var ed = this.colModel.getCellEditor(col, row);
  185. if (!ed.rendered) {
  186. ed.render(this.view.getEditorParent(ed));
  187. }
  188. (function () { // complex but required for focus issues in
  189. // safari, ie and opera
  190. ed.row = row;
  191. ed.col = col;
  192. ed.record = r;
  193. ed.on("complete", this.onEditComplete, this, {
  194. single : true
  195. });
  196. ed.on("specialkey", this.selModel.onEditorKey,
  197. this.selModel);
  198. this.activeEditor = ed;
  199. var v = r.data[field];
  200. ed.startEdit(this.view.getCell(row, col), v);
  201. }).defer(50, this);
  202. }
  203. }
  204. },
  205. /**
  206. * Stops any active editing
  207. */
  208. stopEditing : function() {
  209. if (this.activeEditor) {
  210. this.activeEditor.completeEdit();
  211. }
  212. this.activeEditor = null;
  213. }
  214. });
  215. Ext.reg('editorgrid', Ext.grid.EditorGridPanel);