define([ 'dojo/_base/array', 'dojo/_base/declare', 'dojo/_base/event', 'dojo/_base/lang', 'dojo/when', '../app/Router', '../store', '../app/Page', '../model/classes/Survey', '../model/widgets/QuestionListView', '../model/widgets/TabbedQuestionBrowser', 'dojo/text!./templates/survey.html' ],function(array,declare,event,lang,when,Router,store,Page,Survey, QuestionListView,TabbedQuestionBrowser,template){ return declare([Page],{ templateString: template, survey: null, questionList: null, startup: function() { if ( this._started ) { return; } this.inherited(arguments); if ( this.surveyId ) { this._setupQuestionBrowser(); this._setupListView(); this._loadSurvey(); } else { throw "No valid uid or survey passed!"; } }, _setupQuestionBrowser: function() { this.questionBrowser = new TabbedQuestionBrowser({ region: 'center', 'class': 'blue', include: 'published', selectedActions: { "Include": { callback: lang.hitch(this,this._includeQuestion), icon: "Accept", description: "Include in survey" } }, itemActions: { "Info": { callback: function(item){ if ( item.description ) { alert(item.description); } }, icon: "Inspect", description: "Show item description" } } },this.questionBrowser); this.questionBrowser.startup(); }, _setupListView: function() { this.questionList = new QuestionListView({ region: 'center' },this.surveyListViewNode); this.questionList.startup(); }, _loadSurvey: function() { if ( this.surveyId === "new" ) { this.survey = Survey.create(); this.refresh(); } else { when(store.get(this.surveyId)) .then(lang.hitch(this,function(survey){ this.survey = survey; array.forEach(Survey.Questions.get(this.survey), lang.hitch(this.questionList,'appendItem')); this.refresh(); })); } }, _includeQuestion: function(question) { this.questionList.insertItem(question); }, refresh: function() { this.titleNode.innerHTML = Survey.DisplayTitle.get(this.survey) || "(set title in properties)"; this.propertiesDialog.set('value',this.survey); }, _onShowProperties: function(evt) { this.propertiesDialog.show(); }, _onPropertiesOk: function(evt) { this.propertiesDialog.hide(); lang.mixin(this.survey, this.propertiesDialog.get('value')); this.refresh(); event.stop(evt); return false; }, _onPropertiesCancel: function(evt) { this.propertiesDialog.hide(); this.propertiesDialog.reset('value',this.survey); event.stop(evt); return false; }, _onSave: function(evt) { this.survey.questions = this.questionList.getItems(); store.put(this.survey) .then(function() { Router.go('/surveys'); }); event.stop(evt); return false; }, _onDiscard: function(evt) { Router.go('/surveys'); }, _onShowPreview: function() { Router.go('/previewSurvey/'+store.getIdentity(this.survey),{ preview: true }); } }); });