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