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

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

Support data validation and increase dendency separation.

Added json-schema validation before documents are saved. Acts both as
a safe-guard and as a reference of the data model.

Added directory for server related material. For now it contains scripts
to configure CouchDB and check validity of documents against the schema.

Started separating out parts that depend on the data model from parts
that do not.

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