Skip to content

Commit 314decb

Browse files
committed
[Add] add feature to only create rcSwitcher once, optimization, add new theme yellowish-green, enhance autrostick
1 parent 16d6dad commit 314decb

File tree

4 files changed

+111
-29
lines changed

4 files changed

+111
-29
lines changed

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
>@author Ahmed Saad <a7mad.sa3d.2014@gmail.com>
2525
2626
### Version
27-
> 4.0
27+
> 4.1
2828
2929
### Updated
30-
> 29 Sep 2016
31-
30+
> 18 Feb 2017
3231
3332
----
3433

@@ -77,6 +76,25 @@
7776
----
7877
### Changelog:
7978

79+
> __V 4.1__
80+
81+
1. Add feature that input should only have on switcher
82+
83+
$(':checkbox').rcSwitcher(); // create switcher
84+
$(':checkbox').rcSwitcher(); // will not create switcher as it has already creatred
85+
86+
87+
2. Add rcSwitcher Object on input as property
88+
89+
// We now can access rcSwitcher Object from element itself
90+
$('#input').rcSwitcher(); // Create Switcher
91+
92+
$('#input').[0].rcSwitcher; // Access its rcSwitcher Object
93+
94+
3. Add attribute 'data-has-rcswitcher="1"' on input to mark that input has rcswitcher
95+
4. Enhance Auto Stick
96+
97+
> __V 4.0.1__
8098
8199
1. add support for control switch by changing input _`check`_ status
82100

@@ -96,7 +114,9 @@
96114
2. change `change.rcSwitcher` Event to `toggle.rcSwitcher` to avoid built-in javascript `change` event
97115
3. enhance __*Demo*__ example to adapt with screen sizes and mobile phones
98116
4. some other tweaks.
99-
####*Version 4 Changes :*
117+
118+
> __V 4.0__
119+
100120
5. Add Support For Track Disable And Enable Status on changing _`disable`_ status on Input'
101121

102122
$input = $('input[type=checkbox]').first().rcSwitcher();

css/rcswitcher.css

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,38 @@ background: linear-gradient(#e4e4e4, #f9f9f9);
292292
.swraper.flat .stoggler .slabel-on:before, .swraper.flat .stoggler .slabel-off:before{}
293293

294294
/* ------------ END THEME Flat ------------ */
295+
296+
/* ------------ THEME Yellowish-green ------------ */
297+
.swraper.yellowish-green
298+
{
299+
border-radius: 99px;
300+
border: 2px solid #58d30b;
301+
}
302+
303+
.swraper.yellowish-green .stoggler
304+
{
305+
color: rgba( 0, 0, 0, 0.6 );
306+
}
307+
308+
.swraper.yellowish-green .stoggler.on
309+
{
310+
}
311+
312+
.swraper.yellowish-green .stoggler .sblob
313+
{
314+
border-radius: 99px;
315+
background: #ff8700;
316+
transition: background linear .5s;
317+
}
318+
319+
.swraper.yellowish-green .stoggler.on .sblob
320+
{
321+
background: #58d30b;
322+
}
323+
324+
.swraper.yellowish-green .stoggler .sblob:hover
325+
{
326+
background: #0bcfd3;
327+
}
328+
329+
/* ------------ END THEME yellowish-green ------------ */

index.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@
55
<meta charset="UTF-8" >
66
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
77
<link type="text/css" rel="stylesheet" href="css/style.min.css">
8-
<link type="text/css" rel="stylesheet" href="css/rcswitcher.min.css">
8+
<link type="text/css" rel="stylesheet" href="css/rcswitcher.css">
99
<script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>
10-
<script type="text/javascript" src="js/rcswitcher-4.0.min.js"></script>
10+
<script type="text/javascript" src="js/rcswitcher.js"></script>
1111

1212
<script>
1313

1414
window.onload = function(){
1515

1616
$('.gender :radio').rcSwitcher({
1717
// reverse: true,
18-
theme: 'light',
19-
// width: 70,
20-
blobOffset: 0,
18+
theme: 'yellowish-green',
19+
width: 48,
20+
height: 16,
21+
onText: '&check;',
22+
offText: '&cross;',
23+
blobOffset: 2,
2124
inputs: true,
2225
autoStick: true,
2326
})

js/rcswitcher-4.0.js renamed to js/rcswitcher.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
*
1010
* @package rcSwitcher
1111
* @name rcSwitcher
12-
* @version 4.0 < 29 Sep 2016 >
12+
* @version 4.1 < 18 Feb 2017 >
13+
* @changes 4.1 - Add feature that input shold only have on switcher
14+
* - Add rcSwitcher Object on input as property
15+
* - Add attribute 'data-has-rcswitcher="1"' on input to mark that input has rcswitcher
16+
* - Enhance Auto Stick
1317
* @author ahmed saad <a7mad.sa3d.2014@gmail.com><ahmedfadlshaker@gmail.com>
1418
* @copyright ahmed saad april 2015
1519
* @link http://plus.google.com/+AhmedSaadGmail
@@ -34,7 +38,7 @@
3438
$blob: $( '<span>', {'class': 'sblob'} ),
3539

3640
// Allowed Themes
37-
themes: [ 'light', 'modern', 'dark', 'flat' ],
41+
themes: [ 'light', 'modern', 'dark', 'flat', 'yellowish-green' ],
3842

3943
};
4044

@@ -64,6 +68,12 @@
6468
switcherP.$this.each( function( i, input )
6569
{
6670
// console.log( $input.type );
71+
if( input.hasOwnProperty( 'rcSwitcher' ) )
72+
{
73+
console.warn( 'already has instance' );
74+
return;
75+
}
76+
6777
this.makeSwitcher( $( input ), switcherP, options );
6878
}.bind( this ) );
6979

@@ -198,6 +208,12 @@
198208
// Add to switchers collections
199209
switcherP.$switchers = switcherP.$switchers.add( data.$switcher );
200210

211+
// Store In Element Input JS
212+
$input[0].rcSwitcher = data;
213+
214+
// Add attribute to mark this input that it has rcswitcher
215+
$input.attr( 'data-has-rcswitcher', 1 );
216+
201217
// Update input type length
202218
cat.length++;
203219

@@ -308,7 +324,17 @@
308324
var margin = parentAW - options.width;
309325

310326
// remove border width if exists
311-
margin -= ( options.theme == 'dark' ) ? 0 : 2;
327+
switch( options.theme )
328+
{
329+
case 'dark':
330+
margin -= 2;
331+
break;
332+
333+
case 'yellowish-green':
334+
margin -= 4;
335+
break;
336+
}
337+
312338

313339
// Left OR Right margin
314340
if( switcherP.cssValues.dir == 'rtl' )
@@ -471,7 +497,6 @@
471497
{
472498
--family.disabled;
473499
++family.enabled;
474-
// If Checked, Disable All Group
475500
if( family.enabled - family.disabled >= 2 )
476501
family.switchable = true;
477502

@@ -509,9 +534,11 @@
509534
data.$input.prop( 'checked', false );
510535

511536
// Fire Event and pass data object to event handler
512-
data.$input.trigger( 'turnoff.rcSwitcher', data );
513-
data.$input.trigger( 'toggle.rcSwitcher', [ data, 'turnoff' ] );
514-
537+
// Wait Transition 500ms Time
538+
setTimeout( function(){
539+
data.$input.trigger( 'turnoff.rcSwitcher', data );
540+
data.$input.trigger( 'toggle.rcSwitcher', [ data, 'turnoff' ] );
541+
}, 500 );
515542
},
516543

517544

@@ -540,8 +567,12 @@
540567
if( !outsideChange )
541568
data.$input.prop( 'checked', true );
542569

543-
data.$input.trigger( 'turnon.rcSwitcher', data );
544-
data.$input.trigger( 'toggle.rcSwitcher', [ data, 'turnon' ] );
570+
// Wait Transition 500ms Time
571+
setTimeout( function(){
572+
data.$input.trigger( 'turnon.rcSwitcher', data );
573+
data.$input.trigger( 'toggle.rcSwitcher', [ data, 'turnon' ] );
574+
}, 500 );
575+
545576

546577
},
547578

@@ -565,20 +596,14 @@
565596

566597

567598
// On Click on switchers
568-
switcherP.$switchers.on( 'click', function( e ){
569-
// $switcher.on( 'click', function( e ){
570-
571-
// console.log( e.currentTarget );
599+
switcherP.$switchers.on( 'click', function( e )
600+
{
572601
var obj = {};
573602

574603
obj.type = e.currentTarget.getAttribute( 'input-type' );
575604
obj.name = e.currentTarget.getAttribute( 'input-name' );
576605
obj.value = e.currentTarget.getAttribute( 'input-value' );
577606

578-
// obj.type = $switcher.attr( 'input-type' );
579-
// obj.name = $switcher.attr( 'input-name' );
580-
// obj.value = $switcher.attr( 'input-value' );
581-
582607
// Toggle
583608
this.toggle( obj, switcherP );
584609

@@ -628,11 +653,11 @@
628653
// Filter this
629654
// Get Only Checkbox and Radio inputs only
630655

631-
switcherP.$this = this.filter( 'input[type=checkbox], input[type=radio]' );
656+
switcherP.$this = this.filter( 'input[type=checkbox], input[type=radio]' ).not( '[data-has-rcswitcher]' );
632657

633658
// Stop if we havenot any checkboxs or radios
634659
if( switcherP.$this.length == 0 )
635-
return;
660+
return this;
636661

637662
switcherP.cssValues.dir = window.getComputedStyle( switcherP.$this[0], null ).direction || 'ltr';
638663

@@ -672,7 +697,6 @@
672697
// Start
673698
switcherM.start( switcherP, options );
674699

675-
676700
// Return selected jquery object to allow chaining
677701
return this;
678702

0 commit comments

Comments
 (0)