|
2 | 2 |
|
3 | 3 | (function ($) { |
4 | 4 |
|
5 | | - //////////////////////// |
6 | | - /* Checkboxes object. */ |
7 | | - //////////////////////// |
8 | | - |
9 | | - /** |
10 | | - * Create a new checkbox context. |
11 | | - * |
12 | | - * @param {Object} context DOM context. |
13 | | - */ |
14 | | - var Checkboxes = function (context) { |
15 | | - this.$context = context; |
16 | | - }; |
17 | | - |
18 | | - /** |
19 | | - * Check all checkboxes in context. |
20 | | - */ |
21 | | - Checkboxes.prototype.check = function () { |
22 | | - this.$context.find(':checkbox') |
23 | | - .filter(':not(:disabled)') |
24 | | - .prop('checked', true); |
25 | | - }; |
26 | | - |
27 | | - /** |
28 | | - * Uncheck all checkboxes in context. |
29 | | - */ |
30 | | - Checkboxes.prototype.uncheck = function () { |
31 | | - this.$context.find(':checkbox') |
32 | | - .filter(':not(:disabled)') |
33 | | - .prop('checked', false); |
34 | | - }; |
35 | | - |
36 | | - /** |
37 | | - * Toggle the state of all checkboxes in context. |
38 | | - */ |
39 | | - Checkboxes.prototype.toggle = function () { |
40 | | - this.$context.find(':checkbox') |
41 | | - .filter(':not(:disabled)') |
42 | | - .each(function () { |
43 | | - var $checkbox = $(this); |
44 | | - $checkbox.prop('checked', !$checkbox.is(':checked')); |
45 | | - }); |
46 | | - }; |
47 | | - |
48 | | - /** |
49 | | - * Set the maximum number of checkboxes that can be checked. |
50 | | - * |
51 | | - * @param {Number} max The maximum number of checkbox allowed to be checked. |
52 | | - */ |
53 | | - Checkboxes.prototype.max = function (max) { |
54 | | - if (max > 0) { |
55 | | - // Enable max. |
56 | | - var instance = this; |
57 | | - this.$context.on('click.checkboxes.max', ':checkbox', function () { |
58 | | - if (instance.$context.find(':checked').length === max) { |
59 | | - instance.$context.find(':checkbox:not(:checked)').prop('disabled', true); |
| 5 | + //////////////////////// |
| 6 | + /* Checkboxes object. */ |
| 7 | + //////////////////////// |
| 8 | + |
| 9 | + /** |
| 10 | + * Create a new checkbox context. |
| 11 | + * |
| 12 | + * @param {Object} context DOM context. |
| 13 | + */ |
| 14 | + var Checkboxes = function (context) { |
| 15 | + this.$context = context; |
| 16 | + }; |
| 17 | + |
| 18 | + /** |
| 19 | + * Check all checkboxes in context. |
| 20 | + */ |
| 21 | + Checkboxes.prototype.check = function () { |
| 22 | + this.$context.find(':checkbox') |
| 23 | + .filter(':not(:disabled)') |
| 24 | + .prop('checked', true); |
| 25 | + }; |
| 26 | + |
| 27 | + /** |
| 28 | + * Uncheck all checkboxes in context. |
| 29 | + */ |
| 30 | + Checkboxes.prototype.uncheck = function () { |
| 31 | + this.$context.find(':checkbox') |
| 32 | + .filter(':not(:disabled)') |
| 33 | + .prop('checked', false); |
| 34 | + }; |
| 35 | + |
| 36 | + /** |
| 37 | + * Toggle the state of all checkboxes in context. |
| 38 | + */ |
| 39 | + Checkboxes.prototype.toggle = function () { |
| 40 | + this.$context.find(':checkbox') |
| 41 | + .filter(':not(:disabled)') |
| 42 | + .each(function () { |
| 43 | + var $checkbox = $(this); |
| 44 | + $checkbox.prop('checked', !$checkbox.is(':checked')); |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + /** |
| 49 | + * Set the maximum number of checkboxes that can be checked. |
| 50 | + * |
| 51 | + * @param {Number} max The maximum number of checkbox allowed to be checked. |
| 52 | + */ |
| 53 | + Checkboxes.prototype.max = function (max) { |
| 54 | + if (max > 0) { |
| 55 | + // Enable max. |
| 56 | + var instance = this; |
| 57 | + this.$context.on('click.checkboxes.max', ':checkbox', function () { |
| 58 | + if (instance.$context.find(':checked').length === max) { |
| 59 | + instance.$context.find(':checkbox:not(:checked)').prop('disabled', true); |
| 60 | + } else { |
| 61 | + instance.$context.find(':checkbox:not(:checked)').prop('disabled', false); |
| 62 | + } |
| 63 | + }); |
60 | 64 | } else { |
61 | | - instance.$context.find(':checkbox:not(:checked)').prop('disabled', false); |
| 65 | + // Disable max. |
| 66 | + this.$context.off('click.checkboxes'); |
62 | 67 | } |
63 | | - }); |
64 | | - } else { |
65 | | - // Disable max. |
66 | | - this.$context.off('click.checkboxes'); |
67 | | - } |
68 | | - }; |
69 | | - |
70 | | - /** |
71 | | - * Enable or disable range selection. |
72 | | - * |
73 | | - * @param {Boolean} enable Indicate is range selection has to be enabled. |
74 | | - */ |
75 | | - Checkboxes.prototype.range = function (enable) { |
76 | | - if (enable) { |
77 | | - var instance = this; |
78 | | - |
79 | | - this.$context.on('click.checkboxes.range', ':checkbox', function (event) { |
80 | | - var $checkbox = $(event.target); |
81 | | - |
82 | | - if (event.shiftKey && instance.$last) { |
83 | | - var $checkboxes = instance.$context.find(':checkbox'); |
84 | | - var from = $checkboxes.index(instance.$last); |
85 | | - var to = $checkboxes.index($checkbox); |
86 | | - var start = Math.min(from, to); |
87 | | - var end = Math.max(from, to) + 1; |
88 | | - |
89 | | - $checkboxes.slice(start, end) |
90 | | - .filter(':not(:disabled)') |
91 | | - .prop('checked', $checkbox.prop('checked')); |
| 68 | + }; |
| 69 | + |
| 70 | + /** |
| 71 | + * Enable or disable range selection. |
| 72 | + * |
| 73 | + * @param {Boolean} enable Indicate is range selection has to be enabled. |
| 74 | + */ |
| 75 | + Checkboxes.prototype.range = function (enable) { |
| 76 | + if (enable) { |
| 77 | + var instance = this; |
| 78 | + |
| 79 | + this.$context.on('click.checkboxes.range', ':checkbox', function (event) { |
| 80 | + var $checkbox = $(event.target); |
| 81 | + |
| 82 | + if (event.shiftKey && instance.$last) { |
| 83 | + var $checkboxes = instance.$context.find(':checkbox'); |
| 84 | + var from = $checkboxes.index(instance.$last); |
| 85 | + var to = $checkboxes.index($checkbox); |
| 86 | + var start = Math.min(from, to); |
| 87 | + var end = Math.max(from, to) + 1; |
| 88 | + |
| 89 | + $checkboxes.slice(start, end) |
| 90 | + .filter(':not(:disabled)') |
| 91 | + .prop('checked', $checkbox.prop('checked')); |
| 92 | + } |
| 93 | + instance.$last = $checkbox; |
| 94 | + }); |
| 95 | + } else { |
| 96 | + this.$context.off('click.checkboxes.range'); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + /////////////////////////////// |
| 101 | + /* Checkboxes jQuery plugin. */ |
| 102 | + /////////////////////////////// |
| 103 | + |
| 104 | + // Keep old Checkboxes jQuery plugin, if any, to no override it. |
| 105 | + var old = $.fn.checkboxes; |
| 106 | + |
| 107 | + /** |
| 108 | + * Checkboxes jQuery plugin. |
| 109 | + * |
| 110 | + * @param {String} method Method to invoke. |
| 111 | + * |
| 112 | + * @return {Object} jQuery object. |
| 113 | + */ |
| 114 | + $.fn.checkboxes = function (method) { |
| 115 | + // Get extra arguments as method arguments. |
| 116 | + var methodArgs = Array.prototype.slice.call(arguments, 1); |
| 117 | + |
| 118 | + return this.each(function () { |
| 119 | + var $this = $(this); |
| 120 | + |
| 121 | + // Check if we already have an instance. |
| 122 | + var instance = $this.data('checkboxes'); |
| 123 | + if (!instance) { |
| 124 | + $this.data('checkboxes', (instance = new Checkboxes($this, typeof method === 'object' && method))); |
| 125 | + } |
| 126 | + |
| 127 | + // Check if we need to invoke a public method. |
| 128 | + if (typeof method === 'string' && instance[method]) { |
| 129 | + instance[method].apply(instance, methodArgs); |
| 130 | + } |
| 131 | + }); |
| 132 | + }; |
| 133 | + |
| 134 | + // Store a constructor reference. |
| 135 | + $.fn.checkboxes.Constructor = Checkboxes; |
| 136 | + |
| 137 | + |
| 138 | + //////////////////////////////////// |
| 139 | + /* Checkboxes jQuery no conflict. */ |
| 140 | + //////////////////////////////////// |
| 141 | + |
| 142 | + /** |
| 143 | + * No conflictive Checkboxes jQuery plugin. |
| 144 | + */ |
| 145 | + $.fn.checkboxes.noConflict = function () { |
| 146 | + $.fn.checkboxes = old; |
| 147 | + return this; |
| 148 | + }; |
| 149 | + |
| 150 | + |
| 151 | + ////////////////////////// |
| 152 | + /* Checkboxes data-api. */ |
| 153 | + ////////////////////////// |
| 154 | + |
| 155 | + /** |
| 156 | + * Handle data-api click. |
| 157 | + * |
| 158 | + * @param {Object} event Click event. |
| 159 | + */ |
| 160 | + var dataApiClickHandler = function (event) { |
| 161 | + var el = $(event.target); |
| 162 | + var href = el.attr('href'); |
| 163 | + var $context = $(el.data('context') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))); |
| 164 | + var action = el.data('action'); |
| 165 | + |
| 166 | + if ($context && action) { |
| 167 | + if (!el.is(':checkbox')) { |
| 168 | + event.preventDefault(); |
| 169 | + } |
| 170 | + $context.checkboxes(action); |
92 | 171 | } |
93 | | - instance.$last = $checkbox; |
94 | | - }); |
95 | | - } else { |
96 | | - this.$context.off('click.checkboxes.range'); |
97 | | - } |
98 | | - }; |
99 | | - |
100 | | - /////////////////////////////// |
101 | | - /* Checkboxes jQuery plugin. */ |
102 | | - /////////////////////////////// |
103 | | - |
104 | | - // Keep old Checkboxes jQuery plugin, if any, to no override it. |
105 | | - var old = $.fn.checkboxes; |
106 | | - |
107 | | - /** |
108 | | - * Checkboxes jQuery plugin. |
109 | | - * |
110 | | - * @param {String} method Method to invoke. |
111 | | - * |
112 | | - * @return {Object} jQuery object. |
113 | | - */ |
114 | | - $.fn.checkboxes = function (method) { |
115 | | - // Get extra arguments as method arguments. |
116 | | - var methodArgs = Array.prototype.slice.call(arguments, 1); |
117 | | - |
118 | | - return this.each(function () { |
119 | | - var $this = $(this); |
120 | | - |
121 | | - // Check if we already have an instance. |
122 | | - var instance = $this.data('checkboxes'); |
123 | | - if (!instance) { |
124 | | - $this.data('checkboxes', (instance = new Checkboxes($this, typeof method === 'object' && method))); |
125 | | - } |
126 | | - |
127 | | - // Check if we need to invoke a public method. |
128 | | - if (typeof method === 'string' && instance[method]) { |
129 | | - instance[method].apply(instance, methodArgs); |
130 | | - } |
131 | | - }); |
132 | | - }; |
133 | | - |
134 | | - // Store a constructor reference. |
135 | | - $.fn.checkboxes.Constructor = Checkboxes; |
136 | | - |
137 | | - |
138 | | - //////////////////////////////////// |
139 | | - /* Checkboxes jQuery no conflict. */ |
140 | | - //////////////////////////////////// |
141 | | - |
142 | | - /** |
143 | | - * No conflictive Checkboxes jQuery plugin. |
144 | | - */ |
145 | | - $.fn.checkboxes.noConflict = function () { |
146 | | - $.fn.checkboxes = old; |
147 | | - return this; |
148 | | - }; |
149 | | - |
150 | | - |
151 | | - ////////////////////////// |
152 | | - /* Checkboxes data-api. */ |
153 | | - ////////////////////////// |
154 | | - |
155 | | - /** |
156 | | - * Handle data-api click. |
157 | | - * |
158 | | - * @param {Object} event Click event. |
159 | | - */ |
160 | | - var dataApiClickHandler = function (event) { |
161 | | - var el = $(event.target); |
162 | | - var href = el.attr('href'); |
163 | | - var $context = $(el.data('context') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))); |
164 | | - var action = el.data('action'); |
165 | | - |
166 | | - if ($context && action) { |
167 | | - if (!el.is(':checkbox')) { |
168 | | - event.preventDefault(); |
169 | | - } |
170 | | - $context.checkboxes(action); |
171 | | - } |
172 | | - }; |
173 | | - |
174 | | - /** |
175 | | - * Handle data-api DOM ready. |
176 | | - */ |
177 | | - var dataApiDomReadyHandler = function () { |
178 | | - $('[data-toggle^=checkboxes]').each(function () { |
179 | | - var el = $(this), |
180 | | - actions = el.data(); |
181 | | - delete actions.toggle; |
182 | | - for (var action in actions) { |
183 | | - el.checkboxes(action, actions[action]); |
184 | | - } |
185 | | - }); |
186 | | - }; |
187 | | - |
188 | | - // Register data-api listeners. |
189 | | - $(document).on('click.checkboxes.data-api', '[data-toggle^=checkboxes]', dataApiClickHandler); |
190 | | - $(document).on('ready.checkboxes.data-api', dataApiDomReadyHandler); |
| 172 | + }; |
| 173 | + |
| 174 | + /** |
| 175 | + * Handle data-api DOM ready. |
| 176 | + */ |
| 177 | + var dataApiDomReadyHandler = function () { |
| 178 | + $('[data-toggle^=checkboxes]').each(function () { |
| 179 | + var el = $(this), |
| 180 | + actions = el.data(); |
| 181 | + delete actions.toggle; |
| 182 | + for (var action in actions) { |
| 183 | + el.checkboxes(action, actions[action]); |
| 184 | + } |
| 185 | + }); |
| 186 | + }; |
| 187 | + |
| 188 | + // Register data-api listeners. |
| 189 | + $(document).on('click.checkboxes.data-api', '[data-toggle^=checkboxes]', dataApiClickHandler); |
| 190 | + $(document).on('ready.checkboxes.data-api', dataApiDomReadyHandler); |
191 | 191 |
|
192 | 192 | })(window.jQuery); |
0 commit comments