|
5 | 5 | /* Checkboxes class definition. */ |
6 | 6 | ////////////////////////////////// |
7 | 7 |
|
8 | | - var defaults = { |
9 | | - range : false, |
10 | | - max : 0 |
11 | | - }; |
12 | | - |
13 | 8 | var Checkboxes = function($context, options) { |
14 | 9 | 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 | | - } |
42 | 10 | }; |
43 | 11 |
|
44 | 12 | // Check all checkboxes in context. |
|
59 | 27 | }); |
60 | 28 | }; |
61 | 29 |
|
| 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 | + |
62 | 69 | /////////////////////////////////// |
63 | 70 | /* Checkboxes plugin definition. */ |
64 | 71 | /////////////////////////////////// |
65 | 72 |
|
66 | 73 | var old = $.fn.checkboxes; |
67 | 74 |
|
68 | | - $.fn.checkboxes = function(options) { |
| 75 | + $.fn.checkboxes = function(method) { |
| 76 | + var methodArgs = Array.prototype.slice.call(arguments, 1); |
69 | 77 | return this.each(function() { |
70 | 78 | var $this = $(this), |
71 | 79 | data = $this.data('checkboxes'); |
72 | 80 | 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))); |
74 | 82 | } |
75 | | - if (typeof options === 'string') { |
76 | | - data[options](); |
| 83 | + if (typeof method === 'string') { |
| 84 | + data[method].apply(data, methodArgs); |
77 | 85 | } |
78 | 86 | }); |
79 | 87 | }; |
|
0 commit comments