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

Last change on this file since 288 was 288, checked in by hendrikvanantwerpen, 13 years ago

[Client] MultipleChoiceWidget? for editing multiple choice questions
[Client] Move templates to separate directories.
[Client] Created QuestionWidget? to edit complete questions.
[Client] Fixed startup race condition where parsing was started before all classes were loaded.

File size: 4.7 KB
Line 
1define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/event',
2    'dojo/_base/Deferred','dojo/dom-construct','dojo/store/JsonRest','dijit/layout/ContentPane','dijit/TitlePane',
3    'rft/ui/_Page','rft/ui/LineWithActionsWidget'],
4    function(declare,lang,array,event,Deferred,domConstruct,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(true);
16            },
17            _refresh: function(initial) {
18                Deferred.when( this._store.query() ).then(lang.hitch(this,function(results){
19                    array.forEach(results,lang.hitch(this,'_addQuestion'));
20                    initial && this.accordion.selectChild(true);
21                }));
22            },
23            _addQuestion: function(q) {
24                var uid = q.uid;
25                var question = this.questions[uid];
26                if ( !question ) {
27                    var qw = new LineWithActionsWidget({
28                        title: q.title,
29                        userObject: q,
30                        actions: {
31                            'Edit': lang.hitch(this,'_editQuestion')
32                        }
33                    });
34                    qw.startup();
35                    question = this.questions[uid] = {
36                        question: q,
37                        widget: qw
38                    }
39                } else {
40                    // update info
41                    question.question = q;
42                    question.widget.title = q.title;
43                    question.widget.userObject = q;
44                    question.widget.refresh();
45                }
46                var container = this._getContainerForQuestion(q);
47                question.widget.placeAt(container.containerNode);
48                this._cleanupEmptyContainers();
49            },
50            _getContainerForQuestion: function(q) {
51                var category = q.category || 'Unsorted';
52                var widgets = this.accordion.getChildren();
53                var containerWidget = null;
54                var placeNode = this.accordion.domNode;
55                var placePos = 'last';
56                array.some(widgets,lang.hitch(this,function(widget,idx) {
57                    if ( widget.title == category ) {
58                        containerWidget = widget;
59                        return true;
60                    } else if ( widget.title > category  ) {
61                        placeNode = widget.domNode;
62                        placePos = "before";
63                        return true;
64                    }
65                    return false;
66                }));
67                if ( !containerWidget ) {
68                    containerWidget = new TitlePane({
69                        title:category
70                    });
71                    containerWidget.startup();
72                    domConstruct.place(containerWidget.domNode,placeNode,placePos);
73                }
74                return containerWidget;
75            },
76            _cleanupEmptyContainers: function() {
77                var widgets = this.accordion.getChildren();
78                array.forEach(widgets,lang.hitch(this,function(widget){
79                    if ( !widget.getChildren().length ) {
80                        this.accordion.removeChild(widget).destroy();
81                    }
82                }));
83            },
84            onNewQuestion: function() {
85                Deferred.when( this._store.add({}) )
86                .then(lang.hitch(this,function(question){
87                    this._editQuestion(question);
88                }));
89            },
90            _editQuestion: function(question) {
91                this.questionForm.reset();
92                this.questionWidget.set('value',question);
93                this.questionDialog.show();
94            },
95            onSaveQuestion: function(evt) {
96                var value = this.questionWidget.get('value');
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.