Skip to content

Commit 058c1a2

Browse files
committed
range and max features were standarized to be invoked the same way as check and uncheck. Data-api created to support range and max features. Range selection allow uncheck by range.
1 parent f876861 commit 058c1a2

File tree

3 files changed

+73
-49
lines changed

3 files changed

+73
-49
lines changed

build/jquery.checkboxes.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.checkboxes.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,35 @@
99
this.$context = $context;
1010
};
1111

12-
// Check all checkboxes in context.
12+
/**
13+
* Check all checkboxes in context.
14+
*/
1315
Checkboxes.prototype.check = function() {
1416
this.$context.find(':checkbox').prop('checked', true);
1517
};
1618

17-
// Uncheck all checkboxes in context.
19+
/**
20+
* Uncheck all checkboxes in context.
21+
*/
1822
Checkboxes.prototype.uncheck = function() {
1923
this.$context.find(':checkbox').prop('checked', false);
2024
};
2125

22-
// Toggle the state of all checkboxes in context.
26+
/**
27+
* Toggle the state of all checkboxes in context.
28+
*/
2329
Checkboxes.prototype.toggle = function() {
2430
this.$context.find(':checkbox').each(function() {
2531
var $checkbox = $(this);
2632
$checkbox.prop('checked', ! $checkbox.is(':checked'));
2733
});
2834
};
2935

30-
// Set the maximum number of checkboxes that can be checked.
36+
/**
37+
* Set the maximum number of checkboxes that can be checked.
38+
*
39+
* @param max {number} The maximum number of checkbox allowed to be checked.
40+
*/
3141
Checkboxes.prototype.max = function(max) {
3242
if (max == 0) {
3343
// Disable max.
@@ -45,21 +55,25 @@
4555
}
4656
};
4757

48-
// Enable or disable range selection.
58+
/**
59+
* Enable or disable range selection.
60+
*
61+
* @param enable {boolean} Indicate is range selection has to be enabled.
62+
*/
4963
Checkboxes.prototype.range = function(enable) {
5064
if (enable) {
5165
var instance = this;
5266
this.$context.on('click.checkboxes.range', ':checkbox', function(e) {
5367
var $checkbox = $(e.target);
54-
if (e.shiftKey && instance.$last && ($checkbox.prop('checked') == instance.$last.prop('checked'))) {
68+
if (e.shiftKey && instance.$last) {
5569
var $checkboxes = instance.$context.find(':checkbox'),
5670
from = $checkboxes.index(instance.$last),
5771
to = $checkboxes.index($checkbox),
5872
start = Math.min(from, to),
59-
end = Math.max(from, to);
60-
$checkboxes.slice(start, end).prop('checked', true);
73+
end = Math.max(from, to) + 1;
74+
$checkboxes.slice(start, end).prop('checked', $checkbox.prop('checked'));
6175
}
62-
instance.$last = $checkbox.is(':checked') ? $checkbox : null;
76+
instance.$last = $checkbox;
6377
});
6478
} else {
6579
this.$context.off('click.checkboxes.range');
@@ -108,8 +122,21 @@
108122
href = el.attr('href'),
109123
$context = $(el.data('context') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))),
110124
action = el.data('action');
111-
e.preventDefault();
112-
$context.checkboxes(action);
125+
if ($context && action) {
126+
e.preventDefault();
127+
$context.checkboxes(action);
128+
}
113129
});
130+
131+
$(document).on('ready.checkboxes.data-api', function() {
132+
$('[data-toggle^=checkboxes]').each(function() {
133+
var el = $(this),
134+
actions = el.data();
135+
delete actions.toggle;
136+
for (var action in actions) {
137+
el.checkboxes(action, actions[action]);
138+
}
139+
});
140+
});
114141

115142
}(window.jQuery);

test/test.html

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,40 @@
55
<title>Document</title>
66
<script src="../lib/jquery/jquery.js"></script>
77
<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>
148
</head>
159
<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>
10+
<div data-toggle="checkboxes"
11+
data-max="3"
12+
data-range="true">
13+
<input type="checkbox" id="1">
14+
<label for="1">Row #1</label>
15+
<br>
16+
<input type="checkbox" id="2">
17+
<label for="2">Row #2</label>
18+
<br>
19+
<input type="checkbox" id="3">
20+
<label for="3">Row #3</label>
21+
<br>
22+
<input type="checkbox" id="4">
23+
<label for="4">Row #4</label>
24+
<br>
25+
<input type="checkbox" id="5">
26+
<label for="5">Row #5</label>
27+
<br>
28+
<input type="checkbox" id="6">
29+
<label for="6">Row #6</label>
30+
<br>
31+
<input type="checkbox" id="7">
32+
<label for="7">Row #7</label>
33+
<br>
34+
<input type="checkbox" id="8">
35+
<label for="8">Row #8</label>
36+
<br>
37+
<input type="checkbox" id="9">
38+
<label for="9">Row #9</label>
39+
<br>
40+
<input type="checkbox" id="10">
41+
<label for="10">Row #10</label>
42+
</div>
4643
</body>
4744
</html>

0 commit comments

Comments
 (0)