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','./MultipleChoiceWidget'], |
---|
5 | function(declare,lang,domConstruct,_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin,templateString,TextBox,Textarea,MultipleChoiceWidget){ |
---|
6 | return declare('rft.ui.QuestionWidget',[_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin],{ |
---|
7 | templateString: templateString, |
---|
8 | mode: 'view', // view || edit |
---|
9 | name: '', |
---|
10 | value: null, |
---|
11 | _type: 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.typeSelector.set('disabled', this.mode == 'edit'); |
---|
21 | }, |
---|
22 | _setValueAttr: function(value) { |
---|
23 | this.value = value; |
---|
24 | this._onTypeChange(value.type || '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._question; |
---|
31 | }, |
---|
32 | _onTypeChange: function(type) { |
---|
33 | if ( this._type == type ) return; |
---|
34 | this._type = type; |
---|
35 | domConstruct.empty(this.typeDetails); |
---|
36 | var widget = this._getTypeWidget(type); |
---|
37 | widget && widget.placeAt(this.typeDetails,'only'); |
---|
38 | }, |
---|
39 | _getTypeWidget: function(type) { |
---|
40 | var widget = this._widgetCache[type]; |
---|
41 | if (!widget) { |
---|
42 | switch(type) { |
---|
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: type == 'multipleChoice' |
---|
62 | }); |
---|
63 | break; |
---|
64 | } |
---|
65 | this._widgetCache[type] = widget; |
---|
66 | } |
---|
67 | return widget; |
---|
68 | }, |
---|
69 | reset: function() { |
---|
70 | this.ourForm.reset(); |
---|
71 | this._setValueAttr(this._resetValue); |
---|
72 | } |
---|
73 | }); |
---|
74 | }); |
---|