[443] | 1 | define([ |
---|
| 2 | "../app/Page", |
---|
[487] | 3 | "../model/classes/responses", |
---|
[443] | 4 | "dojo/_base/declare", |
---|
| 5 | "dojo/_base/event", |
---|
[477] | 6 | "dojo/_base/json", |
---|
[443] | 7 | "dojo/_base/lang", |
---|
| 8 | "dojo/promise/all", |
---|
[477] | 9 | "dojo/request", |
---|
[443] | 10 | "dojo/when", |
---|
| 11 | "require", |
---|
| 12 | "dojo/text!./templates/response.html" |
---|
[490] | 13 | ], function(Page, responses, declare, event, json, lang, all, request, when, require, template) { |
---|
[443] | 14 | return declare([Page],{ |
---|
| 15 | contextRequire: require, |
---|
| 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); |
---|
[461] | 24 | this._disableSubmit(); |
---|
[487] | 25 | if ( !this.response ) { |
---|
| 26 | this._showInfo("<div>The url seems to be incorrect, no response found.</div>"); |
---|
[443] | 27 | } else { |
---|
[487] | 28 | this.titleNode.innerHTML = this.response._surveyRun.survey.title || ""; |
---|
| 29 | this.surveyWidget.set('survey', this.response._surveyRun.survey); |
---|
| 30 | this.surveyWidget.set('value', this.response.answers || {}); |
---|
| 31 | if ( this.response.publicationDate ) { |
---|
| 32 | this._showInfo("<div>You already submitted your survey and cannot change it anymore. You can still view your answers here.</div>"); |
---|
| 33 | this._disableSubmit(); |
---|
| 34 | } else { |
---|
| 35 | this._enableSubmit(); |
---|
| 36 | } |
---|
[443] | 37 | } |
---|
| 38 | }, |
---|
[461] | 39 | _enableSubmit: function() { |
---|
[443] | 40 | this.submitButton.set('disabled',false); |
---|
| 41 | this.continueButton.set('disabled',false); |
---|
[492] | 42 | var canDelete = (this.response && |
---|
| 43 | this.response._surveyRun.respondentCanDeleteOwnResponse); |
---|
[508] | 44 | this.cancelButton.set('disabled',canDelete?false:true); |
---|
[503] | 45 | this.surveyWidget.set('readOnly', false); |
---|
[443] | 46 | }, |
---|
[461] | 47 | _disableSubmit: function() { |
---|
[443] | 48 | this.submitButton.set('disabled',true); |
---|
| 49 | this.continueButton.set('disabled',true); |
---|
[478] | 50 | this.cancelButton.set('disabled',true); |
---|
[503] | 51 | this.surveyWidget.set('readOnly', true); |
---|
[443] | 52 | }, |
---|
[461] | 53 | _showInfo: function(html) { |
---|
| 54 | this.infoNode.innerHTML = html; |
---|
| 55 | }, |
---|
[443] | 56 | _getAnswersAndSaveResponse: function() { |
---|
[461] | 57 | var answers = this.surveyWidget.get('value'); |
---|
| 58 | this.response.answers = answers; |
---|
[487] | 59 | return responses.putWithSecret(this.response,this.response.secret) |
---|
| 60 | .then(lang.hitch(this,function(response){ |
---|
| 61 | this.response = response; |
---|
[490] | 62 | this.notify("Your response is saved."); |
---|
| 63 | }), lang.hitch(this,function(err){ |
---|
| 64 | this.notify(err.error,'error'); |
---|
| 65 | })); |
---|
[443] | 66 | }, |
---|
| 67 | _onSubmit: function(e) { |
---|
[487] | 68 | this.response.publicationDate = new Date(); |
---|
[461] | 69 | this._getAnswersAndSaveResponse() |
---|
[477] | 70 | .then(lang.hitch(this,function(){ |
---|
[461] | 71 | this._showInfo("<div>Thanks for filling in the survey. You cannot edit your answers anymore.</div>"); |
---|
| 72 | this._disableSubmit(); |
---|
[477] | 73 | })); |
---|
[443] | 74 | if ( e ) { event.stop(e); } |
---|
| 75 | return false; |
---|
| 76 | }, |
---|
| 77 | _onContinueLater: function(e) { |
---|
[461] | 78 | this._getAnswersAndSaveResponse() |
---|
| 79 | .then(lang.hitch(this,function(){ |
---|
| 80 | this._showInfo("<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>"); |
---|
| 81 | })); |
---|
[443] | 82 | if ( e ) { event.stop(e); } |
---|
| 83 | return false; |
---|
| 84 | }, |
---|
[478] | 85 | _onCancel: function(e) { |
---|
| 86 | this._disableSubmit(); |
---|
| 87 | this.surveyWidget.destroy(); |
---|
[487] | 88 | responses.removeWithSecret(this.response,this.response.secret) |
---|
| 89 | .then(lang.hitch(this,function(res){ |
---|
[478] | 90 | this._showInfo("<div>Your response has been deleted, no answers have been saved.</div>"); |
---|
[490] | 91 | this.notify("Your response is deleted."); |
---|
| 92 | }), lang.hitch(this,function(err){ |
---|
| 93 | this.notify(err.error,'error'); |
---|
| 94 | })); |
---|
[478] | 95 | if ( e ) { event.stop(e); } |
---|
| 96 | return false; |
---|
| 97 | }, |
---|
[443] | 98 | _ignoreEvent: function(e) { |
---|
| 99 | if ( e ) { event.stop(e); } |
---|
| 100 | return false; |
---|
| 101 | } |
---|
| 102 | }); |
---|
| 103 | }); |
---|