1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { LitTable } from '../../../../base-ui/table/lit-table.js'; 17 18export function resizeObserver( 19 parentEl: HTMLElement, 20 tableEl: LitTable, 21 tblOffsetHeight: number = 45, 22 loadingPage?: HTMLElement, 23 loadingPageOffsetHeight: number = 24 24) { 25 new ResizeObserver((entries) => { 26 if (parentEl.clientHeight != 0) { 27 // @ts-ignore 28 tableEl?.shadowRoot.querySelector('.table').style.height = parentEl.clientHeight - tblOffsetHeight + 'px'; 29 tableEl?.reMeauseHeight(); 30 if (loadingPage) { 31 loadingPage.style.height = parentEl.clientHeight - loadingPageOffsetHeight + 'px'; 32 } 33 } 34 }).observe(parentEl); 35} 36 37export function resizeObserverFromMemory( 38 parentElement: HTMLElement, 39 tableEl: LitTable, 40 filterEl: HTMLElement, 41 tblOffsetHeight: number = 45 42) { 43 new ResizeObserver((entries) => { 44 let filterHeight = 0; 45 if (parentElement.clientHeight != 0) { 46 // @ts-ignore 47 tableEl?.shadowRoot.querySelector('.table').style.height = parentElement.clientHeight - tblOffsetHeight + 'px'; 48 tableEl?.reMeauseHeight(); 49 if (filterEl!.clientHeight > 0) { 50 filterHeight = filterEl!.clientHeight; 51 } 52 if (parentElement!.clientHeight > filterHeight) { 53 filterEl!.style.display = 'flex'; 54 } else { 55 filterEl!.style.display = 'none'; 56 } 57 } 58 }).observe(parentElement); 59} 60 61export function showButtonMenu(filter: any, isShow: boolean) { 62 if (isShow) { 63 filter.setAttribute('tree', ''); 64 filter.setAttribute('input', ''); 65 filter.setAttribute('inputLeftText', ''); 66 } else { 67 filter.removeAttribute('tree'); 68 filter.removeAttribute('input'); 69 filter.removeAttribute('inputLeftText'); 70 } 71} 72 73export function compare<T extends CompareStruct>(base: T[], target: T[]): T[] { 74 const diffMap = new Map<string, T>(); 75 76 for (const item of base) { 77 diffMap.set(item.key, item.clone(true) as T); 78 } 79 80 for (const item of target) { 81 if (diffMap.has(item.key)) { 82 const diffItem = diffMap.get(item.key); 83 diffItem!.value = diffItem!.value - item.value; 84 } else { 85 diffMap.set(item.key, item.clone() as T); 86 } 87 } 88 return [...diffMap.values()]; 89} 90 91export class CompareStruct { 92 key: string; 93 value: number; 94 95 constructor(key: string, value: number) { 96 this.key = key; 97 this.value = value; 98 } 99 100 clone(isBase?: boolean): CompareStruct { 101 const value = isBase ? this.value : -this.value; 102 return new CompareStruct(this.key, value); 103 } 104} 105