define([ "../app/Content", "../app/Page", "../app/Router", "../lib/func", "../model/classes/SurveyRun", "../store", "../widgets/LineWithActionsWidget", "dojo/_base/declare", "dojo/_base/event", "dojo/_base/lang", "dojo/when", "require", "dojo/text!./templates/surveyRun.html" ], function(Content, Page, Router, func, SurveyRun, store, LineWithActionsWidget, declare, event, lang, when, require, template) { return declare([Page],{ contextRequire: require, templateString: template, surveyRun: null, startup: function() { if ( this._started ) { return; } this.inherited(arguments); this.propertiesForm.on("blur",lang.hitch(this,'_onPropChange')); if ( this.surveyRunId ) { this._loadSurveyRun(); this._loadResponses(); } else { throw "No valid uid or survey passed!"; } }, _loadSurveyRun: function() { when(store.get(this.surveyRunId)) .then(lang.hitch(this,function(surveyRun){ this.surveyRun = surveyRun; this.refreshSurveyRun(); })); }, refreshSurveyRun: function() { this.titleNode.innerHTML = SurveyRun.DisplayTitle.get(this.surveyRun); this.surveyNode.set('value',SurveyRun.Survey.get(this.surveyRun)); this.propertiesForm.set('value',this.surveyRun); this._onPropChange(); }, _loadResponses: function() { when(store.query("_design/responses/_view/by_surveyrun",{key:this.surveyRunId})) .forEach(lang.hitch(this,function(response){ var actions = { view: { callback: function(){}, properties: { title: "View response" } } }; if ( !response.publicationDate ) { actions.remove = { callback: function(){}, properties: { title: "Remove response" } }; } var w = new LineWithActionsWidget({ actions: actions }); var responseId = store.getIdentity(response); w.set('title',this._link(this._getResponseURL(this.surveyRunId,responseId),responseId)); w.placeAt(this.responsesNode); })); }, _onPropChange: function(e) { var surveyRun = this.propertiesForm.get('value'); if ( surveyRun.mode === "open" ) { this.runURLNode.innerHTML = this._link(this._getGeneralURL(store.getIdentity(this.surveyRun))); } else { this.runURLNode.innerHTML = "No general URL. Add individual respondents below."; } }, _getGeneralURL: function(surveyRunId) { return 'response.html#!/'+surveyRunId; }, _getResponseURL: function(surveyRunId,responseId) { return 'response.html#!/'+surveyRunId+'!id='+responseId; }, _link: function(url,label) { return ''+(label || url)+''; }, _onSave: function(evt) { lang.mixin(this.surveyRun,this.propertiesForm.get('value')); var not = function(p){ return !p; }; func.modPropIf(this.surveyRun,"startDate",not.compose(lang.isString),store.formatDate); func.modPropIf(this.surveyRun,"endDate",not.compose(lang.isString),store.formatDate); store.put(this.surveyRun) .then(function() { Router.go('/surveys'); },function(err){ Content.notify(err); }); event.stop(evt); return false; }, _onDiscard: function(evt) { Router.go('/surveys'); } }); });