1 | define([ |
---|
2 | "../app/Content", |
---|
3 | "../app/Path", |
---|
4 | "../app/Router", |
---|
5 | "../model/classes/responses", |
---|
6 | "../model/classes/surveyRuns", |
---|
7 | "../widgets/LineWithActionsWidget", |
---|
8 | "./_ObjectPage", |
---|
9 | "dojo/Deferred", |
---|
10 | "dojo/_base/array", |
---|
11 | "dojo/_base/declare", |
---|
12 | "dojo/_base/event", |
---|
13 | "dojo/_base/lang", |
---|
14 | "dojo/when", |
---|
15 | "require", |
---|
16 | "dojo/text!./templates/surveyRun.html" |
---|
17 | ], function(Content, Path, Router, responses, surveyRuns, LineWithActionsWidget, _ObjectPage, Deferred, array, declare, event, lang, when, require, template) { |
---|
18 | return declare([_ObjectPage],{ |
---|
19 | contextRequire: require, |
---|
20 | templateString: template, |
---|
21 | classStore: surveyRuns, |
---|
22 | _isValid: false, |
---|
23 | startup: function() { |
---|
24 | if ( this._started ) { return; } |
---|
25 | this.inherited(arguments); |
---|
26 | this.own(this.surveyRunWidget.on("change",lang.hitch(this,'_handlePropChange'))); |
---|
27 | this._load(); |
---|
28 | }, |
---|
29 | _refresh: function(initial) { |
---|
30 | if ( initial === true ) { |
---|
31 | this.surveySummaryWidget.set('value',this.object.survey,null); |
---|
32 | this.surveyRunWidget.set('value',this.object,null); |
---|
33 | this._loadResponses(); |
---|
34 | } |
---|
35 | this.titleNode.innerHTML = this.object.title || ""; |
---|
36 | if ( this.object.mode === "open" ) { |
---|
37 | this.runURLNode.innerHTML = |
---|
38 | this._link(this._buildGeneralURL(this.object)); |
---|
39 | } else { |
---|
40 | this.runURLNode.innerHTML = |
---|
41 | "No general URL. Add individual respondents below."; |
---|
42 | } |
---|
43 | }, |
---|
44 | _loadResponses: function() { |
---|
45 | responses.query({surveyRunId:surveyRuns.getId(this.object)}) |
---|
46 | .then(lang.hitch(this,function(allResponses){ |
---|
47 | array.forEach(allResponses, function(response){ |
---|
48 | var actions = {}, w; |
---|
49 | if ( !response.publicationDate ) { |
---|
50 | actions.Delete = { |
---|
51 | callback: lang.hitch(this,function(){ |
---|
52 | // We cannot bind _onDeleteResponse |
---|
53 | // directly, because of the |
---|
54 | // initialization problem with w. We |
---|
55 | // need it in the handler, but we need |
---|
56 | // to pass the handler as an argument |
---|
57 | // on the creation of w. |
---|
58 | this._onDeleteResponse(response,w); |
---|
59 | }), |
---|
60 | properties: { |
---|
61 | icon: 'Delete', |
---|
62 | title: "Delete response" |
---|
63 | } |
---|
64 | }; |
---|
65 | } |
---|
66 | w = new LineWithActionsWidget({ |
---|
67 | actions: actions |
---|
68 | }); |
---|
69 | var rid = responses.getId(response); |
---|
70 | w.set('title',this._link(this._buildResponseURL(response),rid),rid); |
---|
71 | w.placeAt(this.responsesNode); |
---|
72 | }, this); |
---|
73 | })); |
---|
74 | }, |
---|
75 | _onDeleteResponse: function(response,w) { |
---|
76 | if ( !confirm("Are you sure you want to delete this survey response?") ) { |
---|
77 | return; |
---|
78 | } |
---|
79 | responses.remove(response) |
---|
80 | .then(function(){ |
---|
81 | w.destroy(); |
---|
82 | }); |
---|
83 | }, |
---|
84 | _handlePropChange: function(e) { |
---|
85 | this._updateObject(); |
---|
86 | this.markDirty(); |
---|
87 | this._refresh(); |
---|
88 | }, |
---|
89 | _buildGeneralURL: function(surveyRun) { |
---|
90 | return 'response.html#'+Path.format(surveyRuns.getObjectPath(surveyRun),{secret:surveyRun.secret}); |
---|
91 | }, |
---|
92 | _buildResponseURL: function(response) { |
---|
93 | return 'response.html#'+Path.format(responses.getObjectPath(response),{secret:response.secret}); |
---|
94 | }, |
---|
95 | _link: function(url,label) { |
---|
96 | return '<a target="_blank" href="'+url+'">'+(label || url)+'</a>'; |
---|
97 | }, |
---|
98 | _save: function() { |
---|
99 | if ( this._updateObject() ) { |
---|
100 | return this.inherited(arguments); |
---|
101 | } else { |
---|
102 | return new Deferred().reject({error:"Please correct invalid values."}); |
---|
103 | } |
---|
104 | }, |
---|
105 | _updateObject: function() { |
---|
106 | this._isValid = this.surveyRunWidget.validate(); |
---|
107 | lang.mixin(this.object,this.surveyRunWidget.get('value')); |
---|
108 | return this._isValid; |
---|
109 | }, |
---|
110 | _onSave: function(evt) { |
---|
111 | this._save(); |
---|
112 | if ( evt ) { event.stop(evt); } |
---|
113 | return false; |
---|
114 | }, |
---|
115 | _onSaveAndClose: function(evt) { |
---|
116 | this._save() |
---|
117 | .then(function() { |
---|
118 | Router.go(surveyRuns.getCollectionPath()); |
---|
119 | }); |
---|
120 | if ( evt ) { event.stop(evt); } |
---|
121 | return false; |
---|
122 | }, |
---|
123 | _onDiscard: function(evt) { |
---|
124 | this.markClean(); |
---|
125 | Router.go(surveyRuns.getCollectionPath()); |
---|
126 | if ( evt ) { event.stop(evt); } |
---|
127 | return false; |
---|
128 | }, |
---|
129 | markDirty: function() { |
---|
130 | this.saveBtn.set('disabled',!this._isValid); |
---|
131 | this.saveAndCloseBtn.set('disabled',!this._isValid); |
---|
132 | this.discardBtn.set('label','Discard & Close'); |
---|
133 | this.inherited(arguments); |
---|
134 | }, |
---|
135 | markClean: function() { |
---|
136 | this.saveBtn.set('disabled',true); |
---|
137 | this.saveAndCloseBtn.set('disabled',true); |
---|
138 | this.discardBtn.set('label','Close'); |
---|
139 | this.inherited(arguments); |
---|
140 | } |
---|
141 | }); |
---|
142 | }); |
---|
143 | |
---|