define([ "../app/Page", "../app/Router", "../model/classes/surveyRuns", "../model/classes/surveys", "../widgets/LineWithActionsWidget", "dojo/_base/array", "dojo/_base/declare", "dojo/_base/lang", "dojo/when", "dojo/text!./templates/surveys.html" ], function(Page, Router, surveyRuns, surveys, LineWithActionsWidget, array, declare, lang, when, template) { return declare([Page],{ templateString: template, startup: function() { if ( this._started ) { return; } this.inherited(arguments); this.draftsTab.set('title','Drafts ()'); this.publishedTab.set('title','Published ()'); this.refresh(); }, _onNewSurvey: function(){ Router.go(surveys.getObjectPath('new')); }, _onPublishSurvey:function(survey){ if ( !confirm("After publication the survey cannot be modified anymore, are you sure?") ) { return; } survey.publicationDate = new Date(); surveys.save(survey) .then(lang.hitch(this,function(){ this.refreshDrafts(); this.refreshPublished(); }),lang.hitch(this,function(err){ this.notify(err.error,'error'); })); }, _onDeleteSurvey:function(survey){ if ( !confirm("Are you sure you want to delete this draft survey?") ) { return; } surveys.remove(survey) .then(lang.hitch(this,function(){ this.refreshDrafts(); }),lang.hitch(this,function(err){ this.notify(err.error,'error'); })); }, _onEditSurvey:function(survey){ Router.go(surveys.getObjectPath(survey)); }, _onPreviewSurvey:function(survey){ Router.go(surveys.getPreviewPath(survey)); }, _onRunSurvey:function(survey){ var surveyRun = surveyRuns.create(); surveyRun.title = 'Run of "'+survey.title+'" of '+ (new Date().toString()); surveyRun.survey = survey; surveyRuns.save(surveyRun) .then(lang.hitch(this,function(surveyRun){ Router.go(surveyRuns.getObjectPath( surveyRuns.getId(surveyRun))); }),lang.hitch(this,function(err){ this.notify(err.error,'error'); })); }, refresh: function() { this.refreshDrafts(); this.refreshPublished(); }, refreshDrafts: function() { this.draftsContainer.set('content','Loading draft surveys '); when(surveys.query({drafts:true}), lang.hitch(this,function(surveys) { this.draftsTab.set('title','Drafts ('+surveys.length+')'); this.draftsContainer.set('content',''); array.forEach(surveys,function(survey){ var w = new LineWithActionsWidget({ title: survey.title || '(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','Loading published surveys '); when(surveys.query({published:true}), lang.hitch(this, function(surveys) { this.publishedContainer.set('content',''); this.publishedTab.set('title','Published ('+surveys.length+')'); array.forEach(surveys,function(survey){ var w = new LineWithActionsWidget({ title: survey.title || "", 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: 'Forward' } }] }); this.publishedContainer.addChild(w); },this); })); } }); });