@@ -2,8 +2,14 @@ import jquery from 'jquery';
22
33const $ = jquery || window . jQuery || window . $ ;
44
5- function getClasses ( options , id ) {
6- const { state, size, disabled, readonly, indeterminate, inverse } = options ;
5+ function getClasses ( {
6+ state,
7+ size,
8+ disabled,
9+ readonly,
10+ indeterminate,
11+ inverse,
12+ } , id ) {
713 return [
814 state ? 'on' : 'off' ,
915 size ,
@@ -12,10 +18,9 @@ function getClasses(options, id) {
1218 indeterminate ? 'indeterminate' : undefined ,
1319 inverse ? 'inverse' : undefined ,
1420 id ? `id-${ id } ` : undefined ,
15- ] . filter ( v => v == null ) ;
21+ ] . filter ( ( v ) => v == null ) ;
1622}
1723
18-
1924function prvgetElementOptions ( ) {
2025 return {
2126 state : this . $element . is ( ':checked' ) ,
@@ -84,23 +89,24 @@ function prvgetClass(name) {
8489function prvinit ( ) {
8590 const init = ( ) => {
8691 this . setPrevOptions ( ) ;
87- this ::prvwidth ( ) ;
88- this ::prvcontainerPosition ( ) ;
92+ prvwidth . call ( this ) ;
93+ prvcontainerPosition . call ( this ) ;
94+
8995 setTimeout ( ( ) => (
90- this . options . animate &&
91- this . $wrapper . addClass ( this ::prvgetClass ( 'animate' ) ,
92- ) ) , 50 ) ;
96+ this . options . animate
97+ && this . $wrapper . addClass ( prvgetClass . call ( this , 'animate' ) ) ) , 50 ) ;
9398 } ;
9499 if ( this . $wrapper . is ( ':visible' ) ) {
95100 init ( ) ;
96101 return ;
97102 }
98103 const initInterval = window . setInterval (
99104 ( ) => (
100- this . $wrapper . is ( ':visible' ) &&
101- ( init ( ) || true ) &&
102- window . clearInterval ( initInterval )
103- ) , 50 ) ;
105+ this . $wrapper . is ( ':visible' )
106+ && ( init ( ) || true )
107+ && window . clearInterval ( initInterval )
108+ ) , 50 ,
109+ ) ;
104110}
105111
106112function prvelementHandlers ( ) {
@@ -110,7 +116,7 @@ function prvelementHandlers() {
110116 'previousState.bootstrapSwitch' : ( ) => {
111117 this . options = this . prevOptions ;
112118 if ( this . options . indeterminate ) {
113- this . $wrapper . addClass ( this :: prvgetClass ( 'indeterminate' ) ) ;
119+ this . $wrapper . addClass ( prvgetClass . call ( this , 'indeterminate' ) ) ;
114120 }
115121 this . $element
116122 . prop ( 'checked' , this . options . state )
@@ -121,14 +127,14 @@ function prvelementHandlers() {
121127 event . preventDefault ( ) ;
122128 event . stopImmediatePropagation ( ) ;
123129 const state = this . $element . is ( ':checked' ) ;
124- this :: prvcontainerPosition ( state ) ;
130+ prvcontainerPosition . call ( this , state ) ;
125131 if ( state === this . options . state ) {
126132 return ;
127133 }
128134 this . options . state = state ;
129135 this . $wrapper
130- . toggleClass ( this :: prvgetClass ( 'off' ) )
131- . toggleClass ( this :: prvgetClass ( 'on' ) ) ;
136+ . toggleClass ( prvgetClass . call ( this , 'off' ) )
137+ . toggleClass ( prvgetClass . call ( this , 'on' ) ) ;
132138 if ( ! skip ) {
133139 if ( this . $element . is ( ':radio' ) ) {
134140 $ ( `[name="${ this . $element . attr ( 'name' ) } "]` )
@@ -142,12 +148,12 @@ function prvelementHandlers() {
142148
143149 'focus.bootstrapSwitch' : ( event ) => {
144150 event . preventDefault ( ) ;
145- this . $wrapper . addClass ( this :: prvgetClass ( 'focused' ) ) ;
151+ this . $wrapper . addClass ( prvgetClass . call ( this , 'focused' ) ) ;
146152 } ,
147153
148154 'blur.bootstrapSwitch' : ( event ) => {
149155 event . preventDefault ( ) ;
150- this . $wrapper . removeClass ( this :: prvgetClass ( 'focused' ) ) ;
156+ this . $wrapper . removeClass ( prvgetClass . call ( this , 'focused' ) ) ;
151157 } ,
152158
153159 'keydown.bootstrapSwitch' : ( event ) => {
@@ -192,7 +198,7 @@ function prvlabelHandlers() {
192198 event . stopPropagation ( ) ;
193199 dragStart = ( event . pageX || event . originalEvent . touches [ 0 ] . pageX ) - parseInt ( this . $container . css ( 'margin-left' ) , 10 ) ;
194200 if ( this . options . animate ) {
195- this . $wrapper . removeClass ( this :: prvgetClass ( 'animate' ) ) ;
201+ this . $wrapper . removeClass ( prvgetClass . call ( this , 'animate' ) ) ;
196202 }
197203 this . $element . trigger ( 'focus.bootstrapSwitch' ) ;
198204 } ,
@@ -210,7 +216,7 @@ function prvlabelHandlers() {
210216 if ( ! dragStart ) { return ; }
211217 event . preventDefault ( ) ;
212218 if ( this . options . animate ) {
213- this . $wrapper . addClass ( this :: prvgetClass ( 'animate' ) ) ;
219+ this . $wrapper . addClass ( prvgetClass . call ( this , 'animate' ) ) ;
214220 }
215221 if ( dragEnd ) {
216222 const state = dragEnd > - ( this . privateHandleWidth / 2 ) ;
@@ -266,40 +272,39 @@ function prvformHandler() {
266272
267273function prvgetClasses ( classes ) {
268274 if ( ! Array . isArray ( classes ) ) {
269- return [ this :: prvgetClass ( classes ) ] ;
275+ return [ prvgetClass . call ( this , classes ) ] ;
270276 }
271- return classes . map ( v => this :: prvgetClass ( v ) ) ;
277+ return classes . map ( ( v ) => prvgetClass . call ( this , v ) ) ;
272278}
273279
274-
275280class BootstrapSwitch {
276281 constructor ( element , options = { } ) {
277282 this . $element = $ ( element ) ;
278283 this . options = $ . extend (
279284 { } ,
280285 $ . fn . bootstrapSwitch . defaults ,
281- this :: prvgetElementOptions ( ) ,
286+ prvgetElementOptions . call ( this ) ,
282287 options ,
283288 ) ;
284289 this . prevOptions = { } ;
285290 this . $wrapper = $ ( '<div>' , {
286291 class : ( ) => getClasses ( this . options , this . $element . attr ( 'id' ) )
287- . map ( v => this :: prvgetClass ( v ) )
288- . concat ( [ this . options . baseClass ] , this :: prvgetClasses ( this . options . wrapperClass ) )
292+ . map ( ( v ) => prvgetClass . call ( this , v ) )
293+ . concat ( [ this . options . baseClass ] , prvgetClasses . call ( this , this . options . wrapperClass ) )
289294 . join ( ' ' ) ,
290295 } ) ;
291- this . $container = $ ( '<div>' , { class : this :: prvgetClass ( 'container' ) } ) ;
296+ this . $container = $ ( '<div>' , { class : prvgetClass . call ( this , 'container' ) } ) ;
292297 this . $on = $ ( '<span>' , {
293298 html : this . options . onText ,
294- class : `${ this :: prvgetClass ( 'handle-on' ) } ${ this :: prvgetClass ( this . options . onColor ) } ` ,
299+ class : `${ prvgetClass . call ( this , 'handle-on' ) } ${ prvgetClass . call ( this , this . options . onColor ) } ` ,
295300 } ) ;
296301 this . $off = $ ( '<span>' , {
297302 html : this . options . offText ,
298- class : `${ this :: prvgetClass ( 'handle-off' ) } ${ this :: prvgetClass ( this . options . offColor ) } ` ,
303+ class : `${ prvgetClass . call ( this , 'handle-off' ) } ${ prvgetClass . call ( this , this . options . offColor ) } ` ,
299304 } ) ;
300305 this . $label = $ ( '<span>' , {
301306 html : this . options . labelText ,
302- class : this :: prvgetClass ( 'label' ) ,
307+ class : prvgetClass . call ( this , 'label' ) ,
303308 } ) ;
304309
305310 this . $element . on ( 'init.bootstrapSwitch' , ( ) => this . options . onInit ( element ) ) ;
@@ -325,12 +330,12 @@ class BootstrapSwitch {
325330 this . $element . prop ( 'indeterminate' , true ) ;
326331 }
327332
328- this :: prvinit ( ) ;
329- this :: prvelementHandlers ( ) ;
330- this :: prvhandleHandlers ( ) ;
331- this :: prvlabelHandlers ( ) ;
332- this :: prvformHandler ( ) ;
333- this :: prvexternalLabelHandler ( ) ;
333+ prvinit . call ( this ) ;
334+ prvelementHandlers . call ( this ) ;
335+ prvhandleHandlers . call ( this ) ;
336+ prvlabelHandlers . call ( this ) ;
337+ prvformHandler . call ( this ) ;
338+ prvexternalLabelHandler . call ( this ) ;
334339 this . $element . trigger ( 'init.bootstrapSwitch' , this . options . state ) ;
335340 }
336341
@@ -341,8 +346,8 @@ class BootstrapSwitch {
341346 state ( value , skip ) {
342347 if ( typeof value === 'undefined' ) { return this . options . state ; }
343348 if (
344- ( this . options . disabled || this . options . readonly ) ||
345- ( this . options . state && ! this . options . radioAllOff && this . $element . is ( ':radio' ) )
349+ ( this . options . disabled || this . options . readonly )
350+ || ( this . options . state && ! this . options . radioAllOff && this . $element . is ( ':radio' ) )
346351 ) { return this . $element ; }
347352 if ( this . $element . is ( ':radio' ) ) {
348353 $ ( `[name="${ this . $element . attr ( 'name' ) } "]` ) . trigger ( 'setPreviousOptions.bootstrapSwitch' ) ;
@@ -370,13 +375,13 @@ class BootstrapSwitch {
370375 size ( value ) {
371376 if ( typeof value === 'undefined' ) { return this . options . size ; }
372377 if ( this . options . size != null ) {
373- this . $wrapper . removeClass ( this :: prvgetClass ( this . options . size ) ) ;
378+ this . $wrapper . removeClass ( prvgetClass . call ( this , this . options . size ) ) ;
374379 }
375380 if ( value ) {
376- this . $wrapper . addClass ( this :: prvgetClass ( value ) ) ;
381+ this . $wrapper . addClass ( prvgetClass . call ( this , value ) ) ;
377382 }
378- this :: prvwidth ( ) ;
379- this :: prvcontainerPosition ( ) ;
383+ prvwidth . call ( this ) ;
384+ prvcontainerPosition . call ( this ) ;
380385 this . options . size = value ;
381386 return this . $element ;
382387 }
@@ -389,7 +394,7 @@ class BootstrapSwitch {
389394
390395 toggleAnimate ( ) {
391396 this . options . animate = ! this . options . animate ;
392- this . $wrapper . toggleClass ( this :: prvgetClass ( 'animate' ) ) ;
397+ this . $wrapper . toggleClass ( prvgetClass . call ( this , 'animate' ) ) ;
393398 return this . $element ;
394399 }
395400
@@ -402,7 +407,7 @@ class BootstrapSwitch {
402407 toggleDisabled ( ) {
403408 this . options . disabled = ! this . options . disabled ;
404409 this . $element . prop ( 'disabled' , this . options . disabled ) ;
405- this . $wrapper . toggleClass ( this :: prvgetClass ( 'disabled' ) ) ;
410+ this . $wrapper . toggleClass ( prvgetClass . call ( this , 'disabled' ) ) ;
406411 return this . $element ;
407412 }
408413
@@ -415,7 +420,7 @@ class BootstrapSwitch {
415420 toggleReadonly ( ) {
416421 this . options . readonly = ! this . options . readonly ;
417422 this . $element . prop ( 'readonly' , this . options . readonly ) ;
418- this . $wrapper . toggleClass ( this :: prvgetClass ( 'readonly' ) ) ;
423+ this . $wrapper . toggleClass ( prvgetClass . call ( this , 'readonly' ) ) ;
419424 return this . $element ;
420425 }
421426
@@ -428,8 +433,8 @@ class BootstrapSwitch {
428433 toggleIndeterminate ( ) {
429434 this . options . indeterminate = ! this . options . indeterminate ;
430435 this . $element . prop ( 'indeterminate' , this . options . indeterminate ) ;
431- this . $wrapper . toggleClass ( this :: prvgetClass ( 'indeterminate' ) ) ;
432- this :: prvcontainerPosition ( ) ;
436+ this . $wrapper . toggleClass ( prvgetClass . call ( this , 'indeterminate' ) ) ;
437+ prvcontainerPosition . call ( this ) ;
433438 return this . $element ;
434439 }
435440
@@ -440,7 +445,7 @@ class BootstrapSwitch {
440445 }
441446
442447 toggleInverse ( ) {
443- this . $wrapper . toggleClass ( this :: prvgetClass ( 'inverse' ) ) ;
448+ this . $wrapper . toggleClass ( prvgetClass . call ( this , 'inverse' ) ) ;
444449 const $on = this . $on . clone ( true ) ;
445450 const $off = this . $off . clone ( true ) ;
446451 this . $on . replaceWith ( $off ) ;
@@ -454,62 +459,62 @@ class BootstrapSwitch {
454459 onColor ( value ) {
455460 if ( typeof value === 'undefined' ) { return this . options . onColor ; }
456461 if ( this . options . onColor ) {
457- this . $on . removeClass ( this :: prvgetClass ( this . options . onColor ) ) ;
462+ this . $on . removeClass ( prvgetClass . call ( this , this . options . onColor ) ) ;
458463 }
459- this . $on . addClass ( this :: prvgetClass ( value ) ) ;
464+ this . $on . addClass ( prvgetClass . call ( this , value ) ) ;
460465 this . options . onColor = value ;
461466 return this . $element ;
462467 }
463468
464469 offColor ( value ) {
465470 if ( typeof value === 'undefined' ) { return this . options . offColor ; }
466471 if ( this . options . offColor ) {
467- this . $off . removeClass ( this :: prvgetClass ( this . options . offColor ) ) ;
472+ this . $off . removeClass ( prvgetClass . call ( this , this . options . offColor ) ) ;
468473 }
469- this . $off . addClass ( this :: prvgetClass ( value ) ) ;
474+ this . $off . addClass ( prvgetClass . call ( this , value ) ) ;
470475 this . options . offColor = value ;
471476 return this . $element ;
472477 }
473478
474479 onText ( value ) {
475480 if ( typeof value === 'undefined' ) { return this . options . onText ; }
476481 this . $on . html ( value ) ;
477- this :: prvwidth ( ) ;
478- this :: prvcontainerPosition ( ) ;
482+ prvwidth . call ( this ) ;
483+ prvcontainerPosition . call ( this ) ;
479484 this . options . onText = value ;
480485 return this . $element ;
481486 }
482487
483488 offText ( value ) {
484489 if ( typeof value === 'undefined' ) { return this . options . offText ; }
485490 this . $off . html ( value ) ;
486- this :: prvwidth ( ) ;
487- this :: prvcontainerPosition ( ) ;
491+ prvwidth . call ( this ) ;
492+ prvcontainerPosition . call ( this ) ;
488493 this . options . offText = value ;
489494 return this . $element ;
490495 }
491496
492497 labelText ( value ) {
493498 if ( typeof value === 'undefined' ) { return this . options . labelText ; }
494499 this . $label . html ( value ) ;
495- this :: prvwidth ( ) ;
500+ prvwidth . call ( this ) ;
496501 this . options . labelText = value ;
497502 return this . $element ;
498503 }
499504
500505 handleWidth ( value ) {
501506 if ( typeof value === 'undefined' ) { return this . options . handleWidth ; }
502507 this . options . handleWidth = value ;
503- this :: prvwidth ( ) ;
504- this :: prvcontainerPosition ( ) ;
508+ prvwidth . call ( this ) ;
509+ prvcontainerPosition . call ( this ) ;
505510 return this . $element ;
506511 }
507512
508513 labelWidth ( value ) {
509514 if ( typeof value === 'undefined' ) { return this . options . labelWidth ; }
510515 this . options . labelWidth = value ;
511- this :: prvwidth ( ) ;
512- this :: prvcontainerPosition ( ) ;
516+ prvwidth . call ( this ) ;
517+ prvcontainerPosition . call ( this ) ;
513518 return this . $element ;
514519 }
515520
@@ -520,8 +525,8 @@ class BootstrapSwitch {
520525 wrapperClass ( value ) {
521526 if ( typeof value === 'undefined' ) { return this . options . wrapperClass ; }
522527 const wrapperClass = value || $ . fn . bootstrapSwitch . defaults . wrapperClass ;
523- this . $wrapper . removeClass ( this :: prvgetClasses ( this . options . wrapperClass ) . join ( ' ' ) ) ;
524- this . $wrapper . addClass ( this :: prvgetClasses ( wrapperClass ) . join ( ' ' ) ) ;
528+ this . $wrapper . removeClass ( prvgetClasses . call ( this , this . options . wrapperClass ) . join ( ' ' ) ) ;
529+ this . $wrapper . addClass ( prvgetClasses . call ( this , wrapperClass ) . join ( ' ' ) ) ;
525530 this . options . wrapperClass = wrapperClass ;
526531 return this . $element ;
527532 }
@@ -544,8 +549,7 @@ class BootstrapSwitch {
544549 if ( typeof value === 'undefined' ) {
545550 return this . options . onSwitchChange ;
546551 }
547- this . options . onSwitchChange =
548- value || $ . fn . bootstrapSwitch . defaults . onSwitchChange ;
552+ this . options . onSwitchChange = value || $ . fn . bootstrapSwitch . defaults . onSwitchChange ;
549553 return this . $element ;
550554 }
551555
0 commit comments