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