-
Notifications
You must be signed in to change notification settings - Fork 24
/
d3.flexdivs.js
64 lines (61 loc) · 1.81 KB
/
d3.flexdivs.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
59
60
61
62
63
64
function flex_div_helper_mapper(map) {
return function flex_div_helper(data) {
var me = d3.select(this);
me.style({
flex: function(d) {
if(d.flex)
return d.flex;
var pdat = d3.select(this.parentNode).datum();
return pdat && pdat.deflex || null;
},
display: function(d) {
return d.direction ? 'flex' : null;
},
'flex-direction': function(d) {
return d.direction || null;
}
});
var divs = me.selectAll(function() {
return this.childNodes;
}).data(data.divs || []);
divs.enter().append('div');
divs.exit().remove();
divs.attr({
class: function(d) {
return d.class || null;
},
id: function(d) {
return d.bring ? 'wrap-' + d.id : d.id;
}
});
divs.each(flex_div_helper);
divs.filter(function(d) { return d.bring && !map[d.id]; })
.append('div')
.attr('id', function(d) {
return d.id;
});
};
}
function bringover(data, map) {
if(data.id && data.bring) {
var e = document.getElementById(data.id);
if(e)
map[data.id] = e.parentNode.removeChild(e);
}
if(data.divs)
data.divs.forEach(function(d) {
bringover(d, map);
});
}
function flex_divs(root, data, place) {
var map = {};
bringover(data, map);
var flex_div_helper = flex_div_helper_mapper(map);
d3.select(root).data([data])
.each(flex_div_helper);
Object.keys(map).forEach(function(k) {
document.getElementById('wrap-' + k).appendChild(map[k]);
if(place)
place(k);
});
}