1 | define([
|
---|
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], {
|
---|
17 | templateString: template,
|
---|
18 | _toolkit: null,
|
---|
19 | _preview: null,
|
---|
20 | value: null,
|
---|
21 |
|
---|
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 | },
|
---|
38 | startup: function() {
|
---|
39 | if ( this._started ) { return; }
|
---|
40 | this.inherited(arguments);
|
---|
41 | if ( !this.questionId ) {
|
---|
42 | throw new Error("Error: no reference to object set!");
|
---|
43 | }
|
---|
44 | if (this.questionId === "new") {
|
---|
45 | this.set('value', Question.create());
|
---|
46 | } else {
|
---|
47 | when(store.get(this.questionId))
|
---|
48 | .then(lang.hitch(this, function(value) {
|
---|
49 | this.set('value', value);
|
---|
50 | }));
|
---|
51 | }
|
---|
52 | },
|
---|
53 | _setValueAttr: function(value) {
|
---|
54 | this.value = value;
|
---|
55 | this.inherited(arguments);
|
---|
56 | this.titleNode.innerHTML = Question.DisplayTitle.get(value);
|
---|
57 | },
|
---|
58 | _getValueAttr: function() {
|
---|
59 | var value = this.inherited(arguments);
|
---|
60 | lang.mixin(this.value, value);
|
---|
61 | return this.value;
|
---|
62 | },
|
---|
63 | _onSave: function(evt) {
|
---|
64 | if ( this.validate() ) {
|
---|
65 | var value = this.get('value');
|
---|
66 | store.put(value)
|
---|
67 | .then(function() {
|
---|
68 | Router.go('/questions');
|
---|
69 | },function(err){
|
---|
70 | Content.notify(err,'error');
|
---|
71 | });
|
---|
72 | }
|
---|
73 | if ( evt ) { event.stop( evt ); }
|
---|
74 | return false;
|
---|
75 | },
|
---|
76 | _onDiscard: function() {
|
---|
77 | Router.go('/questions');
|
---|
78 | return true;
|
---|
79 | }
|
---|
80 | });
|
---|
81 | });
|
---|