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