Skip to content

Commit

Permalink
Fix IE
Browse files Browse the repository at this point in the history
  • Loading branch information
kloverde committed Apr 12, 2017
1 parent c25cdf0 commit 1cc3121
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Release 2.1 (UPCOMING RELEASE)
# Release 3.0 (April 11, 2017)

* The select-all checkbox no longer changes the state of disabled checkboxes

* BREAKING CHANGE: If you are using SelectAllCheckbox in an unusual scenario of ticking the
select-all checkbox via script and then firing `change` to trigger the select-all action,
you'll find that that no longer works. IE has a known bug where it doesn't fire the change
event when an indeterminate checkbox is clicked, meaning that until this release, this
plugin was never fully compatible with IE. To get around the issue, the plugin now listens
for the `click` event on the select-all checkbox instead of `change`. The other
checkboxes continue to use the change event as they always have.

* The select-all checkbox initializes to the proper state (checked, unchecked, indeterminate)
based on the state of the checkbox group when the plugin is initialized

Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
SelectAllCheckbox v2.1 (UPCOMING RELEASE)
SelectAllCheckbox v3.0
======================

# You've stumbled upon a development branch. Have a look around if you'd like, but only use code that has been posted to the releases page.

See LICENSE for this software's licensing terms.

SelectAllCheckbox is a jQuery plugin which makes it easy to create "select all" checkboxes for checkbox groups.
Expand All @@ -16,7 +14,7 @@ SelectAllCheckbox is a jQuery plugin which makes it easy to create "select all"
* Supports any number of checkbox groups
* Configuration accepts a callback which executes when one or more checkboxes changes state
* The callback receives the changed checkbox(es) as an array of jQuery objects, plus the status of the checkbox group (all/some/none checked)
* [NEW!] Select-all checkboxes do not modify the state of disabled checkboxes
* Select-all checkboxes do not modify the state of disabled checkboxes


## Using
Expand Down
22 changes: 10 additions & 12 deletions src/jquery.selectallcheckbox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SelectAllCheckbox v2.1
* SelectAllCheckbox v3.0
* https://www.github.com/kloverde/jquery-SelectAllCheckbox
*
* Donations: https://paypal.me/KurtisLoVerde/5
Expand Down Expand Up @@ -62,12 +62,10 @@

setSelectAllCheckboxInitialState();

// The select-all checkbox's change handler:
// Selects or deselects all checkboxes and invokes the user-supplied callback.
// If you set a checkbox's state via script, you must trigger change() on the
// modified checkbox to ensure the select-all checkbox's state is updated.
// JQuery does not fire change() for you.
selectAll.change( function() {
// The select-all event listener uses 'click' instead of 'change' as a workaround to
// a known IE issue, where indeterminate checkboxes don't fire the 'change' event.

selectAll.click( function() {
var allCheckboxes = $( CHECKBOX_GROUP_SELECTOR );
var enabledCheckboxes = allCheckboxes.filter( ":enabled" );
var eligibleForChecking = enabledCheckboxes.filter( ":not(:checked)" );
Expand Down Expand Up @@ -112,11 +110,11 @@
$( CHECKBOX_GROUP_SELECTOR ).each( function() {
var box = $( this );

// The checkbox change handler:
// Updates the select-all checkbox's state and invokes the user-supplied callback.
// If you set a checkbox's state via script, you must trigger change() on the
// modified checkbox to ensure the select-all checkbox's state is updated.
// JQuery does not fire change() for you.
// Updates the select-all checkbox's state and invokes the user-supplied callback. If you set a checkbox's state
// via script, you must trigger change() on the modified checkbox to ensure the select-all checkbox's state is
// updated. JQuery does not fire change() for you. The select-all checkbox is different: it responds to the
// click event instead of the change event because IE doesn't fire the change event for indeterminate checkboxes.

box.change( function() {
var someChecked = false,
someNotChecked = false;
Expand Down
34 changes: 34 additions & 0 deletions src/jquery.selectallcheckbox.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* SelectAllCheckbox v3.0
* https://www.github.com/kloverde/jquery-SelectAllCheckbox
*
* Donations: https://paypal.me/KurtisLoVerde/5
*
* Copyright (c) 2016, Kurtis LoVerde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

!function(a){"use strict";a.fn.selectAllCheckbox=function(b){function i(){var b=a(g),c=0,h=null;b.each(function(){if(!0===a(this).prop("checked")&&c++,0===c)h=d;else if(c<b.length)h=e;else{if(c!==b.length)throw"Impossible result: more checkboxes checked than actually exist";h=f}j(h)})}function j(a){"some"===a?(h.prop("checked",!1),c.useIndeterminate&&h.prop("indeterminate",!0)):"all"===a?(h.prop("indeterminate",!1),h.prop("checked",!0)):"none"===a&&(h.prop("indeterminate",!1),h.prop("checked",!1))}var c=a.extend({checkboxesName:"checkboxes",onChangeCallback:void 0,useIndeterminate:!0},b),d="none",e="some",f="all",g="input[type='checkbox'][name='"+c.checkboxesName+"']",h=this;return i(),h.click(function(){var b=a(g),h=b.filter(":enabled"),i=h.filter(":not(:checked)"),k=i.length>0,l=[];h.each(function(){var b=a(this);b.prop("checked")!==k&&(b.prop("checked",k),l.push(b))});var m=0;if(b.each(function(){a(this).is(":checked")&&m++}),0===m)status=d;else if(m<b.length)status=e;else{if(m!==b.length)throw"Impossible result: more checkboxes checked than actually exist";status=f}j(status),l.length>0&&"function"==typeof c.onChangeCallback&&c.onChangeCallback(l,status)}),a(g).each(function(){var b=a(this);b.change(function(){var h=!1,i=!1,k=d;if(a(g).each(function(){!0===a(this).prop("checked")?h=!0:i=!0}),h&&i?k=e:h&&!i?k=f:h||(k=d),j(k),"function"==typeof c.onChangeCallback){var l=[b];c.onChangeCallback(l,k)}})}),h}}(jQuery);
5 changes: 3 additions & 2 deletions test/js/testCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ var testCases;
),

tc( "indeterminate noneDisabled_allChecked", checkboxGroups["noneDisabled_allChecked"], true, SelectAllState.CHECKED,
[ clickScript("3", [3], GroupState.SOME, SelectAllState.PARTIALLY_CHECKED, [true, true, true, false]),
clickScript("selectAll", [3], GroupState.ALL, SelectAllState.CHECKED, [true, true, true, true]) ]
[ clickScript("3", [3], GroupState.SOME, SelectAllState.PARTIALLY_CHECKED, [true, true, true, false]),
clickScript("selectAll", [3], GroupState.ALL, SelectAllState.CHECKED, [true, true, true, true]),
clickScript("selectAll", [0, 1, 2, 3], GroupState.NONE, SelectAllState.NOT_CHECKED, [false, false, false, false]) ]
),

//--------------------
Expand Down

0 comments on commit 1cc3121

Please sign in to comment.