Skip to content

Instantly share code, notes, and snippets.

@bigopon
Forked from MaximBalaganskiy/index.html
Last active April 12, 2020 04:56
Show Gist options
  • Save bigopon/8a7d24121e4ba594e63b33d3c1d2a704 to your computer and use it in GitHub Desktop.
Save bigopon/8a7d24121e4ba594e63b33d3c1d2a704 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,
ViewSlot,
View,
} from 'aurelia-framework';
@autoinject
@customAttribute('enhance')
export class EnhanceCustomAttribute {
@bindable
template: string;
@bindable
model: unknown;
private viewSlot: ViewSlot;
private views: View[];
constructor(
private templatingEngine: TemplatingEngine,
private element: Element,
) {
}
bind() {
this.templateChanged(this.template);
}
unbind() {
this.unbindViews();
}
templateChanged(template) {
this.unbindViews();
if (!template) {
return;
}
const views = [];
this.element.innerHTML = template;
for (let i = 0, ii = this.element.children.length; ii > i; ++i) {
const childElement = this.element.children.item(i);
const view = this.templatingEngine.enhance({
element: childElement,
bindingContext: this.model,
});
views.push(view);
}
this.views = views;
}
unbindViews() {
if (this.views) {
this.views.forEach(view => view.unbind());
this.views = undefined;
}
}
}
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