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