[420] | 1 | define([ |
---|
| 2 | 'dojo/_base/declare', |
---|
| 3 | 'dojo/_base/event', |
---|
| 4 | 'dojo/_base/lang', |
---|
| 5 | 'dojo/promise/all', |
---|
| 6 | 'dojo/when', |
---|
| 7 | '../store', |
---|
| 8 | '../app/Page', |
---|
| 9 | '../lib/async', |
---|
| 10 | '../model/classes/Response', |
---|
| 11 | '../model/classes/Survey', |
---|
| 12 | '../model/classes/SurveyRun', |
---|
| 13 | 'dojo/text!./templates/response.html' |
---|
| 14 | ],function(declare,event,lang,all,when,store,Page,async,Response,Survey,SurveyRun,template){ |
---|
| 15 | return declare([Page],{ |
---|
| 16 | templateString: template, |
---|
| 17 | response: null, |
---|
| 18 | postCreate: function(){ |
---|
| 19 | this.options = this.options || {}; |
---|
| 20 | }, |
---|
| 21 | startup: function() { |
---|
| 22 | if ( this._started ) { return; } |
---|
| 23 | this.inherited(arguments); |
---|
| 24 | this._disableButtons(); |
---|
| 25 | var surveyRunId = this.surveyRunId; |
---|
| 26 | var responseId = this.options && this.options.id; |
---|
| 27 | if ( surveyRunId && responseId ) { |
---|
| 28 | this._loadSurveyAndResponse(surveyRunId,responseId) |
---|
| 29 | .then(lang.hitch(this,"_enableButtons")); |
---|
| 30 | } else { |
---|
| 31 | throw new Error("No valid uid or survey passed!"); |
---|
| 32 | } |
---|
| 33 | }, |
---|
| 34 | _loadSurveyAndResponse: function(surveyRunId,responseId){ |
---|
| 35 | return all([store.get(surveyRunId),store.get(responseId)]) |
---|
| 36 | .then(lang.hitch(this,function(surveyAndResponse){ |
---|
| 37 | var surveyRun = surveyAndResponse[0]; |
---|
| 38 | this.response = surveyAndResponse[1]; |
---|
| 39 | if ( this.response.surveyRunId !== surveyRunId ) { |
---|
| 40 | throw "Survey does not match the response..."; |
---|
| 41 | } |
---|
| 42 | this.titleNode.innerHTML = Survey.DisplayTitle.get(surveyRun.survey); |
---|
| 43 | this.surveyWidget.set('survey',surveyRun.survey); |
---|
| 44 | this.responseForm.set('value',this.response.answers || {}); |
---|
| 45 | })); |
---|
| 46 | }, |
---|
| 47 | _enableButtons: function() { |
---|
| 48 | this.submitButton.set('disabled',false); |
---|
| 49 | this.continueButton.set('disabled',false); |
---|
| 50 | }, |
---|
| 51 | _disableButtons: function() { |
---|
| 52 | this.submitButton.set('disabled',true); |
---|
| 53 | this.continueButton.set('disabled',true); |
---|
| 54 | }, |
---|
| 55 | _getAnswersAndSaveResponse: function() { |
---|
| 56 | this.response.answers = this.responseForm.get('value'); |
---|
| 57 | console.log(this.response); |
---|
| 58 | return store.put(this.response); |
---|
| 59 | }, |
---|
| 60 | _onSubmit: function(e) { |
---|
| 61 | this.response.publicationDate = store.timestamp(); |
---|
| 62 | this._getAnswersAndSaveResponse(); |
---|
| 63 | this.content.domNode.innerHTML = "<div>Thanks for filling in the survey.</div>"; |
---|
[426] | 64 | if ( e ) { event.stop(e); } |
---|
[420] | 65 | return false; |
---|
| 66 | }, |
---|
| 67 | _onContinueLater: function(e) { |
---|
| 68 | this._getAnswersAndSaveResponse(); |
---|
| 69 | this.content.domNode.innerHTML = "<div>To continue with this survey later, just save the URL in the location bar and revisit it later. Your answers will still be there.</div>"; |
---|
[426] | 70 | if ( e ) { event.stop(e); } |
---|
[420] | 71 | return false; |
---|
| 72 | }, |
---|
| 73 | _ignoreEvent: function(e) { |
---|
[426] | 74 | if ( e ) { event.stop(e); } |
---|
[420] | 75 | return false; |
---|
| 76 | } |
---|
| 77 | }); |
---|
| 78 | }); |
---|