Last active
May 28, 2020 00:36
-
-
Save MaximBalaganskiy/a4c5576b155385b5ea599682ee3833ea to your computer and use it in GitHub Desktop.
tree-view-demo-process-content-2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "dependencies": { | |
| "aurelia-bootstrapper": "^2.3.3" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <require from="./tree-view"></require> | |
| <h1>${message}</h1> | |
| <tree-view nodes.bind="nodes"> | |
| ${n.name} | |
| </tree-view> | |
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' }] } | |
| ]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {Aurelia} from 'aurelia-framework'; | |
| export function configure(aurelia: Aurelia) { | |
| aurelia.use | |
| .standardConfiguration() | |
| .developmentLogging('info') | |
| .globalResources(['tree-view', 'tree-node']); | |
| aurelia.start().then(() => aurelia.setRoot()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <require from="./tree-view"></require> | |
| <template replaceable part="node"></template> | |
| <tree-view synthesized if.bind="node.children" nodes.bind="node.children">test</tree-view> | |
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { customElement, bindable, useView, PLATFORM, child, processContent, | |
| ViewCompiler, ViewResources, BehaviorInstruction, ViewCompileInstruction} from 'aurelia-framework'; | |
| @customElement('tree-node') | |
| //@processContent(TreeNode.processContent) | |
| export class TreeNode { | |
| @bindable | |
| node: any; | |
| static processContent(viewCompiler: ViewCompiler, resources: ViewResources, element: Element, instruction: BehaviorInstruction) { | |
| console.log(element); | |
| const elementResource = instruction.type; | |
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| const viewResources = (elementResource as any).viewFactory.resources; | |
| const treeNode = element; | |
| 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> | |
| <require from="./tree-view"></require> | |
| ${treeNode.innerHTML} | |
| <tree-view synthesized if.bind="node.children" nodes.bind="node.children">${treeNode.innerHTML}</tree-view> | |
| </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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { customElement, bindable, useView, PLATFORM, child, processContent, | |
| ViewCompiler, ViewResources, BehaviorInstruction, ViewCompileInstruction} from 'aurelia-framework'; | |
| let parentNodeTemplate: string = ''; | |
| @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; | |
| if(!element.hasAttribute('synthesized')) { | |
| parentNodeTemplate = element.innerHTML; | |
| } | |
| const nodeTemplate = parentNodeTemplate; | |
| console.log(parentNodeTemplate); | |
| 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"> | |
| <tree-node node.bind="n"><template replace-part="node">${nodeTemplate}</template></tree-node> | |
| </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