1 | define([ |
---|
2 | "../app/Page", |
---|
3 | "../app/Router", |
---|
4 | "../model/classes/surveyRuns", |
---|
5 | "../model/classes/surveys", |
---|
6 | "../widgets/LineWithActionsWidget", |
---|
7 | "dojo/_base/array", |
---|
8 | "dojo/_base/declare", |
---|
9 | "dojo/_base/lang", |
---|
10 | "dojo/when", |
---|
11 | "dojo/text!./templates/surveys.html" |
---|
12 | ], function(Page, Router, surveyRuns, surveys, LineWithActionsWidget, array, declare, lang, when, template) { |
---|
13 | return declare([Page],{ |
---|
14 | templateString: template, |
---|
15 | startup: function() { |
---|
16 | if ( this._started ) { return; } |
---|
17 | this.inherited(arguments); |
---|
18 | this.draftsTab.set('title','Drafts (<span class="qedLoading"></span>)'); |
---|
19 | this.publishedTab.set('title','Published (<span class="qedLoading"></span>)'); |
---|
20 | this.refresh(); |
---|
21 | }, |
---|
22 | _onNewSurvey: function(){ |
---|
23 | Router.go(surveys.getObjectPath('new')); |
---|
24 | }, |
---|
25 | _onPublishSurvey:function(survey){ |
---|
26 | if ( !confirm("After publication the survey cannot be modified anymore, are you sure?") ) { |
---|
27 | return; |
---|
28 | } |
---|
29 | survey.publicationDate = new Date(); |
---|
30 | surveys.save(survey) |
---|
31 | .then(lang.hitch(this,function(){ |
---|
32 | this.refreshDrafts(); |
---|
33 | this.refreshPublished(); |
---|
34 | }),lang.hitch(this,function(err){ |
---|
35 | this.notify(err.error,'error'); |
---|
36 | })); |
---|
37 | }, |
---|
38 | _onDeleteSurvey:function(survey){ |
---|
39 | if ( !confirm("Are you sure you want to delete this draft survey?") ) { |
---|
40 | return; |
---|
41 | } |
---|
42 | surveys.remove(survey) |
---|
43 | .then(lang.hitch(this,function(){ |
---|
44 | this.refreshDrafts(); |
---|
45 | }),lang.hitch(this,function(err){ |
---|
46 | this.notify(err.error,'error'); |
---|
47 | })); |
---|
48 | }, |
---|
49 | _onEditSurvey:function(survey){ |
---|
50 | Router.go(surveys.getObjectPath(survey)); |
---|
51 | }, |
---|
52 | _onPreviewSurvey:function(survey){ |
---|
53 | Router.go(surveys.getPreviewPath(survey)); |
---|
54 | }, |
---|
55 | _onRunSurvey:function(survey){ |
---|
56 | var surveyRun = surveyRuns.create(); |
---|
57 | surveyRun.title = 'Run of "'+survey.title+'" of '+ |
---|
58 | (new Date().toString()); |
---|
59 | surveyRun.survey = survey; |
---|
60 | surveyRuns.save(surveyRun) |
---|
61 | .then(lang.hitch(this,function(surveyRun){ |
---|
62 | Router.go(surveyRuns.getObjectPath( |
---|
63 | surveyRuns.getId(surveyRun))); |
---|
64 | }),lang.hitch(this,function(err){ |
---|
65 | this.notify(err.error,'error'); |
---|
66 | })); |
---|
67 | }, |
---|
68 | refresh: function() { |
---|
69 | this.refreshDrafts(); |
---|
70 | this.refreshPublished(); |
---|
71 | }, |
---|
72 | refreshDrafts: function() { |
---|
73 | this.draftsContainer.set('content','Loading draft surveys <span class="qedLoading"></span>'); |
---|
74 | when(surveys.query({drafts:true}), lang.hitch(this,function(surveys) { |
---|
75 | this.draftsTab.set('title','Drafts ('+surveys.length+')'); |
---|
76 | this.draftsContainer.set('content',''); |
---|
77 | array.forEach(surveys,function(survey){ |
---|
78 | var w = new LineWithActionsWidget({ |
---|
79 | title: survey.title || '(unnamed)', |
---|
80 | actions: [{ |
---|
81 | callback: lang.hitch(this,'_onPublishSurvey',survey), |
---|
82 | properties: { |
---|
83 | label: 'Publish', |
---|
84 | tooltip: 'Publish survey', |
---|
85 | icon: 'Publish' |
---|
86 | } |
---|
87 | },{ |
---|
88 | callback: lang.hitch(this,'_onPreviewSurvey',survey), |
---|
89 | properties: { |
---|
90 | label: 'Preview', |
---|
91 | tooltip: 'Preview survey', |
---|
92 | icon: 'Preview' |
---|
93 | } |
---|
94 | },{ |
---|
95 | callback: lang.hitch(this,'_onDeleteSurvey',survey), |
---|
96 | properties: { |
---|
97 | label: 'Delete', |
---|
98 | tooltip: 'Delete survey', |
---|
99 | icon: 'Delete' |
---|
100 | } |
---|
101 | },{ |
---|
102 | callback: lang.hitch(this,'_onEditSurvey',survey), |
---|
103 | properties: { |
---|
104 | label: 'Edit', |
---|
105 | tooltip: 'Edit survey', |
---|
106 | icon: 'Edit' |
---|
107 | } |
---|
108 | }] |
---|
109 | }); |
---|
110 | this.draftsContainer.addChild(w); |
---|
111 | },this); |
---|
112 | })); |
---|
113 | }, |
---|
114 | refreshPublished: function() { |
---|
115 | this.publishedContainer.set('content','Loading published surveys <span class="qedLoading"></span>'); |
---|
116 | when(surveys.query({published:true}), lang.hitch(this, function(surveys) { |
---|
117 | this.publishedContainer.set('content',''); |
---|
118 | this.publishedTab.set('title','Published ('+surveys.length+')'); |
---|
119 | array.forEach(surveys,function(survey){ |
---|
120 | var w = new LineWithActionsWidget({ |
---|
121 | title: survey.title || "", |
---|
122 | actions:[{ |
---|
123 | callback: lang.hitch(this,'_onPreviewSurvey',survey), |
---|
124 | properties: { |
---|
125 | label: 'Preview', |
---|
126 | tooltip: 'Preview survey', |
---|
127 | icon: 'Preview' |
---|
128 | } |
---|
129 | },{ |
---|
130 | callback: lang.hitch(this,'_onRunSurvey',survey), |
---|
131 | properties: { |
---|
132 | label: 'Run', |
---|
133 | tooltip: 'Run survey', |
---|
134 | icon: 'Forward' |
---|
135 | } |
---|
136 | }] |
---|
137 | }); |
---|
138 | this.publishedContainer.addChild(w); |
---|
139 | },this); |
---|
140 | })); |
---|
141 | } |
---|
142 | }); |
---|
143 | }); |
---|
144 | |
---|