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

Last change on this file since 487 was 487, checked in by hendrikvanantwerpen, 11 years ago

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

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