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

Last change on this file since 384 was 383, checked in by jkraaijeveld, 13 years ago
  • Cleaned up some code in the WidgetFactory?
  • Started out on multiple choice input widget, not much progress yet but figured I'd commit everything before holiday anyway
File size: 6.0 KB
Line 
1define( [
2        'dojo',
3        'dojo/_base/lang',
4        'dijit',
5        'dijit/layout/StackContainer',
6        'dojox/form/CheckedMultiSelect',
7        'dojox/layout/TableContainer',
8        'rft/ui/MultipleChoiceListView'],
9        function(dojo, lang, dijit, StackContainer, CheckedMultiSelect, TableContainer, ListView) {
10                dojo.provide('rft.ui.InnerWidgetFactory');
11                dojo.declare('rft.ui.InnerWidgetFactory', [],{
12                        /* No default type, all should be valid */
13                        createWidget: function(/*Object*/ options) {
14                                // options: Object
15                                //                      type: "header", "text", textinput, etc.
16                                //                      contents: "text"
17                                //                      disabled: bool
18                                return this['create'+options.widgetType+'Widget'](options);
19                        },
20                        createHeaderWidget: function(options) {
21                                var headerWidget = new rft.ui.HeaderItem();
22                                headerWidget.setObject(options);
23                                return headerWidget;
24                        },
25                        createTextWidget: function(options) {
26                                var textWidget = new rft.ui.TextItem();
27                                textWidget.setObject(options);
28                                return textWidget;
29                        },
30                        createFreeTextInputWidget: function(options) {
31                                return new rft.ui.FreeTextInput();
32                        },
33                        createIntegerInputWidget: function(options) {
34                                var integerWidget = new rft.ui.IntegerInput();
35                                integerWidget.setObject(options);
36                                return integerWidget;
37                        },
38                        createMultipleChoiceInputWidget: function(options) {
39                                var mcWidget = new rft.ui.MultipleChoiceInput();
40                                mcWidget.setObject(options);
41                                return mcWidget;
42                        }
43                });
44
45                /* Contents */
46                dojo.declare('rft.ui.HeaderItem', dijit.form.TextBox, {
47                        getObject: function() {
48                                return { widgetType : 'Header',
49                                                 contents: this.get('value'),
50                                                 disabled: this.get('disabled')
51                                };
52                        },
53                        setObject: function(object) {
54                                this.set('value', object.contents);
55                                if (object.disabled) {
56                                        this.set('disabled', true);
57                                }
58                        }
59                });
60
61                dojo.declare('rft.ui.TextItem', dijit.form.Textarea, {
62                        getObject: function() {
63                                return { widgetType : 'Text',
64                                                 contents: this.get('value'),
65                                                 disabled: this.get('disabled')
66                                };
67                        },
68                        setObject: function(object) {
69                                this.set('value', object.contents);
70                                if (object.disabled) {
71                                        this.set('disabled', true);
72                                }
73                        }       
74                });
75
76
77
78                /* Inputs */
79                dojo.declare('rft.ui.FreeTextInput', dijit.form.Textarea, {
80                        getObject: function() {
81                                return { widgetType: 'FreeTextInput' };
82                        }
83                });
84
85                //Use CheckedMultiSelect
86                dojo.declare('rft.ui.MultipleChoiceInput',  StackContainer, {
87                        _multiSelect: null,
88                        postCreate: function() {
89                                this.inherited(arguments);
90                                this._multiSelect = new CheckedMultiSelect();
91                                this._multiSelect.addOption({ label: "Hurr durr", value: 1 });
92                                this._multiSelect.addOption({ label: "This is a choice", value: 1 });
93                                this._multiSelect.addOption({ label: "This too", value: 1 });
94                                this._multiSelect.addOption({ label: "So because there are more", value: 1 });
95                                this._multiSelect.addOption({ label: "This must be multiple choice.", value: 1 });
96                                this.addChild(this._multiSelect);
97                        },
98
99                        setObject: function() {
100                                return;
101                        },
102
103                        edit: function() {
104                                return;
105                        }
106
107                });
108
109                dojo.declare('rft.ui.IntegerInput', StackContainer, {
110                        _numberSpinner:  null,
111                        _simpleTable: null,
112                        _editWidgets: null,
113                        _editTable: null,
114                        postCreate: function() {
115                                this.inherited(arguments);
116                                this._numberSpinner = new dijit.form.NumberSpinner( { title: "Answer", value: 0, constraints: { min: -100, max: 100 } });       
117                                this._simpleTable = new TableContainer({ cols: 1, customClass: "labelsAndValues", labelWidth : 150} );
118                                this._simpleTable.addChild(this._numberSpinner);
119                                this._editTable = new TableContainer({ cols: 1, customClass: "labelsAndValues"} );
120                                this._simpleTable.startup();
121                                this.addChild(this._simpleTable);
122                        },
123                        edit: function() {
124                                this.removeChild(this._simpleTable);
125                                this.addChild(this._editTable);
126                        },
127                        save: function () {
128                                for (widget in this._editWidgets) {
129                                        var w = this._editWidgets[widget];
130                                        if (w.value) {
131                                                if (w.constraint)
132                                                        this._numberSpinner.constraints[w.field] = w.value;
133                                                else
134                                                        this._numberSpinner.set(w.field, w.value);
135                                        }
136                                }
137                                this.removeChild(this._editTable);
138                                this.addChild(this._simpleTable);
139                                this._simpleTable.layout();
140                        },
141                        setObject: function(object) {
142                                if(object.contents) {
143                                        lang.mixin(this._numberSpinner, object.contents);
144                                        this._setupEditWidgets(object.contents);
145                                }
146                                else {
147                                        //Create widgets with default values
148                                        this._setupEditWidgets(this.getObject().contents);
149                                }
150                        },
151                        _setupEditWidgets: function(contents) {
152                                //Set up widgets which should appear once edit is pressed.
153                                this._editWidgets = [];
154                                this._editWidgets.push(new dijit.form.NumberSpinner( { field: "value", title: "Default value" }))
155                                this._editWidgets.push(new dijit.form.NumberSpinner( { field: "smallDelta", title: "Increment size" }));
156                                this._editWidgets.push(new dijit.form.NumberSpinner( { constraint: true, field: "max", title: "Maximum value" }));
157                                this._editWidgets.push(new dijit.form.NumberSpinner( { constraint: true, field: "min", title: "Minimum value" }));
158                                this._editWidgets.push(new dijit.form.TextBox ( { field: "invalidMessage", title: "Invalid message" }));
159                                this._editWidgets.push(new dijit.form.TextBox ( { field: "title", title: "Label" }));
160                                for (widget in this._editWidgets) {
161                                        var w = this._editWidgets[widget];
162                                        if (w.constraint)
163                                                w.set('value', contents.constraints[w.field]);
164                                        else
165                                                w.set('value', contents[w.field]);
166                                        this._editTable.addChild(w);
167                                }
168                                this._editTable.startup();
169                        },
170                        getObject: function() {
171                                return {
172                                        widgetType: 'IntegerInput',
173                                        contents: {
174                                                value: this._numberSpinner.value,
175                                            smallDelta: this._numberSpinner.smallDelta,
176                                                constraints: this._numberSpinner.constraints,
177                                                invalidMessage: this._numberSpinner.invalidMessage,
178                                                title: this._numberSpinner.title
179                                        }
180                                };
181                        }
182                });
183        }
184)
Note: See TracBrowser for help on using the repository browser.