Skip to content

Instantly share code, notes, and snippets.

@MaximBalaganskiy
Last active April 10, 2020 05:16
Show Gist options
  • Save MaximBalaganskiy/5b8b8cb9f04784e0a3dfc6efe70b03d2 to your computer and use it in GitHub Desktop.
Save MaximBalaganskiy/5b8b8cb9f04784e0a3dfc6efe70b03d2 to your computer and use it in GitHub Desktop.
tree-view-demo
<!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-node"></require>
<require from="./tree-view"></require>
<h1>${message}</h1>
<tree-view nodes.bind="nodes" node-template.bind="nodeTemplate">
<!-- tree-node can sit anywhere, it's just more convenient here -->
<tree-node ref="nodeTemplate"><span>${name}</span></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 { customAttribute, TemplatingEngine, autoinject, bindable } from 'aurelia-framework';
@autoinject
@customAttribute('enhance')
export class EnhanceCustomAttribute {
constructor(private templatingEngine: TemplatingEngine, private element: Element) { }
@bindable
template: string;
@bindable
model: unknown;
attached(){
this.element.innerHTML = this.template;
// const view = this.templatingEngine.enhance(this.element);
for (let i = 0; i < this.element.children.length; i++) {
const view = this.templatingEngine.enhance(this.element.children.item(i));
view.bind(this.model);
}
}
}
import {Aurelia} from 'aurelia-framework';
export function configure(aurelia: Aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging('info');
aurelia.start().then(() => aurelia.setRoot());
}
import { customElement, processContent, noView } from 'aurelia-framework';
@noView
@customElement('tree-node')
@processContent(false)
export class TreeNode {
constructor(public element: Element) { }
}
<template>
<require from="./enhance"></require>
<ul>
<li repeat.for="n of nodes">
<div enhance="template.bind: nodeTemplate.innerHTML; model.bind: n"></div>
<tree-view if.bind="n.children" nodes.bind="n.children" node-template.bind="nodeTemplate"></tree-view>
</li>
</ul>
<slot></slot>
</template>
import { customElement, bindable, useView, PLATFORM, child } from 'aurelia-framework';
import { INode } from './i-item';
@customElement('tree-view')
@useView(PLATFORM.moduleName('./tree-view.html'))
export class TreeView {
@bindable
nodes: INode[];
@bindable
nodeTemplate: HTMLElement;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment