[387] | 1 | define([
|
---|
[442] | 2 | "../app/Content",
|
---|
| 3 | "../app/Page",
|
---|
| 4 | "../app/Router",
|
---|
| 5 | "../model/classes/Question",
|
---|
| 6 | "../model/widgets/QuestionEditorPreview",
|
---|
| 7 | "../model/widgets/QuestionEditorToolkit",
|
---|
| 8 | "../store",
|
---|
| 9 | "../widgets/_ComplexValueMixin",
|
---|
| 10 | "dojo/_base/declare",
|
---|
| 11 | "dojo/_base/event",
|
---|
| 12 | "dojo/_base/lang",
|
---|
| 13 | "dojo/when",
|
---|
| 14 | "dojo/text!./templates/question.html"
|
---|
| 15 | ], function(Content, Page, Router, Question, QuestionEditorPreview, QuestionEditorToolkit, store, _ComplexValueMixin, declare, event, lang, when, template) {
|
---|
| 16 | return declare([Page,_ComplexValueMixin], {
|
---|
[407] | 17 | templateString: template,
|
---|
| 18 | _toolkit: null,
|
---|
| 19 | _preview: null,
|
---|
[442] | 20 | value: null,
|
---|
[407] | 21 |
|
---|
[442] | 22 | buildRendering: function() {
|
---|
| 23 | this.inherited(arguments);
|
---|
| 24 |
|
---|
| 25 | this._toolkit = new QuestionEditorToolkit({
|
---|
| 26 | },this.QuestionEditorToolkitNode);
|
---|
| 27 | this._toolkit.on('submit',lang.hitch(this,"_onSave"));
|
---|
| 28 | this._toolkit.startup();
|
---|
| 29 |
|
---|
| 30 | this._preview = new QuestionEditorPreview({
|
---|
| 31 | name: 'content',
|
---|
| 32 | delay: 5,
|
---|
| 33 | region: 'center'
|
---|
| 34 | });
|
---|
| 35 | this._preview.startup();
|
---|
| 36 | this.addChild(this._preview);
|
---|
| 37 | },
|
---|
[407] | 38 | startup: function() {
|
---|
| 39 | if ( this._started ) { return; }
|
---|
| 40 | this.inherited(arguments);
|
---|
[415] | 41 | if ( !this.questionId ) {
|
---|
| 42 | throw new Error("Error: no reference to object set!");
|
---|
| 43 | }
|
---|
| 44 | if (this.questionId === "new") {
|
---|
[442] | 45 | this.set('value', Question.create());
|
---|
[415] | 46 | } else {
|
---|
[420] | 47 | when(store.get(this.questionId))
|
---|
[442] | 48 | .then(lang.hitch(this, function(value) {
|
---|
| 49 | this.set('value', value);
|
---|
[407] | 50 | }));
|
---|
[355] | 51 | }
|
---|
[407] | 52 | },
|
---|
[442] | 53 | _setValueAttr: function(value) {
|
---|
| 54 | this.value = value;
|
---|
[407] | 55 | this.inherited(arguments);
|
---|
[442] | 56 | this.titleNode.innerHTML = Question.DisplayTitle.get(value);
|
---|
[407] | 57 | },
|
---|
[442] | 58 | _getValueAttr: function() {
|
---|
| 59 | var value = this.inherited(arguments);
|
---|
| 60 | lang.mixin(this.value, value);
|
---|
| 61 | return this.value;
|
---|
[407] | 62 | },
|
---|
| 63 | _onSave: function(evt) {
|
---|
[442] | 64 | if ( this.validate() ) {
|
---|
| 65 | var value = this.get('value');
|
---|
| 66 | store.put(value)
|
---|
[441] | 67 | .then(function() {
|
---|
| 68 | Router.go('/questions');
|
---|
| 69 | },function(err){
|
---|
| 70 | Content.notify(err,'error');
|
---|
| 71 | });
|
---|
| 72 | }
|
---|
[426] | 73 | if ( evt ) { event.stop( evt ); }
|
---|
[407] | 74 | return false;
|
---|
| 75 | },
|
---|
| 76 | _onDiscard: function() {
|
---|
[410] | 77 | Router.go('/questions');
|
---|
[407] | 78 | return true;
|
---|
| 79 | }
|
---|
| 80 | });
|
---|
[417] | 81 | });
|
---|