Button.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dijit.form.Button"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.form.Button"] = true;
  8. dojo.provide("dijit.form.Button");
  9. dojo.require("dijit.form._FormWidget");
  10. dojo.require("dijit._Container");
  11. dojo.require("dijit._HasDropDown");
  12. dojo.declare("dijit.form.Button",
  13. dijit.form._FormWidget,
  14. {
  15. // summary:
  16. // Basically the same thing as a normal HTML button, but with special styling.
  17. // description:
  18. // Buttons can display a label, an icon, or both.
  19. // A label should always be specified (through innerHTML) or the label
  20. // attribute. It can be hidden via showLabel=false.
  21. // example:
  22. // | <button dojoType="dijit.form.Button" onClick="...">Hello world</button>
  23. //
  24. // example:
  25. // | var button1 = new dijit.form.Button({label: "hello world", onClick: foo});
  26. // | dojo.body().appendChild(button1.domNode);
  27. // label: HTML String
  28. // Text to display in button.
  29. // If the label is hidden (showLabel=false) then and no title has
  30. // been specified, then label is also set as title attribute of icon.
  31. label: "",
  32. // showLabel: Boolean
  33. // Set this to true to hide the label text and display only the icon.
  34. // (If showLabel=false then iconClass must be specified.)
  35. // Especially useful for toolbars.
  36. // If showLabel=true, the label will become the title (a.k.a. tooltip/hint) of the icon.
  37. //
  38. // The exception case is for computers in high-contrast mode, where the label
  39. // will still be displayed, since the icon doesn't appear.
  40. showLabel: true,
  41. // iconClass: String
  42. // Class to apply to DOMNode in button to make it display an icon
  43. iconClass: "",
  44. // type: String
  45. // Defines the type of button. "button", "submit", or "reset".
  46. type: "button",
  47. baseClass: "dijitButton",
  48. templateString: dojo.cache("dijit.form", "templates/Button.html", "<span class=\"dijit dijitReset dijitInline\"\n\t><span class=\"dijitReset dijitInline dijitButtonNode\"\n\t\tdojoAttachEvent=\"ondijitclick:_onButtonClick\"\n\t\t><span class=\"dijitReset dijitStretch dijitButtonContents\"\n\t\t\tdojoAttachPoint=\"titleNode,focusNode\"\n\t\t\trole=\"button\" aria-labelledby=\"${id}_label\"\n\t\t\t><span class=\"dijitReset dijitInline dijitIcon\" dojoAttachPoint=\"iconNode\"></span\n\t\t\t><span class=\"dijitReset dijitToggleButtonIconChar\">&#x25CF;</span\n\t\t\t><span class=\"dijitReset dijitInline dijitButtonText\"\n\t\t\t\tid=\"${id}_label\"\n\t\t\t\tdojoAttachPoint=\"containerNode\"\n\t\t\t></span\n\t\t></span\n\t></span\n\t><input ${!nameAttrSetting} type=\"${type}\" value=\"${value}\" class=\"dijitOffScreen\" tabIndex=\"-1\"\n\t\tdojoAttachPoint=\"valueNode\"\n/></span>\n"),
  49. attributeMap: dojo.delegate(dijit.form._FormWidget.prototype.attributeMap, {
  50. value: "valueNode"
  51. }),
  52. _onClick: function(/*Event*/ e){
  53. // summary:
  54. // Internal function to handle click actions
  55. if(this.disabled){
  56. return false;
  57. }
  58. this._clicked(); // widget click actions
  59. return this.onClick(e); // user click actions
  60. },
  61. _onButtonClick: function(/*Event*/ e){
  62. // summary:
  63. // Handler when the user activates the button portion.
  64. if(this._onClick(e) === false){ // returning nothing is same as true
  65. e.preventDefault(); // needed for checkbox
  66. }else if(this.type == "submit" && !(this.valueNode||this.focusNode).form){ // see if a nonform widget needs to be signalled
  67. for(var node=this.domNode; node.parentNode/*#5935*/; node=node.parentNode){
  68. var widget=dijit.byNode(node);
  69. if(widget && typeof widget._onSubmit == "function"){
  70. widget._onSubmit(e);
  71. break;
  72. }
  73. }
  74. }else if(this.valueNode){
  75. this.valueNode.click();
  76. e.preventDefault(); // cancel BUTTON click and continue with hidden INPUT click
  77. }
  78. },
  79. buildRendering: function(){
  80. this.inherited(arguments);
  81. dojo.setSelectable(this.focusNode, false);
  82. },
  83. _fillContent: function(/*DomNode*/ source){
  84. // Overrides _Templated._fillContent().
  85. // If button label is specified as srcNodeRef.innerHTML rather than
  86. // this.params.label, handle it here.
  87. // TODO: remove the method in 2.0, parser will do it all for me
  88. if(source && (!this.params || !("label" in this.params))){
  89. this.set('label', source.innerHTML);
  90. }
  91. },
  92. _setShowLabelAttr: function(val){
  93. if(this.containerNode){
  94. dojo.toggleClass(this.containerNode, "dijitDisplayNone", !val);
  95. }
  96. this._set("showLabel", val);
  97. },
  98. onClick: function(/*Event*/ e){
  99. // summary:
  100. // Callback for when button is clicked.
  101. // If type="submit", return true to perform submit, or false to cancel it.
  102. // type:
  103. // callback
  104. return true; // Boolean
  105. },
  106. _clicked: function(/*Event*/ e){
  107. // summary:
  108. // Internal overridable function for when the button is clicked
  109. },
  110. setLabel: function(/*String*/ content){
  111. // summary:
  112. // Deprecated. Use set('label', ...) instead.
  113. dojo.deprecated("dijit.form.Button.setLabel() is deprecated. Use set('label', ...) instead.", "", "2.0");
  114. this.set("label", content);
  115. },
  116. _setLabelAttr: function(/*String*/ content){
  117. // summary:
  118. // Hook for set('label', ...) to work.
  119. // description:
  120. // Set the label (text) of the button; takes an HTML string.
  121. this._set("label", content);
  122. this.containerNode.innerHTML = content;
  123. if(this.showLabel == false && !this.params.title){
  124. this.titleNode.title = dojo.trim(this.containerNode.innerText || this.containerNode.textContent || '');
  125. }
  126. },
  127. _setIconClassAttr: function(/*String*/ val){
  128. // Custom method so that icon node is hidden when not in use, to avoid excess padding/margin
  129. // appearing around it (even if it's a 0x0 sized <img> node)
  130. var oldVal = this.iconClass || "dijitNoIcon",
  131. newVal = val || "dijitNoIcon";
  132. dojo.replaceClass(this.iconNode, newVal, oldVal);
  133. this._set("iconClass", val);
  134. }
  135. });
  136. dojo.declare("dijit.form.DropDownButton", [dijit.form.Button, dijit._Container, dijit._HasDropDown], {
  137. // summary:
  138. // A button with a drop down
  139. //
  140. // example:
  141. // | <button dojoType="dijit.form.DropDownButton" label="Hello world">
  142. // | <div dojotype="dijit.Menu">...</div>
  143. // | </button>
  144. //
  145. // example:
  146. // | var button1 = new dijit.form.DropDownButton({ label: "hi", dropDown: new dijit.Menu(...) });
  147. // | dojo.body().appendChild(button1);
  148. //
  149. baseClass : "dijitDropDownButton",
  150. templateString: dojo.cache("dijit.form", "templates/DropDownButton.html", "<span class=\"dijit dijitReset dijitInline\"\n\t><span class='dijitReset dijitInline dijitButtonNode'\n\t\tdojoAttachEvent=\"ondijitclick:_onButtonClick\" dojoAttachPoint=\"_buttonNode\"\n\t\t><span class=\"dijitReset dijitStretch dijitButtonContents\"\n\t\t\tdojoAttachPoint=\"focusNode,titleNode,_arrowWrapperNode\"\n\t\t\trole=\"button\" aria-haspopup=\"true\" aria-labelledby=\"${id}_label\"\n\t\t\t><span class=\"dijitReset dijitInline dijitIcon\"\n\t\t\t\tdojoAttachPoint=\"iconNode\"\n\t\t\t></span\n\t\t\t><span class=\"dijitReset dijitInline dijitButtonText\"\n\t\t\t\tdojoAttachPoint=\"containerNode,_popupStateNode\"\n\t\t\t\tid=\"${id}_label\"\n\t\t\t></span\n\t\t\t><span class=\"dijitReset dijitInline dijitArrowButtonInner\"></span\n\t\t\t><span class=\"dijitReset dijitInline dijitArrowButtonChar\">&#9660;</span\n\t\t></span\n\t></span\n\t><input ${!nameAttrSetting} type=\"${type}\" value=\"${value}\" class=\"dijitOffScreen\" tabIndex=\"-1\"\n\t\tdojoAttachPoint=\"valueNode\"\n/></span>\n"),
  151. _fillContent: function(){
  152. // Overrides Button._fillContent().
  153. //
  154. // My inner HTML contains both the button contents and a drop down widget, like
  155. // <DropDownButton> <span>push me</span> <Menu> ... </Menu> </DropDownButton>
  156. // The first node is assumed to be the button content. The widget is the popup.
  157. if(this.srcNodeRef){ // programatically created buttons might not define srcNodeRef
  158. //FIXME: figure out how to filter out the widget and use all remaining nodes as button
  159. // content, not just nodes[0]
  160. var nodes = dojo.query("*", this.srcNodeRef);
  161. dijit.form.DropDownButton.superclass._fillContent.call(this, nodes[0]);
  162. // save pointer to srcNode so we can grab the drop down widget after it's instantiated
  163. this.dropDownContainer = this.srcNodeRef;
  164. }
  165. },
  166. startup: function(){
  167. if(this._started){ return; }
  168. // the child widget from srcNodeRef is the dropdown widget. Insert it in the page DOM,
  169. // make it invisible, and store a reference to pass to the popup code.
  170. if(!this.dropDown && this.dropDownContainer){
  171. var dropDownNode = dojo.query("[widgetId]", this.dropDownContainer)[0];
  172. this.dropDown = dijit.byNode(dropDownNode);
  173. delete this.dropDownContainer;
  174. }
  175. if(this.dropDown){
  176. dijit.popup.hide(this.dropDown);
  177. }
  178. this.inherited(arguments);
  179. },
  180. isLoaded: function(){
  181. // Returns whether or not we are loaded - if our dropdown has an href,
  182. // then we want to check that.
  183. var dropDown = this.dropDown;
  184. return (!!dropDown && (!dropDown.href || dropDown.isLoaded));
  185. },
  186. loadDropDown: function(){
  187. // Loads our dropdown
  188. var dropDown = this.dropDown;
  189. if(!dropDown){ return; }
  190. if(!this.isLoaded()){
  191. var handler = dojo.connect(dropDown, "onLoad", this, function(){
  192. dojo.disconnect(handler);
  193. this.openDropDown();
  194. });
  195. dropDown.refresh();
  196. }else{
  197. this.openDropDown();
  198. }
  199. },
  200. isFocusable: function(){
  201. // Overridden so that focus is handled by the _HasDropDown mixin, not by
  202. // the _FormWidget mixin.
  203. return this.inherited(arguments) && !this._mouseDown;
  204. }
  205. });
  206. dojo.declare("dijit.form.ComboButton", dijit.form.DropDownButton, {
  207. // summary:
  208. // A combination button and drop-down button.
  209. // Users can click one side to "press" the button, or click an arrow
  210. // icon to display the drop down.
  211. //
  212. // example:
  213. // | <button dojoType="dijit.form.ComboButton" onClick="...">
  214. // | <span>Hello world</span>
  215. // | <div dojoType="dijit.Menu">...</div>
  216. // | </button>
  217. //
  218. // example:
  219. // | var button1 = new dijit.form.ComboButton({label: "hello world", onClick: foo, dropDown: "myMenu"});
  220. // | dojo.body().appendChild(button1.domNode);
  221. //
  222. templateString: dojo.cache("dijit.form", "templates/ComboButton.html", "<table class=\"dijit dijitReset dijitInline dijitLeft\"\n\tcellspacing='0' cellpadding='0' role=\"presentation\"\n\t><tbody role=\"presentation\"><tr role=\"presentation\"\n\t\t><td class=\"dijitReset dijitStretch dijitButtonNode\" dojoAttachPoint=\"buttonNode\" dojoAttachEvent=\"ondijitclick:_onButtonClick,onkeypress:_onButtonKeyPress\"\n\t\t><div id=\"${id}_button\" class=\"dijitReset dijitButtonContents\"\n\t\t\tdojoAttachPoint=\"titleNode\"\n\t\t\trole=\"button\" aria-labelledby=\"${id}_label\"\n\t\t\t><div class=\"dijitReset dijitInline dijitIcon\" dojoAttachPoint=\"iconNode\" role=\"presentation\"></div\n\t\t\t><div class=\"dijitReset dijitInline dijitButtonText\" id=\"${id}_label\" dojoAttachPoint=\"containerNode\" role=\"presentation\"></div\n\t\t></div\n\t\t></td\n\t\t><td id=\"${id}_arrow\" class='dijitReset dijitRight dijitButtonNode dijitArrowButton'\n\t\t\tdojoAttachPoint=\"_popupStateNode,focusNode,_buttonNode\"\n\t\t\tdojoAttachEvent=\"onkeypress:_onArrowKeyPress\"\n\t\t\ttitle=\"${optionsTitle}\"\n\t\t\trole=\"button\" aria-haspopup=\"true\"\n\t\t\t><div class=\"dijitReset dijitArrowButtonInner\" role=\"presentation\"></div\n\t\t\t><div class=\"dijitReset dijitArrowButtonChar\" role=\"presentation\">&#9660;</div\n\t\t></td\n\t\t><td style=\"display:none !important;\"\n\t\t\t><input ${!nameAttrSetting} type=\"${type}\" value=\"${value}\" dojoAttachPoint=\"valueNode\"\n\t\t/></td></tr></tbody\n></table>\n"),
  223. attributeMap: dojo.mixin(dojo.clone(dijit.form.Button.prototype.attributeMap), {
  224. id: "",
  225. tabIndex: ["focusNode", "titleNode"],
  226. title: "titleNode"
  227. }),
  228. // optionsTitle: String
  229. // Text that describes the options menu (accessibility)
  230. optionsTitle: "",
  231. baseClass: "dijitComboButton",
  232. // Set classes like dijitButtonContentsHover or dijitArrowButtonActive depending on
  233. // mouse action over specified node
  234. cssStateNodes: {
  235. "buttonNode": "dijitButtonNode",
  236. "titleNode": "dijitButtonContents",
  237. "_popupStateNode": "dijitDownArrowButton"
  238. },
  239. _focusedNode: null,
  240. _onButtonKeyPress: function(/*Event*/ evt){
  241. // summary:
  242. // Handler for right arrow key when focus is on left part of button
  243. if(evt.charOrCode == dojo.keys[this.isLeftToRight() ? "RIGHT_ARROW" : "LEFT_ARROW"]){
  244. dijit.focus(this._popupStateNode);
  245. dojo.stopEvent(evt);
  246. }
  247. },
  248. _onArrowKeyPress: function(/*Event*/ evt){
  249. // summary:
  250. // Handler for left arrow key when focus is on right part of button
  251. if(evt.charOrCode == dojo.keys[this.isLeftToRight() ? "LEFT_ARROW" : "RIGHT_ARROW"]){
  252. dijit.focus(this.titleNode);
  253. dojo.stopEvent(evt);
  254. }
  255. },
  256. focus: function(/*String*/ position){
  257. // summary:
  258. // Focuses this widget to according to position, if specified,
  259. // otherwise on arrow node
  260. // position:
  261. // "start" or "end"
  262. if(!this.disabled){
  263. dijit.focus(position == "start" ? this.titleNode : this._popupStateNode);
  264. }
  265. }
  266. });
  267. dojo.declare("dijit.form.ToggleButton", dijit.form.Button, {
  268. // summary:
  269. // A button that can be in two states (checked or not).
  270. // Can be base class for things like tabs or checkbox or radio buttons
  271. baseClass: "dijitToggleButton",
  272. // checked: Boolean
  273. // Corresponds to the native HTML <input> element's attribute.
  274. // In markup, specified as "checked='checked'" or just "checked".
  275. // True if the button is depressed, or the checkbox is checked,
  276. // or the radio button is selected, etc.
  277. checked: false,
  278. attributeMap: dojo.mixin(dojo.clone(dijit.form.Button.prototype.attributeMap), {
  279. checked:"focusNode"
  280. }),
  281. _clicked: function(/*Event*/ evt){
  282. this.set('checked', !this.checked);
  283. },
  284. _setCheckedAttr: function(/*Boolean*/ value, /*Boolean?*/ priorityChange){
  285. this._set("checked", value);
  286. dojo.attr(this.focusNode || this.domNode, "checked", value);
  287. dijit.setWaiState(this.focusNode || this.domNode, "pressed", value);
  288. this._handleOnChange(value, priorityChange);
  289. },
  290. setChecked: function(/*Boolean*/ checked){
  291. // summary:
  292. // Deprecated. Use set('checked', true/false) instead.
  293. dojo.deprecated("setChecked("+checked+") is deprecated. Use set('checked',"+checked+") instead.", "", "2.0");
  294. this.set('checked', checked);
  295. },
  296. reset: function(){
  297. // summary:
  298. // Reset the widget's value to what it was at initialization time
  299. this._hasBeenBlurred = false;
  300. // set checked state to original setting
  301. this.set('checked', this.params.checked || false);
  302. }
  303. });
  304. }