@@ -34,8 +34,7 @@ var formResetHandler = function() {
34
34
form . find ( ".ui-checkboxradio" ) . checkboxradio ( "refresh" ) ;
35
35
} ) ;
36
36
} ,
37
- escapeId = new RegExp ( / ( [ ! " # $ % & ' ( ) * + , . / : ; < = > ? @ [ \] ^ ` { | } ~ ] ) / g ) ,
38
- forms = { } ;
37
+ escapeId = new RegExp ( / ( [ ! " # $ % & ' ( ) * + , . / : ; < = > ? @ [ \] ^ ` { | } ~ ] ) / g ) ;
39
38
40
39
$ . widget ( "ui.checkboxradio" , {
41
40
version : "@VERSION" ,
@@ -84,17 +83,19 @@ $.widget( "ui.checkboxradio", {
84
83
} ,
85
84
86
85
_create : function ( ) {
87
- this . formElement = $ ( this . element [ 0 ] . form ) . uniqueId ( ) ;
88
- this . formId = this . formElement . attr ( "id" ) ;
86
+ this . formParent = this . _getFormParent ( ) ;
89
87
90
- forms [ this . formId ] = forms [ this . formId ] || 0 ;
88
+ // We don't use _on and _off here because we want all the checkboxes in the same form to use
89
+ // single handler which handles all the checkboxradio widgets in the form
90
+ var formCount = this . formParent . data ( "uiCheckboxradioCount" ) || 0 ;
91
91
92
92
// We don't use _on and _off here because we want all the checkboxes in the same form to use
93
93
// single handler which handles all the checkboxradio widgets in the form
94
- if ( forms [ this . formId ] === 0 ) {
95
- this . formElement . on ( "reset." + this . widgetFullName , formResetHandler ) ;
94
+ if ( formCount === 0 ) {
95
+ this . formParent . on ( "reset." + this . widgetFullName , formResetHandler ) ;
96
96
}
97
- forms [ this . formId ] ++ ;
97
+
98
+ this . formParent . data ( "uiCheckboxradioCount" , formCount + 1 ) ;
98
99
99
100
if ( this . options . disabled == null ) {
100
101
this . options . disabled = this . element [ 0 ] . disabled || false ;
@@ -115,49 +116,50 @@ $.widget( "ui.checkboxradio", {
115
116
116
117
_findLabel : function ( ) {
117
118
var ancestor , labelSelector , id ,
118
- parent = this . element . closest ( "label" ) ;
119
+ parent ;
119
120
120
121
// Check control.labels first
121
122
if ( this . element [ 0 ] . labels !== undefined && this . element [ 0 ] . labels . length > 0 ) {
122
123
this . label = $ ( this . element [ 0 ] . labels [ 0 ] ) ;
123
- } else {
124
- parent = this . element . closest ( "label" ) ;
124
+ return ;
125
+ }
125
126
126
- if ( parent . length > 0 ) {
127
- this . label = parent ;
128
- this . parentLabel = true ;
129
- return ;
130
- }
127
+ parent = this . element . closest ( "label" ) ;
128
+
129
+ if ( parent . length > 0 ) {
130
+ this . label = parent ;
131
+ this . parentLabel = true ;
132
+ return ;
133
+ }
131
134
132
- // We don't search against the document in case the element
133
- // is disconnected from the DOM
134
- ancestor = this . element . parents ( ) . last ( ) ;
135
+ // We don't search against the document in case the element
136
+ // is disconnected from the DOM
137
+ ancestor = this . element . parents ( ) . last ( ) ;
135
138
136
- // Look for the label based on the id
137
- id = this . element . attr ( "id" ) ;
138
- if ( id ) {
139
- labelSelector = "label[for='" +
140
- this . element . attr ( "id" ) . replace ( escapeId , "\\$1" ) + "']" ;
141
- this . label = ancestor . find ( labelSelector ) ;
139
+ // Look for the label based on the id
140
+ id = this . element . attr ( "id" ) ;
141
+ if ( id ) {
142
+ labelSelector = "label[for='" +
143
+ this . element . attr ( "id" ) . replace ( escapeId , "\\$1" ) + "']" ;
144
+ this . label = ancestor . find ( labelSelector ) ;
142
145
143
- if ( ! this . label . length ) {
146
+ if ( ! this . label . length ) {
144
147
145
- // The label was not found, make sure ancestors exist. If they do check their
146
- // siblings, if they dont check the elements siblings
147
- ancestor = ancestor . length ? ancestor . siblings ( ) : this . element . siblings ( ) ;
148
+ // The label was not found, make sure ancestors exist. If they do check their
149
+ // siblings, if they dont check the elements siblings
150
+ ancestor = ancestor . length ? ancestor . siblings ( ) : this . element . siblings ( ) ;
148
151
149
- // Check if any of the new set of ancestors is the label
150
- this . label = ancestor . filter ( labelSelector ) ;
151
- if ( ! this . label . length ) {
152
+ // Check if any of the new set of ancestors is the label
153
+ this . label = ancestor . filter ( labelSelector ) ;
154
+ if ( ! this . label . length ) {
152
155
153
- // Still not found look inside the ancestors for the label
154
- this . label = ancestor . find ( labelSelector ) ;
155
- }
156
+ // Still not found look inside the ancestors for the label
157
+ this . label = ancestor . find ( labelSelector ) ;
156
158
}
157
159
}
158
- if ( ! this . label || ! this . label . length ) {
159
- $ . error ( "No label found for checkboxradio widget" ) ;
160
- }
160
+ }
161
+ if ( ! this . label || ! this . label . length ) {
162
+ $ . error ( "No label found for checkboxradio widget" ) ;
161
163
}
162
164
} ,
163
165
@@ -196,20 +198,44 @@ $.widget( "ui.checkboxradio", {
196
198
return this . label ;
197
199
} ,
198
200
201
+ _getFormParent : function ( element ) {
202
+ var parent ;
203
+
204
+ element = element || this . element [ 0 ] ;
205
+
206
+ if ( element . form && typeof element . form !== "string" ) {
207
+ return $ ( element . form ) ;
208
+ } else if ( ! element . form ) {
209
+ return $ ( "body" ) ;
210
+ }
211
+
212
+ // Support: IE8 only ( the rest of the method )
213
+ // IE8 supports the form property but not the form attribute worse yet it if you supply the
214
+ // form attribute it overwrites the form property with the string. Other supported browsers
215
+ // like all other IE's and android 2.3 support the form attribute not at all or partially
216
+ // we dont care in those cases because they still always return an element. We are not
217
+ // trying to fix the form attribute here only deal with the prop supplying a string.
218
+ parent = this . document . getElementByID ( element . form ) ;
219
+ if ( parent . length ) {
220
+ return $ ( parent ) ;
221
+ }
222
+ parent = $ ( element ) . closest ( "form" ) ;
223
+ if ( parent . length ) {
224
+ return parent ;
225
+ }
226
+ return $ ( "body" ) ;
227
+ } ,
228
+
199
229
_getRadioGroup : function ( ) {
200
230
var name = this . element [ 0 ] . name ,
201
- form = this . element [ 0 ] . form ,
231
+ that = this ,
202
232
radios = $ ( [ ] ) ;
233
+
203
234
if ( name ) {
204
235
name = name . replace ( escapeId , "\\$1" ) ;
205
- if ( form ) {
206
- radios = $ ( form ) . find ( "[name='" + name + "']" ) ;
207
- } else {
208
- radios = this . document . find ( "[name='" + name + "']" )
209
- . filter ( function ( ) {
210
- return ! this . form ;
211
- } ) ;
212
- }
236
+ radios = this . formParent . find ( "[name='" + name . replace ( escapeId , "\\$1" ) + "']" ) . filter ( function ( ) {
237
+ return that . _getFormParent ( this ) [ 0 ] === that . formParent [ 0 ] ;
238
+ } ) ;
213
239
}
214
240
return radios . not ( this . element ) ;
215
241
} ,
@@ -238,14 +264,17 @@ $.widget( "ui.checkboxradio", {
238
264
} ,
239
265
240
266
_destroy : function ( ) {
241
- if ( this . icon ) {
242
- this . icon . remove ( ) ;
243
- this . iconSpace . remove ( ) ;
267
+ var formCount = this . formParent . data ( "uiCheckboxradioCount" ) - 1 ;
268
+
269
+ this . formParent . data ( "uiCheckboxradioCount" , formCount ) ;
270
+
271
+ if ( formCount === 0 ) {
272
+ this . formParent . off ( "reset." + this . widgetFullName , formResetHandler ) ;
244
273
}
245
274
246
- forms [ this . formId ] -- ;
247
- if ( forms [ this . formId ] === 0 ) {
248
- this . formElement . off ( "reset." + this . widgetFullName , formResetHandler ) ;
275
+ if ( formCount === 0 ) {
276
+ this . icon . remove ( ) ;
277
+ this . iconSpace . remove ( ) ;
249
278
}
250
279
} ,
251
280
0 commit comments