1- ( function ( $ ) {
2-
3- //////////////////////////////////
4- /* Checkboxes class definition. */
5- //////////////////////////////////
6-
7- var Checkboxes = function ( $context , options ) {
8- this . $context = $context ;
9- } ;
10-
11- /**
12- * Check all checkboxes in context.
13- */
14- Checkboxes . prototype . check = function ( ) {
15- this . $context . find ( ':checkbox' )
16- . filter ( ':not(:disabled)' )
17- . prop ( 'checked' , true ) ;
18- } ;
19-
20- /**
21- * Uncheck all checkboxes in context.
22- */
23- Checkboxes . prototype . uncheck = function ( ) {
24- this . $context . find ( ':checkbox' )
25- . filter ( ':not(:disabled)' )
26- . prop ( 'checked' , false ) ;
27- } ;
28-
29- /**
30- * Toggle the state of all checkboxes in context.
31- */
32- Checkboxes . prototype . toggle = function ( ) {
33- this . $context . find ( ':checkbox' ) . filter ( ':not(:disabled)' ) . each ( function ( ) {
34- var $checkbox = $ ( this ) ;
35- $checkbox . prop ( 'checked' , ! $checkbox . is ( ':checked' ) ) ;
36- } ) ;
37- } ;
38-
39- /**
40- * Set the maximum number of checkboxes that can be checked.
41- *
42- * @param max {number} The maximum number of checkbox allowed to be checked.
43- */
44- Checkboxes . prototype . max = function ( max ) {
45- if ( max === 0 ) {
46- // Disable max.
47- this . $context . off ( 'click.checkboxes' ) ;
48- } else if ( max > 0 ) {
49- // Enable max.
50- var instance = this ;
51- this . $context . on ( 'click.checkboxes.max' , ':checkbox' , function ( e ) {
52- if ( instance . $context . find ( ':checked' ) . length === max ) {
53- instance . $context . find ( ':checkbox:not(:checked)' ) . prop ( 'disabled' , true ) ;
54- } else {
55- instance . $context . find ( ':checkbox:not(:checked)' ) . prop ( 'disabled' , false ) ;
56- }
57- } ) ;
58- }
59- } ;
60-
61- /**
62- * Enable or disable range selection.
63- *
64- * @param enable {boolean} Indicate is range selection has to be enabled.
65- */
66- Checkboxes . prototype . range = function ( enable ) {
67- if ( enable ) {
68- var instance = this ;
69- this . $context . on ( 'click.checkboxes.range' , ':checkbox' , function ( e ) {
70- var $checkbox = $ ( e . target ) ;
71- if ( e . shiftKey && instance . $last ) {
72- var $checkboxes = instance . $context . find ( ':checkbox' ) ,
73- from = $checkboxes . index ( instance . $last ) ,
74- to = $checkboxes . index ( $checkbox ) ,
75- start = Math . min ( from , to ) ,
76- end = Math . max ( from , to ) + 1 ;
77- $checkboxes . slice ( start , end )
78- . filter ( ':not(:disabled)' )
79- . prop ( 'checked' , $checkbox . prop ( 'checked' ) ) ;
80- }
81- instance . $last = $checkbox ;
82- } ) ;
83- } else {
84- this . $context . off ( 'click.checkboxes.range' ) ;
85- }
86- } ;
87-
88- ///////////////////////////////////
89- /* Checkboxes plugin definition. */
90- ///////////////////////////////////
91-
92- var old = $ . fn . checkboxes ;
93-
94- $ . fn . checkboxes = function ( method ) {
95- var methodArgs = Array . prototype . slice . call ( arguments , 1 ) ;
96- return this . each ( function ( ) {
97- var $this = $ ( this ) ,
98- data = $this . data ( 'checkboxes' ) ;
99- if ( ! data ) {
100- $this . data ( 'checkboxes' , ( data = new Checkboxes ( $this , typeof method === 'object' && method ) ) ) ;
101- }
102- if ( typeof method === 'string' && data [ method ] ) {
103- data [ method ] . apply ( data , methodArgs ) ;
104- }
105- } ) ;
106- } ;
107-
108- $ . fn . checkboxes . Constructor = Checkboxes ;
109-
110-
111- /////////////////////////////
112- /* Checkboxes no conflict. */
113- /////////////////////////////
114-
115- $ . fn . checkboxes . noConflict = function ( ) {
116- $ . fn . checkboxes = old ;
117- return this ;
118- } ;
119-
120-
121- //////////////////////////
122- /* Checkboxes data-api. */
123- //////////////////////////
124-
125- $ ( document ) . on ( 'click.checkboxes.data-api' , '[data-toggle^=checkboxes]' , function ( e ) {
126- var el = $ ( e . target ) ,
127- href = el . attr ( 'href' ) ,
128- $context = $ ( el . data ( 'context' ) || ( href && href . replace ( / .* (? = # [ ^ \s ] + $ ) / , '' ) ) ) ,
129- action = el . data ( 'action' ) ;
130- if ( $context && action ) {
131- if ( ! el . is ( ':checkbox' ) ) {
132- e . preventDefault ( ) ;
133- }
134- $context . checkboxes ( action ) ;
135- }
136- } ) ;
137-
138- $ ( document ) . on ( 'ready.checkboxes.data-api' , function ( ) {
139- $ ( '[data-toggle^=checkboxes]' ) . each ( function ( ) {
140- var el = $ ( this ) ,
141- actions = el . data ( ) ;
142- delete actions . toggle ;
143- for ( var action in actions ) {
144- el . checkboxes ( action , actions [ action ] ) ;
145- }
146- } ) ;
147- } ) ;
148-
149- } ) ( window . jQuery ) ;
1+ 'use strict' ;
2+
3+ ( function ( $ ) {
4+
5+ //////////////////////////////////
6+ /* Checkboxes class definition. */
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+ } ) ;
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' ) ) ;
92+ }
93+ instance . $last = $checkbox ;
94+ } ) ;
95+ } else {
96+ this . $context . off ( 'click.checkboxes.range' ) ;
97+ }
98+ } ;
99+
100+ ///////////////////////////////////
101+ /* Checkboxes plugin definition. */
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 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 ) ;
191+
192+ } ) ( window . jQuery ) ;
0 commit comments