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

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

End fold open with height auto, so widgets that change size are handled correctly.

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