-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbranch.js.html
267 lines (230 loc) · 8.64 KB
/
branch.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: branch.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: branch.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import {curveStepBefore, line} from "d3-shape";
import {mergeDeep} from "../utilities";
import {Bauble} from "./bauble";
import {mouse, select} from "d3"
import uuid from "uuid"
import p from "../privateConstants";
import {BaubleManager} from "../features/baubleManager";
/** @module bauble */
export class Branch extends Bauble {
static DEFAULT_SETTINGS() {
return {
curveRadius: 0,
attrs: {"fill":"none","stroke":"black"},
}
}
constructor() {
super();
this._curve=curveStepBefore;
this._curveRadius = 0;
this._attrs= {"fill":"none","stroke":"black"};
}
update(selection) {
this.branchPath =this.branchPathGenerator();
if(selection==null&&!this.selection){
return
}
if(selection){
this.selection=selection;
}
const self=this;
return selection
.selectAll(`.${this.id}`)
.data(d => [d].filter(this.filter()),d=>this.id)
.join(
enter => enter
.append("path")
.attr("d", (v1,i) => this.branchPath(v1,i))
.attr("class", `branch-path ${this.id}`)
.attrs(this._attrs)
.each((d,i,n)=>{
const element = select(n[i]);
for( const [key,func] of Object.entries(this._interactions)){
element.on(key,(d,i,n)=>func(d,i,n))
}
}),
update => update
.call(update => update.transition(d=>`u${uuid.v4()}`)
.duration(this.transitions().transitionDuration)
.ease(this.transitions().transitionEase)
.attr("d", (edge,i) => this.branchPath(edge,i))
.attrs(this._attrs)
.each((d,i,n)=>{
const element = select(n[i]);
for( const [key,func] of Object.entries(this._interactions)){
element.on(key,(d,i,n)=>func(d,i,n))
}
})
)
)
};
branchPathGenerator() {
const branchPath = (e, i) => {
const branchLine = line()
.x((v) => v.x)
.y((v) => v.y)
.curve(this._curve);
const factor = e.v0.y - e.v1.y > 0 ? 1 : -1;
const dontNeedCurve = e.v0.y - e.v1.y === 0 ? 0 : 1;
const output = this._curveRadius > 0 ?
branchLine(
[{x: 0, y: this.scales().y(e.v0.y) - this.scales().y(e.v1.y)},
{x: 0, y: dontNeedCurve * factor * this._curveRadius},
{x: 0 + dontNeedCurve * this._curveRadius, y: 0},
{x: this.scales().x(e.v1.x) - this.scales().x(e.v0.x), y: 0}
]) :
branchLine(
[{x: 0, y: this.scales().y(e.v0.y) - this.scales().y(e.v1.y)},
{x: this.scales().x(e.v1.x ) - this.scales().x(e.v0.x), y: 0}
]);
return (output)
};
return branchPath;
}
curve(curve=null){
if(curve){
this._curve = curve;
return this;
}else{
return this._curve;
}
}
curveRadius(curveRadius=null){
if(curveRadius){
this._curveRadius = curveRadius;
return this;
}else{
return this._curveRadius;
}
}
hilightOnHover(strokeWidth = null) {
let oldStrokeWidth;
super.on("mouseenter",
(d, i,n) => {
if(strokeWidth) {
if (!this._attrs["stroke-width"]) {
this._attrs["stroke-width"] = this._attrs["stroke-width"];
}
oldStrokeWidth=this._attrs["stroke-width"];
this.attr("r", strokeWidth);
}
const parent = select(n[i]).node().parentNode;
this.update(select(parent));
select(parent).classed("hovered",true)
.raise();
if(strokeWidth){
this.attr("r", oldStrokeWidth);
}
});
super.on("mouseleave",
(d,i,n) => {
const parent = select(n[i]).node().parentNode;
select(parent).classed("hovered",false);
this.update(select(parent));
});
return this;
}
/**
* Helper function to reroot tree at the clicked position.
* @param distance - how should the distance along the branch be calculated (x - just x distance, euclidean - euclidean)
* @return {Branch}
*/
reRootOnClick(distance ="x"){
super.on("click",
(d,i,n)=>{
const x1 = this.manager().figure().scales.x(d.v1.x),
x2 = this.manager().figure().scales.x(d.v0.x),
y1=this.manager().figure().scales.y(d.v1.y),
y2=this.manager().figure().scales.y(d.v0.y),
[mx,my] = mouse(document.getElementById(this.manager().figure().svgId));
const proportion = distance.toLocaleLowerCase()==="x"? Math.abs( (mx - x2) / (x1 - x2)):distance.toLocaleLowerCase()==="euclidean"?
Math.sqrt(Math.pow(mx-x2,2)+Math.pow(my-y2,2))/Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)):
null;
if(!proportion){
throw new Error(`Error in reroot on click distance ${distance} is not recognize. Please use x or euclidean`)
}
const tree = this.manager().figure().tree();
tree.reroot(tree.getNode(d.id),proportion)
});
return this;
}
}
/**
* Generates a line() function that takes an edge and it's index and returns a line for d3 path element. It is called
* by the figtree class as
* const branchPath = this.layout.branchPathGenerator(this.scales())
* newBranches.append("path")
.attr("class", "branch-path")
.attr("d", (e,i) => branchPath(e,i));
* @param scales
* @param branchCurve
* @return {function(*, *)}
*/
export function branchPathGenerator({scales,curveRadius,curve}) {
const branchPath = (e, i) => {
const branchLine = line()
.x((v) => v.x)
.y((v) => v.y)
.curve(curve);
const factor = e.v0.y - e.v1.y > 0 ? 1 : -1;
const dontNeedCurve = e.v0.y - e.v1.y === 0 ? 0 : 1;
const output = curveRadius > 0 ?
branchLine(
[{x: 0, y: scales.y(e.v0.y) - scales.y(e.v1.y)},
{x: 0, y: dontNeedCurve * factor * curveRadius},
{x: 0 + dontNeedCurve * curveRadius, y: 0},
{x: scales.x(e.v1.x + scales.xOffset) - scales.x(e.v0.x + scales.xOffset), y: 0}
]) :
branchLine(
[{x: 0, y: scales.y(e.v0.y) - scales.y(e.v1.y)},
{x: scales.x(e.v1.x + scales.xOffset) - scales.x(e.v0.x + scales.xOffset), y: 0}
]);
return (output)
};
return branchPath;
}
/**
* branches is a helper function
* @returns {BaubleManager|*}
*/
export function branches(){
return new BaubleManager()
.class("branch")
.data(d=>d["edges"])
.layer("branches-layer")
}
export function branch(){
return new Branch();
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bauble.html">bauble</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Wed Mar 18 2020 15:02:06 GMT+0000 (Greenwich Mean Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>