Debian Codegraph @

dijit/form/_RadioButtonMixin.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.forEach
3 "dojo/_base/declare", // declare
4 "dojo/dom-attr", // domAttr.set
5 "dojo/_base/lang", // lang.hitch
6 "dojo/query!css2", // query
7 "../registry" // registry.getEnclosingWidget
8 ], function(array, declare, domAttr, lang, query, registry){
9
10 // module:
11 // dijit/form/_RadioButtonMixin
12
13 return declare("dijit.form._RadioButtonMixin", null, {
14 // summary:
15 // Mixin to provide widget functionality for an HTML radio button
16
17 // type: [private] String
18 // type attribute on `<input>` node.
19 // Users should not change this value.
20 type: "radio",
21
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 || this.ownerDocument).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
38 _setCheckedAttr: function(/*Boolean*/ value){
39 // If I am being checked then have to deselect currently checked radio button
40 this.inherited(arguments);
41 if(!this._created){
42 return;
43 }
44 if(value){
45 array.forEach(this._getRelatedWidgets(), lang.hitch(this, function(widget){
46 if(widget != this && widget.checked){
47 widget.set('checked', false);
48 }
49 }));
50 }
51 },
52
53 _getSubmitValue: function(/*String*/ value){
54 return value == null ? "on" : value;
55 },
56
57 _onClick: function(/*Event*/ e){
58 if(this.checked || this.disabled){ // nothing to do
59 e.stopPropagation();
60 e.preventDefault();
61 return false;
62 }
63
64 if(this.readOnly){ // ignored by some browsers so we have to resync the DOM elements with widget values
65 e.stopPropagation();
66 e.preventDefault();
67 array.forEach(this._getRelatedWidgets(), lang.hitch(this, function(widget){
68 domAttr.set(this.focusNode || this.domNode, 'checked', widget.checked);
69 }));
70 return false;
71 }
72
73 // RadioButton has some unique logic since it must enforce only a single button being checked at once
74 // For this reason the "_onClick" method does not call this.inherited
75
76 var canceled = false;
77 var previouslyCheckedButton;
78
79 array.some(this._getRelatedWidgets(), function(radioButton){
80 if(radioButton.checked){
81 previouslyCheckedButton = radioButton;
82 return true;
83 }
84 return false;
85 });
86
87 // We want to set the post-click values correctly for any event handlers, but since
88 // the event handlers could revert them, we don't want to fully update the widget state
89 // yet and trigger notifications
90 this.checked = true;
91 previouslyCheckedButton && (previouslyCheckedButton.checked = false);
92
93 // Call event handlers
94 // If event handler prevents it, the clicked radio button will not be checked
95 if(this.onClick(e) === false || e.defaultPrevented){
96 canceled = true;
97 }
98
99 // Reset internal state to how it was before the click
100 this.checked = false;
101 previouslyCheckedButton && (previouslyCheckedButton.checked = true);
102
103 if(canceled){
104 e.preventDefault();
105 }else{
106 this.set('checked', true);
107 }
108
109 return !canceled;
110 }
111 });
112 });