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

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

Support validation for InputWidgets? and have slightly better hover color.

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