Skip to content

Commit b46b5c7

Browse files
authored
Merge pull request #483 from ben12/bugfix/479-getNodeState-vertical-nodes
fix #479 getNodeState with vertical nodes
2 parents 9c55ee1 + 95d147b commit b46b5c7

File tree

3 files changed

+79
-37
lines changed

3 files changed

+79
-37
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: node_js
2+
23
node_js: "8"
34

45
before_install:

src/js/jquery.orgchart.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,27 +400,38 @@
400400
// detect the exist/display state of related node
401401
getNodeState: function ($node, relation) {
402402
var $target = {};
403+
var isVerticalNode = $node.parent().is('li');
403404
var relation = relation || 'self';
404405
if (relation === 'parent') {
405-
$target = $node.closest('.nodes').siblings(':first');
406+
if (isVerticalNode) {
407+
$target = $node.closest('ul').parents('ul');
408+
if (!$target.length) {
409+
$target = $node.closest('.nodes');
410+
if (!$target.length) {
411+
$target = $node.closest('.verticalNodes').siblings(':first');
412+
}
413+
}
414+
} else {
415+
$target = $node.closest('.nodes').siblings(':first');
416+
}
406417
if ($target.length) {
407-
if ($target.is('.hidden') || (!$target.is('.hidden') && $target.closest('.nodes').is('.hidden'))) {
418+
if ($target.is('.hidden') || (!$target.is('.hidden') && $target.closest('.nodes').is('.hidden')) || (!$target.is('.hidden') && $target.closest('.verticalNodes').is('.hidden'))) {
408419
return { 'exist': true, 'visible': false };
409420
}
410421
return { 'exist': true, 'visible': true };
411422
}
412423
} else if (relation === 'children') {
413-
$target = $node.closest('tr').siblings(':last');
424+
$target = isVerticalNode ? $node.parent().children('ul') : $node.closest('tr').siblings(':last');
414425
if ($target.length) {
415426
if (!$target.is('.hidden')) {
416427
return { 'exist': true, 'visible': true };
417428
}
418429
return { 'exist': true, 'visible': false };
419430
}
420431
} else if (relation === 'siblings') {
421-
$target = $node.closest('table').parent().siblings();
422-
if ($target.length) {
423-
if (!$target.is('.hidden') && !$target.parent().is('.hidden')) {
432+
$target = isVerticalNode ? $node.closest('ul') : $node.closest('table').parent().siblings();
433+
if ($target.length && (!isVerticalNode || $target.children('li').length > 1)) {
434+
if (!$target.is('.hidden') && !$target.parent().is('.hidden') && (!isVerticalNode || !$target.closest('.verticalNodes').is('.hidden'))) {
424435
return { 'exist': true, 'visible': true };
425436
}
426437
return { 'exist': true, 'visible': false };
@@ -430,7 +441,7 @@
430441
if ($target.length) {
431442
if (!(($target.closest('.nodes').length && $target.closest('.nodes').is('.hidden')) ||
432443
($target.closest('table').parent().length && $target.closest('table').parent().is('.hidden')) ||
433-
($target.parent().is('li') && ($target.closest('ul').is('.hidden') || $target.closest('verticalNodes').is('.hidden')))
444+
($target.parent().is('li') && ($target.closest('ul').is('.hidden') || $target.closest('.verticalNodes').is('.hidden')))
434445
)) {
435446
return { 'exist': true, 'visible': true };
436447
}

test/unit/tests.js

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -120,36 +120,66 @@ describe('orgchart -- unit tests', function () {
120120
oc2.getHierarchy().should.include('Error');
121121
});
122122

123-
it('getNodeState()', function () {
124-
oc.init({ 'depth': 2 }).$chart.on('init.orgchart', function () {
125-
oc.getNodeState($laolao, 'parent').should.deep.equal({ 'exist': false, 'visible': false });
126-
oc.getNodeState($laolao, 'children').should.deep.equal({ 'exist': true, 'visible': true });
127-
oc.getNodeState($laolao, 'siblings').should.deep.equal({ 'exist': false, 'visible': false });
128-
129-
oc.getNodeState($bomiao, 'parent').should.deep.equal({ 'exist': true, 'visible': true });
130-
oc.getNodeState($bomiao, 'children').should.deep.equal({ 'exist': false, 'visible': false });
131-
oc.getNodeState($bomiao, 'siblings').should.deep.equal({ 'exist': true, 'visible': true });
132-
133-
oc.getNodeState($sumiao, 'parent').should.deep.equal({ 'exist': true, 'visible': true });
134-
oc.getNodeState($sumiao, 'children').should.deep.equal({ 'exist': true, 'visible': false });
135-
oc.getNodeState($sumiao, 'siblings').should.deep.equal({ 'exist': true, 'visible': true });
136-
137-
oc.getNodeState($tiehua, 'parent').should.deep.equal({ 'exist': true, 'visible': true });
138-
oc.getNodeState($tiehua, 'children').should.deep.equal({ 'exist': false, 'visible': false });
139-
oc.getNodeState($tiehua, 'siblings').should.deep.equal({ 'exist': true, 'visible': false });
140-
141-
oc.getNodeState($heihei, 'parent').should.deep.equal({ 'exist': true, 'visible': true });
142-
oc.getNodeState($heihei, 'children').should.deep.equal({ 'exist': true, 'visible': false });
143-
oc.getNodeState($heihei, 'siblings').should.deep.equal({ 'exist': true, 'visible': false });
144-
145-
oc.getNodeState($pangpang, 'parent').should.deep.equal({ 'exist': true, 'visible': false });
146-
oc.getNodeState($pangpang, 'children').should.deep.equal({ 'exist': true, 'visible': false });
147-
oc.getNodeState($pangpang, 'siblings').should.deep.equal({ 'exist': false, 'visible': false });
148-
149-
oc.getNodeState($dandan, 'parent').should.deep.equal({ 'exist': true, 'visible': false });
150-
oc.getNodeState($dandan, 'children').should.deep.equal({ 'exist': false, 'visible': false });
151-
oc.getNodeState($dandan, 'siblings').should.deep.equal({ 'exist': false, 'visible': false });
152-
});
123+
it('getNodeState()', function (done) {
124+
var check = function () {
125+
try {
126+
$laolao = $('#n1');
127+
$bomiao = $('#n2');
128+
$sumiao = $('#n3');
129+
$hongmiao = $('#n4');
130+
$tiehua = $('#n5');
131+
$heihei = $('#n6');
132+
$pangpang = $('#n7');
133+
$dandan = $('#n8');
134+
$erdan = $('#n9');
135+
$sandan = $('#n10');
136+
137+
oc.getNodeState($laolao).should.deep.equal({ 'exist': true, 'visible': true }, "laolao->self");
138+
oc.getNodeState($laolao, 'parent').should.deep.equal({ 'exist': false, 'visible': false }, "laolao->parent");
139+
oc.getNodeState($laolao, 'children').should.deep.equal({ 'exist': true, 'visible': true }, "laolao->children");
140+
oc.getNodeState($laolao, 'siblings').should.deep.equal({ 'exist': false, 'visible': false }, "laolao->siblings");
141+
142+
oc.getNodeState($bomiao).should.deep.equal({ 'exist': true, 'visible': true }, "bomiao->self");
143+
oc.getNodeState($bomiao, 'parent').should.deep.equal({ 'exist': true, 'visible': true }, "bomiao->parent");
144+
oc.getNodeState($bomiao, 'children').should.deep.equal({ 'exist': false, 'visible': false }, "bomiao->children");
145+
oc.getNodeState($bomiao, 'siblings').should.deep.equal({ 'exist': true, 'visible': true }, "bomiao->siblings");
146+
147+
oc.getNodeState($sumiao).should.deep.equal({ 'exist': true, 'visible': true }, "sumiao->self");
148+
oc.getNodeState($sumiao, 'parent').should.deep.equal({ 'exist': true, 'visible': true }, "sumiao->parent");
149+
oc.getNodeState($sumiao, 'children').should.deep.equal({ 'exist': true, 'visible': false }, "sumiao->children");
150+
oc.getNodeState($sumiao, 'siblings').should.deep.equal({ 'exist': true, 'visible': true }, "sumiao->siblings");
151+
152+
oc.getNodeState($tiehua).should.deep.equal({ 'exist': true, 'visible': false }, "tiehua->self");
153+
oc.getNodeState($tiehua, 'parent').should.deep.equal({ 'exist': true, 'visible': true }, "tiehua->parent");
154+
oc.getNodeState($tiehua, 'children').should.deep.equal({ 'exist': true, 'visible': false }, "tiehua->children");
155+
oc.getNodeState($tiehua, 'siblings').should.deep.equal({ 'exist': true, 'visible': false }, "tiehua->siblings");
156+
157+
oc.getNodeState($heihei).should.deep.equal({ 'exist': true, 'visible': false }, "heihei->self");
158+
oc.getNodeState($heihei, 'parent').should.deep.equal({ 'exist': true, 'visible': true }, "heihei->parent");
159+
oc.getNodeState($heihei, 'children').should.deep.equal({ 'exist': true, 'visible': false }, "heihei->children");
160+
oc.getNodeState($heihei, 'siblings').should.deep.equal({ 'exist': true, 'visible': false }, "heihei->siblings");
161+
162+
oc.getNodeState($pangpang).should.deep.equal({ 'exist': true, 'visible': false }, "pangpang->self");
163+
oc.getNodeState($pangpang, 'parent').should.deep.equal({ 'exist': true, 'visible': true }, "pangpang->parent");
164+
oc.getNodeState($pangpang, 'children').should.deep.equal({ 'exist': true, 'visible': false }, "pangpang->children");
165+
oc.getNodeState($pangpang, 'siblings').should.deep.equal({ 'exist': true, 'visible': false }, "pangpang->siblings");
166+
167+
oc.getNodeState($dandan).should.deep.equal({ 'exist': true, 'visible': false }, "dandan->self");
168+
oc.getNodeState($dandan, 'parent').should.deep.equal({ 'exist': true, 'visible': false }, "dandan->parent");
169+
oc.getNodeState($dandan, 'children').should.deep.equal({ 'exist': false, 'visible': false }, "dandan->children");
170+
oc.getNodeState($dandan, 'siblings').should.deep.equal({ 'exist': false, 'visible': false }, "dandan->siblings");
171+
172+
done();
173+
} catch(err) {
174+
done(err);
175+
}
176+
};
177+
if (typeof MutationObserver !== 'undefined') {
178+
oc.init({ 'visibleLevel': 2, 'verticalLevel': 3 }).$chart.on('init.orgchart', check);
179+
} else {
180+
oc.init({ 'visibleLevel': 2, 'verticalLevel': 3 });
181+
setTimeout(check, 500);
182+
}
153183
});
154184

155185
it('getRelatedNodes()', function () {

0 commit comments

Comments
 (0)