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 = {}; |
---|
50 | if ( !response.publicationDate ) { |
---|
51 | actions.Delete = { |
---|
52 | callback: function(){ |
---|
53 | responses.remove(response) |
---|
54 | .then(function(){ |
---|
55 | w.destroy(); |
---|
56 | }); |
---|
57 | }, |
---|
58 | properties: { |
---|
59 | icon: 'Delete', |
---|
60 | title: "Delete response" |
---|
61 | } |
---|
62 | }; |
---|
63 | } |
---|
64 | var w = new LineWithActionsWidget({ |
---|
65 | actions: actions |
---|
66 | }); |
---|
67 | var rid = responses.getId(response); |
---|
68 | w.set('title',this._link(this._buildResponseURL(response),rid),rid); |
---|
69 | w.placeAt(this.responsesNode); |
---|
70 | }, this); |
---|
71 | })); |
---|
72 | }, |
---|
73 | _handlePropChange: function(e) { |
---|
74 | this._updateObject(); |
---|
75 | this.markDirty(); |
---|
76 | this._refresh(); |
---|
77 | }, |
---|
78 | _buildGeneralURL: function(surveyRun) { |
---|
79 | return 'response.html#'+Path.format(surveyRuns.getObjectPath(surveyRun),{secret:surveyRun.secret}); |
---|
80 | }, |
---|
81 | _buildResponseURL: function(response) { |
---|
82 | return 'response.html#'+Path.format(responses.getObjectPath(response),{secret:response.secret}); |
---|
83 | }, |
---|
84 | _link: function(url,label) { |
---|
85 | return '<a target="_blank" href="'+url+'">'+(label || url)+'</a>'; |
---|
86 | }, |
---|
87 | _save: function() { |
---|
88 | if ( this._updateObject() ) { |
---|
89 | return this.inherited(arguments); |
---|
90 | } else { |
---|
91 | return new Deferred().reject({error:"Please correct invalid values."}); |
---|
92 | } |
---|
93 | }, |
---|
94 | _updateObject: function() { |
---|
95 | this._isValid = this.surveyRunWidget.validate(); |
---|
96 | lang.mixin(this.object,this.surveyRunWidget.get('value')); |
---|
97 | return this._isValid; |
---|
98 | }, |
---|
99 | _onSave: function(evt) { |
---|
100 | this._save(); |
---|
101 | if ( evt ) { event.stop(evt); } |
---|
102 | return false; |
---|
103 | }, |
---|
104 | _onSaveAndClose: function(evt) { |
---|
105 | this._save() |
---|
106 | .then(function() { |
---|
107 | Router.go(surveys.getCollectionPath()); |
---|
108 | }); |
---|
109 | if ( evt ) { event.stop(evt); } |
---|
110 | return false; |
---|
111 | }, |
---|
112 | _onDiscard: function(evt) { |
---|
113 | this.markClean(); |
---|
114 | Router.go(surveys.getCollectionPath()); |
---|
115 | if ( evt ) { event.stop(evt); } |
---|
116 | return false; |
---|
117 | }, |
---|
118 | markDirty: function() { |
---|
119 | this.saveBtn.set('disabled',!this._isValid); |
---|
120 | this.saveAndCloseBtn.set('disabled',!this._isValid); |
---|
121 | this.discardBtn.set('label','Discard & Close'); |
---|
122 | this.inherited(arguments); |
---|
123 | }, |
---|
124 | markClean: function() { |
---|
125 | this.saveBtn.set('disabled',true); |
---|
126 | this.saveAndCloseBtn.set('disabled',true); |
---|
127 | this.discardBtn.set('label','Close'); |
---|
128 | this.inherited(arguments); |
---|
129 | } |
---|
130 | }); |
---|
131 | }); |
---|
132 | |
---|