_RadioButtonMixin.js.uncompressed.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "../registry" // registry.getEnclosingWidget
  9. ], function(array, declare, domAttr, event, lang, query, registry){
  10. // module:
  11. // dijit/form/_RadioButtonMixin
  12. return declare("dijit.form._RadioButtonMixin", null, {
  13. // summary:
  14. // Mixin to provide widget functionality for an HTML radio button
  15. // type: [private] String
  16. // type attribute on `<input>` node.
  17. // Users should not change this value.
  18. type: "radio",
  19. _getRelatedWidgets: function(){
  20. // Private function needed to help iterate over all radio buttons in a group.
  21. var ary = [];
  22. query("input[type=radio]", this.focusNode.form || this.ownerDocument).forEach( // can't use name= since query doesn't support [] in the name
  23. lang.hitch(this, function(inputNode){
  24. if(inputNode.name == this.name && inputNode.form == this.focusNode.form){
  25. var widget = registry.getEnclosingWidget(inputNode);
  26. if(widget){
  27. ary.push(widget);
  28. }
  29. }
  30. })
  31. );
  32. return ary;
  33. },
  34. _setCheckedAttr: function(/*Boolean*/ value){
  35. // If I am being checked then have to deselect currently checked radio button
  36. this.inherited(arguments);
  37. if(!this._created){ return; }
  38. if(value){
  39. array.forEach(this._getRelatedWidgets(), lang.hitch(this, function(widget){
  40. if(widget != this && widget.checked){
  41. widget.set('checked', false);
  42. }
  43. }));
  44. }
  45. },
  46. _getSubmitValue: function(/*String*/ value){
  47. return value === null ? "on" : value;
  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. });