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

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

Fixed incompatibility of store with defined interface
dojo.store.Store. Added custom forPairs function to query results and
included documentation about query options.

File size: 4.7 KB
Line 
1define(['dojo/_base/declare',
2    'dojo/_base/lang',
3    'dojo/_base/event',
4    'dojo/_base/Deferred',
5    'dojo/on',
6    'rft/ui/Selector',
7    'rft/ui/SurveyListView',
8    'dijit/layout/ContentPane',
9    'rft/store',
10    'rft/ui/_Page',
11    'rft/content',
12    'dojo/store/Cache',
13    'dojo/store/Memory' ],
14    function(declare,lang,event,Deferred,on,Selector,SurveyListView,ContentPane,store,_Page,content,Cache,Memory){
15        return declare('rft.pages.survey',[_Page],{
16            object: null,
17            listView: null,
18            _dataMap: null,
19            constructor: function(){
20                this._dataMap = {};
21            },
22            onVisit: function() {
23                if ( this.pageArgs.uid ) {
24                    Deferred.when(store.get(this.pageArgs.uid))
25                    .then(lang.hitch(this,function(obj){
26                        this.object = obj;
27                        return Deferred.when( obj.creator && store.dereference(obj.creator) );
28                    }));
29                    this._setupButtons();
30                    this._setupQuestionBrowser();
31                    this._setupListView();
32                } else {
33                    throw "No valid uid or survey passed!";
34                }
35            },
36            _goToPreview: function() {
37                content.goTo('surveyAdvanced', {uid: this.object._id});
38            },
39            _setupButtons: function() {
40                this.btnSave.on("click", lang.hitch(this, this.onSave));
41                this.btnPreview.on("click", lang.hitch(this, this._goToPreview));
42            },
43            _setupQuestionBrowser: function() {
44                this.tabList.watch("selectedChildWidget",lang.hitch(this,function(name,oldTab,newTab){
45                    this._fillCategoryTab(newTab.__category);
46                }));
47                store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:1})
48                .forPairs(lang.hitch(this,function(value,key){
49                    this._createCategoryTab(key[0],value);
50                }));
51            },
52            _createCategoryTab: function(category,count) {
53                if (this._dataMap[category] === undefined) {
54                    var categoryTab = new ContentPane({
55                        __category: category,
56                        title: category+" ("+count+")"
57                    });
58                    categoryTab.startup();
59                    this._dataMap[category] = {
60                        _widget: categoryTab
61                    };
62                    this.tabList.addChild(categoryTab);
63                }
64            },
65            _fillCategoryTab: function(category) {
66                var categoryMap = this._dataMap[category];
67                if (!categoryMap._filled) {
68                    categoryMap._filled = true;
69                    store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]})
70                    .forPairs(lang.hitch(this,function(value,key){
71                        this._createTopicSelector(key[1],category,value);
72                    }));
73                }
74            },
75            _createTopicSelector: function(topic,category,count){
76                var categoryMap = this._dataMap[category];
77                if (categoryMap[topic] === undefined) {
78                    var w = new Selector({
79                        __category: category,
80                        __topic: topic,
81                        title: topic+" ("+count+")"
82                    }).placeAt(categoryMap._widget.containerNode);
83                    w.startup();
84                    categoryMap[topic] = {
85                        _widget: w
86                    };
87                    this._fillTopicSelector(topic,category);
88                    w.on('include',lang.hitch(this,this.includeQuestion));
89                }
90            },
91            _fillTopicSelector: function(topic,category) {
92                var categoryMap = this._dataMap[category];
93                var topicMap = categoryMap[topic];
94                if (!topicMap._filled) {
95                    store.query('_design/default/_view/questions', {reduce:false,include_docs:true,key:[category,topic]})
96                    .forEach(lang.hitch(this,function(value){
97                        topicMap._widget.addItem(value);
98                    }));
99                }
100            },
101            /* ListView code */
102            includeQuestion: function(question) {
103                this.listView.insertItem(question);
104            },
105            _setupListView: function() {
106                this.listView = new SurveyListView({
107                    controller: this
108                }).placeAt(this.surveyListViewNode);
109                this.listView.startup();
110            }
111        });
112});
113
Note: See TracBrowser for help on using the repository browser.