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