source: Dev/trunk/src/client/qed-client/model/widgets/TabbedQuestionBrowser.js @ 477

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

Quick hack to allow responding despite not being authenticated. Something like tokes need to be added.

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