[407] | 1 | define([ |
---|
[410] | 2 | 'dojo/_base/array', |
---|
[407] | 3 | 'dojo/_base/declare', |
---|
| 4 | 'dojo/_base/lang', |
---|
[410] | 5 | 'dojo/when', |
---|
[407] | 6 | '../store', |
---|
[410] | 7 | '../app/Content', |
---|
[407] | 8 | '../app/Page', |
---|
[410] | 9 | '../app/Router', |
---|
[420] | 10 | '../model/classes/Survey', |
---|
| 11 | '../model/classes/SurveyRun', |
---|
[417] | 12 | '../widgets/LineWithActionsWidget', |
---|
| 13 | 'dojo/text!./templates/surveys.html' |
---|
[422] | 14 | ],function(array,declare,lang,when,store,Content,Page,Router,Survey,SurveyRun,LineWithActionsWidget,template){ |
---|
[407] | 15 | return declare([Page],{ |
---|
| 16 | templateString: template, |
---|
| 17 | startup: function() { |
---|
| 18 | if ( this._started ) { return; } |
---|
| 19 | this.inherited(arguments); |
---|
[410] | 20 | this.refresh(); |
---|
| 21 | }, |
---|
| 22 | _onNewSurvey: function(){ |
---|
[416] | 23 | Router.go('/survey/new'); |
---|
[410] | 24 | }, |
---|
| 25 | _onPublishSurvey:function(survey){ |
---|
| 26 | var self = this; |
---|
| 27 | survey.publicationDate = store.timestamp(); |
---|
| 28 | store.put(survey).then(function(){ |
---|
| 29 | self.refreshDrafts(); |
---|
| 30 | self.refreshPublished(); |
---|
| 31 | },function(err){ |
---|
[415] | 32 | Content.notify(err,'error'); |
---|
[410] | 33 | }); |
---|
| 34 | }, |
---|
| 35 | _onDeleteSurvey:function(survey){ |
---|
| 36 | var self = this; |
---|
| 37 | store.remove(store.getIdentity(survey),store.getRevision(survey)) |
---|
| 38 | .then(function(){ |
---|
| 39 | self.refreshDrafts(); |
---|
| 40 | },function(err){ |
---|
[415] | 41 | Content.notify(err,'error'); |
---|
[410] | 42 | }); |
---|
| 43 | }, |
---|
| 44 | _onEditSurvey:function(survey){ |
---|
| 45 | Router.go('/survey/'+store.getIdentity(survey)); |
---|
| 46 | }, |
---|
| 47 | _onPreviewSurvey:function(survey){ |
---|
[420] | 48 | Router.go('/previewSurvey/'+store.getIdentity(survey)); |
---|
[410] | 49 | }, |
---|
| 50 | _onRunSurvey:function(survey){ |
---|
[420] | 51 | var surveyRun = SurveyRun.create(); |
---|
| 52 | SurveyRun.Survey.set(surveyRun,survey); |
---|
[417] | 53 | store.put(surveyRun) |
---|
[420] | 54 | .then(lang.hitch(this,function(surveyRun){ |
---|
| 55 | this._onRunDetails(surveyRun); |
---|
[417] | 56 | }),function(err){ |
---|
| 57 | Content.notify(err); |
---|
| 58 | }); |
---|
| 59 | }, |
---|
[420] | 60 | _onRunDetails: function(surveyRun) { |
---|
| 61 | Router.go('/surveyRun/'+store.getIdentity(surveyRun)); |
---|
[417] | 62 | }, |
---|
[410] | 63 | refresh: function() { |
---|
| 64 | this.refreshDrafts(); |
---|
| 65 | this.refreshPublished(); |
---|
| 66 | this.refreshRuns(); |
---|
| 67 | }, |
---|
| 68 | refreshDrafts: function() { |
---|
| 69 | this.draftsContainer.set('content',''); |
---|
[426] | 70 | when(store.query("_design/surveys/_view/drafts"), |
---|
| 71 | lang.hitch(this,function(surveys) { |
---|
[410] | 72 | this.draftsTab.set('title','Drafts ('+surveys.length+')'); |
---|
| 73 | array.forEach(surveys,function(survey){ |
---|
| 74 | var w = new LineWithActionsWidget({ |
---|
[420] | 75 | title: Survey.DisplayTitle.get(survey) || '(unnamed)', |
---|
[410] | 76 | actions: [{ |
---|
| 77 | callback: lang.hitch(this,'_onPublishSurvey',survey), |
---|
| 78 | properties: { |
---|
| 79 | label: 'Publish', |
---|
| 80 | tooltip: 'Publish survey', |
---|
| 81 | icon: 'Publish' |
---|
| 82 | } |
---|
| 83 | },{ |
---|
| 84 | callback: lang.hitch(this,'_onPreviewSurvey',survey), |
---|
| 85 | properties: { |
---|
| 86 | label: 'Preview', |
---|
| 87 | tooltip: 'Preview survey', |
---|
| 88 | icon: 'Preview' |
---|
| 89 | } |
---|
| 90 | },{ |
---|
| 91 | callback: lang.hitch(this,'_onDeleteSurvey',survey), |
---|
| 92 | properties: { |
---|
| 93 | label: 'Delete', |
---|
| 94 | tooltip: 'Delete survey', |
---|
| 95 | icon: 'Delete' |
---|
| 96 | } |
---|
| 97 | },{ |
---|
| 98 | callback: lang.hitch(this,'_onEditSurvey',survey), |
---|
| 99 | properties: { |
---|
| 100 | label: 'Edit', |
---|
| 101 | tooltip: 'Edit survey', |
---|
| 102 | icon: 'Edit' |
---|
| 103 | } |
---|
| 104 | }] |
---|
| 105 | }); |
---|
| 106 | this.draftsContainer.addChild(w); |
---|
| 107 | },this); |
---|
[407] | 108 | })); |
---|
[410] | 109 | }, |
---|
| 110 | refreshPublished: function() { |
---|
| 111 | this.publishedContainer.set('content',''); |
---|
[426] | 112 | when(store.query("_design/surveys/_view/published"), |
---|
| 113 | lang.hitch(this, function(surveys) { |
---|
[410] | 114 | this.publishedTab.set('title','Published ('+surveys.length+')'); |
---|
| 115 | array.forEach(surveys,function(survey){ |
---|
| 116 | var w = new LineWithActionsWidget({ |
---|
[420] | 117 | title: Survey.DisplayTitle.get(survey), |
---|
[410] | 118 | actions:[{ |
---|
| 119 | callback: lang.hitch(this,'_onPreviewSurvey',survey), |
---|
| 120 | properties: { |
---|
| 121 | label: 'Preview', |
---|
| 122 | tooltip: 'Preview survey', |
---|
| 123 | icon: 'Preview' |
---|
| 124 | } |
---|
| 125 | },{ |
---|
| 126 | callback: lang.hitch(this,'_onRunSurvey',survey), |
---|
| 127 | properties: { |
---|
| 128 | label: 'Run', |
---|
| 129 | tooltip: 'Run survey', |
---|
| 130 | icon: 'Run' |
---|
| 131 | } |
---|
| 132 | }] |
---|
| 133 | }); |
---|
| 134 | this.publishedContainer.addChild(w); |
---|
| 135 | },this); |
---|
[407] | 136 | })); |
---|
[410] | 137 | }, |
---|
| 138 | refreshRuns: function() { |
---|
| 139 | this.runsContainer.set('content',''); |
---|
[426] | 140 | when(store.query("_design/default/_view/by_type",{key:'SurveyRun'}), |
---|
| 141 | lang.hitch(this,function(surveyRuns){ |
---|
[410] | 142 | this.runsTab.set('title','Runs ('+surveyRuns.length+')'); |
---|
| 143 | array.forEach(surveyRuns,function(surveyRun){ |
---|
| 144 | var w = new LineWithActionsWidget({ |
---|
[420] | 145 | title: SurveyRun.DisplayTitle.get(surveyRun), |
---|
[410] | 146 | actions:[{ |
---|
[420] | 147 | callback: lang.hitch(this,'_onRunDetails',surveyRun), |
---|
[410] | 148 | properties: { |
---|
[420] | 149 | label: 'Details', |
---|
| 150 | tooltip: 'Show details for this run', |
---|
| 151 | icon: 'Details' |
---|
[410] | 152 | } |
---|
| 153 | }] |
---|
| 154 | }); |
---|
| 155 | this.runsContainer.addChild(w); |
---|
| 156 | },this); |
---|
[407] | 157 | })); |
---|
| 158 | } |
---|
| 159 | }); |
---|
[358] | 160 | }); |
---|
[292] | 161 | |
---|