-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodes.js
58 lines (50 loc) · 1.24 KB
/
nodes.js
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
import * as _ from "lamb";
import * as d3 from "../vendor/d3";
import {mapValuesToFloatPossibly} from "@svizzle/utils";
/* get */
/**
* Return an object derived from getComputedStyle(domNode) with values converted to numbers.
* It extracts values for `width`, `height` and all keys in `additionalProps`.
*
* @function
* @arg {object} DOM element
* @arg {array} additionalProps
* @return {object}
*
* @example
getElementGeometry(node)
// {width: 200, height: 100}
getElementGeometry(node, ["font-size"])
// {width: 200, height: 100, "font-size": 12}
*
* @version 0.0.1
*/
export const getElementGeometry = (elem, additionalProps = []) =>
mapValuesToFloatPossibly(
_.pick(getComputedStyle(elem), [
"width",
"height",
...additionalProps
])
);
/* edit */
/**
* Moves a node in the DOM.
*
* @function
* @arg {object} node - DOM element
* @arg {object} newContainer - DOM element
*
* @example
d3.selectAll("container1 .toBeMoved")
.each(function(d, i) {
moveNode(this, "container2");
});
*
* @version 0.0.1
*/
export const moveNode = (node, newContainer) =>
d3.select(newContainer).append(
() => d3.select(node).remove().node()
);
// TODO test with jsdom