[443] | 1 | define([ |
---|
[487] | 2 | "../app/Page", |
---|
| 3 | "../app/Router", |
---|
| 4 | "../model/classes/surveyRuns", |
---|
[490] | 5 | "../model/classes/surveys", |
---|
[487] | 6 | "../widgets/LineWithActionsWidget", |
---|
| 7 | "dojo/_base/array", |
---|
| 8 | "dojo/_base/declare", |
---|
| 9 | "dojo/_base/lang", |
---|
| 10 | "dojo/when", |
---|
| 11 | "dojo/text!./templates/surveys.html" |
---|
[490] | 12 | ], function(Page, Router, surveyRuns, surveys, LineWithActionsWidget, array, declare, lang, when, template) { |
---|
[443] | 13 | return declare([Page],{ |
---|
| 14 | templateString: template, |
---|
| 15 | startup: function() { |
---|
| 16 | if ( this._started ) { return; } |
---|
| 17 | this.inherited(arguments); |
---|
[529] | 18 | this.draftsTab.set('title','Drafts (<span class="qedLoading"></span>)'); |
---|
| 19 | this.publishedTab.set('title','Published (<span class="qedLoading"></span>)'); |
---|
[443] | 20 | this.refresh(); |
---|
| 21 | }, |
---|
| 22 | _onNewSurvey: function(){ |
---|
[490] | 23 | Router.go(surveys.getObjectPath('new')); |
---|
[443] | 24 | }, |
---|
| 25 | _onPublishSurvey:function(survey){ |
---|
[529] | 26 | if ( !confirm("After publication the survey cannot be modified anymore, are you sure?") ) { |
---|
| 27 | return; |
---|
| 28 | } |
---|
[487] | 29 | survey.publicationDate = new Date(); |
---|
| 30 | surveys.save(survey) |
---|
[529] | 31 | .then(lang.hitch(this,function(){ |
---|
| 32 | this.refreshDrafts(); |
---|
| 33 | this.refreshPublished(); |
---|
| 34 | }),lang.hitch(this,function(err){ |
---|
[490] | 35 | this.notify(err.error,'error'); |
---|
| 36 | })); |
---|
[443] | 37 | }, |
---|
| 38 | _onDeleteSurvey:function(survey){ |
---|
[529] | 39 | if ( !confirm("Are you sure you want to delete this draft survey?") ) { |
---|
| 40 | return; |
---|
| 41 | } |
---|
[487] | 42 | surveys.remove(survey) |
---|
[529] | 43 | .then(lang.hitch(this,function(){ |
---|
| 44 | this.refreshDrafts(); |
---|
| 45 | }),lang.hitch(this,function(err){ |
---|
[490] | 46 | this.notify(err.error,'error'); |
---|
| 47 | })); |
---|
[443] | 48 | }, |
---|
| 49 | _onEditSurvey:function(survey){ |
---|
[490] | 50 | Router.go(surveys.getObjectPath(survey)); |
---|
[443] | 51 | }, |
---|
| 52 | _onPreviewSurvey:function(survey){ |
---|
[490] | 53 | Router.go(surveys.getPreviewPath(survey)); |
---|
[443] | 54 | }, |
---|
| 55 | _onRunSurvey:function(survey){ |
---|
[487] | 56 | var surveyRun = surveyRuns.create(); |
---|
[531] | 57 | surveyRun.title = 'Run of "'+survey.title+'" of '+ |
---|
| 58 | (new Date().toString()); |
---|
[487] | 59 | surveyRun.survey = survey; |
---|
| 60 | surveyRuns.save(surveyRun) |
---|
[443] | 61 | .then(lang.hitch(this,function(surveyRun){ |
---|
[531] | 62 | Router.go(surveyRuns.getObjectPath( |
---|
| 63 | surveyRuns.getId(surveyRun))); |
---|
[490] | 64 | }),lang.hitch(this,function(err){ |
---|
| 65 | this.notify(err.error,'error'); |
---|
| 66 | })); |
---|
[443] | 67 | }, |
---|
| 68 | refresh: function() { |
---|
| 69 | this.refreshDrafts(); |
---|
| 70 | this.refreshPublished(); |
---|
| 71 | }, |
---|
| 72 | refreshDrafts: function() { |
---|
[531] | 73 | this.draftsContainer.set('content','Loading draft surveys <span class="qedLoading"></span>'); |
---|
[487] | 74 | when(surveys.query({drafts:true}), lang.hitch(this,function(surveys) { |
---|
[443] | 75 | this.draftsTab.set('title','Drafts ('+surveys.length+')'); |
---|
[531] | 76 | this.draftsContainer.set('content',''); |
---|
[443] | 77 | array.forEach(surveys,function(survey){ |
---|
| 78 | var w = new LineWithActionsWidget({ |
---|
[487] | 79 | title: survey.title || '(unnamed)', |
---|
[443] | 80 | actions: [{ |
---|
| 81 | callback: lang.hitch(this,'_onPublishSurvey',survey), |
---|
| 82 | properties: { |
---|
| 83 | label: 'Publish', |
---|
| 84 | tooltip: 'Publish survey', |
---|
| 85 | icon: 'Publish' |
---|
| 86 | } |
---|
| 87 | },{ |
---|
| 88 | callback: lang.hitch(this,'_onPreviewSurvey',survey), |
---|
| 89 | properties: { |
---|
| 90 | label: 'Preview', |
---|
| 91 | tooltip: 'Preview survey', |
---|
| 92 | icon: 'Preview' |
---|
| 93 | } |
---|
| 94 | },{ |
---|
| 95 | callback: lang.hitch(this,'_onDeleteSurvey',survey), |
---|
| 96 | properties: { |
---|
| 97 | label: 'Delete', |
---|
| 98 | tooltip: 'Delete survey', |
---|
| 99 | icon: 'Delete' |
---|
| 100 | } |
---|
| 101 | },{ |
---|
| 102 | callback: lang.hitch(this,'_onEditSurvey',survey), |
---|
| 103 | properties: { |
---|
| 104 | label: 'Edit', |
---|
| 105 | tooltip: 'Edit survey', |
---|
| 106 | icon: 'Edit' |
---|
| 107 | } |
---|
| 108 | }] |
---|
| 109 | }); |
---|
| 110 | this.draftsContainer.addChild(w); |
---|
| 111 | },this); |
---|
| 112 | })); |
---|
| 113 | }, |
---|
| 114 | refreshPublished: function() { |
---|
[531] | 115 | this.publishedContainer.set('content','Loading published surveys <span class="qedLoading"></span>'); |
---|
[487] | 116 | when(surveys.query({published:true}), lang.hitch(this, function(surveys) { |
---|
[531] | 117 | this.publishedContainer.set('content',''); |
---|
[443] | 118 | this.publishedTab.set('title','Published ('+surveys.length+')'); |
---|
| 119 | array.forEach(surveys,function(survey){ |
---|
| 120 | var w = new LineWithActionsWidget({ |
---|
[487] | 121 | title: survey.title || "", |
---|
[443] | 122 | actions:[{ |
---|
| 123 | callback: lang.hitch(this,'_onPreviewSurvey',survey), |
---|
| 124 | properties: { |
---|
| 125 | label: 'Preview', |
---|
| 126 | tooltip: 'Preview survey', |
---|
| 127 | icon: 'Preview' |
---|
| 128 | } |
---|
| 129 | },{ |
---|
| 130 | callback: lang.hitch(this,'_onRunSurvey',survey), |
---|
| 131 | properties: { |
---|
| 132 | label: 'Run', |
---|
| 133 | tooltip: 'Run survey', |
---|
[529] | 134 | icon: 'Forward' |
---|
[443] | 135 | } |
---|
| 136 | }] |
---|
| 137 | }); |
---|
| 138 | this.publishedContainer.addChild(w); |
---|
| 139 | },this); |
---|
| 140 | })); |
---|
| 141 | } |
---|
| 142 | }); |
---|
| 143 | }); |
---|
| 144 | |
---|