1 | define([ |
---|
2 | "require", |
---|
3 | "dojo/_base/declare", // declare |
---|
4 | "dojo/dom-class", // domClass.toggle |
---|
5 | "dojo/has", // has("dijit-legacy-requires") |
---|
6 | "dojo/_base/kernel", // kernel.deprecated |
---|
7 | "dojo/_base/lang", // lang.trim |
---|
8 | "dojo/ready", |
---|
9 | "./_FormWidget", |
---|
10 | "./_ButtonMixin", |
---|
11 | "dojo/text!./templates/Button.html" |
---|
12 | ], function(require, declare, domClass, has, kernel, lang, ready, _FormWidget, _ButtonMixin, template){ |
---|
13 | |
---|
14 | // module: |
---|
15 | // dijit/form/Button |
---|
16 | |
---|
17 | // Back compat w/1.6, remove for 2.0 |
---|
18 | if(has("dijit-legacy-requires")){ |
---|
19 | ready(0, function(){ |
---|
20 | var requires = ["dijit/form/DropDownButton", "dijit/form/ComboButton", "dijit/form/ToggleButton"]; |
---|
21 | require(requires); // use indirection so modules not rolled into a build |
---|
22 | }); |
---|
23 | } |
---|
24 | |
---|
25 | var Button = declare("dijit.form.Button" + (has("dojo-bidi") ? "_NoBidi" : ""), [_FormWidget, _ButtonMixin], { |
---|
26 | // summary: |
---|
27 | // Basically the same thing as a normal HTML button, but with special styling. |
---|
28 | // description: |
---|
29 | // Buttons can display a label, an icon, or both. |
---|
30 | // A label should always be specified (through innerHTML) or the label |
---|
31 | // attribute. It can be hidden via showLabel=false. |
---|
32 | // example: |
---|
33 | // | <button data-dojo-type="dijit/form/Button" onClick="...">Hello world</button> |
---|
34 | // |
---|
35 | // example: |
---|
36 | // | var button1 = new Button({label: "hello world", onClick: foo}); |
---|
37 | // | dojo.body().appendChild(button1.domNode); |
---|
38 | |
---|
39 | // showLabel: Boolean |
---|
40 | // Set this to true to hide the label text and display only the icon. |
---|
41 | // (If showLabel=false then iconClass must be specified.) |
---|
42 | // Especially useful for toolbars. |
---|
43 | // If showLabel=true, the label will become the title (a.k.a. tooltip/hint) of the icon. |
---|
44 | // |
---|
45 | // The exception case is for computers in high-contrast mode, where the label |
---|
46 | // will still be displayed, since the icon doesn't appear. |
---|
47 | showLabel: true, |
---|
48 | |
---|
49 | // iconClass: String |
---|
50 | // Class to apply to DOMNode in button to make it display an icon |
---|
51 | iconClass: "dijitNoIcon", |
---|
52 | _setIconClassAttr: { node: "iconNode", type: "class" }, |
---|
53 | |
---|
54 | baseClass: "dijitButton", |
---|
55 | |
---|
56 | templateString: template, |
---|
57 | |
---|
58 | // Map widget attributes to DOMNode attributes. |
---|
59 | _setValueAttr: "valueNode", |
---|
60 | _setNameAttr: function(name){ |
---|
61 | // avoid breaking existing subclasses where valueNode undefined. Perhaps in 2.0 require it to be defined? |
---|
62 | if(this.valueNode){ |
---|
63 | this.valueNode.setAttribute("name", name); |
---|
64 | } |
---|
65 | }, |
---|
66 | |
---|
67 | _fillContent: function(/*DomNode*/ source){ |
---|
68 | // Overrides _Templated._fillContent(). |
---|
69 | // If button label is specified as srcNodeRef.innerHTML rather than |
---|
70 | // this.params.label, handle it here. |
---|
71 | // TODO: remove the method in 2.0, parser will do it all for me |
---|
72 | if(source && (!this.params || !("label" in this.params))){ |
---|
73 | var sourceLabel = lang.trim(source.innerHTML); |
---|
74 | if(sourceLabel){ |
---|
75 | this.label = sourceLabel; // _applyAttributes will be called after buildRendering completes to update the DOM |
---|
76 | } |
---|
77 | } |
---|
78 | }, |
---|
79 | |
---|
80 | _setShowLabelAttr: function(val){ |
---|
81 | if(this.containerNode){ |
---|
82 | domClass.toggle(this.containerNode, "dijitDisplayNone", !val); |
---|
83 | } |
---|
84 | this._set("showLabel", val); |
---|
85 | }, |
---|
86 | |
---|
87 | setLabel: function(/*String*/ content){ |
---|
88 | // summary: |
---|
89 | // Deprecated. Use set('label', ...) instead. |
---|
90 | kernel.deprecated("dijit.form.Button.setLabel() is deprecated. Use set('label', ...) instead.", "", "2.0"); |
---|
91 | this.set("label", content); |
---|
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 | // If the label is hidden (showLabel=false) then and no title has |
---|
100 | // been specified, then label is also set as title attribute of icon. |
---|
101 | this.inherited(arguments); |
---|
102 | if(!this.showLabel && !("title" in this.params)){ |
---|
103 | this.titleNode.title = lang.trim(this.containerNode.innerText || this.containerNode.textContent || ''); |
---|
104 | } |
---|
105 | } |
---|
106 | }); |
---|
107 | |
---|
108 | if(has("dojo-bidi")){ |
---|
109 | Button = declare("dijit.form.Button", Button, { |
---|
110 | _setLabelAttr: function(/*String*/ content){ |
---|
111 | this.inherited(arguments); |
---|
112 | if(this.titleNode.title){ |
---|
113 | this.applyTextDir(this.titleNode, this.titleNode.title); |
---|
114 | } |
---|
115 | }, |
---|
116 | |
---|
117 | _setTextDirAttr: function(/*String*/ textDir){ |
---|
118 | if(this._created && this.textDir != textDir){ |
---|
119 | this._set("textDir", textDir); |
---|
120 | this._setLabelAttr(this.label); // call applyTextDir on both focusNode and titleNode |
---|
121 | } |
---|
122 | } |
---|
123 | }); |
---|
124 | } |
---|
125 | |
---|
126 | return Button; |
---|
127 | }); |
---|