source: Dev/branches/rest-dojo-ui/client/rft/pages/survey.js @ 360

Last change on this file since 360 was 360, checked in by hendrikvanantwerpen, 13 years ago

Removed controller dependency of Selector and SurveyList?.
Selector emits events now. No id's are used, pass objects around.
Fixed selector wipe in/out confusion for first click.
Read questions from db, no mockup data used anymore in survey page.
Added some sample question to db config, because they cannot be created yet.

File size: 3.6 KB
Line 
1define(['dojo/_base/declare',
2    'dojo/_base/lang',
3    'dojo/_base/event',
4    'dojo/_base/Deferred',
5    'rft/ui/Selector',
6    'rft/ui/SurveyListView',
7    'dijit/layout/ContentPane',
8    'rft/store',
9    'rft/ui/_Page',
10    'rft/content',
11    'dojo/store/Cache',
12    'dojo/store/Memory' ],
13    function(declare,lang,event,Deferred,Selector,SurveyListView,ContentPane,store,_Page,content,Cache,Memory){
14        return declare('rft.pages.survey',[_Page],{
15            object: null,
16            listView: null,
17            _dataMap: null,
18            constructor: function(){
19                this._dataMap = {};
20            },
21            onVisit: function() {
22                if ( this.pageArgs.uid ) {
23                    Deferred.when(store.get(this.pageArgs.uid))
24                    .then(lang.hitch(this,function(obj){
25                        this.object = obj;
26                        return Deferred.when( obj.creator && store.dereference(obj.creator) );
27                    }));
28                    this._createListView();
29                    this._createQuestionBrowser();
30                    this._setupButtons();
31                } else {
32                    throw "No valid uid or survey passed!";
33                }
34            },
35            _goToPreview: function() {
36                content.goTo('surveyAdvanced', {uid: this.object._id});
37            },
38            _setupButtons: function() {
39                this.btnSave.on("click", lang.hitch(this, this.onSave));
40                this.btnPreview.on("click", lang.hitch(this, this._goToPreview));
41            },
42            _createQuestionBrowser: function() {
43                store.query('_design/default/_view/by_type', {key:'Question'})
44                .forEach(function(question){
45                    this._insertQuestion(question);
46                },this);
47            },
48            _insertQuestion: function(question) {
49                for (var c in question.categories) {
50                    var cat = question.categories[c];
51                    if (this._dataMap[cat] === undefined) {
52                        this._dataMap[cat] = {
53                            widget: this._createCategoryTab(cat),
54                            topics: {}
55                        }
56                    }
57                    this._insertIntoCategory(question,this._dataMap[cat]);
58                }
59            },
60            _createCategoryTab: function(category) {
61                var categoryTab = new ContentPane({
62                    title: category
63                });
64                categoryTab.startup();
65                this.tabList.addChild(categoryTab);
66                return categoryTab;
67            },
68            _insertIntoCategory: function(question, categoryMap) {
69                if (categoryMap[question.topic] === undefined) {
70                    var w = new Selector({
71                        title: question.topic
72                    }).placeAt(categoryMap.widget.containerNode);
73                    w.startup();
74                    w.on('include',lang.hitch(this,this.includeQuestion));
75                    categoryMap[question.topic] = {
76                        widget: w
77                    };
78                }
79                categoryMap[question.topic].widget.addItem(question);
80            },
81            /* ListView code */
82            _createListView: function() {
83                this.listView = new SurveyListView({
84                    controller: this
85                }).placeAt(this.surveyListViewNode);
86                this.listView.startup();
87            },
88            includeQuestion: function(question) {
89                this.listView.insertItem(question);
90            }
91        });
92});
93
Note: See TracBrowser for help on using the repository browser.