Skip to content

Commit

Permalink
Fix bug where treeMultiselect()'ing multiple times would change opts
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Tsai committed Sep 10, 2015
1 parent a59af1a commit fdb20d0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 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.4",
"version": "1.14.5",
"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.4
* v1.14.5
*
* (c) Patrick Tsai et al.
* MIT Licensed
Expand Down
36 changes: 17 additions & 19 deletions src/jquery.tree-multiselect.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/*
* jQuery Tree Multiselect
* v1.14.4
* v1.14.5
*
* (c) Patrick Tsai et al.
* MIT Licensed
*/

(function($) {
var options;

$.fn.treeMultiselect = function(opts) {
options = mergeDefaultOptions(opts);
var options = mergeDefaultOptions(opts);
this.each(function() {
var originalSelect = $(this);
originalSelect.attr('multiple', '').css('display', 'none');
Expand All @@ -20,27 +18,27 @@

var selectionContainer = uiBuilder.selections;

generateSelections(originalSelect, selectionContainer);
generateSelections(originalSelect, selectionContainer, options);

addDescriptionHover(selectionContainer);
addCheckboxes(selectionContainer);
checkPreselectedSelections(originalSelect, selectionContainer);
addDescriptionHover(selectionContainer, options);
addCheckboxes(selectionContainer, options);
checkPreselectedSelections(originalSelect, selectionContainer, options);

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

if (options.collapsible) {
addCollapsibility(selectionContainer);
addCollapsibility(selectionContainer, options);
}

var selectedContainer = uiBuilder.selected;
updateSelectedAndOnChange(selectionContainer, selectedContainer, originalSelect);
updateSelectedAndOnChange(selectionContainer, selectedContainer, originalSelect, options);

armRemoveSelectedOnClick(selectionContainer, selectedContainer);
armRemoveSelectedOnClick(selectionContainer, selectedContainer, options);
});
return this;
};
Expand Down Expand Up @@ -84,7 +82,7 @@
return $.extend({}, defaults, options);
}

function generateSelections(originalSelect, selectionContainer) {
function generateSelections(originalSelect, selectionContainer, options) {
var data = {};

function insertOption(path, option) {
Expand Down Expand Up @@ -202,7 +200,7 @@
});
}

function addCheckboxes(selectionContainer) {
function addCheckboxes(selectionContainer, options) {
var checkbox = $('<input />', { type: 'checkbox' });
if (options.freeze) {
checkbox.attr('disabled', 'disabled');
Expand Down Expand Up @@ -291,7 +289,7 @@
onCheckboxChange(selectionContainer, check);
}

function addCollapsibility(selectionContainer) {
function addCollapsibility(selectionContainer, options) {
var hideIndicator = "-";
var expandIndicator = "+";

Expand Down Expand Up @@ -320,7 +318,7 @@
});
}

function updateSelectedAndOnChange(selectionContainer, selectedContainer, originalSelect) {
function updateSelectedAndOnChange(selectionContainer, selectedContainer, originalSelect, options) {
function createSelectedDiv(selection) {
var text = selection.text;
var value = selection.value;
Expand Down
24 changes: 24 additions & 0 deletions test/integration/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,27 @@ QUnit.test("can freeze selections", function(assert) {
var removeSpans = $("div.selected span.remove-selected");
assert.equal(removeSpans.length, 0);
});

QUnit.test("freeze does not affect other treeMultiselects", function(assert) {
$("select").append("<option value='one' data-section='test'>One</option>");
var options = {
freeze: true
};
$("select").treeMultiselect(options);

$("div#qunit-fixture").append("<select id='frozen'></select>");
$("select#frozen").append("<option value='two' data-section='test' selected='selected'>Two</option>");
$("select#frozen").treeMultiselect();

var frozenOption = $("div.selections div.item:contains(One)");
assert.equal(frozenOption.length, 1);
assert.ok(!!frozenOption.find("input[type=checkbox]").attr('disabled'));

var unfrozenOption = $("div.selections div.item:contains(Two)");
assert.equal(unfrozenOption.length, 1);
unfrozenOption.find("input[type=checkbox]").prop('checked', 'true').trigger('change');

var unfrozenSelection = $("div.selected div.item:contains(Two)");
assert.equal(unfrozenSelection.length, 1);
assert.equal(unfrozenSelection.find("span.remove-selected").length, 1);
});

0 comments on commit fdb20d0

Please sign in to comment.