Changeset 360 for Dev/branches/rest-dojo-ui/client/rft/pages/survey.js
- Timestamp:
- 07/13/12 15:13:46 (13 years ago)
- Location:
- Dev/branches/rest-dojo-ui/client
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/branches/rest-dojo-ui/client
-
Property
svn:ignore
set to
-
Property
svn:ignore
set to
-
Dev/branches/rest-dojo-ui/client/rft/pages/survey.js
r358 r360 1 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 }); 2 'dojo/_base/lang', 3 'dojo/_base/event', 4 'dojo/_base/Deferred', 5 'rft/ui/Selector', 6 'rft/ui/SurveyListView', 7 'dijit/layout/ContentPane', 8 'rft/store', 9 'rft/ui/_Page', 10 'rft/content', 11 'dojo/store/Cache', 12 'dojo/store/Memory' ], 13 function(declare,lang,event,Deferred,Selector,SurveyListView,ContentPane,store,_Page,content,Cache,Memory){ 14 return declare('rft.pages.survey',[_Page],{ 15 object: null, 16 listView: null, 17 _dataMap: null, 18 constructor: function(){ 19 this._dataMap = {}; 20 }, 21 onVisit: function() { 22 if ( this.pageArgs.uid ) { 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 this._createListView(); 29 this._createQuestionBrowser(); 30 this._setupButtons(); 31 } else { 32 throw "No valid uid or survey passed!"; 33 } 34 }, 35 _goToPreview: function() { 36 content.goTo('surveyAdvanced', {uid: this.object._id}); 37 }, 38 _setupButtons: function() { 39 this.btnSave.on("click", lang.hitch(this, this.onSave)); 40 this.btnPreview.on("click", lang.hitch(this, this._goToPreview)); 41 }, 42 _createQuestionBrowser: function() { 43 store.query('_design/default/_view/by_type', {key:'Question'}) 44 .forEach(function(question){ 45 this._insertQuestion(question); 46 },this); 47 }, 48 _insertQuestion: function(question) { 49 for (var c in question.categories) { 50 var cat = question.categories[c]; 51 if (this._dataMap[cat] === undefined) { 52 this._dataMap[cat] = { 53 widget: this._createCategoryTab(cat), 54 topics: {} 55 } 56 } 57 this._insertIntoCategory(question,this._dataMap[cat]); 58 } 59 }, 60 _createCategoryTab: function(category) { 61 var categoryTab = new ContentPane({ 62 title: category 63 }); 64 categoryTab.startup(); 65 this.tabList.addChild(categoryTab); 66 return categoryTab; 67 }, 68 _insertIntoCategory: function(question, categoryMap) { 69 if (categoryMap[question.topic] === undefined) { 70 var w = new Selector({ 71 title: question.topic 72 }).placeAt(categoryMap.widget.containerNode); 73 w.startup(); 74 w.on('include',lang.hitch(this,this.includeQuestion)); 75 categoryMap[question.topic] = { 76 widget: w 77 }; 78 } 79 categoryMap[question.topic].widget.addItem(question); 80 }, 81 /* ListView code */ 82 _createListView: function() { 83 this.listView = new SurveyListView({ 84 controller: this 85 }).placeAt(this.surveyListViewNode); 86 this.listView.startup(); 87 }, 88 includeQuestion: function(question) { 89 this.listView.insertItem(question); 90 } 91 }); 168 92 }); 169 93
Note: See TracChangeset
for help on using the changeset viewer.