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

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

Added grunt tasks and code cleanup.

Added grunt for less and jshint procesing.
Cleanup of code to pass jshint (one bug found :).

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