source: Dev/branches/rest-dojo-ui/client/dojox/grid/cells/dijit.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 7.2 KB
Line 
1define([
2        "dojo/_base/kernel",
3        "../../main",
4        "dojo/_base/declare",
5        "dojo/_base/array",
6        "dojo/_base/lang",
7        "dojo/_base/json",
8        "dojo/_base/connect",
9        "dojo/_base/sniff",
10        "dojo/dom",
11        "dojo/dom-attr",
12        "dojo/dom-construct",
13        "dojo/dom-geometry",
14        "dojo/data/ItemFileReadStore",
15        "dijit/form/DateTextBox",
16        "dijit/form/TimeTextBox",
17        "dijit/form/ComboBox",
18        "dijit/form/CheckBox",
19        "dijit/form/TextBox",
20        "dijit/form/NumberSpinner",
21        "dijit/form/NumberTextBox",
22        "dijit/form/CurrencyTextBox",
23        "dijit/form/HorizontalSlider",
24        "dijit/Editor",
25        "../util",
26        "./_base"
27], function(dojo, dojox, declare, array, lang, json, connect, has, dom, domAttr, domConstruct,
28        domGeometry, ItemFileReadStore, DateTextBox, TimeTextBox, ComboBox, CheckBox, TextBox,
29        NumberSpinner, NumberTextBox, CurrencyTextBox, HorizontalSlider, Editor, util, BaseCell){
30               
31// TODO: shouldn't it be the test file's job to require these modules,
32// if it is using them?  Most of these modules aren't referenced by this file.
33       
34        var _Widget = declare("dojox.grid.cells._Widget", BaseCell, {
35                widgetClass: TextBox,
36                constructor: function(inCell){
37                        this.widget = null;
38                        if(typeof this.widgetClass == "string"){
39                                dojo.deprecated("Passing a string to widgetClass is deprecated", "pass the widget class object instead", "2.0");
40                                this.widgetClass = lang.getObject(this.widgetClass);
41                        }
42                },
43                formatEditing: function(inDatum, inRowIndex){
44                        this.needFormatNode(inDatum, inRowIndex);
45                        return "<div></div>";
46                },
47                getValue: function(inRowIndex){
48                        return this.widget.get('value');
49                },
50                _unescapeHTML: function(value){
51                        return (value && value.replace && this.grid.escapeHTMLInData) ?
52                                        value.replace(/&lt;/g, '<').replace(/&amp;/g, '&') : value;
53                },
54                setValue: function(inRowIndex, inValue){
55                        if(this.widget&&this.widget.set){
56                                inValue = this._unescapeHTML(inValue);
57                                //Look for lazy-loading editor and handle it via its deferred.
58                                if(this.widget.onLoadDeferred){
59                                        var self = this;
60                                        this.widget.onLoadDeferred.addCallback(function(){
61                                                self.widget.set("value",inValue===null?"":inValue);
62                                        });
63                                }else{
64                                        this.widget.set("value", inValue);
65                                }
66                        }else{
67                                this.inherited(arguments);
68                        }
69                },
70                getWidgetProps: function(inDatum){
71                        return lang.mixin(
72                                {
73                                        dir: this.dir,
74                                        lang: this.lang
75                                },
76                                this.widgetProps||{},
77                                {
78                                        constraints: lang.mixin({}, this.constraint) || {}, //TODO: really just for ValidationTextBoxes
79                                        value: this._unescapeHTML(inDatum)
80                                }
81                        );
82                },
83                createWidget: function(inNode, inDatum, inRowIndex){
84                        return new this.widgetClass(this.getWidgetProps(inDatum), inNode);
85                },
86                attachWidget: function(inNode, inDatum, inRowIndex){
87                        inNode.appendChild(this.widget.domNode);
88                        this.setValue(inRowIndex, inDatum);
89                },
90                formatNode: function(inNode, inDatum, inRowIndex){
91                        if(!this.widgetClass){
92                                return inDatum;
93                        }
94                        if(!this.widget){
95                                this.widget = this.createWidget.apply(this, arguments);
96                        }else{
97                                this.attachWidget.apply(this, arguments);
98                        }
99                        this.sizeWidget.apply(this, arguments);
100                        this.grid.views.renormalizeRow(inRowIndex);
101                        this.grid.scroller.rowHeightChanged(inRowIndex, true/*fix #11101*/);
102                        this.focus();
103                        return undefined;
104                },
105                sizeWidget: function(inNode, inDatum, inRowIndex){
106                        var
107                                p = this.getNode(inRowIndex),
108                                box = dojo.contentBox(p);
109                        dojo.marginBox(this.widget.domNode, {w: box.w});
110                },
111                focus: function(inRowIndex, inNode){
112                        if(this.widget){
113                                setTimeout(lang.hitch(this.widget, function(){
114                                        util.fire(this, "focus");
115                                }), 0);
116                        }
117                },
118                _finish: function(inRowIndex){
119                        this.inherited(arguments);
120                        util.removeNode(this.widget.domNode);
121                        if(has('ie')){
122                                dom.setSelectable(this.widget.domNode, true);
123                        }
124                }
125        });
126        _Widget.markupFactory = function(node, cell){
127                BaseCell.markupFactory(node, cell);
128                var widgetProps = lang.trim(domAttr.get(node, "widgetProps")||"");
129                var constraint = lang.trim(domAttr.get(node, "constraint")||"");
130                var widgetClass = lang.trim(domAttr.get(node, "widgetClass")||"");
131                if(widgetProps){
132                        cell.widgetProps = json.fromJson(widgetProps);
133                }
134                if(constraint){
135                        cell.constraint = json.fromJson(constraint);
136                }
137                if(widgetClass){
138                        cell.widgetClass = lang.getObject(widgetClass);
139                }
140        };
141
142        var ComboBox = declare("dojox.grid.cells.ComboBox", _Widget, {
143                widgetClass: ComboBox,
144                getWidgetProps: function(inDatum){
145                        var items=[];
146                        array.forEach(this.options, function(o){
147                                items.push({name: o, value: o});
148                        });
149                        var store = new ItemFileReadStore({data: {identifier:"name", items: items}});
150                        return lang.mixin({}, this.widgetProps||{}, {
151                                value: inDatum,
152                                store: store
153                        });
154                },
155                getValue: function(){
156                        var e = this.widget;
157                        // make sure to apply the displayed value
158                        e.set('displayedValue', e.get('displayedValue'));
159                        return e.get('value');
160                }
161        });
162        ComboBox.markupFactory = function(node, cell){
163                _Widget.markupFactory(node, cell);
164                var options = lang.trim(domAttr.get(node, "options")||"");
165                if(options){
166                        var o = options.split(',');
167                        if(o[0] != options){
168                                cell.options = o;
169                        }
170                }
171        };
172
173        var DateTextBox = declare("dojox.grid.cells.DateTextBox", _Widget, {
174                widgetClass: DateTextBox,
175                setValue: function(inRowIndex, inValue){
176                        if(this.widget){
177                                this.widget.set('value', new Date(inValue));
178                        }else{
179                                this.inherited(arguments);
180                        }
181                },
182                getWidgetProps: function(inDatum){
183                        return lang.mixin(this.inherited(arguments), {
184                                value: new Date(inDatum)
185                        });
186                }
187        });
188        DateTextBox.markupFactory = function(node, cell){
189                _Widget.markupFactory(node, cell);
190        };
191
192        var CheckBox = declare("dojox.grid.cells.CheckBox", _Widget, {
193                widgetClass: CheckBox,
194                getValue: function(){
195                        return this.widget.checked;
196                },
197                setValue: function(inRowIndex, inValue){
198                        if(this.widget&&this.widget.attributeMap.checked){
199                                this.widget.set("checked", inValue);
200                        }else{
201                                this.inherited(arguments);
202                        }
203                },
204                sizeWidget: function(inNode, inDatum, inRowIndex){
205                        return;
206                }
207        });
208        CheckBox.markupFactory = function(node, cell){
209                _Widget.markupFactory(node, cell);
210        };
211
212        var Editor = declare("dojox.grid.cells.Editor", _Widget, {
213                widgetClass: Editor,
214                getWidgetProps: function(inDatum){
215                        return lang.mixin({}, this.widgetProps||{}, {
216                                height: this.widgetHeight || "100px"
217                        });
218                },
219                createWidget: function(inNode, inDatum, inRowIndex){
220                        // widget needs its value set after creation
221                        var widget = new this.widgetClass(this.getWidgetProps(inDatum), inNode);
222                        connect.connect(widget, 'onLoad', lang.hitch(this, 'populateEditor'));
223                        return widget;
224                },
225                formatNode: function(inNode, inDatum, inRowIndex){
226                        this.content = inDatum;
227                        this.inherited(arguments);
228                        if(has('mozilla')){
229                                // FIXME: seem to need to reopen the editor and display the toolbar
230                                var e = this.widget;
231                                e.open();
232                                if(this.widgetToolbar){
233                                        domConstruct.place(e.toolbar.domNode, e.editingArea, "before");
234                                }
235                        }
236                },
237                populateEditor: function(){
238                        this.widget.set('value', this.content);
239                        this.widget.placeCursorAtEnd();
240                }
241        });
242        Editor.markupFactory = function(node, cell){
243                _Widget.markupFactory(node, cell);
244                var h = lang.trim(domAttr.get(node, "widgetHeight")||"");
245                if(h){
246                        if((h != "auto")&&(h.substr(-2) != "em")){
247                                h = parseInt(h, 10)+"px";
248                        }
249                        cell.widgetHeight = h;
250                }
251        };
252
253        return dojox.grid.cells.dijit;
254
255});
Note: See TracBrowser for help on using the repository browser.