define([ "../app/Router", "../model/classes/surveys", "../model/widgets/QuestionListView", "../model/widgets/TabbedQuestionBrowser", "./_ObjectPage", "dojo/Deferred", "dojo/_base/array", "dojo/_base/declare", "dojo/_base/event", "dojo/_base/lang", "dojo/when", "require", "dojo/text!./templates/survey.html" ], function(Router, surveys, QuestionListView, TabbedQuestionBrowser, _ObjectPage, Deferred, array, declare, event, lang, when, require, template) { return declare([_ObjectPage],{ contextRequire: require, templateString: template, classStore: surveys, questionList: null, startup: function() { if ( this._started ) { return; } this.inherited(arguments); this._setupQuestionBrowser(); this._setupListView(); this.own(this.questionList.on('change',lang.hitch(this,'_handleQuestionsChange'))); this.own(this.propertiesForm.on('change',lang.hitch(this,'_handlePropertiesChange'))); this._load(); }, _setupQuestionBrowser: function() { this.questionBrowser = new TabbedQuestionBrowser({ region: 'center', 'class': 'blue', include: 'published', getSelectedItemActions: lang.hitch(this,function() { return { "Include": { callback: lang.hitch(this,this._includeQuestion), icon: "Accept", description: "Include in survey" } }; }), getItemActions: lang.hitch(this,function(item) { return { "Info": { callback: lang.hitch(this,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', name: 'questions' },this.surveyListViewNode); this.questionList.startup(); }, _refresh: function(initial) { if ( initial === true ) { this.propertiesForm.set('value',{survey:this.object},null); this.questionList.set('value',this.object.questions,null); } this.titleNode.innerHTML = this.object.title || "(set title in properties)"; if ( this.object.publicationDate ) { this.propertiesForm.set('readOnly',true); this.questionList.set('readOnly',true); } }, _includeQuestion: function(question) { this.questionList.appendItem(question); }, _handleQuestionsChange: function() { this._updateObject(); this.markDirty(); this._refresh(); }, _handlePropertiesChange: function() { this._updateObject(); this.markDirty(); this._refresh(); this.layout(); }, _updateObject: function() { this._isValid = this.propertiesForm.validate(); lang.mixin(this.object, this.propertiesForm.get('value').survey); this.object.questions = this.questionList.get('value'); return this._isValid; }, _onSave: function(evt) { this._save(); if ( evt ) { event.stop(evt); } return false; }, _onSaveAndClose: function(evt) { this._save() .then(function() { Router.go(surveys.getCollectionPath()); }); event.stop(evt); return false; }, _onDiscardAndClose: function(evt) { this.markClean(); Router.go(surveys.getCollectionPath()); }, _onShowPreview: function() { Router.go(surveys.getPreviewPath(this.object),{ preview: true }); }, _save: function() { if ( this._updateObject ) { return this.inherited(arguments); } else { return new Deferred().reject({error:"Please correct invalid values."}); } }, markDirty: function() { this.saveBtn.set('disabled',!this._isValid); this.saveAndCloseBtn.set('disabled',!this._isValid); this.discardBtn.set('label','Discard & Close'); this.inherited(arguments); }, markClean: function() { this.saveBtn.set('disabled',true); this.saveAndCloseBtn.set('disabled',true); this.discardBtn.set('label','Close'); this.inherited(arguments); }, _ignore: function(evt) { event.stop(evt); return false; } }); });