1 | define([ |
---|
2 | "../app/Page", |
---|
3 | "../app/Router", |
---|
4 | "../model/classes/surveys", |
---|
5 | "../model/widgets/QuestionListView", |
---|
6 | "../model/widgets/TabbedQuestionBrowser", |
---|
7 | "dojo/_base/array", |
---|
8 | "dojo/_base/declare", |
---|
9 | "dojo/_base/event", |
---|
10 | "dojo/_base/lang", |
---|
11 | "dojo/when", |
---|
12 | "require", |
---|
13 | "dojo/text!./templates/survey.html" |
---|
14 | ], function(Page, Router, surveys, QuestionListView, TabbedQuestionBrowser, array, declare, event, lang, when, require, template) { |
---|
15 | return declare([Page],{ |
---|
16 | contextRequire: require, |
---|
17 | templateString: template, |
---|
18 | survey: null, |
---|
19 | questionList: null, |
---|
20 | startup: function() { |
---|
21 | if ( this._started ) { return; } |
---|
22 | this.inherited(arguments); |
---|
23 | if ( this.surveyId ) { |
---|
24 | this._setupQuestionBrowser(); |
---|
25 | this._setupListView(); |
---|
26 | this._loadSurvey(); |
---|
27 | } else { |
---|
28 | throw "No valid uid or survey passed!"; |
---|
29 | } |
---|
30 | }, |
---|
31 | _setupQuestionBrowser: function() { |
---|
32 | this.questionBrowser = new TabbedQuestionBrowser({ |
---|
33 | region: 'center', |
---|
34 | 'class': 'blue', |
---|
35 | include: 'published', |
---|
36 | selectedActions: { |
---|
37 | "Include": { |
---|
38 | callback: lang.hitch(this,this._includeQuestion), |
---|
39 | icon: "Accept", |
---|
40 | description: "Include in survey" |
---|
41 | } |
---|
42 | }, |
---|
43 | itemActions: { |
---|
44 | "Info": { |
---|
45 | callback: function(item){ |
---|
46 | if ( item.description ) { alert(item.description); } |
---|
47 | }, |
---|
48 | icon: "Inspect", |
---|
49 | description: "Show item description" |
---|
50 | } |
---|
51 | } |
---|
52 | },this.questionBrowser); |
---|
53 | this.questionBrowser.startup(); |
---|
54 | }, |
---|
55 | _setupListView: function() { |
---|
56 | this.questionList = new QuestionListView({ |
---|
57 | region: 'center', |
---|
58 | name: 'questions' |
---|
59 | },this.surveyListViewNode); |
---|
60 | this.questionList.startup(); |
---|
61 | }, |
---|
62 | _loadSurvey: function() { |
---|
63 | if ( this.surveyId === "new" ) { |
---|
64 | this.survey = surveys.create(); |
---|
65 | this.refresh(); |
---|
66 | } else { |
---|
67 | when(surveys.load(this.surveyId)) |
---|
68 | .then(lang.hitch(this,function(survey){ |
---|
69 | this.survey = survey; |
---|
70 | this.questionList.set('value', |
---|
71 | this.survey.questions); |
---|
72 | this.refresh(); |
---|
73 | })); |
---|
74 | } |
---|
75 | }, |
---|
76 | _includeQuestion: function(question) { |
---|
77 | this.questionList.appendItem(question); |
---|
78 | }, |
---|
79 | refresh: function() { |
---|
80 | this.titleNode.innerHTML = this.survey.title || "(set title in properties)"; |
---|
81 | this.propertiesDialog.set('value',this.survey); |
---|
82 | }, |
---|
83 | _onShowProperties: function(evt) { |
---|
84 | this.propertiesDialog.show(); |
---|
85 | }, |
---|
86 | _onPropertiesOk: function(evt) { |
---|
87 | this.propertiesDialog.hide(); |
---|
88 | lang.mixin(this.survey, this.propertiesDialog.get('value')); |
---|
89 | this.refresh(); |
---|
90 | event.stop(evt); |
---|
91 | return false; |
---|
92 | }, |
---|
93 | _onPropertiesCancel: function(evt) { |
---|
94 | this.propertiesDialog.hide(); |
---|
95 | this.propertiesDialog.reset('value',this.survey); |
---|
96 | event.stop(evt); |
---|
97 | return false; |
---|
98 | }, |
---|
99 | _onSave: function(evt) { |
---|
100 | this.survey.questions = this.questionList.get('value'); |
---|
101 | surveys.save(this.survey) |
---|
102 | .then(function() { |
---|
103 | Router.go('/surveys'); |
---|
104 | }); |
---|
105 | event.stop(evt); |
---|
106 | return false; |
---|
107 | }, |
---|
108 | _onDiscard: function(evt) { |
---|
109 | Router.go('/surveys'); |
---|
110 | }, |
---|
111 | _onShowPreview: function() { |
---|
112 | Router.go('/previewSurvey/'+this.survey._id,{ |
---|
113 | preview: true |
---|
114 | }); |
---|
115 | } |
---|
116 | }); |
---|
117 | }); |
---|
118 | |
---|