1 | define([
|
---|
2 | "../app/Router",
|
---|
3 | "../model/classes/questions",
|
---|
4 | "./_ObjectPage",
|
---|
5 | "dojo/Deferred",
|
---|
6 | "dojo/_base/declare",
|
---|
7 | "dojo/_base/event",
|
---|
8 | "dojo/_base/lang",
|
---|
9 | "require",
|
---|
10 | "dojo/text!./templates/question.html"
|
---|
11 | ], function(Router, questions, _ObjectPage, Deferred, declare, event, lang, require, template) {
|
---|
12 | return declare([_ObjectPage], {
|
---|
13 | contextRequire: require,
|
---|
14 | templateString: template,
|
---|
15 | _preview: null,
|
---|
16 | classStore: questions,
|
---|
17 | startup: function() {
|
---|
18 | if ( this._started ) { return; }
|
---|
19 | this.inherited(arguments);
|
---|
20 | this.own(this.QuestionEditorToolkit.on('change',lang.hitch(this,'_handlePropertiesChange')));
|
---|
21 | this.own(this.contentList.on('change',lang.hitch(this,'_handleContentChange')));
|
---|
22 | this._load();
|
---|
23 | },
|
---|
24 | _refresh: function(initial) {
|
---|
25 | if ( initial === true ) {
|
---|
26 | this.QuestionEditorToolkit.set('value',this.object,null);
|
---|
27 | this.contentList.set('value',this.object.content,null);
|
---|
28 | }
|
---|
29 | this.titleNode.innerHTML = this.object.title || "";
|
---|
30 | if ( this.object.publicationDate ) {
|
---|
31 | this.QuestionEditorToolkit.set('readOnly',true);
|
---|
32 | this.contentList.set('readOnly',true);
|
---|
33 | }
|
---|
34 | },
|
---|
35 | _handlePropertiesChange: function() {
|
---|
36 | this._updateObject();
|
---|
37 | this.markDirty();
|
---|
38 | this._refresh();
|
---|
39 | },
|
---|
40 | _handleContentChange: function() {
|
---|
41 | this._updateObject();
|
---|
42 | this.markDirty();
|
---|
43 | this._refresh();
|
---|
44 | },
|
---|
45 | _updateObject: function() {
|
---|
46 | var pValid = this.QuestionEditorToolkit.validate();
|
---|
47 | lang.mixin(this.object,this.QuestionEditorToolkit.get('value'));
|
---|
48 | var cValid = this.contentList.validate();
|
---|
49 | this.object.content = this.contentList.get('value');
|
---|
50 | this._isValid = pValid && cValid;
|
---|
51 | return this._isValid;
|
---|
52 | },
|
---|
53 | _save: function() {
|
---|
54 | if ( this._updateObject() ) {
|
---|
55 | return this.inherited(arguments);
|
---|
56 | } else {
|
---|
57 | return new Deferred().reject({error:"Please correct invalid values."});
|
---|
58 | }
|
---|
59 | },
|
---|
60 | _onSave: function(evt) {
|
---|
61 | this._save();
|
---|
62 | if ( evt ) { event.stop( evt ); }
|
---|
63 | return false;
|
---|
64 | },
|
---|
65 | _onSaveAndClose: function(evt) {
|
---|
66 | this._save()
|
---|
67 | .then(function(){
|
---|
68 | Router.go(questions.getCollectionPath());
|
---|
69 | });
|
---|
70 | if ( evt ) { event.stop( evt ); }
|
---|
71 | return false;
|
---|
72 | },
|
---|
73 | _onDiscard: function(evt) {
|
---|
74 | this.markClean();
|
---|
75 | Router.go(questions.getCollectionPath());
|
---|
76 | if ( evt ) { event.stop( evt ); }
|
---|
77 | return false;
|
---|
78 | },
|
---|
79 | markDirty: function() {
|
---|
80 | this.saveBtn.set('disabled',!this._isValid);
|
---|
81 | this.saveAndCloseBtn.set('disabled',!this._isValid);
|
---|
82 | this.discardBtn.set('label','Discard & Close');
|
---|
83 | this.inherited(arguments);
|
---|
84 | },
|
---|
85 | markClean: function() {
|
---|
86 | this.saveBtn.set('disabled',true);
|
---|
87 | this.saveAndCloseBtn.set('disabled',true);
|
---|
88 | this.discardBtn.set('label','Close');
|
---|
89 | this.inherited(arguments);
|
---|
90 | },
|
---|
91 | _ignore: function(evt) {
|
---|
92 | if ( evt ) { event.stop( evt ); }
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 | });
|
---|
96 | });
|
---|