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