Skip to content

Commit

Permalink
v1.12.0 - parent sections now auto-check if all children are selected
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Tsai committed Aug 23, 2015
1 parent 49bc76a commit 0d9a9b8
Show file tree
Hide file tree
Showing 13 changed files with 405 additions and 295 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function(grunt) {
options: {
force: true
},
all: ['test/**/*.html']
all: ['test/runner.html']
},
cssmin: {
dist: {
Expand Down
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.11.1",
"version": "1.12.0",
"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.11.1
* v1.12.0
*
* (c) Patrick Tsai
* MIT Licensed
Expand Down
39 changes: 34 additions & 5 deletions src/jquery.tree-multiselect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* jQuery Tree Multiselect
* v1.11.1
* v1.12.0
*
* (c) Patrick Tsai
* MIT Licensed
Expand All @@ -25,8 +25,12 @@
addDescriptionHover(selectionContainer);
addCheckboxes(selectionContainer);
checkPreselectedSelections(originalSelect, selectionContainer);
armTitleCheckboxes(selectionContainer);
uncheckParentsOnUnselect(selectionContainer);

if (options.allowBatchSelection) {
armTitleCheckboxes(selectionContainer);
uncheckParentsOnUnselect(selectionContainer);
checkParentsOnAllChildrenSelected(selectionContainer);
}

if (options.collapsible) {
addCollapsibility(selectionContainer);
Expand Down Expand Up @@ -241,11 +245,36 @@
var checkboxes = $(selectionContainer).find("input[type=checkbox]");
checkboxes.change(function() {
if ($(this).is(":checked")) return;
var sectionParents = $(this).parents("div.section");
var sectionParents = $(this).parentsUntil(selectionContainer, "div.section");
sectionParents.find("> div.title > input[type=checkbox]").prop('checked', false);
});
}

function checkParentsOnAllChildrenSelected(selectionContainer) {
function check() {
var sections = $(selectionContainer).find("div.section");
sections.each(function() {
var section = $(this);
var sectionItems = section.find("div.item");
var unselectedItems = sectionItems.filter(function() {
var checkbox = $(this).find("> input[type=checkbox]");
return !(checkbox.is(":checked"));
});
if (unselectedItems.length === 0) {
var sectionCheckbox = $(this).find("> div.title > input[type=checkbox]");
sectionCheckbox.prop('checked', true);
}
});
}

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

check();
}

function addCollapsibility(selectionContainer) {
var hideIndicator = "-";
var expandIndicator = "+";
Expand Down Expand Up @@ -352,7 +381,7 @@
var value = $(this).attr('data-value');
var index = $(this).attr('data-index');
$(this).attr('data-index', undefined);
var sectionName = $.map($(this).parents("div.section").get().reverse(), function(parentSection) {
var sectionName = $.map($(this).parentsUntil(selectionContainer, "div.section").get().reverse(), function(parentSection) {
return textOf($(parentSection).find("> div.title"));
}).join("/");
selections.push({ text: text, value: value, index: index, sectionName: sectionName });
Expand Down
3 changes: 3 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function textOf(el) {
return $(el).clone().children().remove().end().text();
}
Loading

0 comments on commit 0d9a9b8

Please sign in to comment.