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

Last change on this file since 354 was 354, checked in by tjcschipper, 13 years ago
  • Made change to _Page.js to destroy the page's child widgets on page leave. This was causing widgets with identical names (such as "btnSave") to make regsitry throw a duplicate widget error.
  • survey.js/html now sorts loaded questions into categories and topics and creates or adds them to the proper TabPane/Selectors?. TODO: Allow for spaces in category titles.
  • Added "addQuestion()" method to Selector.js, to internalize question visualization logic.
  • Included surveyAdvanced page in run.js
  • Changes index to use proper button format, still need to figure out a way to bind content.goTo to the onclick field (since there is no index.js script being run!)
  • Various css tweaks.
File size: 5.9 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/store',
8    'rft/ui/_Page',
9    'rft/api',
10    'dijit/registry',
11    'rft/content',
12    'dojo/on',
13    'dojo/query'],
14    function(declare,lang,event,Deferred,AccordionList,LineWithActionsWidget,store,_Page,api,registry,content, on, query){
15        return declare('rft.pages.survey',[_Page],{
16            object: null,
17            onVisit: function() {
18                if ( this.pageArgs.uid ) {
19                    Deferred.when(store.get(this.pageArgs.uid))
20                    .then(lang.hitch(this,function(obj){
21                       this.object = obj;
22                       return Deferred.when( obj.creator && store.dereference(obj.creator) );
23                   }));
24                    this._createQuestionBrowser();
25                } else {
26                    //this.header.innerHTML = "Error: no uid for survey!"
27                }
28                this._setupButtons(this);
29                this._insertQuestion({title: "This is the question title!", sorting: {categories: ['CategoryOne', 'CategoryB123'], topic: 'The first question topic'}});
30                this._insertQuestion({title: "BLablabla question text!", sorting: {categories: ['CategoryOne'], topic: 'The first question topic'}});
31                this._insertQuestion({title: "Yaddayaddayaddaa ... sigh", sorting: {categories: ['CategoryThree', 'CategoryB123'], topic: 'Another topic'}});
32            },
33            onLeave: function() {
34                this.inherited(arguments);
35            },
36            onReset: function() {
37                this.setFields(this.object);
38            },
39            onSave: function(evt) {
40                /*lang.mixin(this.object,this.form.get('value'));
41                Deferred.when( store.put(this.object) )
42                .then(lang.hitch(this,function(obj){
43                    this.object = obj;
44                    this.setFields(obj);
45                    api.notify("Object saved");
46                }),lang.hitch(this,function(){
47                    api.notify("Object save failed",'error');
48                }));
49                event.stop(evt);
50                evt.stopPropagation();
51                return false;
52                */
53            },
54            _goToPreview: function() {
55                content.goTo('surveyAdvanced', {uid: this.object._id});
56                /*
57                * TODO: How to prevent widgets with identical id's on different pages clashing?
58                * For example: btnSave clashes when going from survey to surveyAdvanced, since both contain one. survey.btnSave is not destroyed when loading surveyAdvanced.btnSave!
59                * Since there is no page reload, only a contentPane content swap!
60                * Talk to Hendrik about this!
61                * TEMPORARY FIX IN THIS.ONLEAVE!
62                */
63            },
64            _setupButtons: function() {
65                // Setup button events
66                registry.byId("btnSave").on("click", lang.hitch(this, function(){
67                    this.onSave(arguments);
68                }));
69                registry.byId("btnPreview").on("click", lang.hitch(this, function(){
70                    this._goToPreview();
71                }));
72            },
73            _createQuestionBrowser: function() {
74                Deferred.when(store.query('_design/default/_view/by_type',{key:'Question'}))
75                .then(lang.hitch(this,function(questions){
76                 questions.forEach(function(question) {
77                    this._insertQuestion(question);
78                }, this);
79             }));
80            },
81            _insertQuestion: function(question) {
82                // MASSIVE TODO: The question categories cannot have spaces currently, due to the way we append them to the ID property of the container. ID's cannot have spaces.
83                // Easy fix: Replace spaces with underscores for the time being? The alternative is to make these containers have an actual "questioncategory" property in the governing widget. Not fun.
84                var tabs = registry.byId("tabList");
85                for (var c in question.sorting.categories) {
86                    var cat = question.sorting.categories[c];
87                    var catPane = query("#tab"+cat, tabs.containerNode)[0];
88                    if (catPane) {
89                        this._insertIntoCategory(question, catPane);
90                    } else {
91                        catPane = this._createCategoryTab(cat);
92                        if (catPane) {
93                            this._insertIntoCategory(question, catPane);
94                        } else {
95                            throw "Error: could not find or create category pane!";
96                        }
97                    }
98                }
99            },
100            _createCategoryTab: function(category) {
101                var tabs = registry.byId("tabList");
102                var newCat = new dijit.layout.ContentPane({
103                    title: category
104                });
105                newCat.domNode.id = "tab"+category;
106                tabs.addChild(newCat);
107                var pane = query("#tab"+category, tabs.containerNode)[0];
108                return (pane) ? pane : false;
109            },
110            _insertIntoCategory: function(question, container) {
111                debugger;
112                var selector = query(".rftSelector[title='"+question.sorting.topic+"']", container)[0];
113                var selectorWidget;
114                if (selector) {
115                    selectorWidget = registry.byNode(selector);
116                   
117                } else {
118                    selectorWidget = new rft.ui.Selector({
119                        title: question.sorting.topic
120                    });
121                    selectorWidget.placeAt(container);
122                }
123                selectorWidget.addQuestion(question);
124            },
125
126
127
128
129        });
130});
131
Note: See TracBrowser for help on using the repository browser.