forked from starikovs/alt-checkbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.alt-checkbox.js
112 lines (97 loc) · 3.89 KB
/
jquery.alt-checkbox.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
/*
* jQuery alt-checkbox plugin
* Version 1.0
* http://alt-checkbox.starikovs.com
*
* jQuery Javascript plugin which helps to style checkbox with custom css. It uses icon fonts and it's scaled well.
*
* Copyright (c) 2013 alt-checkbox.starikovs.com
* Licensed under the MIT and GPL licenses.
*/
(function($) {
var methods = {
init: function (options) {
var settings = $.extend({
iconClass: "fontawesome-ok",
sizeClass: "medium",
customClass: "",
labelSameSize: true,
outlineUnchecked: true
}, options);
return this.hide().each(function() {
var cb = $(this),
data = cb.data("alt-checkbox");
if (data) {
return;
}
var alt = $("<a href=\"#\" class=\"alt-checkbox\">")
.addClass(settings.iconClass)
.addClass(settings.sizeClass)
.addClass(settings.customClass)
.addClass(settings.outlineUnchecked ? "outline-unchecked" : "")
.insertBefore(cb),
cbId = cb.attr("id"),
cbLabel = $("[for='" + cbId + "']");
if (settings.labelSameSize) {
cbLabel.addClass("alt-checkbox-label").addClass(settings.sizeClass);
}
cb.data("alt-checkbox", {
alt: alt,
label: cbLabel,
labelSameSize: settings.labelSameSize,
sizeClass: settings.sizeClass
});
alt.bind("click.alt-checkbox", function(event) {
var alt = $(this);
if (alt.hasClass("checked")) {
alt.removeClass("checked");
cb.prop("checked", false);
} else {
alt.addClass("checked");
cb.prop("checked", true);
}
event.preventDefault();
}).bind("keyup.alt-checkbox", function(event) {
if (event.keyCode === 32) {
$(this).click();
event.preventDefault();
event.stopPropagation();
}
}).bind("keydown.alt-checkbox", function(event) {
if (event.keyCode === 32) {
event.preventDefault();
event.stopPropagation();
}
});
cb.bind("change.alt-checkbox", function() {
alt.click();
});
alt.addClass(cb.is(":checked") ? "checked" : "");
});
},
clear: function() {
return this.each(function() {
var cb = $(this),
data = cb.data("alt-checkbox");
if (!data) {
return;
}
data.alt.unbind(".alt-checkbox").remove();
cb.unbind(".alt-checkbox");
if (data.labelSameSize) {
data.label.removeClass(data.sizeClass).removeClass("alt-checkbox-label");
}
cb.removeData("alt-checkbox");
}).show();
}
};
$.fn.altCheckbox = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
$.error("Method " + method + " does not exist on jQuery.altCheckbox.");
}
};
})(jQuery);