Skip to content

Commit 2edf58e

Browse files
committed
Fix Selection.metadata(), additional nullish checks
1 parent 027065a commit 2edf58e

10 files changed

Lines changed: 37 additions & 37 deletions

File tree

anu-examples/examples/Animation/barchartRace.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export const barchartRace = function(engine) {
115115
scaleX = d3.scaleLinear().domain([0, Math.max(...keyframes[timestep][1].map(d => d.value))]).range([0, 3]);
116116

117117
//Animate our bars
118-
bars.prop("metadata.data", (d,n,i) => keyframes[timestep][1][i]) //Bind new data to the Meshes
118+
bars.metadata("data", (d,n,i) => keyframes[timestep][1][i]) //Bind new data to the Meshes
119119
.transition((d,n,i) => ({
120120
duration: interval,
121121
onAnimationEnd: () => { //When the animation ends, call this function again to begin the animation for the next year
@@ -140,7 +140,7 @@ export const barchartRace = function(engine) {
140140
});
141141

142142
//Animate the labels
143-
labels.prop("metadata.data", (d,n,i) => keyframes[timestep][1][i])
143+
labels.metadata("data", (d,n,i) => keyframes[timestep][1][i])
144144
.transition((d,n,i) => ({ duration: interval }))
145145
.tween((d,n,i) => {
146146
let textTween = d3.interpolateNumber(Number(n.text.split('\n').pop().replace(',', '')), d.value);

docs/anu-examples/animationBarChartRace.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function animationBarChartRace(engine) {
120120
scaleX = d3.scaleLinear().domain([0, Math.max(...keyframes[timestep][1].map(d => d.value))]).range([0, 3]);
121121

122122
//Animate our bars
123-
bars.prop('metadata.data', (d,n,i) => keyframes[timestep][1][i]) //Bind new data to the Meshes
123+
bars.metadata('data', (d,n,i) => keyframes[timestep][1][i]) //Bind new data to the Meshes
124124
.transition((d,n,i) => ({
125125
duration: interval,
126126
onAnimationEnd: () => { //When the animation ends, call this function again to begin the animation for the next year
@@ -145,7 +145,7 @@ export function animationBarChartRace(engine) {
145145
});
146146

147147
//Animate the labels
148-
labels.prop('metadata.data', (d,n,i) => keyframes[timestep][1][i])
148+
labels.metadata('data', (d,n,i) => keyframes[timestep][1][i])
149149
.transition((d,n,i) => ({ duration: interval }))
150150
.tween((d,n,i) => {
151151
let textTween = d3.interpolateNumber(Number(n.text.split('\n').pop().replace(',', '')), d.value);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jpmorganchase/anu",
3-
"version": "0.5.8",
3+
"version": "0.5.9",
44
"description": "",
55
"type": "module",
66
"files": [

src/selection/animation/transition.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function transition(
5656
transitionSelection = this;
5757
}
5858
transitionSelection.selected.forEach((node, i) => {
59-
executedOptions.push(options instanceof Function ? options((node.metadata.data ?? {}), node, i) : options || {});
59+
executedOptions.push(options instanceof Function ? options((node.metadata?.data ?? {}), node, i) : options || {});
6060
});
6161

6262
let transition = new Transition(this.transitions.length, executedOptions);
@@ -107,7 +107,7 @@ export function createTransition(selection: Selection, accessor: string, value:
107107
fps,
108108
frames,
109109
get(node, accessor),
110-
value instanceof Function ? value((node.metadata.data ?? {}), node, i) : value,
110+
value instanceof Function ? value((node.metadata?.data ?? {}), node, i) : value,
111111
loop,
112112
ease,
113113
onEnd,
@@ -169,7 +169,7 @@ export function createTransitions(selection: Selection, properties: {}) {
169169
fps,
170170
frames,
171171
get(node, accessor),
172-
value instanceof Function ? value((node.metadata.data ?? {}), node, i) : value,
172+
value instanceof Function ? value((node.metadata?.data ?? {}), node, i) : value,
173173
loop,
174174
ease,
175175
onEnd,
@@ -207,7 +207,7 @@ export function tween(this: Selection, value: (d, n, i) => (t) => void) {
207207
let ease: EasingFunction = transitionOptions.easingFunction || undefined;
208208
let wait: boolean = (transitionOptions.sequence ?? true);
209209
let onEnd: () => void = transitionOptions.onAnimationEnd || undefined;
210-
let func = value((node.metadata.data ?? {}), node, i);
210+
let func = value((node.metadata?.data ?? {}), node, i);
211211
let startTime = null;
212212
//let accumulatedTime = 0;
213213

src/selection/property/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function action(this: Selection, action: Action | ((d: any, n: AbstractMe
1414
this.selected.forEach((node, i) => {
1515
node instanceof AbstractMesh
1616
? action instanceof Function
17-
? node.actionManager?.registerAction(action((node.metadata.data ?? {}), node, i))
17+
? node.actionManager?.registerAction(action((node.metadata?.data ?? {}), node, i))
1818
: node.actionManager?.registerAction(action)
1919
: console.log('Node not a mesh, skipping.');
2020
});

src/selection/property/behavior.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function behavior(this: Selection, behavior: Behavior<Node> | ((d: any, n
1111
this.selected.forEach((node, i) => {
1212
node instanceof Node
1313
? behavior instanceof Function
14-
? node?.addBehavior(behavior(node.metadata.data ?? {}, node, i))
14+
? node?.addBehavior(behavior(node.metadata?.data ?? {}, node, i))
1515
: node?.addBehavior(behavior)
1616
: console.log('Node not a mesh, skipping.');
1717
});

src/selection/property/metadata.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export function metadata(this: Selection, key: string, value: {} | ((d: any, n:
4242
node,
4343
'metadata',
4444
value instanceof Function
45-
? { ...node.metadata, key: value(node.metadata.data, node, i) }
46-
: { ...node.metadata, key: value },
45+
? { ...node.metadata, [key]: value(node.metadata?.data, node, i) }
46+
: { ...node.metadata, [key]: value },
4747
)
4848
: console.error('metadata not a property of ' + node);
4949
});

src/selection/property/prop.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function attr(this: Selection, accessor: string, value: any) {
2020
this.selected.forEach((node, i) => {
2121
node instanceof TransformNode
2222
? get(node, accessor) != undefined
23-
? set(node, accessor, value instanceof Function ? value((node.metadata.data ?? {}), i) : value)
23+
? set(node, accessor, value instanceof Function ? value((node.metadata?.data ?? {}), i) : value)
2424
: console.error(accessor + ' not a property of ' + node)
2525
: console.warn('Node not a mesh, skipping.');
2626
});
@@ -40,7 +40,7 @@ export function prop(this: Selection, accessor: string, value: any) {
4040
} else {
4141
this.selected.forEach((node, i) => {
4242
hasIn(node, accessor)
43-
? set(node, accessor, value instanceof Function ? value((node.metadata.data ?? {}), node, i) : value)
43+
? set(node, accessor, value instanceof Function ? value((node.metadata?.data ?? {}), node, i) : value)
4444
: console.error(accessor + ' not a property of ' + node);
4545
});
4646
return this;
@@ -66,7 +66,7 @@ export function props(this: Selection, properties: {}) {
6666
node,
6767
accessor,
6868
(properties as any)[accessor] instanceof Function
69-
? (properties as any)[accessor]((node.metadata.data ?? {}), node, i)
69+
? (properties as any)[accessor]((node.metadata?.data ?? {}), node, i)
7070
: (properties as any)[accessor],
7171
)
7272
: console.warn(accessor + ' not property of ' + node);

src/selection/property/thin.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function thinInstanceSetBuffer(
1313
? node.hasThinInstances
1414
? node.thinInstanceSetBuffer(
1515
attribute,
16-
value instanceof Function ? value((node.metadata.data ?? [{}]), node, i) : value,
16+
value instanceof Function ? value((node.metadata?.data ?? [{}]), node, i) : value,
1717
stride,
1818
staticBuffer,
1919
)
@@ -35,7 +35,7 @@ export function thinInstancePosition(
3535
? (() => {
3636
let bufferMatrices = new Float32Array(node.thinInstanceCount * 16);
3737
let matrices = node.thinInstanceGetWorldMatrices();
38-
let data = (node.metadata.data ?? [{}]);
38+
let data = (node.metadata?.data ?? [{}]);
3939
data.forEach((e, j) => {
4040
let evaluated = value instanceof Function ? value(e, node, j) : value;
4141
let matrix = matrices[j].setTranslation(evaluated);
@@ -61,7 +61,7 @@ export function thinInstanceScaling(
6161
? (() => {
6262
let bufferMatrices = new Float32Array(node.thinInstanceCount * 16);
6363
let matrices = node.thinInstanceGetWorldMatrices();
64-
let data = (node.metadata.data ?? [{}]);
64+
let data = (node.metadata?.data ?? [{}]);
6565
data.forEach((e, j) => {
6666
let evaluated = value instanceof Function ? value(e, node, j) : value;
6767
let previousMatrix = matrices[j];
@@ -93,7 +93,7 @@ export function thinInstanceRotation(
9393
? (() => {
9494
let bufferMatrices = new Float32Array(node.thinInstanceCount * 16);
9595
let matrices = node.thinInstanceGetWorldMatrices();
96-
let data = (node.metadata.data ?? [{}]);
96+
let data = (node.metadata?.data ?? [{}]);
9797
data.forEach((e, j) => {
9898
let evaluated = value instanceof Function ? value(e, node, j) : value;
9999
let previousMatrix = matrices[j];
@@ -125,7 +125,7 @@ export function thinInstanceColor(
125125
if (node instanceof Mesh && node.hasThinInstances) {
126126
let colorMatrices = new Float32Array(node.thinInstanceCount * 4);
127127
if (value instanceof Function) {
128-
let data = (node.metadata.data ?? [{}]);
128+
let data = (node.metadata?.data ?? [{}]);
129129
data.forEach((e, j) => {
130130
let evaluated = value(e, node, j);
131131
colorMatrices[j * 4 + 0] = evaluated.r;
@@ -170,11 +170,11 @@ export function thinInstanceSetAttribute(
170170
this.selected.forEach((node, i) => {
171171
node instanceof Mesh
172172
? node.hasThinInstances
173-
? (node.metadata.data ?? [{}]).forEach((d, k) => {
173+
? (node.metadata?.data ?? [{}]).forEach((d, k) => {
174174
node.thinInstanceSetAttributeAt(
175175
attribute,
176176
k,
177-
value instanceof Function ? value((node.metadata.data ?? [{}]), node, i) : value,
177+
value instanceof Function ? value((node.metadata?.data ?? [{}]), node, i) : value,
178178
);
179179
})
180180
: console.warn(node + 'has no thin instances, skipping')
@@ -191,14 +191,14 @@ export function thinInstanceAttributeAt(
191191
value: any | ((d: any, n: Node, i: number) => any),
192192
): Selection {
193193
this.selected.forEach((node, i) => {
194-
let evaluated = value instanceof Function ? value((node.metadata.data[index] ?? [{}]), node, index) : value;
194+
let evaluated = value instanceof Function ? value((node.metadata?.data[index] ?? [{}]), node, index) : value;
195195

196196
node instanceof Mesh
197197
? node.hasThinInstances
198198
? node.thinInstanceSetAttributeAt(
199199
attribute,
200200
index,
201-
value instanceof Function ? value((node.metadata.data ?? [{}]), node, i) : value,
201+
value instanceof Function ? value((node.metadata?.data ?? [{}]), node, i) : value,
202202
)
203203
: console.warn(node + 'has no thin instances, skipping')
204204
: console.warn(node + 'Node is not a mesh, skipping');
@@ -213,7 +213,7 @@ export function thinInstanceMatrixAt(
213213
value: Matrix | ((d: any, n: Node, i: number) => Matrix),
214214
): Selection {
215215
this.selected.forEach((node, i) => {
216-
let evaluated = value instanceof Function ? value((node.metadata.data[index] ?? [{}]), node, index) : value;
216+
let evaluated = value instanceof Function ? value((node.metadata?.data[index] ?? [{}]), node, index) : value;
217217
node instanceof Mesh
218218
? node.hasThinInstances
219219
? node.thinInstanceSetMatrixAt(index, evaluated)
@@ -233,7 +233,7 @@ export function thinInstancePositionAt(
233233
node instanceof Mesh
234234
? node.hasThinInstances
235235
? (() => {
236-
let evaluated = value instanceof Function ? value((node.metadata.data[index] ?? [{}]), node, index) : value;
236+
let evaluated = value instanceof Function ? value((node.metadata?.data[index] ?? [{}]), node, index) : value;
237237
let previousMatrix = node.thinInstanceGetWorldMatrices()[index];
238238
let matrix = previousMatrix.setTranslation(evaluated);
239239
node.thinInstanceSetMatrixAt(index, matrix);
@@ -254,7 +254,7 @@ export function thinInstanceScalingAt(
254254
node instanceof Mesh
255255
? node.hasThinInstances
256256
? (() => {
257-
let evaluated = value instanceof Function ? value((node.metadata.data[index] ?? [{}]), node, index) : value;
257+
let evaluated = value instanceof Function ? value((node.metadata?.data[index] ?? [{}]), node, index) : value;
258258
let previousMatrix = node.thinInstanceGetWorldMatrices()[index];
259259
let matrix = Matrix.ComposeToRef(
260260
evaluated,
@@ -280,7 +280,7 @@ export function thinInstanceRotationAt(
280280
node instanceof Mesh
281281
? node.hasThinInstances
282282
? (() => {
283-
let evaluated = value instanceof Function ? value((node.metadata.data[index] ?? [{}]), node, index) : value;
283+
let evaluated = value instanceof Function ? value((node.metadata?.data[index] ?? [{}]), node, index) : value;
284284
let previousMatrix = node.thinInstanceGetWorldMatrices()[index];
285285
let previousScale = new Vector3();
286286
previousMatrix.decompose(previousScale);
@@ -308,7 +308,7 @@ export function thinInstanceColorAt(
308308
node instanceof Mesh
309309
? node.hasThinInstances
310310
? (() => {
311-
let evaluated = value instanceof Function ? value((node.metadata.data[index] ?? [{}]), node, index) : value;
311+
let evaluated = value instanceof Function ? value((node.metadata?.data[index] ?? [{}]), node, index) : value;
312312
node.thinInstanceSetAttributeAt('color', index, [evaluated.r, evaluated.g, evaluated.b, evaluated.a]);
313313
})()
314314
: console.warn(node + 'has no thin instances, skipping')
@@ -325,12 +325,12 @@ export function thinInstanceMatrixFor(
325325
): Selection {
326326
this.selected.forEach((node, i) => {
327327
if (node instanceof Mesh && node.hasThinInstances) {
328-
let data = (node.metadata.data ?? [{}]);
328+
let data = (node.metadata?.data ?? [{}]);
329329
data.forEach((d, k) => {
330330
if (method(d, node, k)) {
331331
node.thinInstanceSetMatrixAt(
332332
k,
333-
value instanceof Function ? value((node.metadata.data ?? [{}]), node, k) : value,
333+
value instanceof Function ? value((node.metadata?.data ?? [{}]), node, k) : value,
334334
false,
335335
);
336336
}
@@ -351,7 +351,7 @@ export function thinInstancePositionFor(
351351
if (node instanceof Mesh && node.hasThinInstances) {
352352
let bufferMatrices = new Float32Array(node.thinInstanceCount * 16);
353353
let matrices = node.thinInstanceGetWorldMatrices();
354-
let data = (node.metadata.data ?? [{}]);
354+
let data = (node.metadata?.data ?? [{}]);
355355
data.forEach((e, j) => {
356356
if (method(e, node, j)) {
357357
let evaluated = value instanceof Function ? value(e, node, j) : value;
@@ -376,7 +376,7 @@ export function thinInstanceRotationFor(
376376
if (node instanceof Mesh && node.hasThinInstances) {
377377
let bufferMatrices = new Float32Array(node.thinInstanceCount * 16);
378378
let matrices = node.thinInstanceGetWorldMatrices();
379-
let data = (node.metadata.data ?? [{}]);
379+
let data = (node.metadata?.data ?? [{}]);
380380
data.forEach((e, j) => {
381381
if (method(e, node, j)) {
382382
let evaluated = value instanceof Function ? value(e, node, j) : value;
@@ -408,7 +408,7 @@ export function thinInstanceScalingFor(
408408
if (node instanceof Mesh && node.hasThinInstances) {
409409
let bufferMatrices = new Float32Array(node.thinInstanceCount * 16);
410410
let matrices = node.thinInstanceGetWorldMatrices();
411-
let data = (node.metadata.data ?? [{}]);
411+
let data = (node.metadata?.data ?? [{}]);
412412
data.forEach((e, j) => {
413413
if (method(e, node, j)) {
414414
let evaluated = value instanceof Function ? value(e, node, j) : value;
@@ -438,7 +438,7 @@ export function thinInstanceColorFor(
438438
if (node instanceof Mesh && node.hasThinInstances) {
439439
let bufferMatrices = new Float32Array(node.thinInstanceCount * 16);
440440
let matrices = node.thinInstanceGetWorldMatrices();
441-
let data = (node.metadata.data ?? [{}]);
441+
let data = (node.metadata?.data ?? [{}]);
442442
data.forEach((e, j) => {
443443
if (method(e, node, j)) {
444444
let evaluated = value instanceof Function ? value(e, node, j) : value;

src/selection/utility/filter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Selection } from '../index';
1313
export function filter(this: Selection, method: (d: any, n: Node, i: number) => boolean) {
1414
let filtered: Node[] = [];
1515
this.selected.forEach((node, i) => {
16-
if (method((node.metadata.data ?? {}), node, i)) filtered.push(node);
16+
if (method((node.metadata?.data ?? {}), node, i)) filtered.push(node);
1717
});
1818

1919
return new Selection(filtered, this.scene);

0 commit comments

Comments
 (0)