forked from 01e9/adaptive-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.adaptive-switch.js
79 lines (68 loc) · 2.35 KB
/
jquery.adaptive-switch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(function(){
var template =
'<div class="adaptive-switch{{wrapper_class}}">'+
'<div class="switch-inner">'+
'<div class="switch-size">'+
'<label class="switch-label switch-label-right"><span>{{biggest_text}}</span></label>'+
'</div>'+
'<div class="switch-dot"><div><span></span></div></div>'+
'<div class="switch-switcher">'+
'<label for="{{id_right}}" class="switch-label switch-label-right"><span>{{switch_right}}</span></label>'+
'<label for="{{id_left}}" class="switch-label switch-label-left" ><span>{{switch_left}}</span></label>'+
'<div class="clear"></div>'+
'</div>'+
'</div>'+
'</div>';
function renderTemplate(switchLeft, switchRight, isRight, inputLeftId, inputRightId) {
switchLeft = String(switchLeft);
switchRight = String(switchRight);
return template
.split('{{switch_left}}' ).join(switchLeft)
.split('{{switch_right}}' ).join(switchRight)
.split('{{biggest_text}}' ).join(switchLeft.length > switchRight.length ? switchLeft : switchRight)
.split('{{id_left}}' ).join(inputLeftId)
.split('{{id_right}}' ).join(inputRightId)
.split('{{wrapper_class}}' ).join(isRight ? ' switch-right' : '');
}
{
var increment = 0;
function getUniqueId() {
return 'switch--'+ (++increment);
}
}
jQuery.fn.adaptiveSwitch = function() {
var $ = jQuery;
this.filter('input[type="checkbox"]').each(function(){
var $this = $(this);
if (!$this.attr('id')) {
$this.attr('id', getUniqueId());
}
var switchId = getUniqueId();
$(
renderTemplate(
$this.attr('data-switch-left') ? $this.attr('data-switch-left') : 'No',
$this.attr('data-switch-right') ? $this.attr('data-switch-right') : 'Yes',
$this.prop('checked'),
$this.attr('id'),
$this.attr('id')
)
)
.attr('id', switchId)
.addClass('input-type--checkbox')
.addClass('input-id--'+ $this.attr('id'))
.addClass(
$this.attr('class')
? $.map($this.attr('class').split(/\s+/), function(c){ return c.length ? 'input-class--'+ c : ''; }).join(' ')
: ''
)
.insertAfter($this);
$this
.attr('data-switch-id', switchId)
.addClass('adaptive-switch-input')
.on('change', function(){
$('#'+ $(this).attr('data-switch-id') )[ $(this).prop('checked') ? 'addClass' : 'removeClass' ]('switch-right');
});
});
return this;
};
})();