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