source: Dev/trunk/src/client/qed-client/widgets/_ComplexValueMixin.js @ 511

Last change on this file since 511 was 511, checked in by hendrikvanantwerpen, 11 years ago
  • Added open item options to singe and multiple choice.
  • Question view widgets are forms again, because we needed the control.
  • Multiple and singel choice share their widgets.
  • QuestionEditorPreview? now has items that were already there close, but opens newly dropped items.
  • PreviewItems? will save modified value even when we change to view before the widget loses its focus (which causes a change event).
File size: 5.0 KB
Line 
1define([
2    "dijit/_Container",
3    "dijit/form/_FormMixin",
4    "dijit/registry",
5    "dojo/_base/array",
6    "dojo/_base/declare",
7    "dojo/_base/event",
8    "dojo/_base/lang",
9    "dojo/on"
10], function(_Container, _FormMixin, registry, array, declare, event, lang, on) {
11    return declare([_Container, _FormMixin], {
12        name: "",
13        value: null,
14        disabled: false,
15        readOnly: false,
16        _onChangeActive: false,
17        _onChangeHandle: null,
18        constructor: function() {
19            this._listenerMap = {};
20        },
21        buildRendering: function() {
22            // capture child change events
23            this.inherited(arguments);
24            this.own(on(this.domNode,'change',
25                        lang.hitch(this,'_handleChange')));
26        },
27        create: function() {
28            this.inherited(arguments);
29            this._onChangeActive = true;
30        },
31        postCreate: function() {
32            this.inherited(arguments);
33            if ( this.domNode.tagName.toLowerCase() !== "form" ) {
34                console.warn("Not scoping a _ComplexValueMixin in a form element can cause name clashes. E.g. radio buttons might stop working correctly. It is recommended to use <form> as the root element in your template for", this.declaredClass);
35            }
36            this.own(on(this.domNode, 'submit', lang.hitch(this,'_handleSubmit')));
37        },
38        startup: function() {
39            if (this._started) { return; }
40            this.inherited(arguments);
41        },
42        _setDisabledAttr: function(value) {
43            this._set("disabled", value);
44            array.forEach(this._getDescendantFormWidgets(), function(child) {
45                child.set("disabled", value);
46            });
47        },
48        // Yuk, this function is taken directly from _FromMixin only
49        // to add the priorityChange parameter
50        _getValueAttr: function() {
51            this.value = this.inherited(arguments);
52            return this.value;
53        },
54                _setValueAttr: function(obj, priorityChange) {
55            this.value = obj;
56
57                        var map = { };
58                        array.forEach(this._getDescendantFormWidgets(), function(widget){
59                                if(!widget.name){ return; }
60                                var entry = map[widget.name] || (map[widget.name] = [] );
61                                entry.push(widget);
62                        });
63
64                        for(var name in map){
65                                if (map.hasOwnProperty(name)) {
66                    var widgets = map[name],
67                        values = lang.getObject(name, false, obj);
68
69                    if (values === undefined) {
70                        continue;
71                    }
72                    values = [].concat(values);
73                    if (typeof widgets[0].checked === 'boolean') {
74                        array.forEach(widgets, function(w){
75                            w.set('value', array.indexOf(values, w._get('value')) !== -1, priorityChange);
76                        });
77                    } else if (widgets[0].multiple) {
78                        widgets[0].set('value', values, priorityChange);
79                    } else {
80                        array.forEach(widgets, function(w, i){
81                            w.set('value', values[i], priorityChange);
82                        });
83                    }
84                                }
85                        }
86                },
87        _setReadOnlyAttr: function(value) {
88            this._set("readOnly", value);
89            array.forEach(this._getDescendantFormWidgets(), function(child) {
90                child.set("readOnly", value);
91            });
92        },
93        focus: function() {
94            /*var children = this._getDescendantFormWidgets();
95            if ( children.length > 0 ) {
96                children[0].focus();
97            }*/
98        },
99        _handleChange: function(evt) {
100            if ( evt.target !== this.domNode ) {
101                this._onChange();
102                if ( evt ) { event.stop(evt); }
103                return false;
104            } else {
105                return evt;
106            }
107        },
108        _handleSubmit: function(evt) {
109            var node = this.domNode;
110            var widget;
111            while ( node ) {
112                node = node.parentNode;
113                widget = registry.byNode(node);
114                if ( widget && typeof widget._onSubmit === "function" ) {
115                    widget._onSubmit(evt);
116                    break;
117                }
118            }
119            if (evt) { event.stop(evt); }
120            return false;
121        },
122        _onChange: function() {
123            if ( this._onChangeActive &&
124                 !(this.readOnly || this.disabled) ) {
125                if ( this._onChangeHandle ) {
126                    this._onChangeHandle.cancel();
127                }
128                this._onChangeHandle = this.defer(function(){
129                    this._onChangeHandle = null;
130                    on.emit(this.domNode,'change',{
131                        target: this.domNode,
132                        value: this.get('value'),
133                        bubbles: true,
134                        cancellable: true
135                    });
136                });
137            }
138        }
139    });
140});
Note: See TracBrowser for help on using the repository browser.