forked from arthurgouveia/prettyCheckable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prettyCheckable.js
116 lines (75 loc) · 3.15 KB
/
prettyCheckable.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
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
/*
* Project: prettyCheckable
* Description: jQuery plugin to replace checkboxes and radios for custom images
* Author: Arthur Gouveia
* License: Licensed under the MIT License
*/
;(function ( $, window, undefined ) {
var pluginName = 'prettyCheckable',
document = window.document,
defaults = {
labelPosition: 'right',
customClass: '',
color: 'blue'
};
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this._defaults = defaults;
this._name = pluginName;
this.init();
}
function loadCheckBoxEvents(){
$('div.prettycheckbox a, div.prettyradio a, div.prettycheckbox label, div.prettyradio label').on('touchstart click', function(e){
e.preventDefault();
var clickedParent = $(this).closest('.clearfix');
var input = clickedParent.find('input');
var fakeCheckable = clickedParent.find('a');
if (input.attr('type') == 'radio') {
$('input[name="' + input.attr('name') + '"]').each(function(index, el){
$(el).removeAttr('checked').parent().find('a').removeClass('checked');
});
}
if (input.attr('checked') !== undefined) {
input.removeAttr('checked');
} else {
input.attr('checked', 'checked').change();
}
fakeCheckable.toggleClass('checked');
});
$('div.prettycheckbox a, div.prettyradio a').on('keypress', function(e){
$(this).click();
e.preventDefault();
});
}
Plugin.prototype.init = function () {
el = $(this.element);
el.css('display', 'none');
var classType = el.data('type') !== undefined ? el.data('type') : el.attr('type');
var label = el.data('label') !== undefined ? el.data('label') : '';
var labelPosition = el.data('labelposition') !== undefined ? 'label' + el.data('labelposition') : 'label' + this.options.labelPosition;
var customClass = el.data('customclass') !== undefined ? el.data('customclass') : this.options.customClass;
var color = el.data('color') !== undefined ? el.data('color') : this.options.color;
var containerClasses = ['pretty' + classType, labelPosition, customClass, color].join(' ');
el.wrap('<div class="clearfix ' + containerClasses + '"></div>').parent().html();
var dom = [];
var isChecked = el.attr('checked') !== undefined ? 'checked' : '';
if (labelPosition === 'labelright') {
dom.push('<a href="#" class="' + isChecked + '"></a>');
dom.push('<label for="' + el.attr('id') + '">' + label + '</label>');
} else {
dom.push('<label for="' + el.attr('id') + '">' + label + '</label>');
dom.push('<a href="#" class="' + isChecked + '"></a>');
}
el.parent().append(dom.join('\n'));
};
$.fn[pluginName] = function ( options ) {
this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
loadCheckBoxEvents();
return this;
};
}(jQuery, window));