source: Dev/branches/rest-dojo-ui/client/rft/ui/TabbedQuestionBrowser.js @ 386

Last change on this file since 386 was 386, checked in by hendrikvanantwerpen, 13 years ago
  • Changed questions view to return null for empty topic/categorie and leave presentation of null to TabbedQuestionWidget?.
File size: 4.7 KB
Line 
1define([
2    'dojo/_base/declare',
3    'dojo/_base/lang',
4    'dojo/_base/window',
5    'dijit/layout/ContentPane',
6    'dijit/layout/TabContainer',
7    'dojox/widget/Standby',
8    'rft/store',
9    'rft/ui/Selector',
10    'dojo/domReady!'
11    ],
12    function(declare,lang,win,ContentPane,TabContainer,Standby,store,Selector){
13        return declare('rft.ui.TabbedQuestionBrowser',[TabContainer],{
14            tabPosition: 'left-h',
15
16            selectedActions: null,
17            itemActions: null,
18
19            _dataMap: null,
20            _busyCount: 0,
21            constructor: function(){
22                this.inherited(arguments);
23                this._dataMap = {};
24            },
25            startup: function() {
26                this.inherited(arguments);
27                this._busyWidget = new Standby({
28                    target: this.domNode,
29                    duration: 200
30                }).placeAt(win.body());
31                this._busyWidget.startup();
32                this.watch("selectedChildWidget",lang.hitch(this,function(name,oldTab,newTab){
33                    this._fillCategoryTab(newTab.__category);
34                }));
35                store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:1})
36                .forPairs(lang.hitch(this,function(value,key){
37                    this._createCategoryTab(key[0],value);
38                }));
39            },
40            _createCategoryTab: function(category,count) {
41                if (this._dataMap[category] === undefined) {
42                    var categoryTab = new ContentPane({
43                        __category: category,
44                        title: (category || '[No category]')+" ("+count+")"
45                    });
46                    categoryTab.startup();
47                    this._dataMap[category] = {
48                        _widget: categoryTab
49                    };
50                    this.addChild(categoryTab);
51                }
52            },
53            _fillCategoryTab: function(category) {
54                var categoryMap = this._dataMap[category];
55                if (!categoryMap._filled) {
56                    this._busy();
57                    categoryMap._filled = true;
58                    store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]})
59                    .forPairs(lang.hitch(this,function(value,key){
60                        this._createTopicSelector(key[1],category,value);
61                    })).then(lang.hitch(this,function(){
62                        this._done();
63                    }));
64                }
65            },
66            _createTopicSelector: function(topic,category,count){
67                var categoryMap = this._dataMap[category];
68                if (categoryMap[topic] === undefined) {
69                    var w = new Selector({
70                        __category: category,
71                        __topic: topic,
72                        title: (topic || '[No topic]')+" ("+count+")",
73                        selectedActions: this.selectedActions,
74                        itemActions: this.itemActions
75                    }).placeAt(categoryMap._widget.containerNode);
76                    w.startup();
77                    categoryMap[topic] = {
78                        _widget: w
79                    };
80                    this._fillTopicSelector(topic,category);
81                }
82            },
83            _fillTopicSelector: function(topic,category) {
84                var categoryMap = this._dataMap[category];
85                var topicMap = categoryMap[topic];
86                if (!topicMap._filled) {
87                    topicMap._filled = true;
88                    this._busy();
89                    store.query('_design/default/_view/questions', {reduce:false,include_docs:true,key:[category,topic]})
90                    .forEach(lang.hitch(this,function(value){
91                        topicMap._widget.addItem(value);
92                    })).then(lang.hitch(this,function(){
93                        this._done();
94                    }));
95                }
96            },
97            _busy: function() {
98                if ( this._busyCount === 0 ) {
99                    this._busyWidget.show();
100                }
101                this._busyCount++;
102            },
103            _done: function() {
104                if ( this._busyCount > 0 ) {
105                    this._busyCount--;
106                    if ( this._busyCount === 0 ) {
107                        this._busyWidget.hide();
108                    }
109                } else {
110                    console.warn('_done() was called more times than _busy().');
111                }
112            },
113            destroy: function() {
114                this._busyWidget.destroyRecursive();
115                this.inherited(arguments);
116            }
117        });
118    });
Note: See TracBrowser for help on using the repository browser.