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