-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.shCheckset.js
167 lines (132 loc) · 5.94 KB
/
jquery.shCheckset.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*!
* jQuery shCheckset v1.2
* http://jquery.sunhater.com/shCheckset
* 2014-02-02
*
* Copyright (c) 2010-2014 Pavel Tzonkov <[email protected]>
* Dual licensed under the MIT and GPL licenses.
*/
(function($) {
var scrollbarWidth = 0;
$.fn.shCheckset = function(settings) {
var c = {
namespace: 'shcs',
search: false,
uniform: true,
labels: {
search: "Search..."
},
widthOffset: 0
},
addOption = function(select, value, text) {
try {
select.add(new Option(text, value), null);
} catch(e) {
select.add(new Option(text, value));
}
};
$.extend(c, settings);
if (!scrollbarWidth) {
var div = $('<div></div>')
.css({width: 100, height: 100, overflow: 'auto', position: 'absolute', top: -1000, left: -1000})
.prependTo('body').append('<div></div>').find('div').css({width: '100%', height: 200});
scrollbarWidth = 100 - div.width();
div.parent().remove();
}
this.each(function() {
var t = this;
if (!$(t).is('select') || !t.length)
return;
$(t).after('<div class="' + c.namespace + '"></div>');
var div = $(t).next().get(0),
name = $(t).attr('name'),
outerCSS = {
width: t.style.width ? t.style.width : $(t).outerWidth() + 'px',
height: t.style.height ? t.style.height : $(t).outerHeight() + 'px'
};
if (!name) name = c.namespace + '[]';
else if (name.substr(name.length - 2, 2) != "[]")
name += "[]";
$(div).html((c.search ? '<input type="text" placeholder="' + c.labels.search + '" />' : "") + '<div></div>');
var search = $(div).find('input[type="text"]'),
maxWidth = 0, i = 0,
lc = $(div).find('div');
lc.css({
overflow: "auto",
height: parseInt(outerCSS.height) - search.outerHeight(true)
- parseInt(lc.css('marginTop')) - parseInt(lc.css('marginBottom'))
- parseInt(lc.css('paddingTop')) - parseInt(lc.css('paddingBottom'))
- parseInt(lc.css('borderTopWidth')) - parseInt(lc.css('borderBottomWidth'))
+ 'px'
});
$(t).find('option').each(function() {
var label, checkbox, w, cid, t = this;
do {
cid = c.namespace + '_' + name.substr(0, name.length - 2) + '_' + i++;
} while ($('#' + cid).get(0));
$(div).find('div').append('<label></label>');
label = $(div).find('label').last();
label.attr('for', cid).html('<input type="checkbox" /><span>' + t.text + '</span>').css({
whitespace: "nowrap",
'float': "left"
});
checkbox = label.find('input').first();
checkbox.attr({
name: name,
id: cid,
value: t.value,
checked: t.selected
});
if (t.selected)
label.addClass('checked');
if (c.uniform && $.uniform) {
checkbox.uniform();
search.uniform();
}
w = label.outerWidth(true);
if (maxWidth < w)
maxWidth = w;
});
$(div).find('label[for]').css({
display: "block",
whiteSpace: "nowrap",
'float': "none"
}).find('label').detach();
$(div).find('input[type="checkbox"]').click(function() {
var th = this,
label = $(th).parents('label[for]').first(),
value = $(th).val();
$(t).find('option[value="' + value + '"]').get(0).selected = th.checked;
if (th.checked)
label.addClass('checked');
else
label.removeClass('checked');
if (document.selection)
document.selection.empty();
else if (window.getSelection)
window.getSelection().removeAllRanges();
});
if (!t.style.width)
outerCSS.width = maxWidth + scrollbarWidth + c.widthOffset + parseInt(lc.css('marginLeft')) + parseInt(lc.css('marginRight')) + parseInt(lc.css('paddingLeft')) + parseInt(lc.css('paddingRight')) + parseInt(lc.css('borderLeftWidth')) + parseInt(lc.css('borderRightWidth'));
$(div).css(outerCSS);
search.attr({
spellcheck: false
}).css({
width: parseInt(outerCSS.width) - (document.doctype ? (parseInt(search.css('borderRightWidth')) + parseInt(search.css('borderLeftWidth')) + parseInt(search.css('paddingRight')) + parseInt(search.css('paddingLeft')) + parseInt(search.css('marginRight')) + parseInt(search.css('marginLeft'))) : 0) + 'px'
}).focus(function() {
lc.addClass('focus');
}).blur(function() {
lc.removeClass('focus');
}).keyup(function() {
var search = $(this).val(),
lastSpace = (search.substr(search.length - 1, 1) == " ");
search = search.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g, " ");
$(this).val(search + (lastSpace ? " " : ""));
$(div).find('label').each(function(i) {
$(this).css('display', (search.length && ($(this).find('span').text().toLowerCase().indexOf(search.toLowerCase()) == -1)) ? 'none' : 'block');
});
});
$(t).detach();
});
};
})(jQuery);