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

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

Support data validation and increase dendency separation.

Added json-schema validation before documents are saved. Acts both as
a safe-guard and as a reference of the data model.

Added directory for server related material. For now it contains scripts
to configure CouchDB and check validity of documents against the schema.

Started separating out parts that depend on the data model from parts
that do not.

File size: 6.1 KB
Line 
1define([
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            when( store.add({type:'Survey'}) )
22            .then(function(survey) {
23                Router.go('/survey/'+store.getIdentity(survey));
24            },function(err){
25                Content.notify(err,'error');
26            });
27        },
28        _onPublishSurvey:function(survey){
29            var self = this;
30            survey.publicationDate = store.timestamp();
31            store.put(survey).then(function(){
32                self.refreshDrafts();
33                self.refreshPublished();
34            },function(err){
35                Content.notify(err,'error');
36            });
37        },
38        _onDeleteSurvey:function(survey){
39            var self = this;
40            store.remove(store.getIdentity(survey),store.getRevision(survey))
41            .then(function(){
42                self.refreshDrafts();
43            },function(err){
44                Content.notify(err,'error');
45            });
46        },
47        _onEditSurvey:function(survey){
48            Router.go('/survey/'+store.getIdentity(survey));
49        },
50        _onPreviewSurvey:function(survey){
51            Router.go('/viewSurvey/'+store.getIdentity(survey),{preview:true});
52        },
53        _onRunSurvey:function(survey){
54            this.surveyRunDialog.show();
55        },
56        refresh: function() {
57            this.refreshDrafts();
58            this.refreshPublished();
59            this.refreshRuns();
60        },
61        refreshDrafts: function() {
62            this.draftsContainer.set('content','');
63            when(store.query("_design/surveys/_view/drafts")
64                    ,lang.hitch(this,function(surveys){
65                this.draftsTab.set('title','Drafts ('+surveys.length+')');
66                array.forEach(surveys,function(survey){
67                    var w = new LineWithActionsWidget({
68                        title: survey.title || '(unnamed)',
69                        actions: [{
70                            callback: lang.hitch(this,'_onPublishSurvey',survey),
71                            properties: {
72                                label: 'Publish',
73                                tooltip: 'Publish survey',
74                                icon: 'Publish'
75                            }
76                        },{
77                            callback: lang.hitch(this,'_onPreviewSurvey',survey),
78                            properties: {
79                                label: 'Preview',
80                                tooltip: 'Preview survey',
81                                icon: 'Preview'
82                            }
83                        },{
84                            callback: lang.hitch(this,'_onDeleteSurvey',survey),
85                            properties: {
86                                label: 'Delete',
87                                tooltip: 'Delete survey',
88                                icon: 'Delete'
89                            }
90                        },{
91                            callback: lang.hitch(this,'_onEditSurvey',survey),
92                            properties: {
93                                label: 'Edit',
94                                tooltip: 'Edit survey',
95                                icon: 'Edit'
96                            }
97                        }]
98                    });
99                    this.draftsContainer.addChild(w);
100                },this);
101            }));
102        },
103        refreshPublished: function() {
104            this.publishedContainer.set('content','');
105            when(store.query("_design/surveys/_view/published")
106                    ,lang.hitch(this,function(surveys){
107                this.publishedTab.set('title','Published ('+surveys.length+')');
108                array.forEach(surveys,function(survey){
109                    var w = new LineWithActionsWidget({
110                        title: survey.title,
111                        actions:[{
112                            callback: lang.hitch(this,'_onPreviewSurvey',survey),
113                            properties: {
114                                label: 'Preview',
115                                tooltip: 'Preview survey',
116                                icon: 'Preview'
117                            }
118                        },{
119                            callback: lang.hitch(this,'_onRunSurvey',survey),
120                            properties: {
121                                label: 'Run',
122                                tooltip: 'Run survey',
123                                icon: 'Run'
124                            }
125                        }]
126                    });
127                    this.publishedContainer.addChild(w);
128                },this);
129            }));
130        },
131        refreshRuns: function() {
132            this.runsContainer.set('content','');
133            when(store.query("_design/default/_view/by_type",{key:'SurveyRun'})
134                    ,lang.hitch(this,function(surveyRuns){
135                this.runsTab.set('title','Runs ('+surveyRuns.length+')');
136                array.forEach(surveyRuns,function(surveyRun){
137                    var w = new LineWithActionsWidget({
138                        title: survey.title,
139                        actions:[{
140                            callback: lang.hitch(this,'_onCloseRun',surveyRun),
141                            properties: {
142                                label: 'Close',
143                                tooltip: 'Close survey',
144                                icon: 'Close'
145                            }
146                        }]
147                    });
148                    this.runsContainer.addChild(w);
149                },this);
150            }));
151        }
152    });
153});
154
Note: See TracBrowser for help on using the repository browser.