1 | define([ |
---|
2 | "../app/Content", |
---|
3 | "../app/Page", |
---|
4 | "../lib/async", |
---|
5 | "../model/classes/Response", |
---|
6 | "../model/classes/Survey", |
---|
7 | "../model/classes/SurveyRun", |
---|
8 | "../store", |
---|
9 | "dojo/_base/declare", |
---|
10 | "dojo/_base/event", |
---|
11 | "dojo/_base/lang", |
---|
12 | "dojo/promise/all", |
---|
13 | "dojo/when", |
---|
14 | "require", |
---|
15 | "dojo/text!./templates/response.html" |
---|
16 | ], function(Content, Page, async, Response, Survey, SurveyRun, store, declare, event, lang, all, when, require, template) { |
---|
17 | return declare([Page],{ |
---|
18 | contextRequire: require, |
---|
19 | templateString: template, |
---|
20 | response: null, |
---|
21 | postCreate: function(){ |
---|
22 | this.options = this.options || {}; |
---|
23 | }, |
---|
24 | startup: function() { |
---|
25 | if ( this._started ) { return; } |
---|
26 | this.inherited(arguments); |
---|
27 | this._disableSubmit(); |
---|
28 | var surveyRunId = this.surveyRunId; |
---|
29 | var responseId = this.options && this.options.id; |
---|
30 | if ( surveyRunId && responseId ) { |
---|
31 | this._loadSurveyAndResponse(surveyRunId,responseId) |
---|
32 | .then(lang.hitch(this, function() { |
---|
33 | if ( this.response.publicationDate ) { |
---|
34 | this._showInfo("<div>You already submitted your survey and cannot change it anymore. You can still view your answers here.</div>"); |
---|
35 | this._disableSubmit(); |
---|
36 | } else { |
---|
37 | this._enableSubmit(); |
---|
38 | } |
---|
39 | }), function() { |
---|
40 | this._showInfo("<div>The url seems to be incorrect, no survey found.</div>"); |
---|
41 | }); |
---|
42 | } else { |
---|
43 | throw new Error("No valid uid or survey passed!"); |
---|
44 | } |
---|
45 | }, |
---|
46 | _loadSurveyAndResponse: function(surveyRunId,responseId){ |
---|
47 | return all([store.get(surveyRunId),store.get(responseId)]) |
---|
48 | .then(lang.hitch(this,function(surveyAndResponse){ |
---|
49 | var surveyRun = surveyAndResponse[0]; |
---|
50 | this.response = surveyAndResponse[1]; |
---|
51 | if ( this.response.surveyRunId !== surveyRunId ) { |
---|
52 | throw "Survey does not match the response..."; |
---|
53 | } |
---|
54 | this.titleNode.innerHTML = Survey.DisplayTitle.get(surveyRun.survey); |
---|
55 | this.surveyWidget.set('survey', surveyRun.survey); |
---|
56 | this.surveyWidget.set('value', this.response.answers || {}); |
---|
57 | })); |
---|
58 | }, |
---|
59 | _enableSubmit: function() { |
---|
60 | this.submitButton.set('disabled',false); |
---|
61 | this.continueButton.set('disabled',false); |
---|
62 | this.surveyWidget.set('disabled', false); |
---|
63 | }, |
---|
64 | _disableSubmit: function() { |
---|
65 | this.submitButton.set('disabled',true); |
---|
66 | this.continueButton.set('disabled',true); |
---|
67 | this.surveyWidget.set('disabled', true); |
---|
68 | }, |
---|
69 | _showInfo: function(html) { |
---|
70 | this.infoNode.innerHTML = html; |
---|
71 | }, |
---|
72 | _getAnswersAndSaveResponse: function() { |
---|
73 | var answers = this.surveyWidget.get('value'); |
---|
74 | this.response.answers = answers; |
---|
75 | return store.put(this.response).then(function(){ |
---|
76 | Content.notify("Your response is saved."); |
---|
77 | }, function(err){ |
---|
78 | Content.notify(err,'error'); |
---|
79 | }); |
---|
80 | }, |
---|
81 | _onSubmit: function(e) { |
---|
82 | this.response.publicationDate = store.timestamp(); |
---|
83 | this._getAnswersAndSaveResponse() |
---|
84 | .then(function(){ |
---|
85 | this._showInfo("<div>Thanks for filling in the survey. You cannot edit your answers anymore.</div>"); |
---|
86 | this._disableSubmit(); |
---|
87 | }); |
---|
88 | if ( e ) { event.stop(e); } |
---|
89 | return false; |
---|
90 | }, |
---|
91 | _onContinueLater: function(e) { |
---|
92 | this._getAnswersAndSaveResponse() |
---|
93 | .then(lang.hitch(this,function(){ |
---|
94 | 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>"); |
---|
95 | })); |
---|
96 | if ( e ) { event.stop(e); } |
---|
97 | return false; |
---|
98 | }, |
---|
99 | _ignoreEvent: function(e) { |
---|
100 | if ( e ) { event.stop(e); } |
---|
101 | return false; |
---|
102 | } |
---|
103 | }); |
---|
104 | }); |
---|