define([
"../app/Page",
"../model/classes/responses",
"dojo/_base/declare",
"dojo/_base/event",
"dojo/_base/json",
"dojo/_base/lang",
"dojo/promise/all",
"dojo/request",
"dojo/when",
"require",
"dojo/text!./templates/response.html"
], function(Page, responses, declare, event, json, lang, all, request, when, require, template) {
return declare([Page],{
contextRequire: require,
templateString: template,
response: null,
postCreate: function(){
this.options = this.options || {};
},
startup: function() {
if ( this._started ) { return; }
this.inherited(arguments);
this._disableSubmit();
if ( !this.response ) {
this._showInfo("
The url seems to be incorrect, no response found.
");
} else {
this.titleNode.innerHTML = this.response._surveyRun.survey.title || "";
this.surveyWidget.set('survey', this.response._surveyRun.survey);
this.surveyWidget.set('value', this.response.answers || {});
if ( this.response.publicationDate ) {
this._showInfo("You already submitted your survey and cannot change it anymore. You can still view your answers here.
");
this._disableSubmit();
} else {
this._enableSubmit();
}
}
},
_enableSubmit: function() {
this.submitButton.set('disabled',false);
this.continueButton.set('disabled',false);
var canDelete = (this.response &&
this.response._surveyRun.respondentCanDeleteOwnResponse);
this.cancelButton.set('disabled',canDelete?false:true);
this.surveyWidget.set('readOnly', false);
},
_disableSubmit: function() {
this.submitButton.set('disabled',true);
this.continueButton.set('disabled',true);
this.cancelButton.set('disabled',true);
this.surveyWidget.set('readOnly', true);
},
_showInfo: function(html) {
this.infoNode.innerHTML = html;
},
_getAnswersAndSaveResponse: function() {
var answers = this.surveyWidget.get('value');
this.response.answers = answers;
return responses.putWithSecret(this.response,this.response.secret)
.then(lang.hitch(this,function(response){
this.response = response;
this.notify("Your response is saved.");
}), lang.hitch(this,function(err){
this.notify(err.error,'error');
}));
},
_onSubmit: function(e) {
this.response.publicationDate = new Date();
this._getAnswersAndSaveResponse()
.then(lang.hitch(this,function(){
this._showInfo("Thanks for filling in the survey. You cannot edit your answers anymore.
");
this._disableSubmit();
}));
if ( e ) { event.stop(e); }
return false;
},
_onContinueLater: function(e) {
this._getAnswersAndSaveResponse()
.then(lang.hitch(this,function(){
this._showInfo("To continue with this survey later, just save the URL in the location bar and revisit it later. Your answers will still be there.
");
}));
if ( e ) { event.stop(e); }
return false;
},
_onCancel: function(e) {
this._disableSubmit();
this.surveyWidget.destroy();
responses.removeWithSecret(this.response,this.response.secret)
.then(lang.hitch(this,function(res){
this._showInfo("Your response has been deleted, no answers have been saved.
");
this.notify("Your response is deleted.");
}), lang.hitch(this,function(err){
this.notify(err.error,'error');
}));
if ( e ) { event.stop(e); }
return false;
},
_ignoreEvent: function(e) {
if ( e ) { event.stop(e); }
return false;
}
});
});