Compare commits
2 Commits
a67e04bdc4
...
199e2dfa59
| Author | SHA1 | Date |
|---|---|---|
|
|
199e2dfa59 | 3 years ago |
|
|
071a32a8b4 | 3 years ago |
@ -0,0 +1,51 @@
|
|||||||
|
import { Renderable } from '../component/componentdefs.js';
|
||||||
|
import { Component } from '../component/Component.js';
|
||||||
|
import { ArrayVAO } from '../vao/ArrayVAO.js';
|
||||||
|
import { ScalarVAO } from '../vao/ScalarVAO.js';
|
||||||
|
import { TAttributes } from '../html/htmldefs.js';
|
||||||
|
|
||||||
|
// === TODO ===
|
||||||
|
// [A] Naive implementation - should be optimzied
|
||||||
|
|
||||||
|
export type TForEachRender = (item: any) => Renderable | Renderable[]
|
||||||
|
|
||||||
|
export class ForEach extends Component {
|
||||||
|
static properties = {
|
||||||
|
array: {
|
||||||
|
$: ArrayVAO
|
||||||
|
},
|
||||||
|
render: {
|
||||||
|
$: ScalarVAO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
constructor ({ attributes, children }: {
|
||||||
|
attributes: TAttributes | undefined,
|
||||||
|
children: Renderable[] | undefined
|
||||||
|
}) {
|
||||||
|
super({ attributes, children });
|
||||||
|
|
||||||
|
const array$ = this.vaos_['array'] as ArrayVAO;
|
||||||
|
|
||||||
|
// TODO: [A]
|
||||||
|
((events) => {
|
||||||
|
for ( const event of events ) {
|
||||||
|
array$.sub(event, this.rerenderSelf.bind(this));
|
||||||
|
}
|
||||||
|
})(['change', 'item.insert', 'item.remove', 'item.replace']);
|
||||||
|
}
|
||||||
|
render () {
|
||||||
|
const array$ = this.vaos_['array'] as ArrayVAO;
|
||||||
|
const render$ = this.vaos_['render'];
|
||||||
|
|
||||||
|
const render = render$.get() as TForEachRender;
|
||||||
|
|
||||||
|
const outputRenderables = [];
|
||||||
|
for ( const item of array$.get() ) {
|
||||||
|
outputRenderables.push(...(result => Array.isArray(result)
|
||||||
|
? result : [result])(render(item)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputRenderables;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in new issue