| 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 |
|
| 12 |
|
| 13 |
constructor: function(inGrid){ |
| 14 |
|
| 15 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
if(this.grid.singleClickEdit || this.isEditRow(inRowIndex)){ |
| 36 |
|
| 37 |
this.setEditCell(inCell, inRowIndex); |
| 38 |
}else{ |
| 39 |
|
| 40 |
this.apply(); |
| 41 |
} |
| 42 |
|
| 43 |
if(this.isEditing() || (inCell && inCell.editable && inCell.alwaysEditing)){ |
| 44 |
|
| 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 |
|
| 67 |
isEditing: function(){ |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
return this.info.rowIndex !== undefined; |
| 73 |
}, |
| 74 |
|
| 75 |
isEditCell: function(inRowIndex, inCellIndex){ |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
return (this.info.rowIndex === inRowIndex) && (this.info.cell.index == inCellIndex); |
| 85 |
}, |
| 86 |
|
| 87 |
isEditRow: function(inRowIndex){ |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
return this.info.rowIndex === inRowIndex; |
| 95 |
}, |
| 96 |
|
| 97 |
setEditCell: function(inCell, inRowIndex){ |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 120 |
_boomerangWindow: 500, |
| 121 |
_shouldCatchBoomerang: function(){ |
| 122 |
return this._catchBoomerang > new Date().getTime(); |
| 123 |
}, |
| 124 |
_boomerangFocus: function(){ |
| 125 |
|
| 126 |
if(this._shouldCatchBoomerang()){ |
| 127 |
|
| 128 |
this.grid.focus.focusGrid(); |
| 129 |
|
| 130 |
this.focusEditor(); |
| 131 |
|
| 132 |
this._catchBoomerang = 0; |
| 133 |
} |
| 134 |
}, |
| 135 |
_doCatchBoomerang: function(){ |
| 136 |
|
| 137 |
if(has('ie')){this._catchBoomerang = new Date().getTime() + this._boomerangWindow;} |
| 138 |
}, |
| 139 |
|
| 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 |
|
| 160 |
this.grid.focus.focusGrid(); |
| 161 |
|
| 162 |
this._focusEditor(inCell, inRowIndex); |
| 163 |
|
| 164 |
this._doCatchBoomerang(); |
| 165 |
}, |
| 166 |
|
| 167 |
_editorDo: function(inMethod){ |
| 168 |
var c = this.info.cell; |
| 169 |
|
| 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 |
|
| 195 |
|
| 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 |
|
| 209 |
|
| 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 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 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 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 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 |
|
| 250 |
return true; |
| 251 |
} |
| 252 |
w.focused = true; |
| 253 |
return w.isValid(true); |
| 254 |
}, |
| 255 |
|
| 256 |
_onPreRender: function(){ |
| 257 |
if(this.isEditing()){ |
| 258 |
|
| 259 |
this.info.value = this.info.cell.getValue(); |
| 260 |
} |
| 261 |
} |
| 262 |
}); |
| 263 |
}); |