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: unknown, isShow: boolean): void { 67 if (isShow) { 68 // @ts-ignore 69 filter.setAttribute('tree', ''); 70 // @ts-ignore 71 filter.setAttribute('input', ''); 72 // @ts-ignore 73 filter.setAttribute('inputLeftText', ''); 74 } else { 75 // @ts-ignore 76 filter.removeAttribute('tree'); 77 // @ts-ignore 78 filter.removeAttribute('input'); 79 // @ts-ignore 80 filter.removeAttribute('inputLeftText'); 81 } 82} 83 84export function compare<T extends CompareStruct>(base: T[], target: T[]): T[] { 85 const diffMap = new Map<string, T>(); 86 87 for (const item of base) { 88 diffMap.set(item.key, item.clone(true) as T); 89 } 90 91 for (const item of target) { 92 if (diffMap.has(item.key)) { 93 const diffItem = diffMap.get(item.key); 94 diffItem!.value = diffItem!.value - item.value; 95 } else { 96 diffMap.set(item.key, item.clone() as T); 97 } 98 } 99 return [...diffMap.values()]; 100} 101 102export class CompareStruct { 103 key: string; 104 value: number; 105 106 constructor(key: string, value: number) { 107 this.key = key; 108 this.value = value; 109 } 110 111 clone(isBase?: boolean): CompareStruct { 112 const value = isBase ? this.value : -this.value; 113 return new CompareStruct(this.key, value); 114 } 115} 116 117export class ParseExpression { 118 private expression: string; //输入的表达式 119 private libTreeMap: Map<string, string[]> = new Map<string, string[]>(); 120 private expressionStruct: NativeMemoryExpression; 121 constructor(expression: string) { 122 this.expressionStruct = new NativeMemoryExpression(); 123 this.expression = expression.trim(); 124 } 125 126 /** 127 * 解析用户输入的表达式 128 * @returns string:sql/ null: 非法表达式 129 */ 130 public parse(): NativeMemoryExpression | null { 131 // 表达式必须以@开头 132 if (!this.expression.startsWith('@')) { 133 return null; 134 } 135 136 const expressions: string[] = []; 137 // 包含- 表示可能有两组表达式 138 if (this.expression.includes('-')) { 139 const multiExpression = this.expression.split('-'); 140 if (multiExpression.length === 0 || multiExpression.length > 2) { 141 return null; 142 } 143 expressions.push(...multiExpression); 144 } else { 145 expressions.push(this.expression); 146 } 147 let include = true; 148 for (let expression of expressions) { 149 this.paseSingleExpression(expression, include); 150 include = false; 151 } 152 return this.expressionStruct; 153 } 154 155 private paseSingleExpression(expression: string, includes: boolean): void { 156 const regex = /\((.*?)\)/; // 匹配括号内的内容 157 const match = expression.match(regex); 158 if (match && match.length > 1) { 159 expression = match[1]; 160 161 const libs = expression.split(','); // 逗号拆分lib 162 for (let lib of libs) { 163 lib = lib.trim(); 164 const items = lib.split(' '); // 空格拆分函数 165 if (items.length > 0) { 166 const path = items[0]; 167 items.splice(0, 1); 168 if (this.libTreeMap.has(path)) { 169 continue; 170 } 171 if (includes) { 172 this.expressionStruct.includeLib.set(`${path}`, items); 173 } else { 174 this.expressionStruct.abandonLib.set(`${path}`, items); 175 } 176 } 177 } 178 } 179 } 180} 181 182export function initSort(table: LitTable, sortKey: string, sortType: number, filterValue?: string): void { 183 sortKey = ''; 184 sortType = 0; 185 if (filterValue) { 186 filterValue = ''; 187 } 188 const theadTable = table!.shadowRoot?.querySelector('.thead'); 189 const thTable = theadTable?.querySelector('.th'); 190 const list = thTable!.querySelectorAll('div'); 191 if (theadTable!.hasAttribute('sort')) { 192 list.forEach((item) => { 193 item.querySelectorAll('svg').forEach((svg): void => { 194 svg.style.display = 'none'; 195 }); 196 }); 197 } 198} 199