Skip to content

Commit 3c4b11d

Browse files
committed
refactor the method getHierarchy()
1 parent 777df14 commit 3c4b11d

File tree

7 files changed

+71
-73
lines changed

7 files changed

+71
-73
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "orgchart",
3-
"version": "2.0.13",
3+
"version": "2.0.14",
44
"homepage": "https://github.com/dabeng/OrgChart",
55
"authors": [
66
"dabeng <[email protected]>"

demo/get-hierarchy.html

+7-7
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@
6161
</head>
6262
<body>
6363
<ul id="ul-data" style="display: none;">
64-
<li id="1">Lao Lao
64+
<li data-id="n1">Lao Lao
6565
<ul>
66-
<li id="2">Bo Miao</li>
67-
<li id="3">Su Miao
66+
<li data-id="n2">Bo Miao</li>
67+
<li data-id="n3">Su Miao
6868
<ul>
69-
<li id="4">Tie Hua</li>
70-
<li id="5">Hei Hei
69+
<li data-id="n4">Tie Hua</li>
70+
<li data-id="n5">Hei Hei
7171
<ul>
72-
<li id="6">Pang Pang</li>
73-
<li id="7">Xiang Xiang</li>
72+
<li data-id="n6">Pang Pang</li>
73+
<li data-id="n7">Xiang Xiang</li>
7474
</ul>
7575
</li>
7676
</ul>

demo/js/jquery.orgchart.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/jquery.orgchart.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "orgchart",
3-
"version": "2.0.13",
3+
"version": "2.0.14",
44
"description": "Simple and direct organization chart(tree-like hierarchy) plugin based on pure DOM and jQuery.",
55
"main": "./dist/js/jquery.orgchart.min.js",
66
"style": [

src/js/jquery.orgchart.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@
344344
'name': $li.contents().eq(0).text().trim(),
345345
'relationship': ($li.parent().parent().is('li') ? '1': '0') + ($li.siblings('li').length ? 1: 0) + ($li.children('ul').length ? 1 : 0)
346346
};
347-
if ($li[0].id) {
348-
subObj.id = $li[0].id;
347+
if ($li.attr('data-id')) {
348+
subObj.id = $li.attr('data-id');
349349
}
350350
$li.children('ul').children().each(function() {
351351
if (!subObj.children) { subObj.children = []; }
@@ -383,8 +383,15 @@
383383
if (!this.$chart.find('.node').length) {
384384
return 'Error: nodes do not exist'
385385
} else {
386-
if (!this.$chart.find('.node')[0].id) {
387-
return 'Error: Nodes of orghcart to be exported must have id attribute!';
386+
var valid = true;
387+
this.$chart.find('.node').each(function () {
388+
if (!this.id) {
389+
valid = false;
390+
return false;
391+
}
392+
});
393+
if (!valid) {
394+
return 'Error: All nodes of orghcart to be exported must have data-id attribute!';
388395
}
389396
}
390397
}

test/unit-tests.js

+47-56
Original file line numberDiff line numberDiff line change
@@ -15,84 +15,71 @@ describe('orgchart -- unit tests', function () {
1515

1616
var $container = $('#chart-container'),
1717
ds = {
18-
'id': '1',
18+
'id': 'n1',
1919
'name': 'Lao Lao',
2020
'title': 'general manager',
2121
'children': [
22-
{ 'id': '2', 'name': 'Bo Miao', 'title': 'department manager' },
23-
{ 'id': '3', 'name': 'Su Miao', 'title': 'department manager',
22+
{ 'id': 'n2', 'name': 'Bo Miao', 'title': 'department manager' },
23+
{ 'id': 'n3', 'name': 'Su Miao', 'title': 'department manager',
2424
'children': [
25-
{ 'id': '6', 'name': 'Tie Hua', 'title': 'senior engineer' },
26-
{ 'id': '7', 'name': 'Hei Hei', 'title': 'senior engineer',
25+
{ 'id': 'n5', 'name': 'Tie Hua', 'title': 'senior engineer' },
26+
{ 'id': 'n6', 'name': 'Hei Hei', 'title': 'senior engineer',
2727
'children': [
28-
{ 'id': '9', 'name': 'Dan Dan', 'title': 'engineer',
29-
'children' : [
30-
{ 'id': '10', 'name': 'Er Dan', 'title': 'intern' }
31-
]
32-
}
28+
{ 'id': 'n8', 'name': 'Dan Dan', 'title': 'engineer' }
3329
]
3430
},
35-
{ 'id': '8', 'name': 'Pang Pang', 'title': 'senior engineer' }
31+
{ 'id': 'n7', 'name': 'Pang Pang', 'title': 'senior engineer' }
3632
]
3733
},
38-
{ 'id': '4', 'name': 'Hong Miao', 'title': 'department manager' },
39-
{ 'id': '5', 'name': 'Chun Miao', 'title': 'department manager' }
34+
{ 'id': 'n4', 'name': 'Hong Miao', 'title': 'department manager' }
4035
]
4136
},
4237
oc = {},
4338
hierarchy = {
44-
id: '1',
39+
id: 'n1',
4540
children: [
46-
{ id: '2' },
47-
{ id: '3',
41+
{ id: 'n2' },
42+
{ id: 'n3',
4843
children: [
49-
{ id: '6' },
50-
{ id: '7',
44+
{ id: 'n5' },
45+
{ id: 'n6',
5146
children: [
52-
{ id: '9',
53-
children: [
54-
{ id: '10' }
55-
]
56-
}
47+
{ id: 'n8' }
5748
]
5849
},
59-
{ id: '8' }
50+
{ id: 'n7' }
6051
]
6152
},
62-
{ id: '4' },
63-
{ id: '5' }
53+
{ id: 'n4' }
6454
]
6555
},
66-
$root,
56+
$laolao,
6757
$bomiao,
6858
$sumiao,
6959
$hongmiao,
7060
$chunmiao,
7161
$tiehua,
7262
$heihei,
7363
$pangpang,
74-
$dandan,
75-
$erdan;
64+
$dandan;
7665

7766
beforeEach(function () {
7867
oc = $('#chart-container').orgchart({
7968
'data': ds,
8069
'nodeContent': 'title'
8170
}),
82-
$root = $('#1'),
83-
$bomiao = $('#2'),
84-
$sumiao = $('#3'),
85-
$hongmiao = $('#4'),
86-
$chunmiao = $('#5'),
87-
$tiehua = $('#6'),
88-
$heihei = $('#7'),
89-
$pangpang = $('#8'),
90-
$dandan = $('#9'),
91-
$erdan = $('#10');
71+
$laolao = $('#n1'),
72+
$bomiao = $('#n2'),
73+
$sumiao = $('#n3'),
74+
$hongmiao = $('#n4'),
75+
$tiehua = $('#n5'),
76+
$heihei = $('#n6'),
77+
$pangpang = $('#n7'),
78+
$dandan = $('#n8');
9279
});
9380

9481
afterEach(function () {
95-
$root = $bomiao = $sumiao = $hongmiao = $chunmiao = $tiehua = $heihei = $pangpang = $dandan = $erdan = null;
82+
$laolao = $bomiao = $sumiao = $hongmiao = $tiehua = $heihei = $pangpang = $dandan = null;
9683
$container.empty();
9784
});
9885

@@ -119,9 +106,9 @@ describe('orgchart -- unit tests', function () {
119106

120107
it('getNodeState() works well', function () {
121108
oc.init({ 'depth': 2 }).$chart.on('init.orgchart', function () {
122-
oc.getNodeState($root, 'parent').should.deep.equal({ 'exist': false, 'visible': false });
123-
oc.getNodeState($root, 'children').should.deep.equal({ 'exist': true, 'visible': true });
124-
oc.getNodeState($root, 'siblings').should.deep.equal({ 'exist': false, 'visible': false });
109+
oc.getNodeState($laolao, 'parent').should.deep.equal({ 'exist': false, 'visible': false });
110+
oc.getNodeState($laolao, 'children').should.deep.equal({ 'exist': true, 'visible': true });
111+
oc.getNodeState($laolao, 'siblings').should.deep.equal({ 'exist': false, 'visible': false });
125112

126113
oc.getNodeState($bomiao, 'parent').should.deep.equal({ 'exist': true, 'visible': true });
127114
oc.getNodeState($bomiao, 'children').should.deep.equal({ 'exist': false, 'visible': false });
@@ -154,32 +141,32 @@ describe('orgchart -- unit tests', function () {
154141
oc.getRelatedNodes($('td:first'), 'children').should.deep.equal($());
155142
oc.getRelatedNodes($('.node:first'), 'child').should.deep.equal($());
156143

157-
oc.getRelatedNodes($root, 'parent').should.deep.equal($());
158-
oc.getRelatedNodes($root, 'children').toArray().should.members([$bomiao[0], $sumiao[0], $hongmiao[0], $chunmiao[0]]);
159-
oc.getRelatedNodes($root, 'siblings').should.deep.equal($());
144+
oc.getRelatedNodes($laolao, 'parent').should.deep.equal($());
145+
oc.getRelatedNodes($laolao, 'children').toArray().should.members([$bomiao[0], $sumiao[0], $hongmiao[0]]);
146+
oc.getRelatedNodes($laolao, 'siblings').should.deep.equal($());
160147

161-
oc.getRelatedNodes($bomiao, 'parent').should.deep.equal($root);
148+
oc.getRelatedNodes($bomiao, 'parent').should.deep.equal($laolao);
162149
oc.getRelatedNodes($bomiao, 'children').should.have.lengthOf(0);
163-
oc.getRelatedNodes($bomiao, 'siblings').toArray().should.members([$sumiao[0], $hongmiao[0], $chunmiao[0]]);
150+
oc.getRelatedNodes($bomiao, 'siblings').toArray().should.members([$sumiao[0], $hongmiao[0]]);
164151
});
165152

166153
it('hideParent() works well', function () {
167154
var spy = sinon.spy(oc, 'hideSiblings');
168155
oc.hideParent($heihei);
169156
spy.should.have.been.callCount(2);
170157
oc.hideParentEnd({ 'target': $sumiao[0], 'data': { 'upperLevel': $heihei.closest('.nodes').siblings() } });
171-
oc.hideParentEnd({ 'target': $root[0], 'data': { 'upperLevel': $sumiao.closest('.nodes').siblings() } });
158+
oc.hideParentEnd({ 'target': $laolao[0], 'data': { 'upperLevel': $sumiao.closest('.nodes').siblings() } });
172159

173160
$heihei.parents('.nodes').each(function () {
174161
$(this).siblings().filter('.hidden').should.lengthOf(3);
175162
});
176163
$sumiao.is('.slide-down').should.be.true;
177-
$root.is('.slide-down').should.be.true;
164+
$laolao.is('.slide-down').should.be.true;
178165
});
179166

180167
it('showParent() works well', function () {
181168
var spy = sinon.spy(oc, 'repaint');
182-
$root.add($sumiao).closest('tr').nextUntil('.nodes').addBack().addClass('hidden');
169+
$laolao.add($sumiao).closest('tr').nextUntil('.nodes').addBack().addClass('hidden');
183170
oc.showParent($heihei);
184171
spy.should.have.been.called;
185172
var $upperLevel = $heihei.closest('.nodes').siblings();
@@ -196,13 +183,17 @@ describe('orgchart -- unit tests', function () {
196183
});
197184

198185
it('hideChildren() works well', function () {
199-
oc.hideChildren($heihei);
200-
$heihei.closest('table').find('.lines').filter('[style*="visibility: hidden"]').should.lengthOf(4);
186+
oc.hideChildren($sumiao);
187+
$sumiao.closest('table').find('.lines').filter('[style*="visibility: hidden"]').should.lengthOf(4);
188+
$tiehua.is('.sliding,.slide-up').should.be.true;
189+
$heihei.is('.sliding,.slide-up').should.be.true;
190+
$pangpang.is('.sliding,.slide-up').should.be.true;
201191
$dandan.is('.sliding,.slide-up').should.be.true;
202-
$erdan.is('.sliding,.slide-up').should.be.true;
203-
oc.hideChildrenEnd({ 'data': { 'visibleNodes': $dandan.add($erdan), 'lowerLevel': $heihei.closest('tr').siblings(), 'isVerticalDesc': false, 'node': $heihei } });
192+
oc.hideChildrenEnd({ 'data': { 'visibleNodes': $([$tiehua[0], $heihei[0], $pangpang[0], $dandan[0]]), 'lowerLevel': $sumiao.closest('tr').siblings(), 'isVerticalDesc': false, 'node': $sumiao } });
193+
$tiehua.is('.sliding').should.be.false;
194+
$heihei.is('.sliding').should.be.false;
195+
$pangpang.is('.sliding').should.be.false;
204196
$dandan.is('.sliding').should.be.false;
205-
$erdan.is('.sliding').should.be.false;
206197
});
207198

208199
});

0 commit comments

Comments
 (0)