_ButtonMixin.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. define("dijit/form/_ButtonMixin", [
  2. "dojo/_base/declare", // declare
  3. "dojo/dom", // dom.setSelectable
  4. "dojo/_base/event", // event.stop
  5. "../registry" // registry.byNode
  6. ], function(declare, dom, event, registry){
  7. // module:
  8. // dijit/form/_ButtonMixin
  9. // summary:
  10. // A mixin to add a thin standard API wrapper to a normal HTML button
  11. return declare("dijit.form._ButtonMixin", 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. // Attach points:
  17. // focusNode (required): this node receives focus
  18. // valueNode (optional): this node's value gets submitted with FORM elements
  19. // containerNode (optional): this node gets the innerHTML assignment for label
  20. // example:
  21. // | <button data-dojo-type="dijit.form.Button" onClick="...">Hello world</button>
  22. //
  23. // example:
  24. // | var button1 = new dijit.form.Button({label: "hello world", onClick: foo});
  25. // | dojo.body().appendChild(button1.domNode);
  26. // label: HTML String
  27. // Content to display in button.
  28. label: "",
  29. // type: [const] String
  30. // Type of button (submit, reset, button, checkbox, radio)
  31. type: "button",
  32. _onClick: function(/*Event*/ e){
  33. // summary:
  34. // Internal function to handle click actions
  35. if(this.disabled){
  36. event.stop(e);
  37. return false;
  38. }
  39. var preventDefault = this.onClick(e) === false; // user click actions
  40. if(!preventDefault && this.type == "submit" && !(this.valueNode||this.focusNode).form){ // see if a non-form widget needs to be signalled
  41. for(var node=this.domNode; node.parentNode; node=node.parentNode){
  42. var widget=registry.byNode(node);
  43. if(widget && typeof widget._onSubmit == "function"){
  44. widget._onSubmit(e);
  45. preventDefault = true;
  46. break;
  47. }
  48. }
  49. }
  50. if(preventDefault){
  51. e.preventDefault();
  52. }
  53. return !preventDefault;
  54. },
  55. postCreate: function(){
  56. this.inherited(arguments);
  57. dom.setSelectable(this.focusNode, false);
  58. },
  59. onClick: function(/*Event*/ /*===== e =====*/){
  60. // summary:
  61. // Callback for when button is clicked.
  62. // If type="submit", return true to perform submit, or false to cancel it.
  63. // type:
  64. // callback
  65. return true; // Boolean
  66. },
  67. _setLabelAttr: function(/*String*/ content){
  68. // summary:
  69. // Hook for set('label', ...) to work.
  70. // description:
  71. // Set the label (text) of the button; takes an HTML string.
  72. this._set("label", content);
  73. (this.containerNode||this.focusNode).innerHTML = content;
  74. }
  75. });
  76. });