/* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import {Component, EventEmitter, Input, Output} from '@angular/core'; import {DiffType, HierarchyTreeNode, UiTreeNode, UiTreeUtils} from 'viewers/common/ui_tree_utils'; import {nodeInnerItemStyles} from 'viewers/components/styles/node.styles'; @Component({ selector: 'tree-node', template: `
`, styles: [nodeInnerItemStyles], }) export class TreeNodeComponent { @Input() item!: UiTreeNode; @Input() isLeaf?: boolean; @Input() flattened?: boolean; @Input() isCollapsed?: boolean; @Input() hasChildren?: boolean = false; @Input() isPinned?: boolean = false; @Input() isInPinnedSection?: boolean = false; @Input() isAlwaysCollapsed?: boolean; @Output() toggleTreeChange = new EventEmitter(); @Output() expandTreeChange = new EventEmitter(); @Output() pinNodeChange = new EventEmitter(); collapseDiffClass = ''; ngOnChanges() { this.collapseDiffClass = this.updateCollapseDiffClass(); } isPropertiesTreeNode() { return !(this.item instanceof HierarchyTreeNode); } showPinNodeIcon() { return ( (!this.isPropertiesTreeNode() && !UiTreeUtils.isParentNode(this.item.kind ?? '')) ?? false ); } toggleTree(event: MouseEvent) { if (!this.isAlwaysCollapsed) { event.stopPropagation(); this.toggleTreeChange.emit(); } } showChevron() { return !this.isLeaf && !this.flattened && !this.isInPinnedSection; } showLeafNodeIcon() { return !this.showChevron() && !this.isInPinnedSection; } expandTree(event: MouseEvent) { event.stopPropagation(); this.expandTreeChange.emit(); } pinNode(event: MouseEvent) { event.stopPropagation(); this.pinNodeChange.emit(this.item); } updateCollapseDiffClass() { if (this.isCollapsed) { return ''; } const childrenDiffClasses = this.getAllDiffTypesOfChildren(this.item); childrenDiffClasses.delete(DiffType.NONE); childrenDiffClasses.delete(undefined); if (childrenDiffClasses.size === 0) { return ''; } if (childrenDiffClasses.size === 1) { const diffType = childrenDiffClasses.values().next().value; return diffType; } return DiffType.MODIFIED; } private getAllDiffTypesOfChildren(item: UiTreeNode) { if (!item.children) { return new Set(); } const classes = new Set(); for (const child of item.children) { if (child.diffType) { classes.add(child.diffType); } for (const diffClass of this.getAllDiffTypesOfChildren(child)) { classes.add(diffClass); } } return classes; } }