_RadioButtonMixin.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. define("dijit/form/_RadioButtonMixin", [
  2. "dojo/_base/array", // array.forEach
  3. "dojo/_base/declare", // declare
  4. "dojo/dom-attr", // domAttr.set
  5. "dojo/_base/event", // event.stop
  6. "dojo/_base/lang", // lang.hitch
  7. "dojo/query", // query
  8. "dojo/_base/window", // win.doc
  9. "../registry" // registry.getEnclosingWidget
  10. ], function(array, declare, domAttr, event, lang, query, win, registry){
  11. // module:
  12. // dijit/form/_RadioButtonMixin
  13. // summary:
  14. // Mixin to provide widget functionality for an HTML radio button
  15. return declare("dijit.form._RadioButtonMixin", null, {
  16. // summary:
  17. // Mixin to provide widget functionality for an HTML radio button
  18. // type: [private] String
  19. // type attribute on <input> node.
  20. // Users should not change this value.
  21. type: "radio",
  22. _getRelatedWidgets: function(){
  23. // Private function needed to help iterate over all radio buttons in a group.
  24. var ary = [];
  25. query("input[type=radio]", this.focusNode.form || win.doc).forEach( // can't use name= since query doesn't support [] in the name
  26. lang.hitch(this, function(inputNode){
  27. if(inputNode.name == this.name && inputNode.form == this.focusNode.form){
  28. var widget = registry.getEnclosingWidget(inputNode);
  29. if(widget){
  30. ary.push(widget);
  31. }
  32. }
  33. })
  34. );
  35. return ary;
  36. },
  37. _setCheckedAttr: function(/*Boolean*/ value){
  38. // If I am being checked then have to deselect currently checked radio button
  39. this.inherited(arguments);
  40. if(!this._created){ return; }
  41. if(value){
  42. array.forEach(this._getRelatedWidgets(), lang.hitch(this, function(widget){
  43. if(widget != this && widget.checked){
  44. widget.set('checked', false);
  45. }
  46. }));
  47. }
  48. },
  49. _onClick: function(/*Event*/ e){
  50. if(this.checked || this.disabled){ // nothing to do
  51. event.stop(e);
  52. return false;
  53. }
  54. if(this.readOnly){ // ignored by some browsers so we have to resync the DOM elements with widget values
  55. event.stop(e);
  56. array.forEach(this._getRelatedWidgets(), lang.hitch(this, function(widget){
  57. domAttr.set(this.focusNode || this.domNode, 'checked', widget.checked);
  58. }));
  59. return false;
  60. }
  61. return this.inherited(arguments);
  62. }
  63. });
  64. });