Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open parents in addToSelection #766

Merged
merged 4 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/_entries/general/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name: changelog
#### Development version

- Issue #735: compile to es6
- Issue #766: open parent nodes in the addToSelection method (thanks to Tmgarcia)

#### 1.7.5 (october 21 2023)

Expand Down
35 changes: 32 additions & 3 deletions src/test/jqTree/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,29 @@ describe("addToSelection", () => {
given("child1", () => given.$tree.tree("getNodeByNameMustExist", "child1"));
given("child2", () => given.$tree.tree("getNodeByNameMustExist", "child2"));

beforeEach(() => {
it("selects the nodes", () => {
given.$tree.tree({
autoOpen: true,
data: exampleData,
});

given.$tree.tree("addToSelection", given.child1);
given.$tree.tree("addToSelection", given.child2);
});

it("selects the nodes", () => {
expect(given.$tree.tree("getSelectedNodes")).toEqual(
expect.arrayContaining([given.child1, given.child2]),
);
});

it("renders the nodes correctly", () => {
given.$tree.tree({
autoOpen: true,
data: exampleData,
});

given.$tree.tree("addToSelection", given.child1);
given.$tree.tree("addToSelection", given.child2);

expect(given.$tree).toHaveTreeStructure([
expect.objectContaining({
name: "node1",
Expand All @@ -172,6 +178,20 @@ describe("addToSelection", () => {
}),
]);
});

it("opens the parent node when it's closed", () => {
given.$tree.tree({
autoOpen: false,
data: exampleData,
});

const node1 = given.$tree.tree("getNodeByNameMustExist", "node1");
expect(node1.is_open).toBeFalsy();

given.$tree.tree("addToSelection", given.child1);

expect(node1.is_open).toBe(true);
});
});

describe("appendNode", () => {
Expand Down Expand Up @@ -1207,6 +1227,15 @@ describe("selectNode", () => {
expect(given.$tree.tree("getSelectedNode")).toBeFalse();
});
});

it("opens the parent node when it's closed", () => {
expect(given.node1.is_open).toBeFalsy();

const child1 = given.$tree.tree("getNodeByNameMustExist", "child1");
given.$tree.tree("selectNode", child1);

expect(given.node1.is_open).toBe(true);
});
});

describe("setOption", () => {
Expand Down
19 changes: 10 additions & 9 deletions src/tree.jquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ export class JqTreeWidget extends MouseWidget<JQTreeOptions> {
}

this.selectNodeHandler.addToSelection(node);
this.openParents(node);

this._getNodeElementForNode(node).select(
mustSetFocus === undefined ? true : mustSetFocus,
Expand Down Expand Up @@ -1079,14 +1080,6 @@ export class JqTreeWidget extends MouseWidget<JQTreeOptions> {
}
};

const openParents = (): void => {
const parent = node.parent;

if (parent && parent.parent && !parent.is_open) {
this.openNode(parent, false);
}
};

if (!canSelect()) {
return;
}
Expand All @@ -1108,7 +1101,7 @@ export class JqTreeWidget extends MouseWidget<JQTreeOptions> {
node,
deselected_node: deselectedNode,
});
openParents();
this.openParents(node);
}

saveState();
Expand Down Expand Up @@ -1353,6 +1346,14 @@ export class JqTreeWidget extends MouseWidget<JQTreeOptions> {
$treeElement,
});
}

private openParents(node: Node) {
const parent = node.parent;

if (parent && parent.parent && !parent.is_open) {
this.openNode(parent, false);
}
}
}

SimpleWidget.register(JqTreeWidget, "tree");
15 changes: 8 additions & 7 deletions tree.jquery.debug.js

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

2 changes: 1 addition & 1 deletion tree.jquery.debug.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tree.jquery.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tree.jquery.js.map

Large diffs are not rendered by default.