source: Dev/branches/rest-dojo-ui/client/rft/pages/questions.js @ 281

Last change on this file since 281 was 281, checked in by hendrikvanantwerpen, 13 years ago
  • [Client] Removed local Dojo copy and pull in via svn:external.
  • [Client] Keep categories of questions in alphabetical order.
  • [Client] Added skeleton QuestionWidget? for different question types.
File size: 4.6 KB
Line 
1define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/event',
2    'dojo/_base/Deferred','dojo/store/JsonRest','dijit/layout/ContentPane','dijit/TitlePane',
3    'rft/ui/_Page','rft/ui/LineWithActionsWidget'],
4    function(declare,lang,array,event,Deferred,JsonRest,ContentPane,TitlePane,_Page,LineWithActionsWidget) {
5        return declare('rft.pages.questions',[_Page],{
6            constructor: function() {
7                this.inherited(arguments);
8                this.questions = {};
9            },
10            onVisit: function() {
11                this._store = new JsonRest({
12                    target:"../server/api.php/data/Question/",
13                    idProperty: 'uid'
14                });
15                this._refresh();
16            },
17            _refresh: function() {
18                Deferred.when( this._store.query() ).then(lang.hitch(this,function(results){
19                    array.forEach(results,lang.hitch(this,'_addQuestion'));
20                }));
21            },
22            _addQuestion: function(q) {
23                var uid = q.uid;
24                var question = this.questions[uid];
25                if ( !question ) {
26                    var qw = new LineWithActionsWidget({
27                        title: q.title,
28                        userObject: q,
29                        actions: {
30                            'Edit': lang.hitch(this,'_editQuestion')
31                        }
32                    });
33                    qw.startup();
34                    question = this.questions[uid] = {
35                        question: q,
36                        widget: qw
37                    }
38                } else {
39                    // update info
40                    question.question = q;
41                    question.widget.title = q.title;
42                    question.widget.userObject = q;
43                    question.widget.refresh();
44                }
45                var container = this._getContainerForQuestion(q);
46                question.widget.placeAt(container.containerNode);
47                this._cleanupEmptyContainers();
48            },
49            _getContainerForQuestion: function(q) {
50                var category = q.category || 'Unsorted';
51                var widgets = this.accordion.getChildren();
52                var containerWidget;
53                var lastIndex = 'last';
54                array.some(widgets,lang.hitch(this,function(widget,idx) {
55                    if ( widget.title == category ) {
56                        containerWidget = widget;
57                        return true;
58                    } else if ( widget.title > category  ) {
59                        lastIndex = idx;
60                        return true;
61                    }
62                    return false;
63                }));
64                if ( !containerWidget ) {
65                    containerWidget = new TitlePane({
66                        title:category
67                    });
68                    containerWidget.startup();
69                    this.accordion.addChild(containerWidget,lastIndex);
70                }
71                return containerWidget;
72            },
73            _cleanupEmptyContainers: function() {
74                var widgets = this.accordion.getChildren();
75                array.forEach(widgets,lang.hitch(this,function(widget){
76                    if ( !widget.getChildren().length ) {
77                        this.accordion.removeChild(widget).destroy();
78                    }
79                }));
80            },
81            onNewQuestion: function() {
82                Deferred.when( this._store.add({}) )
83                .then(lang.hitch(this,function(question){
84                    this._editQuestion(question);
85                }));
86            },
87            _editQuestion: function(question) {
88                this.questionForm.reset();
89                this.questionForm.set('value',question);
90                this.questionWidget.set('value',question);
91                this.questionDialog.show();
92            },
93            onSaveQuestion: function(evt) {
94                var value = this.questionForm.get('value');
95                var subvalue = this.questionWidget.get('value');
96                lang.mixin(value,subvalue);
97                Deferred.when( this._store.put(value) )
98                .then(lang.hitch(this,function(){
99                    this.questionDialog.hide();
100                    this.questionForm.reset();
101                    this._refresh();
102                }));
103                event.stop(evt);
104                return false;
105            },
106            onCancelQuestion: function() {
107                this.questionDialog.hide();
108                this.questionForm.reset();
109            }
110        });
111    });
Note: See TracBrowser for help on using the repository browser.