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.
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' ) ;
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 ) ;
0 commit comments