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

Last change on this file since 508 was 508, checked in by hendrikvanantwerpen, 11 years ago
  • Server handles the new flat response format correctly.
  • Client widgets and survey rendering creates a flat structure.
  • Fixed logic error in checking if questions in survey are published.
  • Restrict accepted properties in answers and reject empty strings as properties.
File size: 7.0 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.title = 'Run of "' + survey.title + '" of '+(new Date().toString());
52            surveyRun.survey = survey;
53            surveyRuns.save(surveyRun)
54            .then(lang.hitch(this,function(surveyRun){
55                this._onRunDetails(surveyRun);
56            }),lang.hitch(this,function(err){
57                this.notify(err.error,'error');
58            }));
59        },
60        _onRunDetails: function(surveyRun) {
61            Router.go(surveyRuns.getObjectPath(surveyRun));
62        },
63        _onRunDelete: function(surveyRun) {
64            surveyRuns.remove(surveyRun)
65            .then(lang.hitch(this,function(){
66                this.notify("SurveyRun successfully deleted.");
67                this.refreshRuns();
68            }),lang.hitch(this,function(err){
69                this.notify(err.error,'error');
70            }));
71        },
72        refresh: function() {
73            this.refreshDrafts();
74            this.refreshPublished();
75            this.refreshRuns();
76        },
77        refreshDrafts: function() {
78            this.draftsContainer.set('content','');
79            when(surveys.query({drafts:true}), lang.hitch(this,function(surveys) {
80                this.draftsTab.set('title','Drafts ('+surveys.length+')');
81                array.forEach(surveys,function(survey){
82                    var w = new LineWithActionsWidget({
83                        title: survey.title || '(unnamed)',
84                        actions: [{
85                            callback: lang.hitch(this,'_onPublishSurvey',survey),
86                            properties: {
87                                label: 'Publish',
88                                tooltip: 'Publish survey',
89                                icon: 'Publish'
90                            }
91                        },{
92                            callback: lang.hitch(this,'_onPreviewSurvey',survey),
93                            properties: {
94                                label: 'Preview',
95                                tooltip: 'Preview survey',
96                                icon: 'Preview'
97                            }
98                        },{
99                            callback: lang.hitch(this,'_onDeleteSurvey',survey),
100                            properties: {
101                                label: 'Delete',
102                                tooltip: 'Delete survey',
103                                icon: 'Delete'
104                            }
105                        },{
106                            callback: lang.hitch(this,'_onEditSurvey',survey),
107                            properties: {
108                                label: 'Edit',
109                                tooltip: 'Edit survey',
110                                icon: 'Edit'
111                            }
112                        }]
113                    });
114                    this.draftsContainer.addChild(w);
115                },this);
116            }));
117        },
118        refreshPublished: function() {
119            this.publishedContainer.set('content','');
120            when(surveys.query({published:true}), lang.hitch(this, function(surveys) {
121                this.publishedTab.set('title','Published ('+surveys.length+')');
122                array.forEach(surveys,function(survey){
123                    var w = new LineWithActionsWidget({
124                        title: survey.title || "",
125                        actions:[{
126                            callback: lang.hitch(this,'_onPreviewSurvey',survey),
127                            properties: {
128                                label: 'Preview',
129                                tooltip: 'Preview survey',
130                                icon: 'Preview'
131                            }
132                        },{
133                            callback: lang.hitch(this,'_onRunSurvey',survey),
134                            properties: {
135                                label: 'Run',
136                                tooltip: 'Run survey',
137                                icon: 'Run'
138                            }
139                        }]
140                    });
141                    this.publishedContainer.addChild(w);
142                },this);
143            }));
144        },
145        refreshRuns: function() {
146            this.runsContainer.set('content','');
147            when(surveyRuns.query(), lang.hitch(this,function(surveyRuns){
148                this.runsTab.set('title','Runs ('+surveyRuns.length+')');
149                array.forEach(surveyRuns,function(surveyRun){
150                    var w = new LineWithActionsWidget({
151                        title: surveyRun.title || "",
152                        actions:[{
153                            callback: lang.hitch(this,'_onRunDetails',surveyRun),
154                            properties: {
155                                label: 'Details',
156                                tooltip: 'Show details for this run',
157                                icon: 'Edit'
158                            }
159                        },{
160                            callback: lang.hitch(this,'_onRunDelete',surveyRun),
161                            properties: {
162                                label: 'Delete',
163                                tooltip: 'Delete this run',
164                                icon: 'Delete'
165                            }
166                        }]
167                    });
168                    this.runsContainer.addChild(w);
169                },this);
170            }));
171        }
172    });
173});
174
Note: See TracBrowser for help on using the repository browser.