forked from arthurgouveia/prettyCheckable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettyCheckable.js
116 lines (76 loc) · 3.08 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 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.prop('type') == 'radio') {
$('input[name="' + input.attr('name') + '"]').each(function(index, el){
$(el).prop('checked', false).parent().find('a').removeClass('checked');
});
}
if (input.prop('checked')) {
input.prop('checked', false).change();
} else {
input.prop('checked', true).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') : '';
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.prop('checked') ? '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));