source: Dev/branches/rest-dojo-ui/client/rft/pages/survey.js @ 355

Last change on this file since 355 was 355, checked in by tjcschipper, 13 years ago
  • surveyEditor more or less works! Only needed change is addition of "topic" property in question objects (database-side), and the change from "category"(string) to "categories"(string[]).
  • surveyEditor still needs removal function and infofunction to be written properly!
  • Added all files belonging to SurveyAdvanced?. Most do not work properly yet, but at least there will not be a 404 page when you click btnPreview on survey.html.
File size: 5.6 KB
Line 
1define(['dojo/_base/declare',
2        'dojo/_base/lang',
3        'dojo/_base/event',
4        'dojo/_base/Deferred',
5        'rft/ui/AccordionList',
6        'rft/ui/LineWithActionsWidget',
7        'rft/ui/SurveyListView',
8        'rft/store',
9        'rft/ui/_Page',
10        'rft/api',
11        'dijit/registry',
12        'rft/content',
13        'dojo/on',
14        'dojo/query'],
15        function(declare,lang,event,Deferred,AccordionList,LineWithActionsWidget,SurveyListView,store,_Page,api,registry,content, on, query){
16                return declare('rft.pages.survey',[_Page],{
17                        object: null,
18                        questions: null,
19                        listView: null,
20                       
21                        onVisit: function() {
22                                if ( this.pageArgs.uid ) {      // Load survey
23                                        Deferred.when(store.get(this.pageArgs.uid))
24                                        .then(lang.hitch(this,function(obj){
25                                                this.object = obj;
26                                                return Deferred.when( obj.creator && store.dereference(obj.creator) );
27                                        }));
28
29                                        //this._createQuestionBrowser();        // Load questions database and create browser
30                                        this.questions = new dojo.store.Memory({        // Create local copy store of questions database
31                                                data: [],
32                                                idProperty: "_id"
33                                        });
34                                } else {
35                                        throw "No valid uid or survey passed!";
36                                }
37                                this._setupButtons(this);
38                                var testQuestion1 = {_id: "123", title: "How long have you worked here?", categories: ['Professional background'], topic: 'Work experience'};
39                                this.questions.add(testQuestion1);
40                                this._insertQuestion(testQuestion1);
41                                var testQuestion2 = {_id: "234", title: "How many years have you been employed here?", categories: ['Respondent personals','Professional background'], topic: 'Work experience'};
42                                this.questions.add(testQuestion2);
43                                this._insertQuestion(testQuestion2);
44                                var testQuestion3 = {_id: "345", title: "Have you worked here longer than 10 years?", categories: ['Respondent personals','Professional background'], topic: 'Work experience'};
45                                this.questions.add(testQuestion3);
46                                this._insertQuestion(testQuestion3);
47                                var testQuestion4 = {_id: "456", title: "Rate your experience at your current employer from 1 to 10.", categories: ['Respondent personals','Professional background'], topic: 'Work experience'};
48                                this.questions.add(testQuestion4);
49                                this._insertQuestion(testQuestion4);
50                               
51                                this._createListView(this.questions);
52                        },
53                        onLeave: function() {
54                                this.inherited(arguments);
55                        },
56                        onReset: function() {
57                                this.setFields(this.object);
58                        },
59                        onSave: function(evt) {
60                                /*lang.mixin(this.object,this.form.get('value'));
61                                Deferred.when( store.put(this.object) )
62                                .then(lang.hitch(this,function(obj){
63                                this.object = obj;
64                                this.setFields(obj);
65                                api.notify("Object saved");
66                                }),lang.hitch(this,function(){
67                                api.notify("Object save failed",'error');
68                                }));
69                                event.stop(evt);
70                                evt.stopPropagation();
71                                return false;
72                                */
73                        },
74                        _goToPreview: function() {
75                                content.goTo('surveyAdvanced', {uid: this.object._id});
76                        },
77                        _setupButtons: function() {
78                                // Setup button events
79                                registry.byId("btnSave").on("click", lang.hitch(this, function(){
80                                        this.onSave(arguments);
81                                }));
82                                registry.byId("btnPreview").on("click", lang.hitch(this, function(){
83                                        this._goToPreview();
84                                }));
85                        },
86                        /* Store code */
87                        GetQuestion: function(_id) {
88                                return this.questions.get(_id);
89                        },
90                        SetQuestion: function(question) {
91                                return this.questions.put(question);
92                        },
93                        SyncStore: function() {
94                        },
95                        /* Browser code */
96                        _createQuestionBrowser: function() { // TODO: Do all operations from the local store. Put querying routine in SyncStore()!
97                                var getQuestions = function(self){
98                                        return questions = Deferred.when(store.query('_design/default/_view/by_type', {key:'Question'}), function(res){
99                                                self.questions.setData(res);    // Store queried questions in this.questions MemoryStore
100                                                return res;
101                                        });
102                                };
103
104                                Deferred.when(getQuestions(this), lang.hitch(this, function(questions){
105                                        questions.forEach(function(question){
106                                                this._insertQuestion(question)
107                                        }, this);
108                                }), function(err){
109                                        throw "Questions could not be fetched. No connection or null query!";
110                                });
111                        },
112                        _insertQuestion: function(question) {
113                                var tabs = registry.byId("tabList");
114                                for (var c in question.categories) {
115                                        var cat = question.categories[c];
116                                        var q = "div[data-category='"+cat+"']";
117                                        var catPane = query(q, tabs.containerNode)[0];
118                                        if (catPane) {
119                                                this._insertIntoCategory(question, catPane);
120                                        } else {
121                                                catPane = this._createCategoryTab(cat);
122                                                if (catPane) {
123                                                        this._insertIntoCategory(question, catPane);
124                                                } else {
125                                                        throw "Error: could not find or create category pane!";
126                                                }
127                                        }
128                                }
129                        },
130                        _createCategoryTab: function(category) {
131                                var tabs = registry.byId("tabList");
132                                var newCat = new dijit.layout.ContentPane({
133                                        title: category,
134                                        postCreate: function(){
135                                                this.domNode.dataset["category"] = category;
136                                        }
137                                });
138                                newCat.domNode.id = "tab"+category;
139                                tabs.addChild(newCat);
140                                var q = "div[data-category='"+category+"']";
141                                var pane = query(q, tabs.containerNode)[0];
142                                return (pane) ? pane : false;
143                        },
144                        _insertIntoCategory: function(question, container) {
145                                var selector = query(".rftSelector[data-topic='"+question.topic+"']", container)[0];
146                                var selectorWidget;
147                                if (selector) {
148                                        selectorWidget = registry.byNode(selector);
149
150                                } else {
151                                        selectorWidget = new rft.ui.Selector({
152                                                topic: question.topic,
153                                                controller: this
154                                        });
155                                        selectorWidget.placeAt(container);
156                                }
157                                selectorWidget.addQuestion(question._id);
158                        },
159                        /* ListView code */
160                        _createListView: function() {
161                                this.listView = new SurveyListView({
162                                        controller: this
163                                }).placeAt("SurveyListViewNode");
164                        },
165                        IncludeQuestion: function(_id) {
166                                this.listView.insertItem(_id);
167                        },
168
169                });
170});
171
Note: See TracBrowser for help on using the repository browser.