Skip to content

Instantly share code, notes, and snippets.

@MaximBalaganskiy
Last active May 27, 2020 10:40
Show Gist options
  • Save MaximBalaganskiy/92438fb6e61096ea002a96674cfb40bc to your computer and use it in GitHub Desktop.
Save MaximBalaganskiy/92438fb6e61096ea002a96674cfb40bc to your computer and use it in GitHub Desktop.
tree-view-demo-process-content
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dumber Gist</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<base href="/">
</head>
<!--
Dumber gist uses dumber bundler, the default bundle file
is /dist/entry-bundle.js.
The starting module is pointed to aurelia-bootstrapper
(data-main attribute on script) for Aurelia,
The aurelia bootstrapper then loads up user module "main"
(aurelia-app attribute on <body>) which is your src/main.ts.
-->
<body aurelia-app="main">
<script src="/dist/entry-bundle.js" data-main="aurelia-bootstrapper"></script>
</body>
</html>
{
"dependencies": {
"aurelia-bootstrapper": "^2.3.3"
}
}
<template>
<require from="./tree-view"></require>
<h1>${message}</h1>
<tree-view nodes.bind="nodes">
<tree-node>${n.name}</tree-node>
</tree-view>
</template>
export class App {
public message: string = 'Hello Aurelia!';
nodes = [
{ name: 'item1', children: [{ name: 'child11', children: [{ name: 'child111' }, { name: 'child112' }] }, { name: 'child12' }] },
{ name: 'item2', children: [{ name: 'child21' }, { name: 'child22' }] }
];
}
import {Aurelia} from 'aurelia-framework';
export function configure(aurelia: Aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging('info');
aurelia.start().then(() => aurelia.setRoot());
}
<template>
</template>
import { customElement, bindable, useView, PLATFORM, child, processContent,
ViewCompiler, ViewResources, BehaviorInstruction, ViewCompileInstruction} from 'aurelia-framework';
import { INode } from './i-item';
@customElement('tree-view')
@useView(PLATFORM.moduleName('./tree-view.html'))
@processContent(TreeView.processContent)
export class TreeView {
@bindable
nodes: INode[];
static processContent(viewCompiler: ViewCompiler, resources: ViewResources, element: Element, instruction: BehaviorInstruction) {
const elementResource = instruction.type;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const viewResources = (elementResource as any).viewFactory.resources;
const treeNode = element.querySelector('tree-node');
const shouldCreateFactory = !!treeNode;
// inside this if, we are going to create a new factory to supply
// to the creation of the custom element
if (shouldCreateFactory) {
const compileInstruction = new ViewCompileInstruction(/* shadow DOM? */false, true);
const template = `
<template>
<ul>
<li repeat.for="n of nodes">
${treeNode.innerHTML}
<tree-view if.bind="n.children" nodes.bind="n.children">
<tree-node>${treeNode.innerHTML}</tree-node>
</tree-view>
</li>
</ul>
</template>`;
const viewFactory = viewCompiler.compile(template, viewResources, compileInstruction);
// override default view factory
// Aurelia fallbacks to instruction view factory if there's any
instruction.viewFactory = viewFactory;
// only use the content as template
// remove all after use to avoid undesirable visual
element.innerHTML = '';
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment