1 | define([ |
---|
2 | 'dojo/_base/array', |
---|
3 | 'dojo/_base/declare', |
---|
4 | 'dojo/_base/Deferred', |
---|
5 | 'dojo/_base/event', |
---|
6 | 'dojo/_base/lang', |
---|
7 | '../app/Controller', |
---|
8 | '../store', |
---|
9 | '../app/Page', |
---|
10 | '../ui/lists/QuestionListView', |
---|
11 | '../ui/TabbedQuestionBrowser', |
---|
12 | 'dojo/text!./survey.html' |
---|
13 | ],function(array,declare,Deferred,event,lang,Controller,store,Page, |
---|
14 | QuestionListView,TabbedQuestionBrowser,template){ |
---|
15 | return declare([Page],{ |
---|
16 | templateString: template, |
---|
17 | survey: null, |
---|
18 | questionList: null, |
---|
19 | _dataMap: null, |
---|
20 | constructor: function(){ |
---|
21 | this._dataMap = {}; |
---|
22 | }, |
---|
23 | startup: function() { |
---|
24 | if ( this._started ) { return; } |
---|
25 | this.inherited(arguments); |
---|
26 | if ( this.surveyId ) { |
---|
27 | this._setupQuestionBrowser(); |
---|
28 | this._setupListView(); |
---|
29 | Deferred.when(store.get(this.surveyId)) |
---|
30 | .then(lang.hitch(this,function(obj){ |
---|
31 | this.survey = obj; |
---|
32 | store.query(null,{keys:this.survey.questions,include_docs:true}) |
---|
33 | .forEach(lang.hitch(this.questionList,'appendItem')); |
---|
34 | this.refresh(); |
---|
35 | })); |
---|
36 | } else { |
---|
37 | throw "No valid uid or survey passed!"; |
---|
38 | } |
---|
39 | }, |
---|
40 | _setupQuestionBrowser: function() { |
---|
41 | this.questionBrowser = new TabbedQuestionBrowser({ |
---|
42 | region: 'center', |
---|
43 | 'class': 'blue', |
---|
44 | selectedActions: { |
---|
45 | "Include": { |
---|
46 | callback: lang.hitch(this,this._includeQuestion), |
---|
47 | icon: "Accept", |
---|
48 | description: "Include in survey" |
---|
49 | } |
---|
50 | }, |
---|
51 | itemActions: { |
---|
52 | "Info": { |
---|
53 | callback: function(item){ item.description && alert(item.description); }, |
---|
54 | icon: "Inspect", |
---|
55 | description: "Show item description" |
---|
56 | } |
---|
57 | } |
---|
58 | },this.questionBrowser); |
---|
59 | this.questionBrowser.startup(); |
---|
60 | }, |
---|
61 | _includeQuestion: function(question) { |
---|
62 | this.questionList.insertItem(question); |
---|
63 | }, |
---|
64 | _setupListView: function() { |
---|
65 | this.questionList = new QuestionListView({ |
---|
66 | region: 'center' |
---|
67 | },this.surveyListViewNode); |
---|
68 | this.questionList.startup(); |
---|
69 | }, |
---|
70 | refresh: function() { |
---|
71 | this.titleNode.innerHTML = this.survey.title || "(set title in properties)"; |
---|
72 | this.propertiesForm.set('value',this.survey); |
---|
73 | }, |
---|
74 | _onShowProperties: function(evt) { |
---|
75 | this.propertiesDialog.show(); |
---|
76 | }, |
---|
77 | _onPropertiesOk: function(evt) { |
---|
78 | this.propertiesDialog.hide(); |
---|
79 | lang.mixin(this.survey, this.propertiesForm.get('value')); |
---|
80 | this.refresh(); |
---|
81 | event.stop(evt); |
---|
82 | return false; |
---|
83 | }, |
---|
84 | _onPropertiesCancel: function(evt) { |
---|
85 | this.propertiesDialog.hide(); |
---|
86 | this.propertiesForm.set('value',this.survey); |
---|
87 | event.stop(evt); |
---|
88 | return false; |
---|
89 | }, |
---|
90 | _onSave: function(evt) { |
---|
91 | this.survey.questions = array.map(this.questionList.getItems(),function(item){ |
---|
92 | return store.getIdentity(item); |
---|
93 | }); |
---|
94 | store.put(this.survey) |
---|
95 | .then(function() { |
---|
96 | Controller.go('/surveys'); |
---|
97 | }); |
---|
98 | event.stop(evt); |
---|
99 | return false; |
---|
100 | }, |
---|
101 | _onDiscard: function(evt) { |
---|
102 | }, |
---|
103 | _onShowPreview: function() { |
---|
104 | Controller.go('/viewSurvey/'+store.getIdentity(this.survey)); |
---|
105 | } |
---|
106 | }); |
---|
107 | }); |
---|
108 | |
---|