1 | define([ |
---|
2 | "../app/Router", |
---|
3 | "../model/classes/surveys", |
---|
4 | "../model/widgets/QuestionListView", |
---|
5 | "../model/widgets/TabbedQuestionBrowser", |
---|
6 | "./_ObjectPage", |
---|
7 | "dojo/Deferred", |
---|
8 | "dojo/_base/array", |
---|
9 | "dojo/_base/declare", |
---|
10 | "dojo/_base/event", |
---|
11 | "dojo/_base/lang", |
---|
12 | "dojo/when", |
---|
13 | "require", |
---|
14 | "dojo/text!./templates/survey.html" |
---|
15 | ], function(Router, surveys, QuestionListView, TabbedQuestionBrowser, _ObjectPage, Deferred, array, declare, event, lang, when, require, template) { |
---|
16 | return declare([_ObjectPage],{ |
---|
17 | contextRequire: require, |
---|
18 | templateString: template, |
---|
19 | classStore: surveys, |
---|
20 | questionList: null, |
---|
21 | startup: function() { |
---|
22 | if ( this._started ) { return; } |
---|
23 | this.inherited(arguments); |
---|
24 | this._setupQuestionBrowser(); |
---|
25 | this._setupListView(); |
---|
26 | this.own(this.questionList.on('change',lang.hitch(this,'_handleQuestionsChange'))); |
---|
27 | this.own(this.propertiesForm.on('change',lang.hitch(this,'_handlePropertiesChange'))); |
---|
28 | this._load(); |
---|
29 | }, |
---|
30 | _setupQuestionBrowser: function() { |
---|
31 | this.questionBrowser = new TabbedQuestionBrowser({ |
---|
32 | region: 'center', |
---|
33 | 'class': 'blue', |
---|
34 | include: 'published', |
---|
35 | getSelectedItemActions: lang.hitch(this,function() { |
---|
36 | return { |
---|
37 | "Include": { |
---|
38 | callback: lang.hitch(this,this._includeQuestion), |
---|
39 | icon: "Accept", |
---|
40 | description: "Include in survey" |
---|
41 | } |
---|
42 | }; |
---|
43 | }), |
---|
44 | getItemActions: lang.hitch(this,function(item) { |
---|
45 | return { |
---|
46 | "Info": { |
---|
47 | callback: lang.hitch(this,function(item){ |
---|
48 | if ( item.description ) { alert(item.description); } |
---|
49 | }), |
---|
50 | icon: "Inspect", |
---|
51 | description: "Show item description" |
---|
52 | } |
---|
53 | }; |
---|
54 | }) |
---|
55 | },this.questionBrowser); |
---|
56 | this.questionBrowser.startup(); |
---|
57 | }, |
---|
58 | _setupListView: function() { |
---|
59 | this.questionList = new QuestionListView({ |
---|
60 | region: 'center', |
---|
61 | name: 'questions' |
---|
62 | },this.surveyListViewNode); |
---|
63 | this.questionList.startup(); |
---|
64 | }, |
---|
65 | _refresh: function(initial) { |
---|
66 | if ( initial === true ) { |
---|
67 | this.propertiesForm.set('value',{survey:this.object},null); |
---|
68 | this.questionList.set('value',this.object.questions,null); |
---|
69 | } |
---|
70 | this.titleNode.innerHTML = this.object.title || "(set title in properties)"; |
---|
71 | if ( this.object.publicationDate ) { |
---|
72 | this.propertiesForm.set('readOnly',true); |
---|
73 | this.questionList.set('readOnly',true); |
---|
74 | } |
---|
75 | }, |
---|
76 | _includeQuestion: function(question) { |
---|
77 | this.questionList.appendItem(question); |
---|
78 | }, |
---|
79 | _handleQuestionsChange: function() { |
---|
80 | this._updateObject(); |
---|
81 | this.markDirty(); |
---|
82 | this._refresh(); |
---|
83 | }, |
---|
84 | _handlePropertiesChange: function() { |
---|
85 | this._updateObject(); |
---|
86 | this.markDirty(); |
---|
87 | this._refresh(); |
---|
88 | this.layout(); |
---|
89 | }, |
---|
90 | _updateObject: function() { |
---|
91 | this._isValid = this.propertiesForm.validate(); |
---|
92 | lang.mixin(this.object, this.propertiesForm.get('value').survey); |
---|
93 | this.object.questions = this.questionList.get('value'); |
---|
94 | return this._isValid; |
---|
95 | }, |
---|
96 | _onSave: function(evt) { |
---|
97 | this._save(); |
---|
98 | if ( evt ) { event.stop(evt); } |
---|
99 | return false; |
---|
100 | }, |
---|
101 | _onSaveAndClose: function(evt) { |
---|
102 | this._save() |
---|
103 | .then(function() { |
---|
104 | Router.go(surveys.getCollectionPath()); |
---|
105 | }); |
---|
106 | event.stop(evt); |
---|
107 | return false; |
---|
108 | }, |
---|
109 | _onDiscardAndClose: function(evt) { |
---|
110 | this.markClean(); |
---|
111 | Router.go(surveys.getCollectionPath()); |
---|
112 | }, |
---|
113 | _onShowPreview: function() { |
---|
114 | Router.go(surveys.getPreviewPath(this.object),{ |
---|
115 | preview: true |
---|
116 | }); |
---|
117 | }, |
---|
118 | _save: function() { |
---|
119 | if ( this._updateObject ) { |
---|
120 | return this.inherited(arguments); |
---|
121 | } else { |
---|
122 | return new Deferred().reject({error:"Please correct invalid values."}); |
---|
123 | } |
---|
124 | }, |
---|
125 | markDirty: function() { |
---|
126 | this.saveBtn.set('disabled',!this._isValid); |
---|
127 | this.saveAndCloseBtn.set('disabled',!this._isValid); |
---|
128 | this.discardBtn.set('label','Discard & Close'); |
---|
129 | this.inherited(arguments); |
---|
130 | }, |
---|
131 | markClean: function() { |
---|
132 | this.saveBtn.set('disabled',true); |
---|
133 | this.saveAndCloseBtn.set('disabled',true); |
---|
134 | this.discardBtn.set('label','Close'); |
---|
135 | this.inherited(arguments); |
---|
136 | }, |
---|
137 | _ignore: function(evt) { |
---|
138 | event.stop(evt); |
---|
139 | return false; |
---|
140 | } |
---|
141 | }); |
---|
142 | }); |
---|
143 | |
---|