/*
* 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: any) {
let NS = 'http://www.w3.org/2000/svg';
let upSvg: any = document.createElementNS(NS, 'svg');
let upPath: any = 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: any = document.createElementNS(NS, 'svg');
let downPath: any = 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) {
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';
head.appendChild(upSvg);
head.appendChild(downSvg);
return {upSvg, downSvg}
}
export function exportData(that: any): void {
if (that.exportLoading || that.ds.length === 0) {
return;
}
that.exportLoading = true;
that.exportProgress!.loading = true;
let date = new Date();
JSONToCSV.csvExport({
columns: that.columns as any[],
tables: that.ds,
fileName: `${date.getTime()}`,
columnFormatter: that.itemTextHandleMap,
exportFormatter: that.exportTextHandleMap,
}).then((res) => {
that.exportLoading = false;
that.exportProgress!.loading = false;
});
}
export function formatExportData(dataSource: any[], that: any): any[] {
if (dataSource === undefined || dataSource.length === 0) {
return [];
}
if (that.columns === undefined) {
return [];
}
return dataSource.map((item) => {
let formatData: any = {};
that.columns!.forEach((column: any) => {
let dataIndex = column.getAttribute('data-index');
let columnName = column.getAttribute('title');
if (columnName === '') {
columnName = dataIndex;
}
if (dataIndex && columnName && item[dataIndex] != undefined) {
formatData[columnName] = item[dataIndex];
}
});
if (item.children !== undefined) {
formatData.children = formatExportData(item.children, that);
}
return formatData;
});
}
export function recursionExportTableData(columns: any[], dataSource: any[]): string {
let concatStr = '\r\n';
dataSource.forEach((item, index) => {
concatStr += columns
.map((column) => {
let dataIndex = column.getAttribute('data-index');
return `"${item[dataIndex] || ''}" `;
})
.join(',');
if (item.children !== undefined) {
concatStr += recursionExportTableData(columns, item.children);
}
if (index !== dataSource.length - 1) {
concatStr += '\r\n';
}
});
return concatStr;
}
export function addCopyEventListener(that: any): void {
that.tableElement?.addEventListener('copy', (e: any) => {
// @ts-ignore
let clipboardData = e.clipboardData || window.clipboardData;
if (!clipboardData) return;
// @ts-ignore
let text = window.getSelection().toString();
if (text) {
e.preventDefault();
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: any): void {
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: any) => {
that.shadowRoot!.querySelectorAll('.row-checkbox').forEach((a: any) => (a.checked = e.detail.checked));
if (e.detail.checked) {
that.shadowRoot!.querySelectorAll('.tr').forEach((a: any) => a.setAttribute('checked', ''));
} else {
that.shadowRoot!.querySelectorAll('.tr').forEach((a: any) => 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: any, that: any): any {
let content = name;
if (that.itemTextHandleMap.has(key)) {
content = that.itemTextHandleMap.get(key)?.(name) || '';
}
if (content !== undefined && content !== null) {
return content.toString().replace(//g, '>');
}
return '';
}