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