Skip to content

Commit

Permalink
Add indeterminate markings to title checkoxes
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Tsai committed Aug 29, 2015
1 parent 663d03b commit 1df51c1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 17 deletions.
2 changes: 1 addition & 1 deletion jquery.tree-multiselect.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions jquery.tree-multiselect.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery.tree-multiselect.js",
"version": "1.14.0",
"version": "1.14.1",
"description": "jQuery multiple select with nested options",
"main": "jquery.tree-multiselect.min.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/jquery.tree-multiselect.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* jQuery Tree Multiselect
* v1.14.0
* v1.14.1
*
* (c) Patrick Tsai et al.
* MIT Licensed
Expand Down
43 changes: 31 additions & 12 deletions src/jquery.tree-multiselect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* jQuery Tree Multiselect
* v1.14.0
* v1.14.1
*
* (c) Patrick Tsai et al.
* MIT Licensed
Expand Down Expand Up @@ -30,6 +30,7 @@
armTitleCheckboxes(selectionContainer);
uncheckParentsOnUnselect(selectionContainer);
checkParentsOnAllChildrenSelected(selectionContainer);
showSemifilledParents(selectionContainer);
}

if (options.collapsible) {
Expand Down Expand Up @@ -267,12 +268,27 @@
});
}

var checkboxes = $(selectionContainer).find("input[type=checkbox]");
checkboxes.change(function() {
check();
});
onCheckboxChange(selectionContainer, check);
}

function showSemifilledParents(selectionContainer) {
function check() {
var sections = $(selectionContainer).find("div.section");
sections.each(function() {
var section = $(this);
var items = section.find("div.item");
var numSelected = items.filter(function() {
var item = $(this);
return item.find("> input[type=checkbox]").prop('checked');
}).length;

var sectionCheckbox = $(this).find("> div.title > input[type=checkbox]");
var isIndeterminate = (numSelected !== 0 && numSelected !== items.length)
sectionCheckbox.prop('indeterminate', isIndeterminate);
});
}

check();
onCheckboxChange(selectionContainer, check);
}

function addCollapsibility(selectionContainer) {
Expand Down Expand Up @@ -408,12 +424,7 @@
}
}

var checkboxes = $(selectionContainer).find("input[type=checkbox]");
checkboxes.change(function() {
update();
});

update();
onCheckboxChange(selectionContainer, update);
}

function armRemoveSelectedOnClick(selectionContainer, selectedContainer) {
Expand All @@ -425,4 +436,12 @@
matchingCheckbox.trigger('change');
});
}

function onCheckboxChange(selectionContainer, callback) {
var checkboxes = $(selectionContainer).find("input[type=checkbox]");
checkboxes.change(function() {
callback();
});
callback();
}
})(jQuery);
46 changes: 46 additions & 0 deletions test/integration/title-autochecking.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,49 @@ QUnit.test("nested titles should all be checked if a title is batch selected", f

assert.equal($("input[type=checkbox]:checked").length, 4, "all checkboxes should be checked");
});

QUnit.test("title checkbox is indeterminate when some but not all options are selected", function(assert) {
$("select").append("<option value='one' data-section='top'>One</option>");
$("select").append("<option value='two' data-section='top' selected='selected'>Two</option>");
$("select").treeMultiselect();

var titleCheckbox = $("div.selections").find("div.title > input[type=checkbox]");
assert.equal(titleCheckbox.length, 1);
assert.ok(titleCheckbox.prop('indeterminate'));
});

QUnit.test("title checkbox turns indeterminate when some options are selected", function(assert) {
$("select").append("<option value='one' data-section='top'>One</option>");
$("select").append("<option value='two' data-section='top'>Two</option>");
$("select").treeMultiselect();

var lastCheckbox = $("div.selections").find("div.item > input[type=checkbox]").last();
lastCheckbox.prop('checked', true).trigger('change');

var titleCheckbox = $("div.selections").find("div.title > input[type=checkbox]");
assert.ok(titleCheckbox.prop('indeterminate'));
});

QUnit.test("title checkbox is not indeterminate when all options are selected", function(assert) {
$("select").append("<option value='one' data-section='top'>One</option>");
$("select").append("<option value='two' data-section='top'>Two</option>");
$("select").treeMultiselect();

var checkboxes = $("div.selections").find("input[type=checkbox]");
checkboxes.prop('checked', true).trigger('change');

var titleCheckbox = $("div.selections").find("div.title > input[type=checkbox]");
assert.ok(!(titleCheckbox.prop('indeterminate')));
});

QUnit.test("title checkbox is not indeterminate when no options are selected", function(assert) {
$("select").append("<option value='one' data-section='top'>One</option>");
$("select").append("<option value='two' data-section='top' selected='selected'>Two</option>");
$("select").treeMultiselect();

var checkedCheckboxes = $("div.selections").find("input[type=checkbox]:checked");
checkedCheckboxes.prop('checked', false).trigger('change');

var titleCheckbox = $("div.selections").find("div.title > input[type=checkbox]");
assert.ok(!(titleCheckbox.prop('indeterminate')));
});

0 comments on commit 1df51c1

Please sign in to comment.