source: Dev/branches/rest-dojo-ui/client/rft/pages/surveys.js @ 417

Last change on this file since 417 was 417, checked in by hendrikvanantwerpen, 12 years ago

Put all model dependent code in one place. More separation of general and domain code.

File size: 6.7 KB
Line 
1define([
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    '../widgets/LineWithActionsWidget',
12    'dojo/text!./templates/surveys.html'
13],function(array,declare,lang,when,Rx,store,Content,Page,Router,LineWithActionsWidget,template){
14    return declare([Page],{
15        templateString: template,
16        startup: function() {
17            if ( this._started ) { return; }
18            this.inherited(arguments);
19            this.refresh();
20        },
21        _onNewSurvey: function(){
22            Router.go('/survey/new');
23        },
24        _onPublishSurvey:function(survey){
25            var self = this;
26            survey.publicationDate = store.timestamp();
27            store.put(survey).then(function(){
28                self.refreshDrafts();
29                self.refreshPublished();
30            },function(err){
31                Content.notify(err,'error');
32            });
33        },
34        _onDeleteSurvey:function(survey){
35            var self = this;
36            store.remove(store.getIdentity(survey),store.getRevision(survey))
37            .then(function(){
38                self.refreshDrafts();
39            },function(err){
40                Content.notify(err,'error');
41            });
42        },
43        _onEditSurvey:function(survey){
44            Router.go('/survey/'+store.getIdentity(survey));
45        },
46        _onPreviewSurvey:function(survey){
47            Router.go('/viewSurvey/'+store.getIdentity(survey),{preview:true});
48        },
49        _onRunSurvey:function(survey){
50            this.surveyRun = {
51                type: 'SurveyRun',
52                surveyId: store.getIdentity(survey),
53                publicationDate: store.timestamp()
54            };
55            this.surveyRunDialog.set('value',this.surveyRun);
56            this.surveyRunDialog.show();
57        },
58        _onSurveyRunOk: function() {
59            var surveyRun = lang.mixin(lang.clone(this.surveyRun),this.surveyRunDialog.get('value'));
60            store.put(surveyRun)
61            .then(lang.hitch(this,function(){
62                this.surveyRunDialog.hide();
63                this.refreshRuns();
64            }),function(err){
65                Content.notify(err);
66            });
67        },
68        _onSurveyRunCancel: function() {
69            this.surveyRunDialog.hide();
70        },
71        refresh: function() {
72            this.refreshDrafts();
73            this.refreshPublished();
74            this.refreshRuns();
75        },
76        refreshDrafts: function() {
77            this.draftsContainer.set('content','');
78            when(store.query("_design/surveys/_view/drafts")
79                    ,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(store.query("_design/surveys/_view/published")
121                    ,lang.hitch(this,function(surveys){
122                this.publishedTab.set('title','Published ('+surveys.length+')');
123                array.forEach(surveys,function(survey){
124                    var w = new LineWithActionsWidget({
125                        title: survey.title,
126                        actions:[{
127                            callback: lang.hitch(this,'_onPreviewSurvey',survey),
128                            properties: {
129                                label: 'Preview',
130                                tooltip: 'Preview survey',
131                                icon: 'Preview'
132                            }
133                        },{
134                            callback: lang.hitch(this,'_onRunSurvey',survey),
135                            properties: {
136                                label: 'Run',
137                                tooltip: 'Run survey',
138                                icon: 'Run'
139                            }
140                        }]
141                    });
142                    this.publishedContainer.addChild(w);
143                },this);
144            }));
145        },
146        refreshRuns: function() {
147            this.runsContainer.set('content','');
148            when(store.query("_design/default/_view/by_type",{key:'SurveyRun'})
149                    ,lang.hitch(this,function(surveyRuns){
150                this.runsTab.set('title','Runs ('+surveyRuns.length+')');
151                array.forEach(surveyRuns,function(surveyRun){
152                    var w = new LineWithActionsWidget({
153                        title: surveyRun.title+" (from "+surveyRun.startDate+" to "+surveyRun.endDate+")",
154                        actions:[{
155                            callback: lang.hitch(this,'_onCloseRun',surveyRun),
156                            properties: {
157                                label: 'Close',
158                                tooltip: 'Close survey',
159                                icon: 'Close'
160                            }
161                        }]
162                    });
163                    this.runsContainer.addChild(w);
164                },this);
165            }));
166        }
167    });
168});
169
Note: See TracBrowser for help on using the repository browser.