define([ 'dojo/_base/array', 'dojo/_base/declare', 'dojo/_base/lang', 'dojo/when', '../store', '../app/Content', '../app/Page', '../app/Router', '../model/classes/Survey', '../model/classes/SurveyRun', '../widgets/LineWithActionsWidget', 'dojo/text!./templates/surveys.html' ],function(array,declare,lang,when,store,Content,Page,Router,Survey,SurveyRun,LineWithActionsWidget,template){ return declare([Page],{ templateString: template, startup: function() { if ( this._started ) { return; } this.inherited(arguments); this.refresh(); }, _onNewSurvey: function(){ Router.go('/survey/new'); }, _onPublishSurvey:function(survey){ var self = this; survey.publicationDate = store.timestamp(); store.put(survey).then(function(){ self.refreshDrafts(); self.refreshPublished(); },function(err){ Content.notify(err,'error'); }); }, _onDeleteSurvey:function(survey){ var self = this; store.remove(store.getIdentity(survey),store.getRevision(survey)) .then(function(){ self.refreshDrafts(); },function(err){ Content.notify(err,'error'); }); }, _onEditSurvey:function(survey){ Router.go('/survey/'+store.getIdentity(survey)); }, _onPreviewSurvey:function(survey){ Router.go('/previewSurvey/'+store.getIdentity(survey)); }, _onRunSurvey:function(survey){ var surveyRun = SurveyRun.create(); SurveyRun.Survey.set(surveyRun,survey); store.put(surveyRun) .then(lang.hitch(this,function(surveyRun){ this._onRunDetails(surveyRun); }),function(err){ Content.notify(err); }); }, _onRunDetails: function(surveyRun) { Router.go('/surveyRun/'+store.getIdentity(surveyRun)); }, refresh: function() { this.refreshDrafts(); this.refreshPublished(); this.refreshRuns(); }, refreshDrafts: function() { this.draftsContainer.set('content',''); when(store.query("_design/surveys/_view/drafts") ,lang.hitch(this,function(surveys){ this.draftsTab.set('title','Drafts ('+surveys.length+')'); array.forEach(surveys,function(survey){ var w = new LineWithActionsWidget({ title: Survey.DisplayTitle.get(survey) || '(unnamed)', actions: [{ callback: lang.hitch(this,'_onPublishSurvey',survey), properties: { label: 'Publish', tooltip: 'Publish survey', icon: 'Publish' } },{ callback: lang.hitch(this,'_onPreviewSurvey',survey), properties: { label: 'Preview', tooltip: 'Preview survey', icon: 'Preview' } },{ callback: lang.hitch(this,'_onDeleteSurvey',survey), properties: { label: 'Delete', tooltip: 'Delete survey', icon: 'Delete' } },{ callback: lang.hitch(this,'_onEditSurvey',survey), properties: { label: 'Edit', tooltip: 'Edit survey', icon: 'Edit' } }] }); this.draftsContainer.addChild(w); },this); })); }, refreshPublished: function() { this.publishedContainer.set('content',''); when(store.query("_design/surveys/_view/published") ,lang.hitch(this,function(surveys){ this.publishedTab.set('title','Published ('+surveys.length+')'); array.forEach(surveys,function(survey){ var w = new LineWithActionsWidget({ title: Survey.DisplayTitle.get(survey), actions:[{ callback: lang.hitch(this,'_onPreviewSurvey',survey), properties: { label: 'Preview', tooltip: 'Preview survey', icon: 'Preview' } },{ callback: lang.hitch(this,'_onRunSurvey',survey), properties: { label: 'Run', tooltip: 'Run survey', icon: 'Run' } }] }); this.publishedContainer.addChild(w); },this); })); }, refreshRuns: function() { this.runsContainer.set('content',''); when(store.query("_design/default/_view/by_type",{key:'SurveyRun'}) ,lang.hitch(this,function(surveyRuns){ this.runsTab.set('title','Runs ('+surveyRuns.length+')'); array.forEach(surveyRuns,function(surveyRun){ var w = new LineWithActionsWidget({ title: SurveyRun.DisplayTitle.get(surveyRun), actions:[{ callback: lang.hitch(this,'_onRunDetails',surveyRun), properties: { label: 'Details', tooltip: 'Show details for this run', icon: 'Details' } }] }); this.runsContainer.addChild(w); },this); })); } }); });