| [e4c61dd] | 1 | import {Selection} from "./index.js";
|
|---|
| 2 | import {EnterNode} from "./enter.js";
|
|---|
| 3 | import constant from "../constant.js";
|
|---|
| 4 |
|
|---|
| 5 | function bindIndex(parent, group, enter, update, exit, data) {
|
|---|
| 6 | var i = 0,
|
|---|
| 7 | node,
|
|---|
| 8 | groupLength = group.length,
|
|---|
| 9 | dataLength = data.length;
|
|---|
| 10 |
|
|---|
| 11 | // Put any non-null nodes that fit into update.
|
|---|
| 12 | // Put any null nodes into enter.
|
|---|
| 13 | // Put any remaining data into enter.
|
|---|
| 14 | for (; i < dataLength; ++i) {
|
|---|
| 15 | if (node = group[i]) {
|
|---|
| 16 | node.__data__ = data[i];
|
|---|
| 17 | update[i] = node;
|
|---|
| 18 | } else {
|
|---|
| 19 | enter[i] = new EnterNode(parent, data[i]);
|
|---|
| 20 | }
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | // Put any non-null nodes that don’t fit into exit.
|
|---|
| 24 | for (; i < groupLength; ++i) {
|
|---|
| 25 | if (node = group[i]) {
|
|---|
| 26 | exit[i] = node;
|
|---|
| 27 | }
|
|---|
| 28 | }
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | function bindKey(parent, group, enter, update, exit, data, key) {
|
|---|
| 32 | var i,
|
|---|
| 33 | node,
|
|---|
| 34 | nodeByKeyValue = new Map,
|
|---|
| 35 | groupLength = group.length,
|
|---|
| 36 | dataLength = data.length,
|
|---|
| 37 | keyValues = new Array(groupLength),
|
|---|
| 38 | keyValue;
|
|---|
| 39 |
|
|---|
| 40 | // Compute the key for each node.
|
|---|
| 41 | // If multiple nodes have the same key, the duplicates are added to exit.
|
|---|
| 42 | for (i = 0; i < groupLength; ++i) {
|
|---|
| 43 | if (node = group[i]) {
|
|---|
| 44 | keyValues[i] = keyValue = key.call(node, node.__data__, i, group) + "";
|
|---|
| 45 | if (nodeByKeyValue.has(keyValue)) {
|
|---|
| 46 | exit[i] = node;
|
|---|
| 47 | } else {
|
|---|
| 48 | nodeByKeyValue.set(keyValue, node);
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | // Compute the key for each datum.
|
|---|
| 54 | // If there a node associated with this key, join and add it to update.
|
|---|
| 55 | // If there is not (or the key is a duplicate), add it to enter.
|
|---|
| 56 | for (i = 0; i < dataLength; ++i) {
|
|---|
| 57 | keyValue = key.call(parent, data[i], i, data) + "";
|
|---|
| 58 | if (node = nodeByKeyValue.get(keyValue)) {
|
|---|
| 59 | update[i] = node;
|
|---|
| 60 | node.__data__ = data[i];
|
|---|
| 61 | nodeByKeyValue.delete(keyValue);
|
|---|
| 62 | } else {
|
|---|
| 63 | enter[i] = new EnterNode(parent, data[i]);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | // Add any remaining nodes that were not bound to data to exit.
|
|---|
| 68 | for (i = 0; i < groupLength; ++i) {
|
|---|
| 69 | if ((node = group[i]) && (nodeByKeyValue.get(keyValues[i]) === node)) {
|
|---|
| 70 | exit[i] = node;
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | function datum(node) {
|
|---|
| 76 | return node.__data__;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | export default function(value, key) {
|
|---|
| 80 | if (!arguments.length) return Array.from(this, datum);
|
|---|
| 81 |
|
|---|
| 82 | var bind = key ? bindKey : bindIndex,
|
|---|
| 83 | parents = this._parents,
|
|---|
| 84 | groups = this._groups;
|
|---|
| 85 |
|
|---|
| 86 | if (typeof value !== "function") value = constant(value);
|
|---|
| 87 |
|
|---|
| 88 | for (var m = groups.length, update = new Array(m), enter = new Array(m), exit = new Array(m), j = 0; j < m; ++j) {
|
|---|
| 89 | var parent = parents[j],
|
|---|
| 90 | group = groups[j],
|
|---|
| 91 | groupLength = group.length,
|
|---|
| 92 | data = arraylike(value.call(parent, parent && parent.__data__, j, parents)),
|
|---|
| 93 | dataLength = data.length,
|
|---|
| 94 | enterGroup = enter[j] = new Array(dataLength),
|
|---|
| 95 | updateGroup = update[j] = new Array(dataLength),
|
|---|
| 96 | exitGroup = exit[j] = new Array(groupLength);
|
|---|
| 97 |
|
|---|
| 98 | bind(parent, group, enterGroup, updateGroup, exitGroup, data, key);
|
|---|
| 99 |
|
|---|
| 100 | // Now connect the enter nodes to their following update node, such that
|
|---|
| 101 | // appendChild can insert the materialized enter node before this node,
|
|---|
| 102 | // rather than at the end of the parent node.
|
|---|
| 103 | for (var i0 = 0, i1 = 0, previous, next; i0 < dataLength; ++i0) {
|
|---|
| 104 | if (previous = enterGroup[i0]) {
|
|---|
| 105 | if (i0 >= i1) i1 = i0 + 1;
|
|---|
| 106 | while (!(next = updateGroup[i1]) && ++i1 < dataLength);
|
|---|
| 107 | previous._next = next || null;
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | update = new Selection(update, parents);
|
|---|
| 113 | update._enter = enter;
|
|---|
| 114 | update._exit = exit;
|
|---|
| 115 | return update;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // Given some data, this returns an array-like view of it: an object that
|
|---|
| 119 | // exposes a length property and allows numeric indexing. Note that unlike
|
|---|
| 120 | // selectAll, this isn’t worried about “live” collections because the resulting
|
|---|
| 121 | // array will only be used briefly while data is being bound. (It is possible to
|
|---|
| 122 | // cause the data to change while iterating by using a key function, but please
|
|---|
| 123 | // don’t; we’d rather avoid a gratuitous copy.)
|
|---|
| 124 | function arraylike(data) {
|
|---|
| 125 | return typeof data === "object" && "length" in data
|
|---|
| 126 | ? data // Array, TypedArray, NodeList, array-like
|
|---|
| 127 | : Array.from(data); // Map, Set, iterable, string, or anything else
|
|---|
| 128 | }
|
|---|