|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {C, D, E, T, V, a, b, b1, c, defineComponent, detectChanges as _detectChanges, e, p, rC, rc, t, v} from '@angular/core/src/render3/index'; |
| 10 | +import {ComponentDef} from '@angular/core/src/render3/public_interfaces'; |
| 11 | + |
| 12 | +import {TreeNode, buildTree, emptyTree} from '../util'; |
| 13 | + |
| 14 | +export function destroyDom(component: TreeComponent) { |
| 15 | + component.data = emptyTree; |
| 16 | + _detectChanges(component); |
| 17 | +} |
| 18 | + |
| 19 | +export function createDom(component: TreeComponent) { |
| 20 | + component.data = buildTree(); |
| 21 | + _detectChanges(component); |
| 22 | +} |
| 23 | + |
| 24 | +const numberOfChecksEl = document.getElementById('numberOfChecks') !; |
| 25 | +let detectChangesRuns = 0; |
| 26 | +export function detectChanges(component: TreeComponent) { |
| 27 | + for (let i = 0; i < 10; i++) { |
| 28 | + _detectChanges(component); |
| 29 | + } |
| 30 | + detectChangesRuns += 10; |
| 31 | + numberOfChecksEl.textContent = `${detectChangesRuns}`; |
| 32 | +} |
| 33 | + |
| 34 | +export class TreeComponent { |
| 35 | + data: TreeNode = emptyTree; |
| 36 | + |
| 37 | + /** @nocollapse */ |
| 38 | + static ngComponentDef: ComponentDef<TreeComponent> = defineComponent({ |
| 39 | + type: TreeComponent, |
| 40 | + tag: 'tree', |
| 41 | + template: function(ctx: TreeComponent, cm: boolean) { |
| 42 | + if (cm) { |
| 43 | + E(0, 'span'); |
| 44 | + { T(1); } |
| 45 | + e(); |
| 46 | + C(2); |
| 47 | + c(); |
| 48 | + C(3); |
| 49 | + c(); |
| 50 | + } |
| 51 | + a(0, 'style', b1('background-color:', ctx.data.depth % 2 ? '' : 'grey', '')); |
| 52 | + t(1, b1(' ', ctx.data.value, ' ')); |
| 53 | + rC(2); |
| 54 | + { |
| 55 | + if (ctx.data.left != null) { |
| 56 | + let cm0 = V(0); |
| 57 | + { |
| 58 | + if (cm0) { |
| 59 | + E(0, TreeComponent.ngComponentDef); |
| 60 | + { D(0, TreeComponent.ngComponentDef.n(), TreeComponent.ngComponentDef); } |
| 61 | + e(); |
| 62 | + } |
| 63 | + p(0, 'data', b(ctx.data.left)); |
| 64 | + TreeComponent.ngComponentDef.r(0, 0); |
| 65 | + } |
| 66 | + v(); |
| 67 | + } |
| 68 | + } |
| 69 | + rc(); |
| 70 | + rC(3); |
| 71 | + { |
| 72 | + if (ctx.data.right != null) { |
| 73 | + let cm0 = V(0); |
| 74 | + { |
| 75 | + if (cm0) { |
| 76 | + E(0, TreeComponent.ngComponentDef); |
| 77 | + { D(0, TreeComponent.ngComponentDef.n(), TreeComponent.ngComponentDef); } |
| 78 | + e(); |
| 79 | + } |
| 80 | + p(0, 'data', b(ctx.data.right)); |
| 81 | + TreeComponent.ngComponentDef.r(0, 0); |
| 82 | + } |
| 83 | + v(); |
| 84 | + } |
| 85 | + } |
| 86 | + rc(); |
| 87 | + }, |
| 88 | + factory: () => new TreeComponent, |
| 89 | + inputs: {data: 'data'} |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +export class TreeFunction extends TreeComponent { |
| 94 | + data: TreeNode = emptyTree; |
| 95 | + |
| 96 | + /** @nocollapse */ |
| 97 | + static ngComponentDef: ComponentDef<TreeFunction> = defineComponent({ |
| 98 | + type: TreeFunction, |
| 99 | + tag: 'tree', |
| 100 | + template: function(ctx: TreeFunction, cm: boolean) { |
| 101 | + // bit of a hack |
| 102 | + TreeTpl(ctx.data, cm); |
| 103 | + }, |
| 104 | + factory: () => new TreeFunction, |
| 105 | + inputs: {data: 'data'} |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +export function TreeTpl(ctx: TreeNode, cm: boolean) { |
| 110 | + if (cm) { |
| 111 | + E(0, 'span'); |
| 112 | + { T(1); } |
| 113 | + e(); |
| 114 | + C(2); |
| 115 | + c(); |
| 116 | + C(3); |
| 117 | + c(); |
| 118 | + } |
| 119 | + a(0, 'style', b1('background-color:', ctx.depth % 2 ? '' : 'grey', '')); |
| 120 | + t(1, b1(' ', ctx.value, ' ')); |
| 121 | + rC(2); |
| 122 | + { |
| 123 | + if (ctx.left != null) { |
| 124 | + let cm0 = V(0); |
| 125 | + { TreeTpl(ctx.left, cm0); } |
| 126 | + v(); |
| 127 | + } |
| 128 | + } |
| 129 | + rc(); |
| 130 | + rC(3); |
| 131 | + { |
| 132 | + if (ctx.right != null) { |
| 133 | + let cm0 = V(0); |
| 134 | + { TreeTpl(ctx.right, cm0); } |
| 135 | + v(); |
| 136 | + } |
| 137 | + } |
| 138 | + rc(); |
| 139 | +} |
0 commit comments