mirror of
https://github.com/janishutz/fundamentals-of-webengineering.git
synced 2025-11-25 05:44:24 +00:00
++ Glue
This commit is contained in:
44
task_2_ts/ts/glue/list-renderer.ts
Normal file
44
task_2_ts/ts/glue/list-renderer.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
RenderTemplate,
|
||||
StringIndexedObject
|
||||
} from './rendering';
|
||||
|
||||
const renderList = <T extends StringIndexedObject>(
|
||||
data: T,
|
||||
template: RenderTemplate,
|
||||
name: string, id: number
|
||||
): HTMLElement => {
|
||||
const parent = renderer( data, template );
|
||||
|
||||
parent.id = `${ name }--${ id }`;
|
||||
|
||||
return parent;
|
||||
};
|
||||
|
||||
const renderer = <T extends StringIndexedObject>( data: T, template: RenderTemplate ): HTMLElement => {
|
||||
const parent = document.createElement( template.type );
|
||||
|
||||
for ( let i = 0; i < template.cssClasses.length; i++ ) {
|
||||
parent.classList.add( template.cssClasses[i]! );
|
||||
}
|
||||
|
||||
for ( let i = 0; i < template.children.length; i++ ) {
|
||||
const element = template.children[i]!;
|
||||
|
||||
parent.appendChild( renderer( data, element ) );
|
||||
}
|
||||
|
||||
if ( template.children.length === 0 ) {
|
||||
if ( template.attribute ) {
|
||||
parent.textContent = String( data[ template.attribute ] );
|
||||
} else {
|
||||
parent.textContent = String( data );
|
||||
}
|
||||
}
|
||||
|
||||
return parent;
|
||||
};
|
||||
|
||||
export default {
|
||||
renderList
|
||||
};
|
||||
Reference in New Issue
Block a user