source: Dev/trunk/src/client/qed-client/pages/surveys.js @ 502

Last change on this file since 502 was 497, checked in by hendrikvanantwerpen, 11 years ago
  • Better names for db backup and sync scripts.
  • Added SurveyRun? deletion in UI and prevent on server when a run has replies.
File size: 6.9 KB
Line 
1define([
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.refresh();
19        },
20        _onNewSurvey: function(){
21            Router.go(surveys.getObjectPath('new'));
22        },
23        _onPublishSurvey:function(survey){
24            var self = this;
25            survey.publicationDate = new Date();
26            surveys.save(survey)
27            .then(function(){
28                self.refreshDrafts();
29                self.refreshPublished();
30            },lang.hitch(this,function(err){
31                this.notify(err.error,'error');
32            }));
33        },
34        _onDeleteSurvey:function(survey){
35            var self = this;
36            surveys.remove(survey)
37            .then(function(){
38                self.refreshDrafts();
39            },lang.hitch(this,function(err){
40                this.notify(err.error,'error');
41            }));
42        },
43        _onEditSurvey:function(survey){
44            Router.go(surveys.getObjectPath(survey));
45        },
46        _onPreviewSurvey:function(survey){
47            Router.go(surveys.getPreviewPath(survey));
48        },
49        _onRunSurvey:function(survey){
50            var surveyRun = surveyRuns.create();
51            surveyRun.survey = survey;
52            surveyRuns.save(surveyRun)
53            .then(lang.hitch(this,function(surveyRun){
54                this._onRunDetails(surveyRun);
55            }),lang.hitch(this,function(err){
56                this.notify(err.error,'error');
57            }));
58        },
59        _onRunDetails: function(surveyRun) {
60            Router.go(surveyRuns.getObjectPath(surveyRun));
61        },
62        _onRunDelete: function(surveyRun) {
63            surveyRuns.remove(surveyRun)
64            .then(lang.hitch(this,function(){
65                this.notify("SurveyRun successfully deleted.");
66                this.refreshRuns();
67            }),lang.hitch(this,function(err){
68                this.notify(err.error,'error');
69            }));
70        },
71        refresh: function() {
72            this.refreshDrafts();
73            this.refreshPublished();
74            this.refreshRuns();
75        },
76        refreshDrafts: function() {
77            this.draftsContainer.set('content','');
78            when(surveys.query({drafts:true}), lang.hitch(this,function(surveys) {
79                this.draftsTab.set('title','Drafts ('+surveys.length+')');
80                array.forEach(surveys,function(survey){
81                    var w = new LineWithActionsWidget({
82                        title: survey.title || '(unnamed)',
83                        actions: [{
84                            callback: lang.hitch(this,'_onPublishSurvey',survey),
85                            properties: {
86                                label: 'Publish',
87                                tooltip: 'Publish survey',
88                                icon: 'Publish'
89                            }
90                        },{
91                            callback: lang.hitch(this,'_onPreviewSurvey',survey),
92                            properties: {
93                                label: 'Preview',
94                                tooltip: 'Preview survey',
95                                icon: 'Preview'
96                            }
97                        },{
98                            callback: lang.hitch(this,'_onDeleteSurvey',survey),
99                            properties: {
100                                label: 'Delete',
101                                tooltip: 'Delete survey',
102                                icon: 'Delete'
103                            }
104                        },{
105                            callback: lang.hitch(this,'_onEditSurvey',survey),
106                            properties: {
107                                label: 'Edit',
108                                tooltip: 'Edit survey',
109                                icon: 'Edit'
110                            }
111                        }]
112                    });
113                    this.draftsContainer.addChild(w);
114                },this);
115            }));
116        },
117        refreshPublished: function() {
118            this.publishedContainer.set('content','');
119            when(surveys.query({published:true}), lang.hitch(this, function(surveys) {
120                this.publishedTab.set('title','Published ('+surveys.length+')');
121                array.forEach(surveys,function(survey){
122                    var w = new LineWithActionsWidget({
123                        title: survey.title || "",
124                        actions:[{
125                            callback: lang.hitch(this,'_onPreviewSurvey',survey),
126                            properties: {
127                                label: 'Preview',
128                                tooltip: 'Preview survey',
129                                icon: 'Preview'
130                            }
131                        },{
132                            callback: lang.hitch(this,'_onRunSurvey',survey),
133                            properties: {
134                                label: 'Run',
135                                tooltip: 'Run survey',
136                                icon: 'Run'
137                            }
138                        }]
139                    });
140                    this.publishedContainer.addChild(w);
141                },this);
142            }));
143        },
144        refreshRuns: function() {
145            this.runsContainer.set('content','');
146            when(surveyRuns.query(), lang.hitch(this,function(surveyRuns){
147                this.runsTab.set('title','Runs ('+surveyRuns.length+')');
148                array.forEach(surveyRuns,function(surveyRun){
149                    var w = new LineWithActionsWidget({
150                        title: surveyRun.title || "",
151                        actions:[{
152                            callback: lang.hitch(this,'_onRunDetails',surveyRun),
153                            properties: {
154                                label: 'Details',
155                                tooltip: 'Show details for this run',
156                                icon: 'Edit'
157                            }
158                        },{
159                            callback: lang.hitch(this,'_onRunDelete',surveyRun),
160                            properties: {
161                                label: 'Delete',
162                                tooltip: 'Delete this run',
163                                icon: 'Delete'
164                            }
165                        }]
166                    });
167                    this.runsContainer.addChild(w);
168                },this);
169            }));
170        }
171    });
172});
173
Note: See TracBrowser for help on using the repository browser.