Skip to content

Commit

Permalink
Merge pull request #1 from axpauls/items_with_the_same_text
Browse files Browse the repository at this point in the history
Handle situation when different sections can have items with the same text.
  • Loading branch information
patosai committed Aug 26, 2015
2 parents 1956cae + 69d0c03 commit d6a38bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion jquery.tree-multiselect.min.js

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

12 changes: 6 additions & 6 deletions src/jquery.tree-multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
currentPos[pathPart] = [];
}
currentPos = currentPos[pathPart];

if (i == path.length - 1) {
currentPos.push(option);
break;
Expand Down Expand Up @@ -257,7 +257,7 @@
var section = $(this);
var sectionItems = section.find("div.item");
var unselectedItems = sectionItems.filter(function() {
var checkbox = $(this).find("> input[type=checkbox]");
var checkbox = $(this).find("> input[type=checkbox]");
return !(checkbox.is(":checked"));
});
if (unselectedItems.length === 0) {
Expand Down Expand Up @@ -325,11 +325,11 @@
function addNewFromSelected(selections) {
var currentSelections = [];
$(selectedContainer).find("div.item").each(function() {
currentSelections.push(textOf(this));
currentSelections.push($(this).data('value'));
});

var selectionsNotAdded = selections.filter(function(selection) {
return currentSelections.indexOf(selection.text) == -1;
return currentSelections.indexOf(selection.value) == -1;
});

selectionsNotAdded.forEach(function(selection) {
Expand All @@ -342,11 +342,11 @@
function removeOldFromSelected(selections) {
var selectionTexts = [];
selections.forEach(function(selection) {
selectionTexts.push(selection.text);
selectionTexts.push(selection.value);
});

$(selectedContainer).find("div.item").each(function() {
var selection = textOf(this);
var selection = $(this).data('value');
if (selectionTexts.indexOf(selection) == -1) {
$(this).remove();
}
Expand Down
21 changes: 21 additions & 0 deletions test/integration/adding-removing.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ QUnit.test("can add an item", function(assert) {
assert.equal(textOf(selectedItem.find("> span.section-name")), 'section', "selected item section label should be 'section'");
});

QUnit.test("can add an item with the same text", function(assert) {
$("select").append("<option value='one' data-section='section'>One</option>");
$("select").append("<option value='one_more' data-section='section1'>One</option>");
$("select").treeMultiselect();

assert.equal($("div.selections div.item").length, 2, "there should be two items for selection");
assert.equal($("div.selected div.item").length, 0, "no items should be selected");

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

assert.equal($("div.selections div.item > input[type=checkbox]:checked").length, 2, "two items should be checked");
var selectedItem = $("div.selected div.item");
assert.equal($("div.selected div.item").length, 2, "there should be two items in the selected div");

assert.equal(textOf(selectedItem.first()), 'One', "first selected item text should be 'One'");
assert.equal(textOf(selectedItem.last()), 'One', "second selected item text should be 'One'");
assert.equal(textOf(selectedItem.first().find("> span.section-name")), 'section', "first selected item section label should be 'section'");
assert.equal(textOf(selectedItem.last().find("> span.section-name")), 'section1', "second selected item section label should be 'section'");
});

QUnit.test("can remove an item", function(assert) {
$("select").append("<option value='one' data-section='section' selected='selected'>One</option>");
$("select").treeMultiselect();
Expand Down

0 comments on commit d6a38bc

Please sign in to comment.