-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
specs-viewer.ts
35 lines (32 loc) · 1.29 KB
/
specs-viewer.ts
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
import { bindable, customElement, valueConverter } from '@aurelia/runtime';
import { Thing, ThingViewer } from './thing-viewer';
import { Camera, CameraSpecsViewer } from './camera-specs-viewer';
import { Laptop, LaptopSpecsViewer } from './laptop-specs-viewer';
import template from './specs-viewer.html';
@customElement({ name: 'specs-viewer', template })
export class SpecsViewer {
@bindable public things: Thing[];
private pairs: { vm: typeof ThingViewer; thing: Thing }[];
public beforeBind() {
const toVm = (thing: Thing) => {
switch (true) {
case thing instanceof Camera: return CameraSpecsViewer;
case thing instanceof Laptop: return LaptopSpecsViewer;
case thing instanceof Thing: return ThingViewer;
default: throw new Error(`Unsupported type ${thing.constructor.prototype}`);
}
};
this.pairs = this.things.map((thing) => ({ thing, vm: toVm(thing) }));
}
}
@valueConverter('viewer')
export class ViewerValueConverter {
public toView(thing: Thing) {
switch (true) {
case thing instanceof Camera: return CameraSpecsViewer;
case thing instanceof Laptop: return LaptopSpecsViewer;
case thing instanceof Thing: return ThingViewer;
default: throw new Error(`Unsupported type ${thing.constructor.prototype}`);
}
}
}