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