source: Dev/trunk/client/qed/model/widgets/QuestionWidgetFactory.js @ 424

Last change on this file since 424 was 421, checked in by hendrikvanantwerpen, 12 years ago

Show responses with links in SurveyRun? overview.

File size: 12.6 KB
Line 
1define([
2    'dojo/_base/array',
3    'dojo/_base/declare',
4    'dojo/_base/lang',
5    'dojo/dom-construct',
6    'dijit/_TemplatedMixin',
7    'dijit/_WidgetBase',
8    'dijit/_Container',
9    'dijit/form/Button',
10    'dijit/form/CheckBox',
11    'dijit/form/Form',
12    'dijit/form/NumberSpinner',
13    'dijit/form/RadioButton',
14    'dijit/form/Textarea',
15    'dijit/form/TextBox',
16    'dojox/layout/TableContainer',
17    '../../widgets/list/_EditableListMixin',
18    '../../widgets/list/OrderedList'
19],function(array, declare, lang, domConstruct, _TemplatedMixin, _WidgetBase, _Container, Button, CheckBox, Form, NumberSpinner, RadioButton, Textarea, TextBox, TableContainer, _EditableListMixin, OrderedList) {
20    var factory = declare(null, {
21        createViewWidget: function(/*Object*/options) {
22            // options: Object
23            //            type: "header", "text", textinput, etc.
24            //            other type specific fields
25            var fun = this['create'+options.type+'ViewWidget'];
26            var view = fun !== undefined ? fun(options) : null;
27            return view;
28        },
29        createEditWidget: function(/*Object*/options) {
30            var fun = this['create'+options.type+'EditWidget'];
31            var view = fun !== undefined ? fun() : null;
32            if(view)
33                view.set('value', options);
34            return view;
35        },
36
37        createHeaderViewWidget: function(options) {
38            return new HeaderView({
39                options: options
40            });
41        },
42        createHeaderEditWidget: function() {
43            return new HeaderEdit();
44        },
45
46        createTextViewWidget: function(options) {
47            return new TextView({
48                options: options
49            });
50        },
51        createTextEditWidget: function() {
52            return new TextEdit();
53        },
54
55        createDividerViewWidget: function(options) {
56            return new DividerView({
57                options: options
58            });
59        },
60
61        createStringInputViewWidget: function(options) {
62            return new StringInputView({
63                options: options
64            });
65        },
66        createStringInputEditWidget: function() {
67            return new StringInputEdit();
68        },
69
70        createTextInputViewWidget: function(options) {
71            return new TextInputView({
72                options: options
73            });
74        },
75        createTextInputEditWidget: function() {
76            return new TextInputEdit();
77        },
78
79        createIntegerInputViewWidget: function(options) {
80            return new IntegerInputView({
81                options: options
82            });
83        },
84        createIntegerInputEditWidget: function() {
85            return new IntegerInputEdit();
86        },
87
88        createMultipleChoiceInputViewWidget: function(options) {
89            return new MultipleChoiceInputView({
90                options: options
91            });
92        },
93        createMultipleChoiceInputEditWidget: function() {
94            return new MultipleChoiceInputEdit();
95        }
96    });
97
98    var DefaultEdit = declare([Form,_Container],{
99        type: null,
100        addChild: function(widget) {
101            domConstruct.create("label",{
102                innerHTML: widget.title || ''
103            },this.containerNode,'last');
104            this.inherited(arguments,[widget]);
105        },
106        _getValueAttr: function() {
107            var val = this.inherited(arguments);
108            val.type = this.type;
109            return val;
110        }
111    });
112
113    var HeaderView = declare([_WidgetBase], {
114        postCreate: function() {
115            this.domNode.innerHTML = "<h2>"+this.options.content+"</h2>";
116        }
117    });
118
119    var HeaderEdit = declare([DefaultEdit], {
120        type: 'Header',
121        postCreate: function() {
122            this.inherited(arguments);
123            this.addChild(new TextBox({
124                title: 'Content',
125                name: 'content'
126            }));
127        }
128    });
129
130    var TextView = declare([_WidgetBase], {
131        postCreate: function() {
132            this.domNode.innerHTML = "<p>"+this.options.content+"</p>";
133        }
134    });
135
136    var TextEdit = declare([DefaultEdit], {
137        type: 'Text',
138        postCreate: function() {
139            this.inherited(arguments);
140            this.addChild(new Textarea({
141                title: 'Content',
142                name: 'content'
143            }));
144        }
145    });
146
147    var DividerView = declare([_WidgetBase], {
148        postCreate: function() {
149            this.domNode.innerHTML = "<hr>";
150        }
151    });
152
153    var DefaultInputEdit = declare([DefaultEdit],{
154        postCreate: function() {
155            this.inherited(arguments);
156            this.addChild(new TextBox({
157                title: 'Text',
158                name: 'text'
159            }));
160        }
161    });
162
163    var StringInputView = declare([_WidgetBase, _Container],{
164        _textBox: null,
165        postCreate: function() {
166            this._textBox = new TextBox({
167                name: this.options.code || ''
168            });
169            this.addChild(this._textBox);
170            this._textBox.startup();
171        },
172        _setReadOnlyAttr: function(value) {
173            this._textBox.set('readOnly', value);
174        },
175        _getCodeAttr: function() {
176            return this.options.code;
177        },
178        _getValueAttr: function() {
179            return this._textBox.get('value') || "";
180        },
181        _setValueAttr: function(value) {
182            this._textBox.set('value',value || "");
183        }
184    });
185
186    var StringInputEdit = declare([DefaultInputEdit],{
187        type: 'StringInput'
188    });
189
190    var TextInputView = declare([_WidgetBase, _Container], {
191        _textArea: null,
192        postCreate: function() {
193            this._textArea = new Textarea({
194                name: this.options.code
195            });
196            this._textArea.set('maxLength', this.options.maxLength || 1000);
197            this._textArea.set('value', this.options.defaultValue || "");
198            this.addChild(this._textArea);
199            this._textArea.startup();
200        },
201        _getCodeAttr: function() {
202            return this.options.code;
203        },
204        _getValueAttr: function() {
205            return this._textArea.get('value');
206        },
207        _setReadOnlyAttr: function(value) {
208            this._textArea.set('readOnly', value);
209        }
210    });
211
212    var TextInputEdit = declare([DefaultInputEdit], {
213        type: 'TextInput',
214        postCreate: function() {
215            this.inherited(arguments);
216            this.addChild(new NumberSpinner({
217                name: 'maxLength',
218                title: "Maximum length",
219                constraints: {
220                    min: 0
221                }
222            }));
223        }
224    });
225
226    var IntegerInputView = declare([_WidgetBase, _Container], {
227        _numberInput: null,
228        postCreate: function() {
229            this._numberInput = new NumberSpinner({
230                name: this.options.code || '',
231                constraints: {
232                    min: this.options.min,
233                    max: this.options.max,
234                    smallDelta: this.options.step
235                }
236            });
237            this.addChild(this._numberInput);
238            this._numberInput.startup();
239        },
240        _getCodeAttr: function() {
241            return this.options.code;
242        },
243        _getValueAttr: function() {
244            return this._numberInput.get('value');
245        },
246        _setReadOnlyAttr: function(value) {
247            this._numberInput.set('readOnly', value);
248        }
249    });
250
251    var IntegerInputEdit = declare([DefaultInputEdit], {
252        type: 'IntegerInput',
253        postCreate: function() {
254            this.inherited(arguments);
255            this.addChild(new NumberSpinner({
256                title: "Minimum",
257                name: 'min'
258            }));
259            this.addChild(new NumberSpinner({
260                title: "Maximum",
261                name: 'max'
262            }));
263            this.addChild(new NumberSpinner({
264                title: "Step",
265                name: 'step'
266            }));
267        }
268    });
269
270    var MultipleChoiceInputView = declare([_WidgetBase, _Container], {
271        postCreate: function() {
272            var table = new TableContainer({ cols: 1, customClass: "labelsAndValues"} );
273
274            var ctor = this.options.multiple === true ? CheckBox : RadioButton;
275            array.forEach(this.options.items || [],function(item){
276                table.addChild(new ctor({
277                        name: this.options.code || '',
278                        title: item
279                }));
280            },this);
281
282            this.addChild(table);
283            table.startup();
284        },
285        _getCodeAttr: function() {
286            return this.options.code;
287        },
288        _getValueAttr: function() {
289            var checked = array.filter(this.getChildren(),function(child){
290                return child.get('checked');
291            });
292            if ( this.options.multiple ) {
293                return array.map(checked,function(child){
294                    return child.get('value');
295                });
296            } else {
297                return checked.length > 0 ? checked[0].get('value') : null;
298            }
299        },
300        _setReadOnlyAttr: function(value) {
301            array.forEach(this.getChildren(),function(child){
302                child.set('readOnly', value);
303            },this);
304        }
305    });
306
307    var MCOptionItem = declare([_WidgetBase,_TemplatedMixin,_Container],{
308        templateString: '<div><span class="dojoDndHandle">=</span></div>',
309        _textBox: null,
310        postCreate: function() {
311            this._textBox = new TextBox({});
312            this.addChild(this._textBox);
313            this._textBox.startup();
314
315            var del = new Button({
316                label: "X",
317                onClick: lang.hitch(this,'onDelete')
318            });
319            this.addChild(del);
320            del.startup();
321        },
322        _getValueAttr: function() {
323            return this._textBox.set('value');
324        },
325        _setValueAttr: function(value) {
326            this._textBox.set('value',value);
327        },
328        onDelete: function(){}
329    });
330
331    var MCOptionList = declare([OrderedList,_EditableListMixin],{
332        type: 'multipleChoiceOption',
333        withHandles: true,
334        _createAvatarNode: function(item){
335            return domConstruct.create("div",{
336                'class': 'dragAvatar',
337                innerHTML: item
338            });
339        },
340        _createListNode: function(item) {
341            var w = new MCOptionItem();
342            w.startup();
343            w.set('value',item);
344            w.on('delete',lang.hitch(this,'_onRemove',w));
345            return w.domNode;
346        },
347        _onRemove: function(w) {
348            w.destroyRecursive();
349            this.removeCallback && this.removeCallback(item);
350            this.source.sync();
351        }
352    });
353
354    var MultipleChoiceInputEdit = declare([_WidgetBase, _Container], {
355        _multipleInput: null,
356        postCreate: function() {
357            var table = new TableContainer({ cols: 1, customClass: "labelsAndValues"} );
358
359            this._multipleInput = new CheckBox({
360                title: "Allow multiple"
361            });
362            table.addChild(this._multipleInput);
363            this._multipleInput.startup();
364
365            var add = new Button({
366                label: "Add",
367                onClick: lang.hitch(this,'_addOption',"")
368            });
369            table.addChild(add);
370            add.startup();
371
372            this._optionsList = new MCOptionList();
373            table.addChild(this._optionsList);
374            this._optionsList.startup();
375
376            this.addChild(table);
377            table.startup();
378        },
379
380        _addOption: function() {
381            this._optionsList.appendItems([""]);
382        },
383
384        _setValueAttr: function(value) {
385            this._multipleInput.set('checked', value.multiple);
386            this._optionsList.deleteItems();
387            this._optionsList.appendItems(value.items || []);
388        },
389        _getValueAttr: function() {
390            return {
391                type: "MultipleChoiceInput",
392                multiple: this._multipleInput.get('checked'),
393                items: array.map(this._optionsList.getItems(),function(item){
394                    return item.get('value');
395                },this)
396            };
397        }
398    });
399
400    return factory;
401});
Note: See TracBrowser for help on using the repository browser.