/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* 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 { JSONToCSV } from '../utils/CSVFormater';
export const iconWidth = 20;
export const iconPadding = 5;
export const litPageTableHtml = `
`;
export const litTableHtml = `
`;
export function createDownUpSvg(index: number, head: unknown): { upSvg: SVGSVGElement; downSvg: SVGSVGElement } {
let NS = 'http://www.w3.org/2000/svg';
let upSvg: SVGSVGElement = document.createElementNS(NS, 'svg') as SVGSVGElement;
let upPath: Element = document.createElementNS(NS, 'path');
upSvg.setAttribute('fill', 'let(--dark-color1,#212121)');
upSvg.setAttribute('viewBox', '0 0 1024 1024');
upSvg.setAttribute('stroke', 'let(--dark-color1,#212121)');
upSvg.classList.add('up-svg');
upPath.setAttribute(
'd',
'M858.9 689L530.5 308.2c-9.4-10.9-27.5-10.9-37 0L165.1 689c-12.2 14.2-1.2 35 18.5 35h656.8c19.7 0 30.7-20.8 18.5-35z'
);
upSvg.appendChild(upPath);
let downSvg: SVGSVGElement = document.createElementNS(NS, 'svg') as SVGSVGElement;
let downPath: Element = document.createElementNS(NS, 'path');
downSvg.setAttribute('fill', 'let(--dark-color1,#212121)');
downSvg.setAttribute('viewBox', '0 0 1024 1024');
downSvg.setAttribute('stroke', 'let(--dark-color1,#212121)');
downSvg.classList.add('down-svg');
downPath.setAttribute(
'd',
'M840.4 300H183.6c-19.7 0-30.7 20.8-18.5 35l328.4 380.8c9.4 10.9 27.5 10.9 37 0L858.9 335c12.2-14.2 1.2-35-18.5-35z'
);
downSvg.appendChild(downPath);
if (index === 0) {
//@ts-ignore
head.sortType = 0; // 默认以第一列 降序排序 作为默认排序
upSvg.setAttribute('fill', 'let(--dark-color1,#212121)');
downSvg.setAttribute('fill', 'let(--dark-color1,#212121)');
}
upSvg.style.display = 'none';
downSvg.style.display = 'none'; //@ts-ignore
head.appendChild(upSvg); //@ts-ignore
head.appendChild(downSvg);
return { upSvg, downSvg };
}
export function exportData(that: unknown): void {
//@ts-ignore
if (that.exportLoading || that.ds.length === 0) {
return;
} //@ts-ignore
that.exportLoading = true; //@ts-ignore
that.exportProgress!.loading = true;
let date = new Date();
JSONToCSV.csvExport({
//@ts-ignore
columns: that.columns as unknown[], //@ts-ignore
tables: that.ds,
fileName: `${date.getTime()}`, //@ts-ignore
columnFormatter: that.itemTextHandleMap, //@ts-ignore
exportFormatter: that.exportTextHandleMap,
}).then((res) => {
//@ts-ignore
that.exportLoading = false; //@ts-ignore
that.exportProgress!.loading = false;
});
}
export function formatExportData(dataSource: unknown[], that: unknown): unknown[] {
if (dataSource === undefined || dataSource.length === 0) {
return [];
} //@ts-ignore
if (that.columns === undefined) {
return [];
}
return dataSource.map((item) => {
let formatData: unknown = {}; //@ts-ignore
that.columns!.forEach((column: unknown) => {
//@ts-ignore
let dataIndex = column.getAttribute('data-index'); //@ts-ignore
let columnName = column.getAttribute('title');
if (columnName === '') {
columnName = dataIndex;
} //@ts-ignore
if (dataIndex && columnName && item[dataIndex] !== undefined) {
//@ts-ignore
formatData[columnName] = item[dataIndex];
}
}); //@ts-ignore
if (item.children !== undefined) {
//@ts-ignore
formatData.children = formatExportData(item.children, that);
}
return formatData;
});
}
export function recursionExportTableData(columns: unknown[], dataSource: unknown[]): string {
let concatStr = '\r\n';
dataSource.forEach((item, index) => {
concatStr += columns
.map((column) => {
//@ts-ignore
let dataIndex = column.getAttribute('data-index'); //@ts-ignore
return `"${item[dataIndex] || ''}" `;
})
.join(','); //@ts-ignore
if (item.children !== undefined) {
//@ts-ignore
concatStr += recursionExportTableData(columns, item.children);
}
if (index !== dataSource.length - 1) {
concatStr += '\r\n';
}
});
return concatStr;
}
export function addCopyEventListener(that: unknown): void {
//@ts-ignore
that.tableElement?.addEventListener('copy', (e: unknown) => {
// @ts-ignore
let clipboardData = e.clipboardData || window.clipboardData;
if (!clipboardData) {
return;
}
// @ts-ignore
let text = window.getSelection().toString();
if (text) {
//@ts-ignore
e.preventDefault(); //@ts-ignore
let length = that.tableColumns?.length || 1;
let strings = text.split('\n');
let formatStr = '';
for (let i = 0; i < strings.length; i++) {
if (i % length !== 0) {
formatStr += ' ';
}
formatStr += strings[i];
if (i !== 0 && i % length === length - 1) {
formatStr += '\n';
}
}
clipboardData.setData('text/plain', formatStr);
}
});
}
export function addSelectAllBox(rowElement: HTMLDivElement, that: unknown): void {
//@ts-ignore
if (that.selectable) {
let box = document.createElement('div');
box.style.display = 'flex';
box.style.justifyContent = 'center';
box.style.alignItems = 'center';
box.style.gridArea = '_checkbox_';
box.classList.add('td');
box.style.backgroundColor = '#ffffff66';
let checkbox = document.createElement('lit-checkbox');
checkbox.classList.add('row-checkbox-all');
checkbox.onchange = (e: unknown): void => {
//@ts-ignore
that.shadowRoot!.querySelectorAll('.row-checkbox').forEach((a: unknown) => (a.checked = e.detail.checked)); //@ts-ignore
if (e.detail.checked) {
//@ts-ignore
that.shadowRoot!.querySelectorAll('.tr').forEach((a: unknown) => a.setAttribute('checked', ''));
} else {
//@ts-ignore
that.shadowRoot!.querySelectorAll('.tr').forEach((a: unknown) => a.removeAttribute('checked'));
}
};
box.appendChild(checkbox);
rowElement.appendChild(box);
}
}
export function fixed(td: HTMLElement, placement: string, bgColor: string): void {
td.style.position = 'sticky';
if (placement === 'left') {
td.style.left = '0px';
td.style.boxShadow = '3px 0px 5px #33333333';
} else if (placement === 'right') {
td.style.right = '0px';
td.style.boxShadow = '-3px 0px 5px #33333333';
}
}
export function formatName(key: string, name: unknown, that: unknown): unknown {
let content = name; //@ts-ignore
if (that.itemTextHandleMap.has(key)) {
//@ts-ignore
content = that.itemTextHandleMap.get(key)?.(name) || '';
}
if (content !== undefined && content !== null) {
return content.toString().replace(//g, '>');
}
return '';
}