source: Dev/branches/rest-dojo-ui/client/rft/ui/InnerWidgetFactory.js @ 382

Last change on this file since 382 was 381, checked in by jkraaijeveld, 13 years ago

Created the IntegerInput? inner widget. Stores properly and allows for a lot of customization.

File size: 4.9 KB
Line 
1define( ['dojo', 'dijit', 'dojo/_base/lang', 'dojox/layout/TableContainer',
2                 'dijit/layout/StackContainer'],
3        function(dojo, dijit, lang, TableContainer, StackContainer) {
4                dojo.provide('rft.ui.InnerWidgetFactory');
5                dojo.declare('rft.ui.InnerWidgetFactory', [],{
6                        /* No default type, all should be valid */
7                        createWidget: function(/*Object*/ options) {
8                                // options: Object
9                                //                      type: "header", "text", textinput, etc.
10                                //                      contents: "text"
11                                //                      disabled: bool
12                                return this['create'+options.widgetType+'Widget'](options);
13                        },
14                        createHeaderWidget: function(options) {
15                                var headerWidget = new rft.ui.HeaderItem();
16                                headerWidget.setObject(options);
17                                return headerWidget;
18                        },
19                        createTextWidget: function(options) {
20                                var textWidget = new rft.ui.TextItem();
21                                textWidget.setObject(options);
22                                return textWidget;
23                        },
24                        createFreeTextInputWidget: function(options) {
25                                return new rft.ui.FreeTextInput();
26                        },
27                        createIntegerInputWidget: function(options) {
28                                var integerWidget = new rft.ui.IntegerInput();
29                                integerWidget.setObject(options);
30                                return integerWidget;
31                        }
32                });
33
34                /* Contents */
35                dojo.declare('rft.ui.HeaderItem', dijit.form.TextBox, {
36                        getObject: function() {
37                                return { widgetType : 'Header',
38                                                 contents: this.get('value'),
39                                                 disabled: this.get('disabled')
40                                };
41                        },
42                        setObject: function(object) {
43                                this.set('value', object.contents);
44                                if (object.disabled) {
45                                        this.set('disabled', true);
46                                }
47                        }
48                });
49
50                dojo.declare('rft.ui.TextItem', dijit.form.Textarea, {
51                        getObject: function() {
52                                return { widgetType : 'Text',
53                                                 contents: this.get('value'),
54                                                 disabled: this.get('disabled')
55                                };
56                        },
57                        setObject: function(object) {
58                                this.set('value', object.contents);
59                                if (object.disabled) {
60                                        this.set('disabled', true);
61                                }
62                        }       
63                });
64
65
66
67                /* Inputs */
68                dojo.declare('rft.ui.FreeTextInput', dijit.form.Textarea, {
69                        getObject: function() {
70                                return { widgetType: 'FreeTextInput' };
71                        }
72                });
73
74                dojo.declare('rft.ui.IntegerInput', StackContainer, {
75                        _numberSpinner:  null,
76                        _simpleTable: null,
77                        _editWidgets: null,
78                        _editTable: null,
79                        postCreate: function() {
80                                this.inherited(arguments);
81                                this._numberSpinner = new dijit.form.NumberSpinner( { title: "Answer", value: 0 });     
82                                this._numberSpinner.startup();
83                                this._simpleTable = new TableContainer({ cols: 1, customClass: "labelsAndValues", labelWidth : 150} );
84                                this._simpleTable.addChild(this._numberSpinner);
85                               
86                                this._editTable = new TableContainer({ cols: 1, customClass: "labelsAndValues"} );
87
88                                this.addChild(this._simpleTable);
89                        },
90                        edit: function() {
91                                this.removeChild(this._simpleTable);
92                                this.addChild(this._editTable);
93                        },
94                        save: function () {
95                                for (widget in this._editWidgets) {
96                                        var w = this._editWidgets[widget];
97                                        if (w.value) {
98                                                if (w.constraint)
99                                                        this._numberSpinner.constraints[w.field] = w.value;
100                                                else
101                                                        this._numberSpinner.set(w.field, w.value);
102                                        }
103                                }
104                                this.removeChild(this._editTable);
105                                this.addChild(this._simpleTable);
106                                this._simpleTable.layout();
107                        },
108                        setObject: function(object) {
109                                if(object.contents) {
110                                        lang.mixin(this._numberSpinner, object.contents);
111                                        this._setupEditWidgets(object.contents);
112                                }
113                                else {
114                                        //Create widgets with default values
115                                        this._setupEditWidgets(this.getObject().contents);
116                                }
117                        },
118                        _setupEditWidgets: function(contents) {
119                                //Set up widgets which should appear once edit is pressed.
120                                this._editWidgets = [];
121                                this._editWidgets.push(new dijit.form.NumberSpinner( { field: "value", title: "Default value" }))
122                                this._editWidgets.push(new dijit.form.NumberSpinner( { field: "smallDelta", title: "Increment size" }));
123                                this._editWidgets.push(new dijit.form.NumberSpinner( { constraint: true, field: "max", title: "Maximum value" }));
124                                this._editWidgets.push(new dijit.form.NumberSpinner( { constraint: true, field: "min", title: "Minimum value" }));
125                                this._editWidgets.push(new dijit.form.TextBox ( { field: "invalidMessage", title: "Invalid message" }));
126                                this._editWidgets.push(new dijit.form.TextBox ( { field: "title", title: "Label" }));
127                                for (widget in this._editWidgets) {
128                                        var w = this._editWidgets[widget];
129                                        if (w.constraint)
130                                                w.set('value', contents.constraints[w.field]);
131                                        else
132                                                w.set('value', contents[w.field]);
133                                        this._editTable.addChild(w);
134                                }
135                                this._editTable.startup();
136                        },
137                        getObject: function() {
138
139                                return { widgetType: 'IntegerInput',
140                                                 contents:
141                                                 {
142                                                        value: this._numberSpinner.value,
143                                                    smallDelta: this._numberSpinner.smallDelta,
144                                                        constraints: this._numberSpinner.constraints,
145                                                        invalidMessage: this._numberSpinner.invalidMessage,
146                                                        title: this._numberSpinner.title
147                                                 }
148                                };
149                        }
150                });
151        }
152)
Note: See TracBrowser for help on using the repository browser.