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