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

Last change on this file since 493 was 493, checked in by hendrikvanantwerpen, 11 years ago
  • _ComplexValueMixin propagates priorityChange to children.
  • Deserialize updated docs after save too.
  • Validate to false if definitions are missing.
File size: 4.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        disabled: false,
14        readOnly: false,
15        _changeActive: false,
16        postCreate: function() {
17            this.inherited(arguments);
18            if ( this.domNode.tagName.toLowerCase() !== "form" ) {
19                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);
20            }
21            this.own(on(this.domNode, 'submit', lang.hitch(this,'_handleSubmit')));
22        },
23        startup: function() {
24            if (this._started) { return; }
25            this.inherited(arguments);
26            this._connectChildren();
27            this._changeActive = true;
28        },
29        _connectChildren: function() {
30            var children = this._getDescendantFormWidgets();
31            array.forEach(children, lang.hitch(this,function(child) {
32                this.own(child.on('change',lang.hitch(this,'_handleChange')));
33            }));
34        },
35        _setDisabledAttr: function(value) {
36            this._set("disabled", value);
37            array.forEach(this._getDescendantFormWidgets(), function(child) {
38                child.set("disabled", value);
39            });
40        },
41        // Yuk, this function is taken directly from _FromMixin only
42        // to add the priorityChange parameter
43                _setValueAttr: function(obj, priorityChange) {
44                        var map = { };
45                        array.forEach(this._getDescendantFormWidgets(), function(widget){
46                                if(!widget.name){ return; }
47                                var entry = map[widget.name] || (map[widget.name] = [] );
48                                entry.push(widget);
49                        });
50
51                        for(var name in map){
52                                if (map.hasOwnProperty(name)) {
53                    var widgets = map[name],
54                        values = lang.getObject(name, false, obj);
55
56                    if (values === undefined) {
57                        continue;
58                    }
59                    values = [].concat(values);
60                    if (typeof widgets[0].checked === 'boolean') {
61                        array.forEach(widgets, function(w){
62                            w.set('value', array.indexOf(values, w._get('value')) !== -1, priorityChange);
63                        });
64                    } else if (widgets[0].multiple) {
65                        widgets[0].set('value', values, priorityChange);
66                    } else {
67                        array.forEach(widgets, function(w, i){
68                            w.set('value', values[i], priorityChange);
69                        });
70                    }
71                                }
72                        }
73                },
74        _setReadOnlyAttr: function(value) {
75            this._set("readOnly", value);
76            array.forEach(this._getDescendantFormWidgets(), function(child) {
77                child.set("readOnly", value);
78            });
79        },
80        focus: function() {
81            var children = this._getDescendantFormWidgets();
82            if ( children.length > 0 ) {
83                children[0].focus();
84            }
85        },
86        _handleChange: function() {
87            if ( this._changeActive ) {
88                this.onChange(this.get('value'));
89            }
90        },
91        onChange: function(){},
92        _handleSubmit: function(evt) {
93            var node = this.domNode;
94            var widget;
95            while ( node ) {
96                node = node.parentNode;
97                widget = registry.byNode(node);
98                if ( widget && typeof widget._onSubmit === "function" ) {
99                    widget._onSubmit(evt);
100                    break;
101                }
102            }
103            if (evt) { event.stop(evt); }
104            return false;
105        }
106    });
107});
Note: See TracBrowser for help on using the repository browser.