1 | define([ |
---|
2 | "../app/Content", |
---|
3 | "../app/Page", |
---|
4 | "../app/Router", |
---|
5 | "../lib/func", |
---|
6 | "../model/classes/responses", |
---|
7 | "../model/classes/surveyRuns", |
---|
8 | "../widgets/LineWithActionsWidget", |
---|
9 | "dojo/_base/array", |
---|
10 | "dojo/_base/declare", |
---|
11 | "dojo/_base/event", |
---|
12 | "dojo/_base/lang", |
---|
13 | "dojo/when", |
---|
14 | "require", |
---|
15 | "dojo/text!./templates/surveyRun.html" |
---|
16 | ], function(Content, Page, Router, func, responses, surveyRuns, LineWithActionsWidget, array, declare, event, lang, when, require, template) { |
---|
17 | return declare([Page],{ |
---|
18 | contextRequire: require, |
---|
19 | templateString: template, |
---|
20 | surveyRun: null, |
---|
21 | startup: function() { |
---|
22 | if ( this._started ) { return; } |
---|
23 | this.inherited(arguments); |
---|
24 | this.surveyRunWidget.on("blur",lang.hitch(this,'_onPropChange')); |
---|
25 | if ( this.surveyRunId ) { |
---|
26 | this._loadSurveyRun(); |
---|
27 | } else { |
---|
28 | throw "No valid uid or survey passed!"; |
---|
29 | } |
---|
30 | }, |
---|
31 | _loadSurveyRun: function() { |
---|
32 | when(surveyRuns.load(this.surveyRunId)) |
---|
33 | .then(lang.hitch(this,function(surveyRun){ |
---|
34 | this.surveyRun = surveyRun; |
---|
35 | this.refreshSurveyRun(); |
---|
36 | this._loadResponses(); |
---|
37 | })); |
---|
38 | }, |
---|
39 | refreshSurveyRun: function() { |
---|
40 | this.titleNode.innerHTML = this.surveyRun.title || ""; |
---|
41 | this.surveySummaryWidget.set('value',this.surveyRun.survey); |
---|
42 | this.surveyRunWidget.set('value',this.surveyRun); |
---|
43 | this._onPropChange(); |
---|
44 | }, |
---|
45 | _loadResponses: function() { |
---|
46 | responses.query({surveyRunId:surveyRuns.getId(this.surveyRun)}) |
---|
47 | .then(lang.hitch(this,function(allResponses){ |
---|
48 | array.forEach(allResponses, function(response){ |
---|
49 | var actions = { |
---|
50 | view: { |
---|
51 | callback: function(){}, |
---|
52 | properties: { |
---|
53 | title: "View response" |
---|
54 | } |
---|
55 | } |
---|
56 | }; |
---|
57 | if ( !response.publicationDate ) { |
---|
58 | actions.remove = { |
---|
59 | callback: function(){}, |
---|
60 | properties: { |
---|
61 | title: "Remove response" |
---|
62 | } |
---|
63 | }; |
---|
64 | } |
---|
65 | var w = new LineWithActionsWidget({ |
---|
66 | actions: actions |
---|
67 | }); |
---|
68 | var rid = responses.getId(response); |
---|
69 | w.set('title',this._link(this._buildResponseURL(response),rid),rid); |
---|
70 | w.placeAt(this.responsesNode); |
---|
71 | }, this); |
---|
72 | })); |
---|
73 | }, |
---|
74 | _onPropChange: function(e) { |
---|
75 | var surveyRun = this.surveyRunWidget.get('value'); |
---|
76 | if ( surveyRun.mode === "open" ) { |
---|
77 | this.runURLNode.innerHTML = |
---|
78 | this._link(this._buildGeneralURL(this.surveyRun)); |
---|
79 | } else { |
---|
80 | this.runURLNode.innerHTML = |
---|
81 | "No general URL. Add individual respondents below."; |
---|
82 | } |
---|
83 | }, |
---|
84 | _buildGeneralURL: function(surveyRun) { |
---|
85 | return 'response.html#!/surveyRuns/'+surveyRuns.getId(surveyRun)+'!secret='+surveyRun.secret; |
---|
86 | }, |
---|
87 | _buildResponseURL: function(response) { |
---|
88 | return 'response.html#!/responses/'+responses.getId(response)+'!secret='+response.secret; |
---|
89 | }, |
---|
90 | _link: function(url,label) { |
---|
91 | return '<a target="_blank" href="'+url+'">'+(label || url)+'</a>'; |
---|
92 | }, |
---|
93 | _onSave: function(evt) { |
---|
94 | if ( this.surveyRunWidget.validate() ) { |
---|
95 | lang.mixin(this.surveyRun,this.surveyRunWidget.get('value')); |
---|
96 | |
---|
97 | surveyRuns.save(this.surveyRun) |
---|
98 | .then(function() { |
---|
99 | Router.go('/surveys'); |
---|
100 | },function(err){ |
---|
101 | Content.notify(err); |
---|
102 | }); |
---|
103 | } |
---|
104 | if ( evt ) { event.stop(evt); } |
---|
105 | return false; |
---|
106 | }, |
---|
107 | _onDiscard: function(evt) { |
---|
108 | Router.go('/surveys'); |
---|
109 | if ( evt ) { event.stop(evt); } |
---|
110 | return false; |
---|
111 | } |
---|
112 | }); |
---|
113 | }); |
---|
114 | |
---|