source: Dev/branches/rest-dojo-ui/client/rft/ui/QuestionEditorPreviewItem.js @ 389

Last change on this file since 389 was 389, checked in by hendrikvanantwerpen, 13 years ago
File size: 5.6 KB
Line 
1define([
2    'dojo/_base/declare',
3    'dojo/_base/fx',
4    'dojo/_base/lang',
5    'dojo/dom-class',
6    'dojo/on',
7    'dijit/_Container',
8    'dijit/_TemplatedMixin',
9    'dijit/_WidgetBase',
10    'dijit/_WidgetsInTemplateMixin',
11    './content/ContentWidgetFactory',
12    'dojo/text!./templates/QuestionEditorPreviewItem.html',
13    ], function(declare, fx, lang, domClass, on, _Container, _TemplatedMixin, _WidgetBase, _WidgetsInTemplateMixin, ContentWidgetFactory, template) {
14        return declare("rft.ui.QuestionEditorPreviewItem", [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _Container], {
15            version: "full",
16            templateString: template,
17            baseClass: "surveyEditorPreviewItem",
18            previousContentHeight: 200,
19            item: null,
20            innerWidget: null,
21            foldDuration: [250, 250],
22            animation: null,
23            _editing: false,
24
25            startup: function() {
26                this.inherited(arguments);
27                this.foldButtonNode.onClick = lang.hitch(this, this.toggleFold);
28                this.removeButtonNode.onClick = lang.hitch(this, "onClose");
29                this.editButtonNode.onClick = lang.hitch(this, this.toggleEdit);
30                if (this.item) {
31                    this._showViewWidget();
32                } else {
33                    throw "No data supplied to create an innerWidget!";
34                }
35            },
36            _destroyInnerWidget: function() {
37                if ( this.innerWidget !== null ) {
38                    this.innerWidget.destroyRecursive();
39                }
40            },
41            _showViewWidget: function() {
42                var factory = new ContentWidgetFactory();
43                this.innerWidget = factory.createViewWidget( this.item );
44                if ( this.innerWidget !== null ) {
45                    this.innerWidget.placeAt(this.containerNode);
46                    this.innerWidget.startup();
47                }
48                this.titleNode.innerHTML = "[preview]";
49            },
50            _showEditWidget: function() {
51                var factory = new ContentWidgetFactory();
52                this.innerWidget = factory.createEditWidget( this.item );
53                if ( this.innerWidget !== null ) {
54                    this.innerWidget.placeAt(this.containerNode);
55                    this.innerWidget.startup();
56                }
57                this.titleNode.innerHTML = "[editing]";
58            },
59            onClose: function() {},
60            _getValueAttr: function(value) {
61                return this.item;
62            },
63            _setValueAttr: function(value) {
64                this.item = value;
65                this._destroyInnerWidget();
66                if ( this._editing ) {
67                    this._showEditWidget();
68                } else {
69                    this._showViewWidget();
70                }
71            },
72            toggleEdit: function() {
73                if(this._editing) {
74                    if ( this.innerWidget !== null ) {
75                        this.item = this.innerWidget.get('value');
76                    }
77                    this._destroyInnerWidget();
78                    this._showViewWidget();
79                    this.editButtonNode.iconNode.className = this.editButtonNode.iconNode.className.replace("rftIconAccept", "rftIconEdit");
80                    this.editButtonNode.set("label", "Edit");
81                } else {
82                    this._destroyInnerWidget();
83                    this._showEditWidget();
84                    this.editButtonNode.iconNode.className = this.editButtonNode.iconNode.className.replace("rftIconEdit", "rftIconAccept");
85                    this.editButtonNode.set("label", "Save");
86                }
87                this._editing = !this._editing;
88            },
89            toggleFold: function() {
90                if (!this.animation) {
91                    if (!domClass.contains(this.domNode, "fold")) {
92                        this.previousContentHeight = dojo.marginBox(this.innerNode).h;
93                        this.animation = fx.animateProperty({
94                            node: this.innerNode,
95                            duration: this.foldDuration[0],
96                            properties: {
97                                height: 1                       
98                            },
99                            onEnd: lang.hitch(this, function(){
100                                domClass.add(this.domNode, "fold");
101                                this.animation = null;
102                            })
103                        }).play();
104                        this.foldButtonNode.iconNode.className = this.foldButtonNode.iconNode.className.replace("rftIconHalfArrowUp", "rftIconHalfArrowDown");
105                    } else {
106                        this.animation = fx.animateProperty({
107                            node: this.innerNode,
108                            duration: this.foldDuration[1],
109                            properties: {
110                                height: this.previousContentHeight               
111                            },
112                            onEnd: lang.hitch(this, function(){
113                                domClass.remove(this.domNode, "fold");
114                                this.animation = null;
115                            })
116                        }).play();
117                        this.foldButtonNode.iconNode.className = this.foldButtonNode.iconNode.className.replace("rftIconHalfArrowDown", "rftIconHalfArrowUp");
118                    }
119                }
120            }
121
122        });
123    });
Note: See TracBrowser for help on using the repository browser.