source: Dev/trunk/client/qed/model/widgets/QuestionEditorPreviewItem.js @ 442

Last change on this file since 442 was 442, checked in by hendrikvanantwerpen, 12 years ago

Enable validation on all fields of question.

File size: 6.0 KB
Line 
1define([
2    "./questions/Factory",
3    "dijit/_Container",
4    "dijit/_TemplatedMixin",
5    "dijit/_WidgetBase",
6    "dijit/_WidgetsInTemplateMixin",
7    "dojo/_base/declare",
8    "dojo/_base/event",
9    "dojo/_base/fx",
10    "dojo/_base/lang",
11    "dojo/dom-class",
12    "dojo/dom-geometry",
13    "dojo/dom-style",
14    "dojo/on",
15    "dojo/text!./templates/QuestionEditorPreviewItem.html"
16], function(QuestionWidgetFactory, _Container, _TemplatedMixin, _WidgetBase, _WidgetsInTemplateMixin, declare, event, fx, lang, domClass, domGeom, domStyle, on, template) {
17    return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _Container], {
18        templateString: template,
19        baseClass: "qedQuestionEditorPreviewItem",
20        previousContentHeight: 200,
21        value: null,
22        innerWidget: null,
23        foldDuration: [250, 250],
24        animation: null,
25        _editing: false,
26        _factory: new QuestionWidgetFactory(),
27
28        startup: function() {
29            if ( this._started ){ return; }
30            this.inherited(arguments);
31            this.own(this.foldButton.on('click',
32                        lang.hitch(this, 'onToggleFold')));
33            this.own(this.removeButton.on('click',
34                        lang.hitch(this, 'onDestroy')));
35            this.own(this.editButton.on('click',
36                        lang.hitch(this, 'onToggleEdit')));
37            this.showEdit();
38        },
39        onDestroy: function() {},
40        _getValueAttr: function(value) {
41            if ( this._editing ) {
42                this.value = this.innerWidget.get('value');
43            }
44            return this.value;
45        },
46        _setValueAttr: function(value) {
47            this.value = value;
48            if ( this._editing ) {
49                this._showEditWidget();
50            } else {
51                this._showViewWidget();
52            }
53        },
54        validate: function() {
55            return !this._editing || this.innerWidget.validate();
56        },
57        focus: function() {
58            if ( this._editing ) {
59                this.innerWidget.focus();
60            }
61        },
62        showView: function() {
63            if ( this._editing ) {
64                if (!this.innerWidget.validate || this.innerWidget.validate() ) {
65                    this.value = this.innerWidget.get('value');
66                    this._showViewWidget();
67                }
68            }
69        },
70        showEdit: function() {
71            if (!this._editing) {
72                this._showEditWidget();
73            }
74        },
75        onToggleEdit: function(evt) {
76            if (this._editing) {
77                this.showView();
78            } else {
79                this.showEdit();
80            }
81            if ( evt ) { event.stop(evt); }
82            return false;
83        },
84        _showViewWidget: function() {
85            var newWidget = this._factory.createViewWidget( this.value );
86            if ( newWidget !== null ) {
87                this._destroyInnerWidget();
88                this.innerWidget = newWidget;
89                this.innerWidget.placeAt(this.containerNode);
90                this.innerWidget.startup();
91                this.innerWidget.set('readOnly',true);
92                this.titleNode.innerHTML = this.value.type+" [preview]";
93                domClass.replace(this.editButton.iconNode, "rftIconEdit", "rftIconAccept");
94                this.editButton.set("label", "Edit");
95                this._editing = false;
96            }
97        },
98        _showEditWidget: function() {
99            var newWidget = this._factory.createEditWidget( this.value );
100            if ( newWidget !== null ) {
101                this._destroyInnerWidget();
102                this.innerWidget = newWidget;
103                this.innerWidget.placeAt(this.containerNode);
104                this.innerWidget.startup();
105                this.titleNode.innerHTML = this.value.type+" [editing]";
106                domClass.replace(this.editButton.iconNode, "rftIconAccept", "rftIconEdit");
107                this.editButton.set("label", "Done");
108                this._editing = true;
109            }
110        },
111        _destroyInnerWidget: function() {
112            if ( this.innerWidget !== null ) {
113                this.innerWidget.destroyRecursive();
114            }
115        },
116        onToggleFold: function(evt) {
117            if (!this.animation) {
118                if (!domClass.contains(this.domNode, "fold")) {
119                    this.previousContentHeight = domGeom.getMarginBox(this.innerNode).h;
120                    this.animation = fx.animateProperty({
121                        node: this.innerNode,
122                        duration: this.foldDuration[0],
123                        properties: {
124                            height: 1
125                        },
126                        onEnd: lang.hitch(this, function(){
127                            domClass.add(this.domNode, "fold");
128                            this.animation = null;
129                        })
130                    }).play();
131                    domClass.replace(this.foldButton.iconNode, "rftIconHalfArrowDown", "rftIconHalfArrowUp");
132                } else {
133                    this.animation = fx.animateProperty({
134                        node: this.innerNode,
135                        duration: this.foldDuration[1],
136                        properties: {
137                            height: this.previousContentHeight
138                        },
139                        onEnd: lang.hitch(this, function(){
140                            domClass.remove(this.domNode, "fold");
141                            domStyle.set(this.innerNode, 'height', 'auto');
142                            this.animation = null;
143                        })
144                    }).play();
145                    domClass.replace(this.foldButton.iconNode, "rftIconHalfArrowUp", "rftIconHalfArrowDown");
146                }
147            }
148            if ( evt ) { event.stop(evt); }
149            return false;
150        }
151
152    });
153});
Note: See TracBrowser for help on using the repository browser.