define([ "../app/Page", "../app/Router", "../model/classes/surveyRuns", "../widgets/LineWithActionsWidget", "dojo/_base/array", "dojo/_base/declare", "dojo/_base/lang", "dojo/when", "dojo/text!./templates/surveyRuns.html" ], function(Page, Router, surveyRuns, LineWithActionsWidget, array, declare, lang, when, template) { return declare([Page],{ templateString: template, startup: function() { if ( this._started ) { return; } this.inherited(arguments); this.currentTab.set('title','Current ()'); this.pastTab.set('title','Past ()'); this.futureTab.set('title','Future ()'); this.refresh(); }, _onRunDetails: function(surveyRun) { Router.go(surveyRuns.getObjectPath(surveyRun)); }, _onRunDelete: function(surveyRun) { if ( !confirm("Are you sure you want to delete this survey run?") ) { return; } surveyRuns.remove(surveyRun) .then(lang.hitch(this,function(){ this.notify("SurveyRun successfully deleted."); this.refreshRuns(); }),lang.hitch(this,function(err){ this.notify(err.error,'error'); })); }, refresh: function() { this.refreshGroup('current','Current'); this.refreshGroup('past','Past'); this.refreshGroup('future','Future'); }, refreshGroup: function(sub,subname) { var tab = this[sub+'Tab']; var container = this[sub+'Container']; container.set('content','Loading survey runs.'); var qs = {}; qs[sub] = true; when(surveyRuns.query(qs), lang.hitch(this,function(surveyRuns){ container.set('content',''); tab.set('title',subname+' ('+surveyRuns.length+')'); array.forEach(surveyRuns,function(surveyRun){ var w = new LineWithActionsWidget({ title: surveyRun.title || "", actions:[{ callback: lang.hitch(this,'_onRunDetails', surveyRun), properties: { label: 'Details', tooltip: 'Show details for this run', icon: 'Edit' } },{ callback: lang.hitch(this,'_onRunDelete', surveyRun), properties: { label: 'Delete', tooltip: 'Delete this run', icon: 'Delete' } }] }); container.addChild(w); },this); })); } }); });