source: Dev/trunk/client/qed/model/widgets/TabbedQuestionBrowser.js @ 428

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

Allow Selector to be a drag source.

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