|
| 1 | +import { |
| 2 | + Component, OnInit, |
| 3 | + OnDestroy, ChangeDetectionStrategy, Input, Inject, ViewChild, TemplateRef, AfterViewInit, forwardRef, HostListener |
| 4 | +} from '@angular/core'; |
| 5 | +import { IgxSelectionAPIService } from '../../core/selection'; |
| 6 | +import { IExpansionPanelCancelableEventArgs } from '../../expansion-panel/expansion-panel.common'; |
| 7 | +import { IExpansionPanelEventArgs, IgxExpansionPanelComponent } from '../../expansion-panel/public_api'; |
| 8 | +import { IGX_TREE_COMPONENT, ITreeComponent, ITreeNode } from '../common'; |
| 9 | +import { IgxTreeExpandIndicatorDirective, IgxTreeNodeDirective, IgxTreeNodeEditingDirective, IgxTreeSelectMarkerDirective } from '../tree.component'; |
| 10 | +import { IgxTreeService } from '../tree.service'; |
| 11 | +/** |
| 12 | + * |
| 13 | + * The tree node component represents a child node of the tree component or another tree node. |
| 14 | + * Usage: |
| 15 | + * |
| 16 | + * ```html |
| 17 | + * <tree [data]="data" [titleDataKey]="'Title'"></tree> |
| 18 | + * ``` |
| 19 | + */ |
| 20 | +@Component({ |
| 21 | + selector: "igx-tree-node", |
| 22 | + templateUrl: "tree-node.component.html", |
| 23 | + styleUrls: ["tree-node.component.scss"] |
| 24 | +}) |
| 25 | +export class IgxTreeNodeComponent implements ITreeNode, OnInit, AfterViewInit, OnDestroy { |
| 26 | + |
| 27 | + public inEdit: boolean = false; |
| 28 | + constructor(@Inject(IGX_TREE_COMPONENT) public tree: ITreeComponent, private selectionService: IgxSelectionAPIService, private treeService: IgxTreeService) { } |
| 29 | + |
| 30 | + @Input() |
| 31 | + public parentPath: string; |
| 32 | + |
| 33 | + @Input() |
| 34 | + public id: string; |
| 35 | + |
| 36 | + @Input() |
| 37 | + public depth: number; |
| 38 | + |
| 39 | + @Input() |
| 40 | + public data: any; |
| 41 | + |
| 42 | + @Input() |
| 43 | + public nodeTemplate: TemplateRef<any>; |
| 44 | + |
| 45 | + @Input() |
| 46 | + public nodeEditTemplate: TemplateRef<any>; |
| 47 | + |
| 48 | + @Input() |
| 49 | + public selectMarker: TemplateRef<any>; |
| 50 | + |
| 51 | + @Input() |
| 52 | + public expandIndicator: TemplateRef<any>; |
| 53 | + |
| 54 | + @ViewChild(IgxExpansionPanelComponent, { read: IgxExpansionPanelComponent }) |
| 55 | + public expansionPanel: IgxExpansionPanelComponent; |
| 56 | + |
| 57 | + @ViewChild('defaultTemplate', { read: TemplateRef, static: true }) |
| 58 | + private _defaultNodeTemplate: TemplateRef<any>; |
| 59 | + |
| 60 | + public get nodeTemplateRef(): TemplateRef<any> { |
| 61 | + return this.nodeTemplate ? this.nodeTemplate : this._defaultNodeTemplate |
| 62 | + } |
| 63 | + |
| 64 | + @ViewChild('defaultEdit', { read: TemplateRef, static: true }) |
| 65 | + private _defaultNodeEdit: TemplateRef<any>; |
| 66 | + |
| 67 | + public get nodeEditTemplateRef(): TemplateRef<any> { |
| 68 | + return this.nodeEditTemplate ? this.nodeEditTemplate : this._defaultNodeEdit |
| 69 | + } |
| 70 | + @ViewChild('defaultSelect', { read: TemplateRef, static: true }) |
| 71 | + private _defaultSelectMarker: TemplateRef<any>; |
| 72 | + |
| 73 | + public get selectMarkerTemplate(): TemplateRef<any> { |
| 74 | + return this.selectMarker ? this.selectMarker : this._defaultSelectMarker |
| 75 | + } |
| 76 | + |
| 77 | + @ViewChild('defaultIndicator', { read: TemplateRef, static: true }) |
| 78 | + private _defaultExpandIndicatorTemplate: TemplateRef<any>; |
| 79 | + |
| 80 | + public get expandIndicatorTemplate(): TemplateRef<any> { |
| 81 | + return this.expandIndicator ? this.expandIndicator : this._defaultExpandIndicatorTemplate |
| 82 | + } |
| 83 | + |
| 84 | + public get headerTemplate(): TemplateRef<any> { |
| 85 | + return this.inEdit ? this.nodeEditTemplateRef : this.nodeTemplate |
| 86 | + } |
| 87 | + |
| 88 | + public get selected(): boolean { |
| 89 | + return this.selectionService.get(this.tree.id).has(this.id); |
| 90 | + } |
| 91 | + |
| 92 | + @HostListener('keypress.enter') |
| 93 | + public exitEditMode() { |
| 94 | + if (this.inEdit) { |
| 95 | + this.inEdit = false; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public setEditMode() { |
| 100 | + console.log("Double click"); |
| 101 | + this.inEdit = true; |
| 102 | + } |
| 103 | + |
| 104 | + public toggle(event?: IExpansionPanelCancelableEventArgs): void { |
| 105 | + if (event.event) { |
| 106 | + if (event.event.type === "keydown" && (event.event as KeyboardEvent).code === "Enter") { |
| 107 | + this.exitEditMode() |
| 108 | + } |
| 109 | + const targetContainer = (event.event.target as HTMLElement).parentElement; |
| 110 | + const panelIconContainer = this.expansionPanel.header.iconRef.nativeElement; |
| 111 | + if (targetContainer !== panelIconContainer) { |
| 112 | + event.cancel = true; |
| 113 | + return; |
| 114 | + } |
| 115 | + } |
| 116 | + this.tree.toggleNode(this.id); |
| 117 | + } |
| 118 | + |
| 119 | + public get expanded(): boolean { |
| 120 | + return this.treeService.isExpanded(this.id); |
| 121 | + } |
| 122 | + |
| 123 | + public get templateContext(): any { |
| 124 | + return { |
| 125 | + $implicit: { |
| 126 | + data: this.data, |
| 127 | + id: this.id, |
| 128 | + expanded: this.expanded, |
| 129 | + selected: this.selected, |
| 130 | + parentPath: this.parentPath, |
| 131 | + fullPath: this.fullPath |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public get valueKey(): string { |
| 137 | + return this.tree.valueKey; |
| 138 | + } |
| 139 | + |
| 140 | + public get textKey(): string { |
| 141 | + return this.tree.textKey |
| 142 | + } |
| 143 | + |
| 144 | + public get childKey(): string { |
| 145 | + return this.tree.childKey |
| 146 | + } |
| 147 | + |
| 148 | + public get fullPath(): string[] { |
| 149 | + return [...this.parentPath, this.data[this.valueKey]]; |
| 150 | + } |
| 151 | + |
| 152 | + ngOnInit() { |
| 153 | + console.log(this.tree); |
| 154 | + } |
| 155 | + |
| 156 | + ngAfterViewInit() { |
| 157 | + } |
| 158 | + |
| 159 | + ngOnDestroy() { } |
| 160 | +} |
0 commit comments