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

Last change on this file since 406 was 405, checked in by hendrikvanantwerpen, 13 years ago

Guarded widget startup() functions.

File size: 5.8 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                if ( this._started ){ return; }
27                this.inherited(arguments);
28                this.foldButtonNode.onClick = lang.hitch(this, this.toggleFold);
29                this.removeButtonNode.onClick = lang.hitch(this, "onClose");
30                this.editButtonNode.onClick = lang.hitch(this, this.toggleEdit);
31                if (this.item) {
32                    this._showViewWidget();
33                } else {
34                    throw "No data supplied to create an innerWidget!";
35                }
36            },
37            _destroyInnerWidget: function() {
38                if ( this.innerWidget !== null ) {
39                    this.innerWidget.destroyRecursive();
40                }
41            },
42            _showViewWidget: function() {
43                var factory = new ContentWidgetFactory();
44                this.innerWidget = factory.createViewWidget( this.item );
45                if ( this.innerWidget !== null ) {
46                    this.innerWidget.placeAt(this.containerNode);
47                    this.innerWidget.startup();
48                    this.innerWidget.set('readOnly',true);
49                }
50                this.titleNode.innerHTML = (this.item.code||"(no node)")+" : "+this.item.type+" [preview]";
51            },
52            _showEditWidget: function() {
53                var factory = new ContentWidgetFactory();
54                this.innerWidget = factory.createEditWidget( this.item );
55                if ( this.innerWidget !== null ) {
56                    this.innerWidget.placeAt(this.containerNode);
57                    this.innerWidget.startup();
58                }
59                this.titleNode.innerHTML = (this.item.code||"(no node)")+" : "+this.item.type+" [editing]";
60            },
61            onClose: function() {},
62            _getValueAttr: function(value) {
63                return this.item;
64            },
65            _setValueAttr: function(value) {
66                this.item = value;
67                this._destroyInnerWidget();
68                if ( this._editing ) {
69                    this._showEditWidget();
70                } else {
71                    this._showViewWidget();
72                }
73            },
74            toggleEdit: function() {
75                if(this._editing) {
76                    if ( this.innerWidget !== null ) {
77                        this.item = this.innerWidget.get('value');
78                    }
79                    this._destroyInnerWidget();
80                    this._showViewWidget();
81                    this.editButtonNode.iconNode.className = this.editButtonNode.iconNode.className.replace("rftIconAccept", "rftIconEdit");
82                    this.editButtonNode.set("label", "Edit");
83                } else {
84                    this._destroyInnerWidget();
85                    this._showEditWidget();
86                    this.editButtonNode.iconNode.className = this.editButtonNode.iconNode.className.replace("rftIconEdit", "rftIconAccept");
87                    this.editButtonNode.set("label", "Done");
88                }
89                this._editing = !this._editing;
90            },
91            toggleFold: function() {
92                if (!this.animation) {
93                    if (!domClass.contains(this.domNode, "fold")) {
94                        this.previousContentHeight = dojo.marginBox(this.innerNode).h;
95                        this.animation = fx.animateProperty({
96                            node: this.innerNode,
97                            duration: this.foldDuration[0],
98                            properties: {
99                                height: 1                       
100                            },
101                            onEnd: lang.hitch(this, function(){
102                                domClass.add(this.domNode, "fold");
103                                this.animation = null;
104                            })
105                        }).play();
106                        this.foldButtonNode.iconNode.className = this.foldButtonNode.iconNode.className.replace("rftIconHalfArrowUp", "rftIconHalfArrowDown");
107                    } else {
108                        this.animation = fx.animateProperty({
109                            node: this.innerNode,
110                            duration: this.foldDuration[1],
111                            properties: {
112                                height: this.previousContentHeight               
113                            },
114                            onEnd: lang.hitch(this, function(){
115                                domClass.remove(this.domNode, "fold");
116                                this.animation = null;
117                            })
118                        }).play();
119                        this.foldButtonNode.iconNode.className = this.foldButtonNode.iconNode.className.replace("rftIconHalfArrowDown", "rftIconHalfArrowUp");
120                    }
121                }
122            }
123
124        });
125    });
Note: See TracBrowser for help on using the repository browser.