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

Last change on this file since 493 was 490, checked in by hendrikvanantwerpen, 11 years ago
  • Mark content as dirty to prevent moving away from unsaved data.
  • Better change propagation from lists and our own widgets.
  • Generate notifications for errors and show correct message.
  • Moved all path/url generation to the class stores, not everywhere we use it.
  • Give user always a choice between Save and Save & Close.
  • Better refresh behaviour on form changes and saves.
  • Don't generate duplicate code error when existing object is the one you're storing.
File size: 6.2 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        refresh: function() {
63            this.refreshDrafts();
64            this.refreshPublished();
65            this.refreshRuns();
66        },
67        refreshDrafts: function() {
68            this.draftsContainer.set('content','');
69            when(surveys.query({drafts:true}), lang.hitch(this,function(surveys) {
70                this.draftsTab.set('title','Drafts ('+surveys.length+')');
71                array.forEach(surveys,function(survey){
72                    var w = new LineWithActionsWidget({
73                        title: survey.title || '(unnamed)',
74                        actions: [{
75                            callback: lang.hitch(this,'_onPublishSurvey',survey),
76                            properties: {
77                                label: 'Publish',
78                                tooltip: 'Publish survey',
79                                icon: 'Publish'
80                            }
81                        },{
82                            callback: lang.hitch(this,'_onPreviewSurvey',survey),
83                            properties: {
84                                label: 'Preview',
85                                tooltip: 'Preview survey',
86                                icon: 'Preview'
87                            }
88                        },{
89                            callback: lang.hitch(this,'_onDeleteSurvey',survey),
90                            properties: {
91                                label: 'Delete',
92                                tooltip: 'Delete survey',
93                                icon: 'Delete'
94                            }
95                        },{
96                            callback: lang.hitch(this,'_onEditSurvey',survey),
97                            properties: {
98                                label: 'Edit',
99                                tooltip: 'Edit survey',
100                                icon: 'Edit'
101                            }
102                        }]
103                    });
104                    this.draftsContainer.addChild(w);
105                },this);
106            }));
107        },
108        refreshPublished: function() {
109            this.publishedContainer.set('content','');
110            when(surveys.query({published:true}), lang.hitch(this, function(surveys) {
111                this.publishedTab.set('title','Published ('+surveys.length+')');
112                array.forEach(surveys,function(survey){
113                    var w = new LineWithActionsWidget({
114                        title: survey.title || "",
115                        actions:[{
116                            callback: lang.hitch(this,'_onPreviewSurvey',survey),
117                            properties: {
118                                label: 'Preview',
119                                tooltip: 'Preview survey',
120                                icon: 'Preview'
121                            }
122                        },{
123                            callback: lang.hitch(this,'_onRunSurvey',survey),
124                            properties: {
125                                label: 'Run',
126                                tooltip: 'Run survey',
127                                icon: 'Run'
128                            }
129                        }]
130                    });
131                    this.publishedContainer.addChild(w);
132                },this);
133            }));
134        },
135        refreshRuns: function() {
136            this.runsContainer.set('content','');
137            when(surveyRuns.query(), lang.hitch(this,function(surveyRuns){
138                this.runsTab.set('title','Runs ('+surveyRuns.length+')');
139                array.forEach(surveyRuns,function(surveyRun){
140                    var w = new LineWithActionsWidget({
141                        title: surveyRun.title || "",
142                        actions:[{
143                            callback: lang.hitch(this,'_onRunDetails',surveyRun),
144                            properties: {
145                                label: 'Details',
146                                tooltip: 'Show details for this run',
147                                icon: 'Details'
148                            }
149                        }]
150                    });
151                    this.runsContainer.addChild(w);
152                },this);
153            }));
154        }
155    });
156});
157
Note: See TracBrowser for help on using the repository browser.