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'; 17import { NativeMemoryExpression } from '../../../bean/NativeHook'; 18 19export function resizeObserver( 20 parentEl: HTMLElement, 21 tableEl: LitTable, 22 tblOffsetHeight: number = 50, 23 loadingPage?: HTMLElement, 24 loadingPageOffsetHeight: number = 24 25): void { 26 new ResizeObserver((entries) => { 27 if (parentEl.clientHeight !== 0) { 28 if (tableEl) { 29 // @ts-ignore 30 tableEl.shadowRoot.querySelector('.table').style.height = parentEl.clientHeight - tblOffsetHeight + 'px'; 31 tableEl.reMeauseHeight(); 32 } 33 if (loadingPage) { 34 loadingPage.style.height = parentEl.clientHeight - loadingPageOffsetHeight + 'px'; 35 } 36 } 37 }).observe(parentEl); 38} 39 40export function resizeObserverFromMemory( 41 parentElement: HTMLElement, 42 tableEl: LitTable, 43 filterEl: HTMLElement, 44 tblOffsetHeight: number = 45 45): void { 46 new ResizeObserver((entries) => { 47 let filterHeight = 0; 48 if (parentElement.clientHeight !== 0) { 49 if (tableEl) { 50 // @ts-ignore 51 tableEl.shadowRoot.querySelector('.table').style.height = parentElement.clientHeight - tblOffsetHeight + 'px'; 52 tableEl.reMeauseHeight(); 53 } 54 } 55 if (filterEl!.clientHeight > 0) { 56 filterHeight = filterEl!.clientHeight; 57 } 58 if (parentElement!.clientHeight > filterHeight) { 59 filterEl!.style.display = 'flex'; 60 } else { 61 filterEl!.style.display = 'none'; 62 } 63 }).observe(parentElement); 64} 65 66export function showButtonMenu(filter: any, isShow: boolean): void { 67 if (isShow) { 68 filter.setAttribute('tree', ''); 69 filter.setAttribute('input', ''); 70 filter.setAttribute('inputLeftText', ''); 71 } else { 72 filter.removeAttribute('tree'); 73 filter.removeAttribute('input'); 74 filter.removeAttribute('inputLeftText'); 75 } 76} 77 78export function compare<T extends CompareStruct>(base: T[], target: T[]): T[] { 79 const diffMap = new Map<string, T>(); 80 81 for (const item of base) { 82 diffMap.set(item.key, item.clone(true) as T); 83 } 84 85 for (const item of target) { 86 if (diffMap.has(item.key)) { 87 const diffItem = diffMap.get(item.key); 88 diffItem!.value = diffItem!.value - item.value; 89 } else { 90 diffMap.set(item.key, item.clone() as T); 91 } 92 } 93 return [...diffMap.values()]; 94} 95 96export class CompareStruct { 97 key: string; 98 value: number; 99 100 constructor(key: string, value: number) { 101 this.key = key; 102 this.value = value; 103 } 104 105 clone(isBase?: boolean): CompareStruct { 106 const value = isBase ? this.value : -this.value; 107 return new CompareStruct(this.key, value); 108 } 109} 110 111export class ParseExpression { 112 private expression: string; //输入的表达式 113 private libTreeMap: Map<string, string[]> = new Map<string, string[]>(); 114 private expressionStruct: NativeMemoryExpression; 115 constructor(expression: string) { 116 this.expressionStruct = new NativeMemoryExpression(); 117 this.expression = expression.trim(); 118 } 119 120 /** 121 * 解析用户输入的表达式 122 * @returns string:sql/ null: 非法表达式 123 */ 124 public parse(): NativeMemoryExpression | null { 125 // 表达式必须以@开头 126 if (!this.expression.startsWith('@')) { 127 return null; 128 } 129 130 const expressions: string[] = []; 131 // 包含- 表示可能有两组表达式 132 if (this.expression.includes('-')) { 133 const multiExpression = this.expression.split('-'); 134 if (multiExpression.length === 0 || multiExpression.length > 2) { 135 return null; 136 } 137 expressions.push(...multiExpression); 138 } else { 139 expressions.push(this.expression); 140 } 141 let include = true; 142 for (let expression of expressions) { 143 this.paseSingleExpression(expression, include); 144 include = false; 145 } 146 return this.expressionStruct; 147 } 148 149 private paseSingleExpression(expression: string, includes: boolean): void { 150 const regex = /\((.*?)\)/; // 匹配括号内的内容 151 const match = expression.match(regex); 152 if (match && match.length > 1) { 153 expression = match[1]; 154 155 const libs = expression.split(','); // 逗号拆分lib 156 for (let lib of libs) { 157 lib = lib.trim(); 158 const items = lib.split(' '); // 空格拆分函数 159 if (items.length > 0) { 160 const path = items[0]; 161 items.splice(0, 1); 162 if (this.libTreeMap.has(path)) { 163 continue; 164 } 165 if (includes) { 166 this.expressionStruct.includeLib.set(`${path}`, items); 167 } else { 168 this.expressionStruct.abandonLib.set(`${path}`, items); 169 } 170 } 171 } 172 } 173 } 174} 175 176export function initSort(table: LitTable, sortKey: string, sortType: number, filterValue?: string): void { 177 sortKey = ''; 178 sortType = 0; 179 if (filterValue) { 180 filterValue = ''; 181 } 182 const theadTable = table!.shadowRoot?.querySelector('.thead'); 183 const thTable = theadTable?.querySelector('.th'); 184 const list = thTable!.querySelectorAll('div'); 185 if (theadTable!.hasAttribute('sort')) { 186 list.forEach((item) => { 187 item.querySelectorAll('svg').forEach((svg): void => { 188 svg.style.display = 'none'; 189 }); 190 }); 191 } 192} 193