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

Last change on this file since 378 was 378, checked in by jkraaijeveld, 13 years ago

Created a factory for the inner widgets on the Question edit screen - can be easily made more generic if necessary too.

File size: 3.8 KB
Line 
1define([
2        'dojo/_base/declare',
3        'dojo/_base/fx',
4        'dijit/_WidgetBase',
5        'dojo/dom-class',
6        'dojo/_base/lang',
7        'dojo/on',
8        'dijit/_TemplatedMixin',
9        'dijit/_WidgetsInTemplateMixin',
10        'dijit/form/TextBox',
11        'rft/ui/InnerWidgetFactory',
12        'dojo/text!./templates/QuestionEditorPreviewItem.html',
13        ], function(declare, fx, _WidgetBase, domClass, lang, on, _TemplatedMixin, _WidgetsInTemplateMixin, TextBox, InnerWidgetFactory, templateFull) {
14                return declare("rft.ui.QuestionEditorPreviewItem", [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
15
16                        version: "full",
17                        templateString: templateFull,
18                        baseClass: "surveyEditorPreviewItem",
19                        previousContentHeight: 200,
20                        innerWidget: null,
21                        foldDuration: [250, 250],
22                        animation: null,
23                        _editing: false,
24
25                        postCreate: function() {
26                                this.foldButtonNode.onClick = lang.hitch(this, this.toggleFold);
27                                this.removeButtonNode.onClick = lang.hitch(this, this.removeObject);
28                                this.editButtonNode.onClick = lang.hitch(this, this.toggleEdit);
29                                if (this.item) {
30                                        this._createInnerWidget();
31                                } else {
32                                        throw "No data supplied to create an innerWidget off of!";
33                                }
34
35                                on(this.innerWidget, "onSetTitle", function(title) {
36                                        this.titleNode.innerHTML = title;
37                                });
38                        },
39                        toggleEdit: function() {
40                                if(this._editing) {
41                                        this.editButtonNode.iconNode.className = this.editButtonNode.iconNode.className.replace("rftIconAccept", "rftIconEdit");
42                                        this.editButtonNode.set("label", "Edit");
43                                }
44                                else {
45                                        this.editButtonNode.iconNode.className = this.editButtonNode.iconNode.className.replace("rftIconEdit", "rftIconAccept");
46                                        this.editButtonNode.set("label", "Save");
47                                }
48                                this._editing = !this._editing;
49                        },
50                        toggleFold: function() {
51                                if (!this.animation) {
52                                        if (!domClass.contains(this.domNode, "fold")) {
53                                                this.previousContentHeight = dojo.marginBox(this.innerNode).h;
54                                                this.animation = fx.animateProperty({
55                                                        node: this.innerNode,
56                                                        duration: this.foldDuration[0],
57                                                        properties: {
58                                                                height: 1                                               
59                                                        },
60                                                        onEnd: lang.hitch(this, function(){
61                                                                domClass.add(this.domNode, "fold");
62                                                                this.animation = null;
63                                                        })
64                                                }).play();
65                                                this.foldButtonNode.iconNode.className = this.foldButtonNode.iconNode.className.replace("rftIconHalfArrowUp", "rftIconHalfArrowDown");
66                                        } else {
67                                                this.animation = fx.animateProperty({
68                                                        node: this.innerNode,
69                                                        duration: this.foldDuration[1],
70                                                        properties: {
71                                                                height: this.previousContentHeight                             
72                                                        },
73                                                        onEnd: lang.hitch(this, function(){
74                                                                domClass.remove(this.domNode, "fold");
75                                                                this.animation = null;
76                                                        })
77                                                }).play();
78                                                this.foldButtonNode.iconNode.className = this.foldButtonNode.iconNode.className.replace("rftIconHalfArrowDown", "rftIconHalfArrowUp");
79                                        }
80                                }
81                        },
82                        getContent: function() {
83                                return this.innerWidget.getObject();
84                        },
85                        removeObject: function(widget) {
86                                this.destroyRecursive();
87                        },
88                        _createInnerWidget: function() {
89                                // This always creates a textbox as innerwidget pending creation of actual innerWidgets.
90                                // Introduce a better way to create these widgets than a switch statement, based on item.widgetType? Perhaps "new eval(item.widgetType)({});" ?
91
92                                this.innerWidget = new InnerWidgetFactory().createWidget( this.item );
93                                /*
94                                this.innerWidget = eval("new "+ this.item.type + "({ disabled: true });");
95                                this.innerWidget.setContent(this.item.data);
96                                this.titleNode.innerHTML = this.item.type;
97                                /*
98                                switch (this.item.widgetType) {
99                                        default:
100                                        this.innerWidget = new HeaderItem({ disabled: true });
101                                        this.titleNode.innerHTML = "Header";
102                                        break;
103                                }*/
104                                this.innerWidget.placeAt(this.containerNode);
105                        }
106
107                });
108});
Note: See TracBrowser for help on using the repository browser.