1 | define([ |
---|
2 | 'dojo/_base/declare', |
---|
3 | 'dojo/_base/lang', |
---|
4 | 'dijit/layout/ContentPane', |
---|
5 | 'dijit/layout/TabContainer', |
---|
6 | 'rft/store', |
---|
7 | 'rft/ui/Selector', |
---|
8 | 'dojo/domReady!' |
---|
9 | ], |
---|
10 | function(declare,lang,ContentPane,TabContainer,store,Selector){ |
---|
11 | return declare('rft.ui.TabbedQuestionBrowser',[TabContainer],{ |
---|
12 | tabPosition: 'left-h', |
---|
13 | |
---|
14 | selectedActions: null, |
---|
15 | itemActions: null, |
---|
16 | |
---|
17 | _dataMap: null, |
---|
18 | constructor: function(){ |
---|
19 | this._dataMap = {}; |
---|
20 | }, |
---|
21 | startup: function() { |
---|
22 | this.watch("selectedChildWidget",lang.hitch(this,function(name,oldTab,newTab){ |
---|
23 | this._fillCategoryTab(newTab.__category); |
---|
24 | })); |
---|
25 | store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:1}) |
---|
26 | .forPairs(lang.hitch(this,function(value,key){ |
---|
27 | this._createCategoryTab(key[0],value); |
---|
28 | })); |
---|
29 | }, |
---|
30 | _createCategoryTab: function(category,count) { |
---|
31 | if (this._dataMap[category] === undefined) { |
---|
32 | var categoryTab = new ContentPane({ |
---|
33 | __category: category, |
---|
34 | title: category+" ("+count+")" |
---|
35 | }); |
---|
36 | categoryTab.startup(); |
---|
37 | this._dataMap[category] = { |
---|
38 | _widget: categoryTab |
---|
39 | }; |
---|
40 | this.addChild(categoryTab); |
---|
41 | } |
---|
42 | }, |
---|
43 | _fillCategoryTab: function(category) { |
---|
44 | var categoryMap = this._dataMap[category]; |
---|
45 | if (!categoryMap._filled) { |
---|
46 | categoryMap._filled = true; |
---|
47 | store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]}) |
---|
48 | .forPairs(lang.hitch(this,function(value,key){ |
---|
49 | this._createTopicSelector(key[1],category,value); |
---|
50 | })); |
---|
51 | } |
---|
52 | }, |
---|
53 | _createTopicSelector: function(topic,category,count){ |
---|
54 | var categoryMap = this._dataMap[category]; |
---|
55 | if (categoryMap[topic] === undefined) { |
---|
56 | var w = new Selector({ |
---|
57 | __category: category, |
---|
58 | __topic: topic, |
---|
59 | title: topic+" ("+count+")", |
---|
60 | selectedActions: this.selectedActions, |
---|
61 | itemActions: this.itemActions |
---|
62 | }).placeAt(categoryMap._widget.containerNode); |
---|
63 | w.startup(); |
---|
64 | categoryMap[topic] = { |
---|
65 | _widget: w |
---|
66 | }; |
---|
67 | this._fillTopicSelector(topic,category); |
---|
68 | w.on('include',lang.hitch(this,this.onSelect)); |
---|
69 | } |
---|
70 | }, |
---|
71 | _fillTopicSelector: function(topic,category) { |
---|
72 | var categoryMap = this._dataMap[category]; |
---|
73 | var topicMap = categoryMap[topic]; |
---|
74 | if (!topicMap._filled) { |
---|
75 | store.query('_design/default/_view/questions', {reduce:false,include_docs:true,key:[category,topic]}) |
---|
76 | .forEach(lang.hitch(this,function(value){ |
---|
77 | topicMap._widget.addItem(value); |
---|
78 | })); |
---|
79 | } |
---|
80 | }, |
---|
81 | onSelect: function(question) {} |
---|
82 | }); |
---|
83 | }); |
---|