source: Dev/branches/rest-dojo-ui/client/qed/pages/survey.js @ 418

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

Reflect new naming.

File size: 4.1 KB
Line 
1define([
2    'dojo/_base/array',
3    'dojo/_base/declare',
4    'dojo/_base/Deferred',
5    'dojo/_base/event',
6    'dojo/_base/lang',
7    '../app/Router',
8    '../store',
9    '../app/Page',
10    '../model/widgets/QuestionListView',
11    '../model/widgets/TabbedQuestionBrowser',
12    'dojo/text!./templates/survey.html'
13],function(array,declare,Deferred,event,lang,Router,store,Page,
14         QuestionListView,TabbedQuestionBrowser,template){
15    return declare([Page],{
16        templateString: template,
17        survey: null,
18        questionList: null,
19        _dataMap: null,
20        constructor: function(){
21            this._dataMap = {};
22        },
23        startup: function() {
24            if ( this._started ) { return; }
25            this.inherited(arguments);
26            if ( this.surveyId ) {
27                this._setupQuestionBrowser();
28                this._setupListView();
29                this._loadSurvey();
30            } else {
31                throw "No valid uid or survey passed!";
32            }
33        },
34        _setupQuestionBrowser: function() {
35            this.questionBrowser = new TabbedQuestionBrowser({
36                region: 'center',
37                'class': 'blue',
38                include: 'published',
39                selectedActions: {
40                    "Include": {
41                        callback: lang.hitch(this,this._includeQuestion),
42                        icon: "Accept",
43                        description: "Include in survey"
44                    }
45                },
46                itemActions: {
47                    "Info": {
48                        callback: function(item){ item.description && alert(item.description); },
49                        icon: "Inspect",
50                        description: "Show item description"
51                    }
52                }
53            },this.questionBrowser);
54            this.questionBrowser.startup();
55        },
56        _setupListView: function() {
57            this.questionList = new QuestionListView({
58                region: 'center'
59            },this.surveyListViewNode);
60            this.questionList.startup();
61        },
62        _loadSurvey: function() {
63            if ( this.surveyId === "new" ) {
64                this.survey = {
65                    type: 'Survey'
66                };
67                this.refresh();
68            } else {
69                Deferred.when(store.get(this.surveyId))
70                .then(lang.hitch(this,function(survey){
71                    this.survey = survey;
72                    store.query(null,{keys:this.survey.questions || [], include_docs: true})
73                    .forEach(lang.hitch(this.questionList,'appendItem'));
74                    this.refresh();
75                }));
76            }
77        },
78        _includeQuestion: function(question) {
79            this.questionList.insertItem(question);
80        },
81        refresh: function() {
82            this.titleNode.innerHTML = this.survey.title || "(set title in properties)";
83            this.propertiesDialog.set('value',this.survey);
84        },
85        _onShowProperties: function(evt) {
86            this.propertiesDialog.show();
87        },
88        _onPropertiesOk: function(evt) {
89            this.propertiesDialog.hide();
90            lang.mixin(this.survey, this.propertiesDialog.get('value'));
91            this.refresh();
92            event.stop(evt);
93            return false;
94        },
95        _onPropertiesCancel: function(evt) {
96            this.propertiesDialog.hide();
97            this.propertiesDialog.reset('value',this.survey);
98            event.stop(evt);
99            return false;
100        },
101        _onSave: function(evt) {
102            this.survey.questions = array.map(this.questionList.getItems(),function(item){
103                return store.getIdentity(item);
104            });
105            store.put(this.survey)
106            .then(function() {
107                Router.go('/surveys');
108            });
109            event.stop(evt);
110            return false;
111        },
112        _onDiscard: function(evt) {
113            Router.go('/surveys');
114        },
115        _onShowPreview: function() {
116            Router.go('/viewSurvey/'+store.getIdentity(this.survey),{
117                preview: true
118            });
119        }
120    });
121});
122
Note: See TracBrowser for help on using the repository browser.