1 | define([ |
---|
2 | "../app/Content", |
---|
3 | "../app/Path", |
---|
4 | "../app/Router", |
---|
5 | "../lib/func", |
---|
6 | "../model/classes/responses", |
---|
7 | "../model/classes/surveyRuns", |
---|
8 | "../model/classes/surveys", |
---|
9 | "../widgets/LineWithActionsWidget", |
---|
10 | "./_ObjectPage", |
---|
11 | "dojo/Deferred", |
---|
12 | "dojo/_base/array", |
---|
13 | "dojo/_base/declare", |
---|
14 | "dojo/_base/event", |
---|
15 | "dojo/_base/lang", |
---|
16 | "dojo/when", |
---|
17 | "require", |
---|
18 | "dojo/text!./templates/surveyRun.html" |
---|
19 | ], function(Content, Path, Router, func, responses, surveyRuns, surveys, LineWithActionsWidget, _ObjectPage, Deferred, array, declare, event, lang, when, require, template) { |
---|
20 | return declare([_ObjectPage],{ |
---|
21 | contextRequire: require, |
---|
22 | templateString: template, |
---|
23 | classStore: surveyRuns, |
---|
24 | startup: function() { |
---|
25 | if ( this._started ) { return; } |
---|
26 | this.inherited(arguments); |
---|
27 | this.surveyRunWidget.on("change",lang.hitch(this,'_onPropChange')); |
---|
28 | this._load(); |
---|
29 | }, |
---|
30 | _refresh: function() { |
---|
31 | this.titleNode.innerHTML = this.object.title || ""; |
---|
32 | this.surveySummaryWidget.set('value',this.object.survey); |
---|
33 | this.surveyRunWidget.set('value',this.object); |
---|
34 | this._refreshURL(); |
---|
35 | this._loadResponses(); |
---|
36 | }, |
---|
37 | _refreshURL: function() { |
---|
38 | if ( this.object.mode === "open" ) { |
---|
39 | this.runURLNode.innerHTML = |
---|
40 | this._link(this._buildGeneralURL(this.object)); |
---|
41 | } else { |
---|
42 | this.runURLNode.innerHTML = |
---|
43 | "No general URL. Add individual respondents below."; |
---|
44 | } |
---|
45 | }, |
---|
46 | _loadResponses: function() { |
---|
47 | responses.query({surveyRunId:surveyRuns.getId(this.object)}) |
---|
48 | .then(lang.hitch(this,function(allResponses){ |
---|
49 | array.forEach(allResponses, function(response){ |
---|
50 | var actions = { |
---|
51 | view: { |
---|
52 | callback: function(){}, |
---|
53 | properties: { |
---|
54 | title: "View response" |
---|
55 | } |
---|
56 | } |
---|
57 | }; |
---|
58 | if ( !response.publicationDate ) { |
---|
59 | actions.remove = { |
---|
60 | callback: function(){}, |
---|
61 | properties: { |
---|
62 | title: "Remove response" |
---|
63 | } |
---|
64 | }; |
---|
65 | } |
---|
66 | var 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 | _onPropChange: function(e) { |
---|
76 | if ( this.surveyRunWidget.validate() ) { |
---|
77 | lang.mixin(this.object,this.surveyRunWidget.get('value')); |
---|
78 | this._refreshURL(); |
---|
79 | } |
---|
80 | this.markDirty(); |
---|
81 | }, |
---|
82 | _buildGeneralURL: function(surveyRun) { |
---|
83 | return 'response.html#'+Path.format(surveyRuns.getObjectPath(surveyRun),{secret:surveyRun.secret}); |
---|
84 | }, |
---|
85 | _buildResponseURL: function(response) { |
---|
86 | return 'response.html#'+Path.format(responses.getObjectPath(response),{secret:response.secret}); |
---|
87 | }, |
---|
88 | _link: function(url,label) { |
---|
89 | return '<a target="_blank" href="'+url+'">'+(label || url)+'</a>'; |
---|
90 | }, |
---|
91 | _save: function() { |
---|
92 | if ( this.surveyRunWidget.validate() ) { |
---|
93 | lang.mixin(this.object,this.surveyRunWidget.get('value')); |
---|
94 | return this.inherited(arguments); |
---|
95 | } else { |
---|
96 | return new Deferred.reject(); |
---|
97 | } |
---|
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 | }); |
---|
119 | }); |
---|
120 | |
---|