forked from arthurgouveia/prettyCheckable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.prettycheckable.js
131 lines (86 loc) · 3.36 KB
/
jquery.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* 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 addCheckableEvents(element){
element.find('a,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').change();
} else {
input.attr('checked', 'checked').change();
}
fakeCheckable.toggleClass('checked');
});
element.find('a').on('keyup', function(e){
if (e.keyCode === 32) {
$(this).click();
}
});
}
Plugin.prototype.init = function () {
var 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') : '';
// Fixed to accept the element default label
if(label == ''){
var labelObject = $('label[for="'+el.attr('id')+'"]');
if(labelObject.length)
{
label =labelObject.html();
labelObject.hide();
}
}
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.parent().addClass('clearfix');
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'));
addCheckableEvents(el.parent());
};
$.fn[pluginName] = function ( options ) {
this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
return this;
};
}(jQuery, window));