toggler.js
3.61 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(function($){
$.fn.extend({
toggler: function(options) {
var defaults = {
onText: 'Enabled',
offText: 'Disabled',
bold: false,
icons: true,
onIcon: 'fa fa-check',
offIcon: 'fa fa-times',
onColor: '#57A155',
offColor: '#D87261',
mode: 'text',
effect: 'slide',
};
return this.each(function() {
var o = $.extend({}, defaults, options, $(this).data());
var obj = $(this);
var items = $("li", obj);
//obj.attr('type', 'hidden');
obj.hide();
if(obj.val() == 1) {
var onDisabled = '';
var offDisabled = 'display:none;';
} else {
var onDisabled = 'display:none;';
var offDisabled = '';
}
// create main container and insert it in dom
var html = '';
html += '<div class="form-control" style="position:relative;padding:0;overflow:hidden">';
html += '<div data-type="on" style="'+onDisabled+getStyle(o, 1)+'">'+getText(o, 1)+'</div>';
html += '<div data-type="on" style="'+offDisabled+getStyle(o, 0)+'">'+getText(o, 0)+'</div>';
html += '</div>';
html = $(html);
obj.after(html);
// create events
html.on('click', function(){
if (html.prev().val() == 1) {
if (o.effect == 'slide') {
html.children('[data-type=on]').slideToggle();
html.children('[data-type=off]').slideToggle();
html.children('[data-type=off]').detach().prependTo(html);
} else {
html.children('[data-type=on]').fadeOut();
html.children('[data-type=off]').fadeIn();
}
var val = 0;
} else {
if (o.effect == 'slide') {
html.children('[data-type=on]').slideToggle();
html.children('[data-type=off]').slideToggle();
} else {
html.children('[data-type=on]').fadeIn();
html.children('[data-type=off]').fadeOut();
}
var val = 1;
}
// assign value to real input and trigger change
html.prev().val(val).trigger('change');
});
});
}
});
// function to get style of each element
function getStyle(o, val) {
var style = '';
// set style values
if (o.effect == 'fade') {
style += 'width:100%;position:absolute;top:0;';
}
if (o.mode == 'background') {
style += 'text-shadow: 1px 1px 1px #555;';
}
style += '-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;padding: 8px 13px;'
if (o.bold) {
style += 'font-weight:bold';
}
if (val == 1) {
if (o.mode == 'text') {
style += 'color:'+o.onColor;
} else if (o.mode == 'background') {
style += 'background:'+o.onColor;
}
} else {
if (o.mode == 'text') {
style += 'color:'+o.offColor;
} else if (o.mode == 'background') {
style += 'background:'+o.offColor;
}
}
if (o.mode == 'background') {
style += ';color:#fff';
}
return style;
}
// function to get formated text
function getText(o, val) {
// use icons ?
if (!o.icons) {
var onIcon = offIcon = '';
} else {
var onIcon = '<i class="'+o.onIcon+'"></i> ';
var offIcon = '<i class="'+o.offIcon+'"></i> ';
}
if (val == 1) {
var text = onIcon + o.onText;
} else {
var text = offIcon + o.offText;
}
return text;
}
})(jQuery);