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

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

Guarded widget startup() functions.

File size: 4.8 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                if ( this._started ){ return; }
27                this.inherited(arguments);
28                this._busyWidget = new Standby({
29                    target: this.domNode,
30                    duration: 200
31                }).placeAt(win.body());
32                this._busyWidget.startup();
33                this.watch("selectedChildWidget",lang.hitch(this,function(name,oldTab,newTab){
34                    this._fillCategoryTab(newTab.__category);
35                }));
36                store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:1})
37                .forPairs(lang.hitch(this,function(value,key){
38                    this._createCategoryTab(key[0],value);
39                }));
40            },
41            _createCategoryTab: function(category,count) {
42                if (this._dataMap[category] === undefined) {
43                    var categoryTab = new ContentPane({
44                        __category: category,
45                        title: (category || '[No category]')+" ("+count+")"
46                    });
47                    categoryTab.startup();
48                    this._dataMap[category] = {
49                        _widget: categoryTab
50                    };
51                    this.addChild(categoryTab);
52                }
53            },
54            _fillCategoryTab: function(category) {
55                var categoryMap = this._dataMap[category];
56                if (!categoryMap._filled) {
57                    this._busy();
58                    categoryMap._filled = true;
59                    store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]})
60                    .forPairs(lang.hitch(this,function(value,key){
61                        this._createTopicSelector(key[1],category,value);
62                    })).then(lang.hitch(this,function(){
63                        this._done();
64                    }));
65                }
66            },
67            _createTopicSelector: function(topic,category,count){
68                var categoryMap = this._dataMap[category];
69                if (categoryMap[topic] === undefined) {
70                    var w = new Selector({
71                        __category: category,
72                        __topic: topic,
73                        title: (topic || '[No topic]')+" ("+count+")",
74                        selectedActions: this.selectedActions,
75                        itemActions: this.itemActions
76                    }).placeAt(categoryMap._widget.containerNode);
77                    w.startup();
78                    categoryMap[topic] = {
79                        _widget: w
80                    };
81                    this._fillTopicSelector(topic,category);
82                }
83            },
84            _fillTopicSelector: function(topic,category) {
85                var categoryMap = this._dataMap[category];
86                var topicMap = categoryMap[topic];
87                if (!topicMap._filled) {
88                    topicMap._filled = true;
89                    this._busy();
90                    store.query('_design/default/_view/questions', {reduce:false,include_docs:true,key:[category,topic]})
91                    .forEach(lang.hitch(this,function(value){
92                        topicMap._widget.addItem(value);
93                    })).then(lang.hitch(this,function(){
94                        this._done();
95                    }));
96                }
97            },
98            _busy: function() {
99                if ( this._busyCount === 0 ) {
100                    this._busyWidget.show();
101                }
102                this._busyCount++;
103            },
104            _done: function() {
105                if ( this._busyCount > 0 ) {
106                    this._busyCount--;
107                    if ( this._busyCount === 0 ) {
108                        this._busyWidget.hide();
109                    }
110                } else {
111                    console.warn('_done() was called more times than _busy().');
112                }
113            },
114            destroy: function() {
115                this._busyWidget.destroyRecursive();
116                this.inherited(arguments);
117            }
118        });
119    });
Note: See TracBrowser for help on using the repository browser.