1 | define(['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 | /* Browser code */ |
---|
94 | _createQuestionBrowser: function() { // TODO: Do all operations from the local store. Put querying routine in SyncStore()! |
---|
95 | var getQuestions = function(self){ |
---|
96 | return questions = Deferred.when(store.query('_design/default/_view/by_type', {key:'Question'}), function(res){ |
---|
97 | self.questions.setData(res); // Store queried questions in this.questions MemoryStore |
---|
98 | return res; |
---|
99 | }); |
---|
100 | }; |
---|
101 | |
---|
102 | Deferred.when(getQuestions(this), lang.hitch(this, function(questions){ |
---|
103 | questions.forEach(function(question){ |
---|
104 | this._insertQuestion(question) |
---|
105 | }, this); |
---|
106 | }), function(err){ |
---|
107 | throw "Questions could not be fetched. No connection or null query!"; |
---|
108 | }); |
---|
109 | }, |
---|
110 | _insertQuestion: function(question) { |
---|
111 | var tabs = registry.byId("tabList"); |
---|
112 | for (var c in question.categories) { |
---|
113 | var cat = question.categories[c]; |
---|
114 | var q = "div[data-category='"+cat+"']"; |
---|
115 | var catPane = query(q, tabs.containerNode)[0]; |
---|
116 | if (catPane) { |
---|
117 | this._insertIntoCategory(question, catPane); |
---|
118 | } else { |
---|
119 | catPane = this._createCategoryTab(cat); |
---|
120 | if (catPane) { |
---|
121 | this._insertIntoCategory(question, catPane); |
---|
122 | } else { |
---|
123 | throw "Error: could not find or create category pane!"; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | }, |
---|
128 | _createCategoryTab: function(category) { |
---|
129 | var tabs = registry.byId("tabList"); |
---|
130 | var newCat = new dijit.layout.ContentPane({ |
---|
131 | title: category, |
---|
132 | postCreate: function(){ |
---|
133 | this.domNode.dataset["category"] = category; |
---|
134 | } |
---|
135 | }); |
---|
136 | newCat.domNode.id = "tab"+category; |
---|
137 | tabs.addChild(newCat); |
---|
138 | var q = "div[data-category='"+category+"']"; |
---|
139 | var pane = query(q, tabs.containerNode)[0]; |
---|
140 | return (pane) ? pane : false; |
---|
141 | }, |
---|
142 | _insertIntoCategory: function(question, container) { |
---|
143 | var selector = query(".rftSelector[data-topic='"+question.topic+"']", container)[0]; |
---|
144 | var selectorWidget; |
---|
145 | if (selector) { |
---|
146 | selectorWidget = registry.byNode(selector); |
---|
147 | |
---|
148 | } else { |
---|
149 | selectorWidget = new rft.ui.Selector({ |
---|
150 | topic: question.topic, |
---|
151 | controller: this |
---|
152 | }); |
---|
153 | selectorWidget.placeAt(container); |
---|
154 | } |
---|
155 | selectorWidget.addQuestion(question._id); |
---|
156 | }, |
---|
157 | /* ListView code */ |
---|
158 | _createListView: function() { |
---|
159 | this.listView = new SurveyListView({ |
---|
160 | controller: this |
---|
161 | }).placeAt("SurveyListViewNode"); |
---|
162 | }, |
---|
163 | IncludeQuestion: function(_id) { |
---|
164 | this.listView.insertItem(_id); |
---|
165 | }, |
---|
166 | |
---|
167 | }); |
---|
168 | }); |
---|
169 | |
---|