[483] | 1 | define([ |
---|
| 2 | "dojo/_base/lang", |
---|
| 3 | "dojo/_base/array", |
---|
| 4 | "dojo/_base/declare", |
---|
| 5 | "dojo/_base/connect", |
---|
| 6 | "dojo/_base/sniff", |
---|
| 7 | "./util" |
---|
| 8 | ], function(lang, array, declare, connect, has, util){ |
---|
| 9 | |
---|
| 10 | return declare("dojox.grid._EditManager", null, { |
---|
| 11 | // summary: |
---|
| 12 | // Controls grid cell editing process. Owned by grid and used internally for editing. |
---|
| 13 | constructor: function(inGrid){ |
---|
| 14 | // inGrid: dojox.Grid |
---|
| 15 | // The dojox.Grid this editor should be attached to |
---|
| 16 | this.grid = inGrid; |
---|
| 17 | this.connections = !has('ie') ? [] : [connect.connect(document.body, "onfocus", lang.hitch(this, "_boomerangFocus"))]; |
---|
| 18 | this.connections.push(connect.connect(this.grid, 'onBlur', this, 'apply')); |
---|
| 19 | this.connections.push(connect.connect(this.grid, 'prerender', this, '_onPreRender')); |
---|
| 20 | }, |
---|
| 21 | |
---|
| 22 | info: {}, |
---|
| 23 | |
---|
| 24 | destroy: function(){ |
---|
| 25 | array.forEach(this.connections, connect.disconnect); |
---|
| 26 | }, |
---|
| 27 | |
---|
| 28 | cellFocus: function(inCell, inRowIndex){ |
---|
| 29 | // summary: |
---|
| 30 | // Invoke editing when cell is focused |
---|
| 31 | // inCell: cell object |
---|
| 32 | // Grid cell object |
---|
| 33 | // inRowIndex: Integer |
---|
| 34 | // Grid row index |
---|
| 35 | if(this.grid.singleClickEdit || this.isEditRow(inRowIndex)){ |
---|
| 36 | // if same row or quick editing, edit |
---|
| 37 | this.setEditCell(inCell, inRowIndex); |
---|
| 38 | }else{ |
---|
| 39 | // otherwise, apply any pending row edits |
---|
| 40 | this.apply(); |
---|
| 41 | } |
---|
| 42 | // if dynamic or static editing... |
---|
| 43 | if(this.isEditing() || (inCell && inCell.editable && inCell.alwaysEditing)){ |
---|
| 44 | // let the editor focus itself as needed |
---|
| 45 | this._focusEditor(inCell, inRowIndex); |
---|
| 46 | } |
---|
| 47 | }, |
---|
| 48 | |
---|
| 49 | rowClick: function(e){ |
---|
| 50 | if(this.isEditing() && !this.isEditRow(e.rowIndex)){ |
---|
| 51 | this.apply(); |
---|
| 52 | } |
---|
| 53 | }, |
---|
| 54 | |
---|
| 55 | styleRow: function(inRow){ |
---|
| 56 | if(inRow.index == this.info.rowIndex){ |
---|
| 57 | inRow.customClasses += ' dojoxGridRowEditing'; |
---|
| 58 | } |
---|
| 59 | }, |
---|
| 60 | |
---|
| 61 | dispatchEvent: function(e){ |
---|
| 62 | var c = e.cell, ed = (c && c["editable"]) ? c : 0; |
---|
| 63 | return ed && ed.dispatchEvent(e.dispatch, e); |
---|
| 64 | }, |
---|
| 65 | |
---|
| 66 | // Editing |
---|
| 67 | isEditing: function(){ |
---|
| 68 | // summary: |
---|
| 69 | // Indicates editing state of the grid. |
---|
| 70 | // returns: Boolean |
---|
| 71 | // True if grid is actively editing |
---|
| 72 | return this.info.rowIndex !== undefined; |
---|
| 73 | }, |
---|
| 74 | |
---|
| 75 | isEditCell: function(inRowIndex, inCellIndex){ |
---|
| 76 | // summary: |
---|
| 77 | // Indicates if the given cell is being edited. |
---|
| 78 | // inRowIndex: Integer |
---|
| 79 | // Grid row index |
---|
| 80 | // inCellIndex: Integer |
---|
| 81 | // Grid cell index |
---|
| 82 | // returns: Boolean |
---|
| 83 | // True if given cell is being edited |
---|
| 84 | return (this.info.rowIndex === inRowIndex) && (this.info.cell.index == inCellIndex); |
---|
| 85 | }, |
---|
| 86 | |
---|
| 87 | isEditRow: function(inRowIndex){ |
---|
| 88 | // summary: |
---|
| 89 | // Indicates if the given row is being edited. |
---|
| 90 | // inRowIndex: Integer |
---|
| 91 | // Grid row index |
---|
| 92 | // returns: Boolean |
---|
| 93 | // True if given row is being edited |
---|
| 94 | return this.info.rowIndex === inRowIndex; |
---|
| 95 | }, |
---|
| 96 | |
---|
| 97 | setEditCell: function(inCell, inRowIndex){ |
---|
| 98 | // summary: |
---|
| 99 | // Set the given cell to be edited |
---|
| 100 | // inRowIndex: Integer |
---|
| 101 | // Grid row index |
---|
| 102 | // inCell: Object |
---|
| 103 | // Grid cell object |
---|
| 104 | if(!this.isEditCell(inRowIndex, inCell.index) && this.grid.canEdit && this.grid.canEdit(inCell, inRowIndex)){ |
---|
| 105 | this.start(inCell, inRowIndex, this.isEditRow(inRowIndex) || inCell.editable); |
---|
| 106 | } |
---|
| 107 | }, |
---|
| 108 | |
---|
| 109 | _focusEditor: function(inCell, inRowIndex){ |
---|
| 110 | util.fire(inCell, "focus", [inRowIndex]); |
---|
| 111 | }, |
---|
| 112 | |
---|
| 113 | focusEditor: function(){ |
---|
| 114 | if(this.isEditing()){ |
---|
| 115 | this._focusEditor(this.info.cell, this.info.rowIndex); |
---|
| 116 | } |
---|
| 117 | }, |
---|
| 118 | |
---|
| 119 | // implement fix for focus boomerang effect on IE |
---|
| 120 | _boomerangWindow: 500, |
---|
| 121 | _shouldCatchBoomerang: function(){ |
---|
| 122 | return this._catchBoomerang > new Date().getTime(); |
---|
| 123 | }, |
---|
| 124 | _boomerangFocus: function(){ |
---|
| 125 | //console.log("_boomerangFocus"); |
---|
| 126 | if(this._shouldCatchBoomerang()){ |
---|
| 127 | // make sure we don't utterly lose focus |
---|
| 128 | this.grid.focus.focusGrid(); |
---|
| 129 | // let the editor focus itself as needed |
---|
| 130 | this.focusEditor(); |
---|
| 131 | // only catch once |
---|
| 132 | this._catchBoomerang = 0; |
---|
| 133 | } |
---|
| 134 | }, |
---|
| 135 | _doCatchBoomerang: function(){ |
---|
| 136 | // give ourselves a few ms to boomerang IE focus effects |
---|
| 137 | if(has('ie')){this._catchBoomerang = new Date().getTime() + this._boomerangWindow;} |
---|
| 138 | }, |
---|
| 139 | // end boomerang fix API |
---|
| 140 | |
---|
| 141 | start: function(inCell, inRowIndex, inEditing){ |
---|
| 142 | if(!this._isValidInput()){ |
---|
| 143 | return; |
---|
| 144 | } |
---|
| 145 | this.grid.beginUpdate(); |
---|
| 146 | this.editorApply(); |
---|
| 147 | if(this.isEditing() && !this.isEditRow(inRowIndex)){ |
---|
| 148 | this.applyRowEdit(); |
---|
| 149 | this.grid.updateRow(inRowIndex); |
---|
| 150 | } |
---|
| 151 | if(inEditing){ |
---|
| 152 | this.info = { cell: inCell, rowIndex: inRowIndex }; |
---|
| 153 | this.grid.doStartEdit(inCell, inRowIndex); |
---|
| 154 | this.grid.updateRow(inRowIndex); |
---|
| 155 | }else{ |
---|
| 156 | this.info = {}; |
---|
| 157 | } |
---|
| 158 | this.grid.endUpdate(); |
---|
| 159 | // make sure we don't utterly lose focus |
---|
| 160 | this.grid.focus.focusGrid(); |
---|
| 161 | // let the editor focus itself as needed |
---|
| 162 | this._focusEditor(inCell, inRowIndex); |
---|
| 163 | // give ourselves a few ms to boomerang IE focus effects |
---|
| 164 | this._doCatchBoomerang(); |
---|
| 165 | }, |
---|
| 166 | |
---|
| 167 | _editorDo: function(inMethod){ |
---|
| 168 | var c = this.info.cell; |
---|
| 169 | //c && c.editor && c.editor[inMethod](c, this.info.rowIndex); |
---|
| 170 | if(c && c.editable){ |
---|
| 171 | c[inMethod](this.info.rowIndex); |
---|
| 172 | } |
---|
| 173 | }, |
---|
| 174 | |
---|
| 175 | editorApply: function(){ |
---|
| 176 | this._editorDo("apply"); |
---|
| 177 | }, |
---|
| 178 | |
---|
| 179 | editorCancel: function(){ |
---|
| 180 | this._editorDo("cancel"); |
---|
| 181 | }, |
---|
| 182 | |
---|
| 183 | applyCellEdit: function(inValue, inCell, inRowIndex){ |
---|
| 184 | if(this.grid.canEdit(inCell, inRowIndex)){ |
---|
| 185 | this.grid.doApplyCellEdit(inValue, inRowIndex, inCell.field); |
---|
| 186 | } |
---|
| 187 | }, |
---|
| 188 | |
---|
| 189 | applyRowEdit: function(){ |
---|
| 190 | this.grid.doApplyEdit(this.info.rowIndex, this.info.cell.field); |
---|
| 191 | }, |
---|
| 192 | |
---|
| 193 | apply: function(){ |
---|
| 194 | // summary: |
---|
| 195 | // Apply a grid edit |
---|
| 196 | if(this.isEditing() && this._isValidInput()){ |
---|
| 197 | this.grid.beginUpdate(); |
---|
| 198 | this.editorApply(); |
---|
| 199 | this.applyRowEdit(); |
---|
| 200 | this.info = {}; |
---|
| 201 | this.grid.endUpdate(); |
---|
| 202 | this.grid.focus.focusGrid(); |
---|
| 203 | this._doCatchBoomerang(); |
---|
| 204 | } |
---|
| 205 | }, |
---|
| 206 | |
---|
| 207 | cancel: function(){ |
---|
| 208 | // summary: |
---|
| 209 | // Cancel a grid edit |
---|
| 210 | if(this.isEditing()){ |
---|
| 211 | this.grid.beginUpdate(); |
---|
| 212 | this.editorCancel(); |
---|
| 213 | this.info = {}; |
---|
| 214 | this.grid.endUpdate(); |
---|
| 215 | this.grid.focus.focusGrid(); |
---|
| 216 | this._doCatchBoomerang(); |
---|
| 217 | } |
---|
| 218 | }, |
---|
| 219 | |
---|
| 220 | save: function(inRowIndex, inView){ |
---|
| 221 | // summary: |
---|
| 222 | // Save the grid editing state |
---|
| 223 | // inRowIndex: Integer |
---|
| 224 | // Grid row index |
---|
| 225 | // inView: Object |
---|
| 226 | // Grid view |
---|
| 227 | var c = this.info.cell; |
---|
| 228 | if(this.isEditRow(inRowIndex) && (!inView || c.view==inView) && c.editable){ |
---|
| 229 | c.save(c, this.info.rowIndex); |
---|
| 230 | } |
---|
| 231 | }, |
---|
| 232 | |
---|
| 233 | restore: function(inView, inRowIndex){ |
---|
| 234 | // summary: |
---|
| 235 | // Restores the grid editing state |
---|
| 236 | // inRowIndex: Integer |
---|
| 237 | // Grid row index |
---|
| 238 | // inView: Object |
---|
| 239 | // Grid view |
---|
| 240 | var c = this.info.cell; |
---|
| 241 | if(this.isEditRow(inRowIndex) && c.view == inView && c.editable){ |
---|
| 242 | c.restore(this.info.rowIndex); |
---|
| 243 | } |
---|
| 244 | }, |
---|
| 245 | |
---|
| 246 | _isValidInput: function(){ |
---|
| 247 | var w = (this.info.cell || {}).widget; |
---|
| 248 | if(!w || !w.isValid){ |
---|
| 249 | //no validation needed |
---|
| 250 | return true; |
---|
| 251 | } |
---|
| 252 | w.focused = true; |
---|
| 253 | return w.isValid(true); |
---|
| 254 | }, |
---|
| 255 | |
---|
| 256 | _onPreRender: function(){ |
---|
| 257 | if(this.isEditing()){ |
---|
| 258 | //cache the current editing value before render |
---|
| 259 | this.info.value = this.info.cell.getValue(); |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | }); |
---|
| 263 | }); |
---|