Debian Codegraph @

dojox/grid/cells/_base.js

raw symbols SCIP
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){// IE sux bad
35 }
36 };
37
38 var whenIdle = function(/*inContext, inMethod, args ...*/){
39 setTimeout(lang.hitch.apply(dojo, arguments), 0);
40 };
41
42 var BaseCell = declare("dojox.grid.cells._Base", null, {
43 // summary:
44 // Represents a grid cell and contains information about column options and methods
45 // for retrieving cell related information.
46 // Each column in a grid layout has a cell object and most events and many methods
47 // provide access to these objects.
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 //private
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 // Check if it's a deferred
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 // data source
98 format: function(inRowIndex, inItem){
99 // summary:
100 // provides the html for a given grid cell.
101 // inRowIndex: int
102 // grid row index
103 // returns:
104 // html for a given grid cell
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, '&amp;').replace(/</g, '&lt;');
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 // summary:
118 // formats the cell for editing
119 // inDatum: anything
120 // cell data to edit
121 // inRowIndex: int
122 // grid row index
123 // returns:
124 // string of html to place in grid cell
125 },
126 // utility
127 getNode: function(inRowIndex){
128 // summary:
129 // gets the dom node for a given grid cell.
130 // inRowIndex: int
131 // grid row index
132 // returns:
133 // dom node for a given grid cell
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 // edit support
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 //console.log('editor onblur', e);
162 this.grid.edit.apply();
163 }
164 },
165 registerOnBlur: function(inNode, inRowIndex){
166 if(this.commitOnBlur){
167 connect.connect(inNode, "onblur", function(e){
168 // hack: if editor still thinks this editor is current some ms after it blurs, assume we've focused away from grid
169 setTimeout(lang.hitch(this, "_onEditBlur", inRowIndex), 250);
170 });
171 }
172 },
173 //protected
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 //private
182 _formatNode: function(inDatum, inRowIndex){
183 if(this._formatPending){
184 this._formatPending = false;
185 // make cell selectable
186 if(!has('ie')){
187 dom.setSelectable(this.grid.domNode, true);
188 }
189 this.formatNode(this.getEditNode(inRowIndex), inDatum, inRowIndex);
190 }
191 },
192 //protected
193 formatNode: function(inNode, inDatum, inRowIndex){
194 // summary:
195 // format the editing dom node. Use when editor is a widget.
196 // inNode: dom node
197 // dom node for the editor
198 // inDatum: anything
199 // cell data to edit
200 // inRowIndex: int
201 // grid row index
202 if(has('ie')){
203 // IE sux bad
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 //public
215 getValue: function(inRowIndex){
216 // summary:
217 // returns value entered into editor
218 // inRowIndex: int
219 // grid row index
220 // returns:
221 // value of editor
222 return this.getEditNode(inRowIndex)[this._valueProp];
223 },
224 setValue: function(inRowIndex, inValue){
225 // summary:
226 // set the value of the grid editor
227 // inRowIndex: int
228 // grid row index
229 // inValue: anything
230 // value of editor
231 var n = this.getEditNode(inRowIndex);
232 if(n){
233 n[this._valueProp] = inValue;
234 }
235 },
236 focus: function(inRowIndex, inNode){
237 // summary:
238 // focus the grid editor
239 // inRowIndex: int
240 // grid row index
241 // inNode: dom node
242 // editor node
243 focusSelectNode(inNode || this.getEditNode(inRowIndex));
244 },
245 save: function(inRowIndex){
246 // summary:
247 // save editor state
248 // inRowIndex: int
249 // grid row index
250 this.value = this.value || this.getValue(inRowIndex);
251 //console.log("save", this.value, inCell.index, inRowIndex);
252 },
253 restore: function(inRowIndex){
254 // summary:
255 // restore editor state
256 // inRowIndex: int
257 // grid row index
258 this.setValue(inRowIndex, this.value);
259 //console.log("restore", this.value, inCell.index, inRowIndex);
260 },
261 //protected
262 _finish: function(inRowIndex){
263 // summary:
264 // called when editing is completed to clean up editor
265 // inRowIndex: int
266 // grid row index
267 dom.setSelectable(this.grid.domNode, false);
268 this.cancelFormatNode();
269 },
270 //public
271 apply: function(inRowIndex){
272 // summary:
273 // apply edit from cell editor
274 // inRowIndex: int
275 // grid row index
276 this.applyEdit(this.getValue(inRowIndex), inRowIndex);
277 this._finish(inRowIndex);
278 },
279 cancel: function(inRowIndex){
280 // summary:
281 // cancel cell edit
282 // inRowIndex: int
283 // grid row index
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 // summary:
326 // grid cell that provides a standard text input box upon editing
327 constructor: function(){
328 this.keyFilter = this.keyFilter;
329 },
330 // keyFilter: RegExp
331 // optional regex for disallowing keypresses
332 keyFilter: null,
333 formatEditing: function(inDatum, inRowIndex){
334 this.needFormatNode(inDatum, inRowIndex);
335 if (inDatum && inDatum.replace) {
336 // escape quotes to avoid XSS
337 inDatum = inDatum.replace(/"/g, '&quot;')
338 }
339 return '<input class="dojoxGridInput" type="text" value="' + inDatum + '">';
340 },
341 formatNode: function(inNode, inDatum, inRowIndex){
342 this.inherited(arguments);
343 // FIXME: feels too specific for this interface
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 // summary:
386 // grid cell that provides a standard select for editing
387
388 // options: Array
389 // text of each item
390 options: null,
391
392 // values: Array
393 // value for each item
394 values: null,
395
396 // returnIndex: Integer
397 // editor returns only the index of the selected option and not the value
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, '&amp;').replace(/</g, '&lt;') : v;
408 o = o.replace ? o.replace(/&/g, '&amp;').replace(/</g, '&lt;') : 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 // when 'values' and 'options' both provided and there is no cutomized formatter,
417 // then we use 'options' as label in order to be consistent
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 // summary:
454 // grid cell that is always in an editable state, regardless of grid editing state
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 // summary:
471 // grid cell that provides a standard checkbox that is always on for editing
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 });