source: Dev/branches/rest-dojo-ui/client/qed/model/widgets/QuestionWidget.js @ 420

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

Put all model dependent code in one place. More separation of general and domain code.

File size: 3.1 KB
Line 
1define(['dojo/_base/declare','dojo/_base/lang','dojo/dom-construct','dijit/_WidgetBase',
2    'dijit/_TemplatedMixin','dijit/_WidgetsInTemplateMixin',
3    'dojo/text!./templates/QuestionWidget.html','dijit/form/TextBox',
4    'dijit/form/Textarea','../../widgets/MultipleChoiceWidget'],
5    function(declare,lang,domConstruct,_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin,templateString,TextBox,Textarea,MultipleChoiceWidget){
6        return declare([_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin],{
7            templateString: templateString,
8            mode: 'view', // view || edit
9            name: '',
10            value: null,
11            _scale: null,
12            _widgetCache: null,
13            constructor: function() {
14                this.inherited(arguments);
15                this.value = {};
16                this._widgetCache = {};
17            },
18            postCreate: function() {
19                this._resetValue = this.value;
20                this.scaleSelector.set('disabled', this.mode == 'edit');
21            },
22            _setValueAttr: function(value) {
23                this.value = value;
24                this._onTypeChange(value.scale || 'string');
25                this.ourForm.set('value',value);
26            },
27            _getValueAttr: function() {
28                var value = this.ourForm.get('value');
29                lang.mixin(this.value,value);
30                return this.value;
31            },
32            _onTypeChange: function(scale) {
33                if ( this._scale == scale ) return;
34                this._scale = scale;
35                domConstruct.empty(this.scaleDetails);
36                var widget = this._getTypeWidget(scale);
37                widget && widget.placeAt(this.scaleDetails,'only');
38            },
39            _getTypeWidget: function(scale) {
40                var widget = this._widgetCache[scale];
41                if (!widget) {
42                    switch(scale) {
43                        case 'string':
44                            widget = new TextBox({
45                                name: 'answers',
46                                disabled: this.mode == 'view'
47                            });
48                            break;
49                        case 'text':
50                            widget = new Textarea({
51                                name: 'answers',
52                                disabled: this.mode == 'view',
53                                style: 'min-height: 120px'
54                            });
55                            break;
56                        case 'singleChoice':
57                        case 'multipleChoice':
58                            widget = new MultipleChoiceWidget({
59                                name: 'answers',
60                                mode: this.mode,
61                                allowMultiple: scale == 'multipleChoice'
62                            });
63                            break;
64                    }
65                    this._widgetCache[scale] = widget;
66                }
67                return widget;
68            },
69            reset: function() {
70                this.ourForm.reset();
71                this._setValueAttr(this._resetValue);
72            }
73        });
74    });
Note: See TracBrowser for help on using the repository browser.