source: Dev/trunk/src/client/dijit/form/_ButtonMixin.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 3.4 KB
Line 
1define([
2        "dojo/_base/declare", // declare
3        "dojo/dom", // dom.setSelectable
4        "dojo/has",
5        "../registry"        // registry.byNode
6], function(declare, dom, has, registry){
7
8        // module:
9        //              dijit/form/_ButtonMixin
10
11        var ButtonMixin = declare("dijit.form._ButtonMixin" + (has("dojo-bidi") ? "_NoBidi" : ""), null, {
12                // summary:
13                //              A mixin to add a thin standard API wrapper to a normal HTML button
14                // description:
15                //              A label should always be specified (through innerHTML) or the label attribute.
16                //
17                //              Attach points:
18                //
19                //              - focusNode (required): this node receives focus
20                //              - valueNode (optional): this node's value gets submitted with FORM elements
21                //              - containerNode (optional): this node gets the innerHTML assignment for label
22                // example:
23                // |    <button data-dojo-type="dijit/form/Button" onClick="...">Hello world</button>
24                // example:
25                // |    var button1 = new Button({label: "hello world", onClick: foo});
26                // |    dojo.body().appendChild(button1.domNode);
27
28                // label: HTML String
29                //              Content to display in button.
30                label: "",
31
32                // type: [const] String
33                //              Type of button (submit, reset, button, checkbox, radio)
34                type: "button",
35
36                __onClick: function(/*Event*/ e){
37                        // summary:
38                        //              Internal function to divert the real click onto the hidden INPUT that has a native default action associated with it
39                        // type:
40                        //              private
41                        e.stopPropagation();
42                        e.preventDefault();
43                        if(!this.disabled){
44                                // cannot use on.emit since button default actions won't occur
45                                this.valueNode.click(e);
46                        }
47                        return false;
48                },
49
50                _onClick: function(/*Event*/ e){
51                        // summary:
52                        //              Internal function to handle click actions
53                        if(this.disabled){
54                                e.stopPropagation();
55                                e.preventDefault();
56                                return false;
57                        }
58                        if(this.onClick(e) === false){
59                                e.preventDefault();
60                        }
61                        var cancelled = e.defaultPrevented;
62
63                        // Signal Form/Dialog to submit/close.  For 2.0, consider removing this code and instead making the Form/Dialog
64                        // listen for bubbled click events where evt.target.type == "submit" && !evt.defaultPrevented.
65                        if(!cancelled && this.type == "submit" && !(this.valueNode || this.focusNode).form){
66                                for(var node = this.domNode; node.parentNode; node = node.parentNode){
67                                        var widget = registry.byNode(node);
68                                        if(widget && typeof widget._onSubmit == "function"){
69                                                widget._onSubmit(e);
70                                                e.preventDefault(); // action has already occurred
71                                                cancelled = true;
72                                                break;
73                                        }
74                                }
75                        }
76
77                        return !cancelled;
78                },
79
80                postCreate: function(){
81                        this.inherited(arguments);
82                        dom.setSelectable(this.focusNode, false);
83                },
84
85                onClick: function(/*Event*/ /*===== e =====*/){
86                        // summary:
87                        //              Callback for when button is clicked.
88                        //              If type="submit", return true to perform submit, or false to cancel it.
89                        // type:
90                        //              callback
91                        return true;            // Boolean
92                },
93
94                _setLabelAttr: function(/*String*/ content){
95                        // summary:
96                        //              Hook for set('label', ...) to work.
97                        // description:
98                        //              Set the label (text) of the button; takes an HTML string.
99                        this._set("label", content);
100                        var labelNode = this.containerNode || this.focusNode;
101                        labelNode.innerHTML = content;
102                }
103        });
104
105        if(has("dojo-bidi")){
106                ButtonMixin = declare("dijit.form._ButtonMixin", ButtonMixin, {
107                        _setLabelAttr: function(){
108                                this.inherited(arguments);
109                                var labelNode = this.containerNode || this.focusNode;
110                                this.applyTextDir(labelNode);
111                        }
112                });
113        }
114
115        return ButtonMixin;
116});
Note: See TracBrowser for help on using the repository browser.