Skip to content

Commit f876861

Browse files
committed
Minor changes on API for range selection and max.
1 parent 5878da0 commit f876861

File tree

2 files changed

+91
-36
lines changed

2 files changed

+91
-36
lines changed

src/jquery.checkboxes.js

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,8 @@
55
/* Checkboxes class definition. */
66
//////////////////////////////////
77

8-
var defaults = {
9-
range : false,
10-
max : 0
11-
};
12-
138
var Checkboxes = function($context, options) {
149
this.$context = $context;
15-
options = $.extend({}, defaults, options);
16-
var instance = this;
17-
if (options.range) {
18-
this.$context.on('click.checkboxes', ':checkbox', function(e) {
19-
var $checkbox = $(e.target);
20-
if (e.shiftKey && instance.$last) {
21-
var $checkboxes = instance.$context.find(':checkbox'),
22-
from = $checkboxes.index(instance.$last),
23-
to = $checkboxes.index($checkbox);
24-
if (to > from) {
25-
$checkboxes.slice(from, to).prop('checked', true);
26-
} else {
27-
$checkboxes.slice(to, from).prop('checked', true);
28-
}
29-
}
30-
instance.$last = $checkbox.is(':checked') ? $checkbox : null;
31-
});
32-
}
33-
if (options.max > 0) {
34-
this.$context.on('click.checkboxes', ':checkbox', function(e) {
35-
if (instance.$context.find(':checked').length == options.max) {
36-
instance.$context.find(':checkbox:not(:checked)').prop('disabled', true);
37-
} else {
38-
instance.$context.find(':checkbox:not(:checked)').prop('disabled', false);
39-
}
40-
});
41-
}
4210
};
4311

4412
// Check all checkboxes in context.
@@ -59,21 +27,61 @@
5927
});
6028
};
6129

30+
// Set the maximum number of checkboxes that can be checked.
31+
Checkboxes.prototype.max = function(max) {
32+
if (max == 0) {
33+
// Disable max.
34+
this.$context.off('click.checkboxes');
35+
} else if (max > 0) {
36+
// Enable max.
37+
var instance = this;
38+
this.$context.on('click.checkboxes.max', ':checkbox', function(e) {
39+
if (instance.$context.find(':checked').length == max) {
40+
instance.$context.find(':checkbox:not(:checked)').prop('disabled', true);
41+
} else {
42+
instance.$context.find(':checkbox:not(:checked)').prop('disabled', false);
43+
}
44+
});
45+
}
46+
};
47+
48+
// Enable or disable range selection.
49+
Checkboxes.prototype.range = function(enable) {
50+
if (enable) {
51+
var instance = this;
52+
this.$context.on('click.checkboxes.range', ':checkbox', function(e) {
53+
var $checkbox = $(e.target);
54+
if (e.shiftKey && instance.$last && ($checkbox.prop('checked') == instance.$last.prop('checked'))) {
55+
var $checkboxes = instance.$context.find(':checkbox'),
56+
from = $checkboxes.index(instance.$last),
57+
to = $checkboxes.index($checkbox),
58+
start = Math.min(from, to),
59+
end = Math.max(from, to);
60+
$checkboxes.slice(start, end).prop('checked', true);
61+
}
62+
instance.$last = $checkbox.is(':checked') ? $checkbox : null;
63+
});
64+
} else {
65+
this.$context.off('click.checkboxes.range');
66+
}
67+
};
68+
6269
///////////////////////////////////
6370
/* Checkboxes plugin definition. */
6471
///////////////////////////////////
6572

6673
var old = $.fn.checkboxes;
6774

68-
$.fn.checkboxes = function(options) {
75+
$.fn.checkboxes = function(method) {
76+
var methodArgs = Array.prototype.slice.call(arguments, 1);
6977
return this.each(function() {
7078
var $this = $(this),
7179
data = $this.data('checkboxes');
7280
if (!data) {
73-
$this.data('checkboxes', (data = new Checkboxes($this, typeof options == 'object' && options)));
81+
$this.data('checkboxes', (data = new Checkboxes($this, typeof method == 'object' && method)));
7482
}
75-
if (typeof options === 'string') {
76-
data[options]();
83+
if (typeof method === 'string') {
84+
data[method].apply(data, methodArgs);
7785
}
7886
});
7987
};

test/test.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document</title>
6+
<script src="../lib/jquery/jquery.js"></script>
7+
<script src="../src/jquery.checkboxes.js"></script>
8+
<script>
9+
jQuery(function($) {
10+
$('body').checkboxes('range', true, true)
11+
.checkboxes('max', 4);
12+
});
13+
</script>
14+
</head>
15+
<body>
16+
<input type="checkbox" id="1">
17+
<label for="1">Row #1</label>
18+
<br>
19+
<input type="checkbox" id="2">
20+
<label for="2">Row #2</label>
21+
<br>
22+
<input type="checkbox" id="3">
23+
<label for="3">Row #3</label>
24+
<br>
25+
<input type="checkbox" id="4">
26+
<label for="4">Row #4</label>
27+
<br>
28+
<input type="checkbox" id="5">
29+
<label for="5">Row #5</label>
30+
<br>
31+
<input type="checkbox" id="6">
32+
<label for="6">Row #6</label>
33+
<br>
34+
<input type="checkbox" id="7">
35+
<label for="7">Row #7</label>
36+
<br>
37+
<input type="checkbox" id="8">
38+
<label for="8">Row #8</label>
39+
<br>
40+
<input type="checkbox" id="9">
41+
<label for="9">Row #9</label>
42+
<br>
43+
<input type="checkbox" id="10">
44+
<label for="10">Row #10</label>
45+
<br>
46+
</body>
47+
</html>

0 commit comments

Comments
 (0)