define([ "../app/Content", "../app/Path", "../app/Router", "../model/classes/responses", "../model/classes/surveyRuns", "../widgets/LineWithActionsWidget", "./_ObjectPage", "dojo/Deferred", "dojo/_base/array", "dojo/_base/declare", "dojo/_base/event", "dojo/_base/lang", "dojo/when", "require", "dojo/text!./templates/surveyRun.html" ], function(Content, Path, Router, responses, surveyRuns, LineWithActionsWidget, _ObjectPage, Deferred, array, declare, event, lang, when, require, template) { return declare([_ObjectPage],{ contextRequire: require, templateString: template, classStore: surveyRuns, _isValid: false, startup: function() { if ( this._started ) { return; } this.inherited(arguments); this.own(this.surveyRunWidget.on("change",lang.hitch(this,'_handlePropChange'))); this._load(); }, _refresh: function(initial) { if ( initial === true ) { this.surveySummaryWidget.set('value',this.object.survey,null); this.surveyRunWidget.set('value',this.object,null); this._loadResponses(); } this.titleNode.innerHTML = this.object.title || ""; if ( this.object.mode === "open" ) { this.runURLNode.innerHTML = this._link(this._buildGeneralURL(this.object)); } else { this.runURLNode.innerHTML = "No general URL. Add individual respondents below."; } }, _loadResponses: function() { responses.query({surveyRunId:surveyRuns.getId(this.object)}) .then(lang.hitch(this,function(allResponses){ array.forEach(allResponses, function(response){ var actions = {}, w; if ( !response.publicationDate ) { actions.Delete = { callback: lang.hitch(this,function(){ // We cannot bind _onDeleteResponse // directly, because of the // initialization problem with w. We // need it in the handler, but we need // to pass the handler as an argument // on the creation of w. this._onDeleteResponse(response,w); }), properties: { icon: 'Delete', title: "Delete response" } }; } w = new LineWithActionsWidget({ actions: actions }); var rid = responses.getId(response); w.set('title',this._link(this._buildResponseURL(response),rid),rid); w.placeAt(this.responsesNode); }, this); })); }, _onDeleteResponse: function(response,w) { if ( !confirm("Are you sure you want to delete this survey response?") ) { return; } responses.remove(response) .then(function(){ w.destroy(); }); }, _handlePropChange: function(e) { this._updateObject(); this.markDirty(); this._refresh(); }, _buildGeneralURL: function(surveyRun) { return 'response.html#'+Path.format(surveyRuns.getObjectPath(surveyRun),{secret:surveyRun.secret}); }, _buildResponseURL: function(response) { return 'response.html#'+Path.format(responses.getObjectPath(response),{secret:response.secret}); }, _link: function(url,label) { return ''+(label || url)+''; }, _save: function() { if ( this._updateObject() ) { return this.inherited(arguments); } else { return new Deferred().reject({error:"Please correct invalid values."}); } }, _updateObject: function() { this._isValid = this.surveyRunWidget.validate(); lang.mixin(this.object,this.surveyRunWidget.get('value')); return this._isValid; }, _onSave: function(evt) { this._save(); if ( evt ) { event.stop(evt); } return false; }, _onSaveAndClose: function(evt) { this._save() .then(function() { Router.go(surveyRuns.getCollectionPath()); }); if ( evt ) { event.stop(evt); } return false; }, _onDiscard: function(evt) { this.markClean(); Router.go(surveyRuns.getCollectionPath()); if ( evt ) { event.stop(evt); } return false; }, markDirty: function() { this.saveBtn.set('disabled',!this._isValid); this.saveAndCloseBtn.set('disabled',!this._isValid); this.discardBtn.set('label','Discard & Close'); this.inherited(arguments); }, markClean: function() { this.saveBtn.set('disabled',true); this.saveAndCloseBtn.set('disabled',true); this.discardBtn.set('label','Close'); this.inherited(arguments); } }); });