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