| 1 |
define([ |
| 2 |
"dojo/_base/kernel", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/lang", |
| 5 |
"dojo/_base/event", |
| 6 |
"dojo/_base/connect", |
| 7 |
"dojo/_base/array", |
| 8 |
"dojo/_base/sniff", |
| 9 |
"dojo/dom", |
| 10 |
"dojo/dom-attr", |
| 11 |
"dojo/dom-construct", |
| 12 |
"dijit/_Widget", |
| 13 |
"../util" |
| 14 |
], function(dojo, declare, lang, event, connect, array, has, dom, domAttr, domConstruct, _Widget, util){ |
| 15 |
|
| 16 |
var _DeferredTextWidget = declare("dojox.grid._DeferredTextWidget", _Widget, { |
| 17 |
deferred: null, |
| 18 |
_destroyOnRemove: true, |
| 19 |
postCreate: function(){ |
| 20 |
if(this.deferred){ |
| 21 |
this.deferred.addBoth(lang.hitch(this, function(text){ |
| 22 |
if(this.domNode){ |
| 23 |
this.domNode.innerHTML = text; |
| 24 |
} |
| 25 |
})); |
| 26 |
} |
| 27 |
} |
| 28 |
}); |
| 29 |
|
| 30 |
var focusSelectNode = function(inNode){ |
| 31 |
try{ |
| 32 |
util.fire(inNode, "focus"); |
| 33 |
util.fire(inNode, "select"); |
| 34 |
}catch(e){ |
| 35 |
} |
| 36 |
}; |
| 37 |
|
| 38 |
var whenIdle = function(){ |
| 39 |
setTimeout(lang.hitch.apply(dojo, arguments), 0); |
| 40 |
}; |
| 41 |
|
| 42 |
var BaseCell = declare("dojox.grid.cells._Base", null, { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
styles: '', |
| 49 |
classes: '', |
| 50 |
editable: false, |
| 51 |
alwaysEditing: false, |
| 52 |
formatter: null, |
| 53 |
defaultValue: '...', |
| 54 |
value: null, |
| 55 |
hidden: false, |
| 56 |
noresize: false, |
| 57 |
draggable: true, |
| 58 |
|
| 59 |
_valueProp: "value", |
| 60 |
_formatPending: false, |
| 61 |
|
| 62 |
constructor: function(inProps){ |
| 63 |
this._props = inProps || {}; |
| 64 |
lang.mixin(this, inProps); |
| 65 |
if(this.draggable === undefined){ |
| 66 |
this.draggable = true; |
| 67 |
} |
| 68 |
}, |
| 69 |
|
| 70 |
_defaultFormat: function(inValue, callArgs){ |
| 71 |
var s = this.grid.formatterScope || this; |
| 72 |
var f = this.formatter; |
| 73 |
if(f && s && typeof f == "string"){ |
| 74 |
f = this.formatter = s[f]; |
| 75 |
} |
| 76 |
var v = (inValue != this.defaultValue && f) ? f.apply(s, callArgs) : inValue; |
| 77 |
if(typeof v == "undefined"){ |
| 78 |
return this.defaultValue; |
| 79 |
} |
| 80 |
if(v && v.addBoth){ |
| 81 |
|
| 82 |
v = new _DeferredTextWidget({deferred: v}, |
| 83 |
domConstruct.create("span", {innerHTML: this.defaultValue})); |
| 84 |
} |
| 85 |
if(v && v.declaredClass && v.startup){ |
| 86 |
return "<div class='dojoxGridStubNode' linkWidget='" + |
| 87 |
v.id + |
| 88 |
"' cellIdx='" + |
| 89 |
this.index + |
| 90 |
"'>" + |
| 91 |
this.defaultValue + |
| 92 |
"</div>"; |
| 93 |
} |
| 94 |
return v; |
| 95 |
}, |
| 96 |
|
| 97 |
|
| 98 |
format: function(inRowIndex, inItem){ |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
var i = this.grid.edit.info; |
| 106 |
var d = this.get ? this.get(inRowIndex, inItem) : (this.value || this.defaultValue); |
| 107 |
if (d && d.replace && this.grid.escapeHTMLInData) { |
| 108 |
d = d.replace(/&/g, '&').replace(/</g, '<'); |
| 109 |
} |
| 110 |
if (this.editable && (this.alwaysEditing || (i.rowIndex==inRowIndex && i.cell==this))){ |
| 111 |
return this.formatEditing(i.value ? i.value : d, inRowIndex); |
| 112 |
} else { |
| 113 |
return this._defaultFormat(d, [d, inRowIndex, this]); |
| 114 |
} |
| 115 |
}, |
| 116 |
formatEditing: function(inDatum, inRowIndex){ |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
}, |
| 126 |
|
| 127 |
getNode: function(inRowIndex){ |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
return this.view.getCellNode(inRowIndex, this.index); |
| 135 |
}, |
| 136 |
getHeaderNode: function(){ |
| 137 |
return this.view.getHeaderCellNode(this.index); |
| 138 |
}, |
| 139 |
getEditNode: function(inRowIndex){ |
| 140 |
return (this.getNode(inRowIndex) || 0).firstChild || 0; |
| 141 |
}, |
| 142 |
canResize: function(){ |
| 143 |
var uw = this.unitWidth; |
| 144 |
return uw && (uw!=='auto'); |
| 145 |
}, |
| 146 |
isFlex: function(){ |
| 147 |
var uw = this.unitWidth; |
| 148 |
return uw && lang.isString(uw) && (uw=='auto' || uw.slice(-1)=='%'); |
| 149 |
}, |
| 150 |
|
| 151 |
applyEdit: function(inValue, inRowIndex){ |
| 152 |
if(this.getNode(inRowIndex)){ |
| 153 |
this.grid.edit.applyCellEdit(inValue, this, inRowIndex); |
| 154 |
} |
| 155 |
}, |
| 156 |
cancelEdit: function(inRowIndex){ |
| 157 |
this.grid.doCancelEdit(inRowIndex); |
| 158 |
}, |
| 159 |
_onEditBlur: function(inRowIndex){ |
| 160 |
if(this.grid.edit.isEditCell(inRowIndex, this.index)){ |
| 161 |
|
| 162 |
this.grid.edit.apply(); |
| 163 |
} |
| 164 |
}, |
| 165 |
registerOnBlur: function(inNode, inRowIndex){ |
| 166 |
if(this.commitOnBlur){ |
| 167 |
connect.connect(inNode, "onblur", function(e){ |
| 168 |
|
| 169 |
setTimeout(lang.hitch(this, "_onEditBlur", inRowIndex), 250); |
| 170 |
}); |
| 171 |
} |
| 172 |
}, |
| 173 |
|
| 174 |
needFormatNode: function(inDatum, inRowIndex){ |
| 175 |
this._formatPending = true; |
| 176 |
whenIdle(this, "_formatNode", inDatum, inRowIndex); |
| 177 |
}, |
| 178 |
cancelFormatNode: function(){ |
| 179 |
this._formatPending = false; |
| 180 |
}, |
| 181 |
|
| 182 |
_formatNode: function(inDatum, inRowIndex){ |
| 183 |
if(this._formatPending){ |
| 184 |
this._formatPending = false; |
| 185 |
|
| 186 |
if(!has('ie')){ |
| 187 |
dom.setSelectable(this.grid.domNode, true); |
| 188 |
} |
| 189 |
this.formatNode(this.getEditNode(inRowIndex), inDatum, inRowIndex); |
| 190 |
} |
| 191 |
}, |
| 192 |
|
| 193 |
formatNode: function(inNode, inDatum, inRowIndex){ |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if(has('ie')){ |
| 203 |
|
| 204 |
whenIdle(this, "focus", inRowIndex, inNode); |
| 205 |
}else{ |
| 206 |
this.focus(inRowIndex, inNode); |
| 207 |
} |
| 208 |
}, |
| 209 |
dispatchEvent: function(m, e){ |
| 210 |
if(m in this){ |
| 211 |
return this[m](e); |
| 212 |
} |
| 213 |
}, |
| 214 |
|
| 215 |
getValue: function(inRowIndex){ |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
return this.getEditNode(inRowIndex)[this._valueProp]; |
| 223 |
}, |
| 224 |
setValue: function(inRowIndex, inValue){ |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
var n = this.getEditNode(inRowIndex); |
| 232 |
if(n){ |
| 233 |
n[this._valueProp] = inValue; |
| 234 |
} |
| 235 |
}, |
| 236 |
focus: function(inRowIndex, inNode){ |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
focusSelectNode(inNode || this.getEditNode(inRowIndex)); |
| 244 |
}, |
| 245 |
save: function(inRowIndex){ |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
this.value = this.value || this.getValue(inRowIndex); |
| 251 |
|
| 252 |
}, |
| 253 |
restore: function(inRowIndex){ |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
this.setValue(inRowIndex, this.value); |
| 259 |
|
| 260 |
}, |
| 261 |
|
| 262 |
_finish: function(inRowIndex){ |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
dom.setSelectable(this.grid.domNode, false); |
| 268 |
this.cancelFormatNode(); |
| 269 |
}, |
| 270 |
|
| 271 |
apply: function(inRowIndex){ |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
this.applyEdit(this.getValue(inRowIndex), inRowIndex); |
| 277 |
this._finish(inRowIndex); |
| 278 |
}, |
| 279 |
cancel: function(inRowIndex){ |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
this.cancelEdit(inRowIndex); |
| 285 |
this._finish(inRowIndex); |
| 286 |
} |
| 287 |
}); |
| 288 |
BaseCell.markupFactory = function(node, cellDef){ |
| 289 |
var formatter = lang.trim(domAttr.get(node, "formatter")||""); |
| 290 |
if(formatter){ |
| 291 |
cellDef.formatter = lang.getObject(formatter)||formatter; |
| 292 |
} |
| 293 |
var get = lang.trim(domAttr.get(node, "get")||""); |
| 294 |
if(get){ |
| 295 |
cellDef.get = lang.getObject(get); |
| 296 |
} |
| 297 |
var getBoolAttr = function(attr, cell, cellAttr){ |
| 298 |
var value = lang.trim(domAttr.get(node, attr)||""); |
| 299 |
if(value){ cell[cellAttr||attr] = !(value.toLowerCase()=="false"); } |
| 300 |
}; |
| 301 |
getBoolAttr("sortDesc", cellDef); |
| 302 |
getBoolAttr("editable", cellDef); |
| 303 |
getBoolAttr("alwaysEditing", cellDef); |
| 304 |
getBoolAttr("noresize", cellDef); |
| 305 |
getBoolAttr("draggable", cellDef); |
| 306 |
|
| 307 |
var value = lang.trim(domAttr.get(node, "loadingText")||domAttr.get(node, "defaultValue")||""); |
| 308 |
if(value){ |
| 309 |
cellDef.defaultValue = value; |
| 310 |
} |
| 311 |
|
| 312 |
var getStrAttr = function(attr, cell, cellAttr){ |
| 313 |
var value = lang.trim(domAttr.get(node, attr)||"")||undefined; |
| 314 |
if(value){ cell[cellAttr||attr] = value; } |
| 315 |
}; |
| 316 |
getStrAttr("styles", cellDef); |
| 317 |
getStrAttr("headerStyles", cellDef); |
| 318 |
getStrAttr("cellStyles", cellDef); |
| 319 |
getStrAttr("classes", cellDef); |
| 320 |
getStrAttr("headerClasses", cellDef); |
| 321 |
getStrAttr("cellClasses", cellDef); |
| 322 |
}; |
| 323 |
|
| 324 |
var Cell = BaseCell.Cell = declare("dojox.grid.cells.Cell", BaseCell, { |
| 325 |
|
| 326 |
|
| 327 |
constructor: function(){ |
| 328 |
this.keyFilter = this.keyFilter; |
| 329 |
}, |
| 330 |
|
| 331 |
|
| 332 |
keyFilter: null, |
| 333 |
formatEditing: function(inDatum, inRowIndex){ |
| 334 |
this.needFormatNode(inDatum, inRowIndex); |
| 335 |
if (inDatum && inDatum.replace) { |
| 336 |
|
| 337 |
inDatum = inDatum.replace(/"/g, '"') |
| 338 |
} |
| 339 |
return '<input class="dojoxGridInput" type="text" value="' + inDatum + '">'; |
| 340 |
}, |
| 341 |
formatNode: function(inNode, inDatum, inRowIndex){ |
| 342 |
this.inherited(arguments); |
| 343 |
|
| 344 |
this.registerOnBlur(inNode, inRowIndex); |
| 345 |
}, |
| 346 |
doKey: function(e){ |
| 347 |
if(this.keyFilter){ |
| 348 |
var key = String.fromCharCode(e.charCode); |
| 349 |
if(key.search(this.keyFilter) == -1){ |
| 350 |
event.stop(e); |
| 351 |
} |
| 352 |
} |
| 353 |
}, |
| 354 |
_finish: function(inRowIndex){ |
| 355 |
this.inherited(arguments); |
| 356 |
var n = this.getEditNode(inRowIndex); |
| 357 |
try{ |
| 358 |
util.fire(n, "blur"); |
| 359 |
}catch(e){} |
| 360 |
} |
| 361 |
}); |
| 362 |
Cell.markupFactory = function(node, cellDef){ |
| 363 |
BaseCell.markupFactory(node, cellDef); |
| 364 |
var keyFilter = lang.trim(domAttr.get(node, "keyFilter")||""); |
| 365 |
if(keyFilter){ |
| 366 |
cellDef.keyFilter = new RegExp(keyFilter); |
| 367 |
} |
| 368 |
}; |
| 369 |
|
| 370 |
var RowIndex = BaseCell.RowIndex = declare("dojox.grid.cells.RowIndex", Cell, { |
| 371 |
name: 'Row', |
| 372 |
|
| 373 |
postscript: function(){ |
| 374 |
this.editable = false; |
| 375 |
}, |
| 376 |
get: function(inRowIndex){ |
| 377 |
return inRowIndex + 1; |
| 378 |
} |
| 379 |
}); |
| 380 |
RowIndex.markupFactory = function(node, cellDef){ |
| 381 |
Cell.markupFactory(node, cellDef); |
| 382 |
}; |
| 383 |
|
| 384 |
var Select = BaseCell.Select = declare("dojox.grid.cells.Select", Cell, { |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
options: null, |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
values: null, |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
returnIndex: -1, |
| 399 |
|
| 400 |
constructor: function(inCell){ |
| 401 |
this.values = this.values || this.options; |
| 402 |
}, |
| 403 |
formatEditing: function(inDatum, inRowIndex){ |
| 404 |
this.needFormatNode(inDatum, inRowIndex); |
| 405 |
var h = [ '<select class="dojoxGridSelect">' ]; |
| 406 |
for (var i=0, o, v; ((o=this.options[i]) !== undefined)&&((v=this.values[i]) !== undefined); i++){ |
| 407 |
v = v.replace ? v.replace(/&/g, '&').replace(/</g, '<') : v; |
| 408 |
o = o.replace ? o.replace(/&/g, '&').replace(/</g, '<') : o; |
| 409 |
h.push("<option", (inDatum==v ? ' selected' : ''), ' value="' + v + '"', ">", o, "</option>"); |
| 410 |
} |
| 411 |
h.push('</select>'); |
| 412 |
return h.join(''); |
| 413 |
}, |
| 414 |
_defaultFormat: function(inValue, callArgs){ |
| 415 |
var v = this.inherited(arguments); |
| 416 |
|
| 417 |
|
| 418 |
if(!this.formatter && this.values && this.options){ |
| 419 |
var i = array.indexOf(this.values, v); |
| 420 |
if(i >= 0){ |
| 421 |
v = this.options[i]; |
| 422 |
} |
| 423 |
} |
| 424 |
return v; |
| 425 |
}, |
| 426 |
getValue: function(inRowIndex){ |
| 427 |
var n = this.getEditNode(inRowIndex); |
| 428 |
if(n){ |
| 429 |
var i = n.selectedIndex, o = n.options[i]; |
| 430 |
return this.returnIndex > -1 ? i : o.value || o.innerHTML; |
| 431 |
} |
| 432 |
} |
| 433 |
}); |
| 434 |
Select.markupFactory = function(node, cell){ |
| 435 |
Cell.markupFactory(node, cell); |
| 436 |
var options = lang.trim(domAttr.get(node, "options")||""); |
| 437 |
if(options){ |
| 438 |
var o = options.split(','); |
| 439 |
if(o[0] != options){ |
| 440 |
cell.options = o; |
| 441 |
} |
| 442 |
} |
| 443 |
var values = lang.trim(domAttr.get(node, "values")||""); |
| 444 |
if(values){ |
| 445 |
var v = values.split(','); |
| 446 |
if(v[0] != values){ |
| 447 |
cell.values = v; |
| 448 |
} |
| 449 |
} |
| 450 |
}; |
| 451 |
|
| 452 |
var AlwaysEdit = BaseCell.AlwaysEdit = declare("dojox.grid.cells.AlwaysEdit", Cell, { |
| 453 |
|
| 454 |
|
| 455 |
alwaysEditing: true, |
| 456 |
_formatNode: function(inDatum, inRowIndex){ |
| 457 |
this.formatNode(this.getEditNode(inRowIndex), inDatum, inRowIndex); |
| 458 |
}, |
| 459 |
applyStaticValue: function(inRowIndex){ |
| 460 |
var e = this.grid.edit; |
| 461 |
e.applyCellEdit(this.getValue(inRowIndex), this, inRowIndex); |
| 462 |
e.start(this, inRowIndex, true); |
| 463 |
} |
| 464 |
}); |
| 465 |
AlwaysEdit.markupFactory = function(node, cell){ |
| 466 |
Cell.markupFactory(node, cell); |
| 467 |
}; |
| 468 |
|
| 469 |
var Bool = BaseCell.Bool = declare("dojox.grid.cells.Bool", AlwaysEdit, { |
| 470 |
|
| 471 |
|
| 472 |
_valueProp: "checked", |
| 473 |
formatEditing: function(inDatum, inRowIndex){ |
| 474 |
return '<input class="dojoxGridInput" type="checkbox"' + (inDatum ? ' checked="checked"' : '') + ' style="width: auto" />'; |
| 475 |
}, |
| 476 |
doclick: function(e){ |
| 477 |
if(e.target.tagName == 'INPUT'){ |
| 478 |
this.applyStaticValue(e.rowIndex); |
| 479 |
} |
| 480 |
} |
| 481 |
}); |
| 482 |
Bool.markupFactory = function(node, cell){ |
| 483 |
AlwaysEdit.markupFactory(node, cell); |
| 484 |
}; |
| 485 |
|
| 486 |
return BaseCell; |
| 487 |
|
| 488 |
}); |