1 | define([ |
---|
2 | 'dojo/_base/array', |
---|
3 | 'dojo/_base/declare', |
---|
4 | 'dojo/_base/Deferred', |
---|
5 | 'dojo/_base/event', |
---|
6 | 'dojo/_base/lang', |
---|
7 | '../store', |
---|
8 | '../app/Page', |
---|
9 | '../ui/model/QuestionWidgetFactory', |
---|
10 | 'dojo/text!./viewSurvey.html' |
---|
11 | ],function(array,declare,Deferred,event,lang,store,Page,QuestionWidgetFactory,template){ |
---|
12 | return declare([Page],{ |
---|
13 | templateString: template, |
---|
14 | survey: null, |
---|
15 | surveyId: "", |
---|
16 | options: null, |
---|
17 | constructor: function(){ |
---|
18 | this._dataMap = {}; |
---|
19 | this.options = this.options || {}; |
---|
20 | }, |
---|
21 | startup: function() { |
---|
22 | if ( this._started ) { return; } |
---|
23 | this.inherited(arguments); |
---|
24 | |
---|
25 | |
---|
26 | if ( this.surveyId ) { |
---|
27 | Deferred.when(store.get(this.surveyId)) |
---|
28 | .then(lang.hitch(this,function(survey){ |
---|
29 | if ( !survey.published ) { |
---|
30 | this.options.preview = true; |
---|
31 | } |
---|
32 | if ( this.options.preview ) { |
---|
33 | this.buttonsPane.destroyRecursive(); |
---|
34 | } |
---|
35 | this.titleNode.innerHTML = survey.title + |
---|
36 | (this.options.preview?' [preview]':''); |
---|
37 | var f = new QuestionWidgetFactory(); |
---|
38 | this.survey = survey; |
---|
39 | store.query(null,{keys:this.survey.questions,include_docs:true}) |
---|
40 | .forEach(function(question){ |
---|
41 | array.forEach(question.content || [],function(item){ |
---|
42 | var w = f.createViewWidget(item,{ |
---|
43 | name: question.code+'.'+item.code |
---|
44 | }); |
---|
45 | if ( w !== null ) { |
---|
46 | w.placeAt(this.questionsAnchor,'before'); |
---|
47 | } |
---|
48 | },this); |
---|
49 | },this); |
---|
50 | })); |
---|
51 | } else { |
---|
52 | throw new Error("No valid uid or survey passed!"); |
---|
53 | } |
---|
54 | }, |
---|
55 | _onSubmit: function(evt) { |
---|
56 | if ( this.options.preview ) { return; } |
---|
57 | var value = this.questionsForm.get('value'); |
---|
58 | this.questionsPane.set('content','<pre>'+JSON.stringify(value)+'</pre>'); |
---|
59 | event.stop(evt); |
---|
60 | return false; |
---|
61 | }, |
---|
62 | _onCancel: function(evt) { |
---|
63 | if ( this.options.preview ) { return; } |
---|
64 | event.stop(evt); |
---|
65 | return false; |
---|
66 | } |
---|
67 | }); |
---|
68 | }); |
---|