Skip to content

Commit 497df0e

Browse files
committed
add: Treant#destroy method
1 parent 2beaea8 commit 497df0e

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

Treant.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
.Treant { position: relative; overflow: hidden; padding: 0 !important; }
44
.Treant > .node,
55
.Treant > .pseudo { position: absolute; display: block; visibility: hidden; }
6-
.Treant.loaded .node,
7-
.Treant.loaded .pseudo { visibility: visible; }
6+
.Treant.Treant-loaded .node,
7+
.Treant.Treant-loaded .pseudo { visibility: visible; }
88
.Treant > .pseudo { width: 0; height: 0; border: none; padding: 0; }
99
.Treant .collapse-switch { width: 3px; height: 3px; display: block; border: 1px solid black; position: absolute; top: 1px; right: 1px; cursor: pointer; }
1010
.Treant .collapsed .collapse-switch { background-color: #868DEE; }

Treant.js

+35-5
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,29 @@
125125
return this.store[this.store.length - 1]; // return newly created tree
126126
},
127127
get: function (treeId) {
128-
return this.store[ treeId ];
128+
return this.store[treeId];
129+
},
130+
destroy: function(tree_id){
131+
var tree = this.get(tree_id);
132+
if (tree) {
133+
tree._R.remove();
134+
var draw_area = tree.drawArea;
135+
while(draw_area.firstChild) {
136+
draw_area.removeChild(draw_area.firstChild);
137+
}
138+
var classes = draw_area.className.split(' '),
139+
classes_to_stay = [];
140+
for (var i = 0; i < classes.length; i++) {
141+
var cls = classes[i];
142+
if (cls != 'Treant' && cls != 'Treant-loaded') {
143+
classes_to_stay.push(cls);
144+
}
145+
};
146+
draw_area.style.overflowY = '';
147+
draw_area.style.overflowX = '';
148+
draw_area.className = classes_to_stay.join(' ');
149+
this.store[tree_id] = null;
150+
}
129151
}
130152
};
131153

@@ -170,7 +192,7 @@
170192
}
171193

172194
if(!this.loaded) {
173-
this.drawArea.className += " loaded"; // nodes are hidden until .loaded class is add
195+
this.drawArea.className += " Treant-loaded"; // nodes are hidden until .loaded class is add
174196
if (Object.prototype.toString.call(callback) === "[object Function]") { callback(self); }
175197
this.loaded = true;
176198
}
@@ -466,7 +488,7 @@
466488
if(this._R) {
467489
this._R.setSize(viewWidth, viewHeight);
468490
} else {
469-
this._R = this._R || Raphael(this.drawArea, viewWidth, viewHeight);
491+
this._R = Raphael(this.drawArea, viewWidth, viewHeight);
470492
}
471493

472494

@@ -1233,7 +1255,7 @@
12331255
// Makes a JSON chart config out of Array config
12341256
// #############################################
12351257

1236-
var JSOnconfig = {
1258+
var JSONconfig = {
12371259
make: function( configArray ) {
12381260

12391261
var i = configArray.length, node;
@@ -1318,10 +1340,18 @@
13181340
*/
13191341
var Treant = function(jsonConfig, callback) {
13201342

1321-
if (jsonConfig instanceof Array) jsonConfig = JSOnconfig.make(jsonConfig);
1343+
if (jsonConfig instanceof Array) {
1344+
jsonConfig = JSONconfig.make(jsonConfig);
1345+
}
13221346

13231347
var newTree = TreeStore.createTree(jsonConfig);
13241348
newTree.positionTree(callback);
1349+
1350+
this.tree_id = newTree.id;
1351+
};
1352+
1353+
Treant.prototype.destroy = function() {
1354+
TreeStore.destroy(this.tree_id);
13251355
};
13261356

13271357
/* expose constructor globaly */

examples/basic-example/basic-example.js

-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ var config = {
171171
image: "../headshots/5.jpg",
172172
children: [
173173
{
174-
parent: cbo,
175174
text:{
176175
name: "Alice Lopez",
177176
title: "Chief Communications Officer"

examples/test_ground/index.html

+13-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<link rel="stylesheet" href="../../Treant.css">
99
<link rel="stylesheet" href="index.css">
1010

11-
<link rel="stylesheet" href="../../perfect-scrollbar/perfect-scrollbar.css">
11+
<link rel="stylesheet" href="../../vendor/perfect-scrollbar/perfect-scrollbar.css">
1212

1313
</head>
1414
<body>
@@ -20,9 +20,9 @@
2020
<div class="chart" id="OrganiseChart4"></div>
2121
<div class="chart" id="OrganiseChart5"></div>
2222

23-
<script src="../../jquery.min.js"></script>
24-
<script src="../../perfect-scrollbar/jquery.mousewheel.js"></script>
25-
<script src="../../perfect-scrollbar/perfect-scrollbar.js"></script>
23+
<script src="../../vendor/jquery.min.js"></script>
24+
<script src="../../vendor/jquery.mousewheel.js"></script>
25+
<script src="../../vendor/perfect-scrollbar/perfect-scrollbar.js"></script>
2626

2727
<script src="../../vendor/raphael.js"></script>
2828

@@ -32,7 +32,7 @@
3232
<script>
3333

3434
var UTIL = {
35-
inheritAttrs: function(me, from) { // poanta je da nodeovi i tree lako nasljeđuju iz jsonConfig objekta
35+
inheritAttrs: function(me, from) {
3636

3737
for (var attr in from) {
3838
if(typeof from[attr] !== 'function') {
@@ -48,8 +48,8 @@
4848
},
4949

5050
createMerge: function(obj1, obj2) {
51-
var newObj = {}; // obj1 se mora klonirat jer je to uvijek glavni config od Tree
52-
if(obj1) this.inheritAttrs( newObj, this.clone(obj1) ); // obj1 i obj2 mogu uletit kao undefined ako user nije zadao u configu
51+
var newObj = {};
52+
if(obj1) this.inheritAttrs( newObj, this.clone(obj1) );
5353
if(obj2) this.inheritAttrs( newObj, obj2 );
5454
return newObj;
5555
},
@@ -66,10 +66,8 @@
6666
}
6767
};
6868

69-
// ###########################################
70-
// INICIJALIZACIJA
71-
// ###########################################
72-
69+
var T1 = new Treant(example1);
70+
7371
var example2 = UTIL.createMerge(
7472
example1,
7573
{
@@ -88,6 +86,7 @@
8886
}
8987
}
9088
);
89+
var T2 = new Treant(example2);
9190

9291
var example3 = UTIL.createMerge(
9392
example1,
@@ -107,6 +106,7 @@
107106
}
108107
}
109108
);
109+
var T3 = new Treant(example3);
110110

111111
var example4 = UTIL.createMerge(
112112
example1,
@@ -126,12 +126,9 @@
126126
}
127127
}
128128
);
129+
var T4 = new Treant(example4);
129130

130-
new Treant( example1 );
131-
new Treant( example2 );
132-
new Treant( example3 );
133-
new Treant( example4 );
134-
new Treant( ALTERNATIVE );
131+
var T5 = new Treant(ALTERNATIVE);
135132

136133
</script>
137134

vendor/raphael.js

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

0 commit comments

Comments
 (0)