<commit_before>/*
 * exportFile: 数据导出
 */
import jTool from '@common/jTool';
import { showLoading, hideLoading, getVisibleTh, getTbody } from '@common/base';
import { outError, isFunction, jEach } from '@common/utils';
import { getSettings, getCheckedData } from '@common/cache';
import { GM_CREATE, TD_VISIBLE } from '@common/constants';
class ExportFile {
	/**
	 * 获取文件名称
	 * @param gridManagerName
	 * @param fileName: 文件名
	 * @param query: 查询参数
	 * @param exportConfig: 配置信息
     */
    getFileName(gridManagerName, fileName, query, exportConfig) {
        // 未存在指定下载名称时, 使用exportConfig.fileName
		if (!fileName) {
		    const confName = exportConfig.fileName;
		    fileName = isFunction(confName) ? confName(query) : confName;
		}

		// 未存在指定下载名称 且 未指定exportConfig.fileName时, 使用 gridManagerName
		if (!fileName) {
            fileName = gridManagerName;
        }

		return `${fileName}.${exportConfig.suffix}`;
	}

    /**
     * 执行下载
     * @param fileName
     * @param href
     */
    dispatchDownload(fileName, href) {
        const a = document.createElement('a');
        a.addEventListener('click', () => {
            a.download = fileName;
            a.href = href;
        });
        const e = document.createEvent('MouseEvents');
        e.initEvent('click', false, false);
        a.dispatchEvent(e);
    }

	/**
	 * 导出表格 .xls
	 * @param gridManagerName
	 * @param fileName: 导出后的文件名, 该文件名不包含后缀名
	 * @param onlyChecked: 是否只导出已选中的表格
	 * @returns {boolean}
     * @private
     */
	async exportGrid(gridManagerName, fileName, onlyChecked) {
	    const settings = getSettings(gridManagerName);
	    const { query, loadingTemplate, exportConfig, pageData, sortData } = settings;

        fileName = this.getFileName(gridManagerName, fileName, query, exportConfig);

        const selectedList = onlyChecked ? getCheckedData(gridManagerName) : undefined;

        if (!isFunction(exportConfig.handler)) {
            return Promise.reject('exportConfig.handler not return promise');
        }

	    switch (exportConfig.mode) {
            case 'static': {
                this.downStatic(gridManagerName, fileName, onlyChecked, exportConfig.suffix);
                break;
            }
            case 'blob': {
                await this.downBlob(gridManagerName, loadingTemplate, fileName, query, exportConfig.handler, pageData, sortData, selectedList);
                break;
            }
            // TODO 待添加
            // case 'filePath': {
            //     await this.downFilePath(fileName, exportHandler, pageData, sortData, selectedList);
            //     break;
            // }
            //
            // case 'fileStream': {
            //     await this.downFileStream(fileName, exportHandler, pageData, sortData, selectedList);
            //     break;
            // }
        }

		// 成功后返回true
		// return true;
	}

    /**
     * 下载方式: 文件路径
     * @param fileName
     * @param exportHandler
     * @param pageData
     * @param sortData
     * @param selectedList
     * @returns {Promise<void>}
     */
    // async downFilePath(fileName, exportHandler, pageData, sortData, selectedList) {
    //     try {
    //         const res = await exportHandler(fileName, pageData, sortData, selectedList);
    //         this.dispatchDownload(fileName, res);
    //     } catch (e) {
    //         outError(e);
    //     }
    // }

    /**
     * 下载方式: 文件流
     * @param fileName
     * @param exportHandler
     * @param pageData
     * @param sortData
     * @param selectedList
     * @returns {Promise<void>}
     */
    // async downFileStream(fileName, exportHandler, pageData, sortData, selectedList) {
    //     try {
    //         const res = await exportHandler(fileName, pageData, sortData, selectedList);
    //         window.open(res);
    //     } catch (e) {
    //         outError(e);
    //     }
    //
    // }

    /**
     * 下载方式: 静态下载
     * @param gridManagerName
     * @param fileName
     * @param onlyChecked
     * @returns {boolean}
     */
	downStatic(gridManagerName, fileName, onlyChecked, suffix) {
        const thDOM = getVisibleTh(gridManagerName, false);
        const $tbody = getTbody(gridManagerName);
        let	trDOM = null;
        // 验证：是否只导出已选中的表格
        if (onlyChecked) {
            trDOM = jTool('tr[checked="true"]', $tbody);
        } else {
            trDOM = jTool('tr', $tbody);
        }
        // 存储导出的thead
        const thead = [];
        jEach(thDOM, (i, v) => {
            thead.push(`"${v.getElementsByClassName('th-text')[0].textContent || ''}"`);
        });

        // 存储导出的tbody
        let exportHTML = thead.join(',');
        jEach(trDOM, (i, v) => {
            exportHTML += '\r\n';
            const tdDOM = jTool(`td[${GM_CREATE}="false"][${TD_VISIBLE}="visible"]`, v);
            jEach(tdDOM, (i2, v2) => {
                if (i2 !== 0) {
                    exportHTML += ',';
                }<commit_msg>add exportConfig.mode url<commit_after>/*
 * exportFile: 数据导出
 */
import jTool from '@common/jTool';
import { showLoading, hideLoading, getFakeVisibleTh, getTbody } from '@common/base';
import { outError, isFunction, jEach } from '@common/utils';
import { getSettings, getCheckedData } from '@common/cache';
import { GM_CREATE, TD_VISIBLE } from '@common/constants';
class ExportFile {
	/**
	 * 获取文件名称
	 * @param gridManagerName
	 * @param fileName: 文件名
	 * @param query: 查询参数
	 * @param exportConfig: 配置信息
     */
    getFileName(gridManagerName, fileName, query, exportConfig) {
        // 未存在指定下载名称时, 使用exportConfig.fileName
		if (!fileName) {
		    const confName = exportConfig.fileName;
		    fileName = isFunction(confName) ? confName(query) : confName;
		}

		// 未存在指定下载名称 且 未指定exportConfig.fileName时, 使用 gridManagerName
		if (!fileName) {
            fileName = gridManagerName;
        }

		return `${fileName}.${exportConfig.suffix}`;
	}

    /**
     * 执行下载
     * @param fileName
     * @param href
     */
    dispatchDownload(fileName, href) {
        const a = document.createElement('a');
        a.addEventListener('click', () => {
            a.download = fileName;
            a.href = href;
        });
        const e = document.createEvent('MouseEvents');
        e.initEvent('click', false, false);
        a.dispatchEvent(e);
    }

	/**
	 * 导出表格 .xls
	 * @param gridManagerName
	 * @param fileName: 导出后的文件名, 该文件名不包含后缀名
	 * @param onlyChecked: 是否只导出已选中的表格
	 * @returns {boolean}
     * @private
     */
	async exportGrid(gridManagerName, fileName, onlyChecked) {
	    const settings = getSettings(gridManagerName);
	    const { query, loadingTemplate, exportConfig, pageData, sortData } = settings;

        fileName = this.getFileName(gridManagerName, fileName, query, exportConfig);

        const selectedList = onlyChecked ? getCheckedData(gridManagerName) : undefined;

        const handler = exportConfig.handler;
        if (!isFunction(handler)) {
            return Promise.reject('exportConfig.handler not return promise');
        }

	    switch (exportConfig.mode) {
            case 'static': {
                this.downStatic(gridManagerName, fileName, onlyChecked, exportConfig.suffix);
                break;
            }
            case 'blob': {
                await this.downBlob(gridManagerName, loadingTemplate, fileName, query, handler, pageData, sortData, selectedList);
                break;
            }

            case 'url': {
                await this.downFilePath(fileName, handler, pageData, sortData, selectedList);
                break;
            }
        }
	}

    /**
     * 下载方式: 文件路径
     * @param fileName
     * @param exportHandler
     * @param pageData
     * @param sortData
     * @param selectedList
     * @returns {Promise<void>}
     */
    async downFilePath(fileName, exportHandler, pageData, sortData, selectedList) {
        try {
            const res = await exportHandler(fileName, pageData, sortData, selectedList);
            this.dispatchDownload(fileName, res);
        } catch (e) {
            outError(e);
        }
    }

    /**
     * 下载方式: 静态下载
     * @param gridManagerName
     * @param fileName
     * @param onlyChecked
     * @returns {boolean}
     */
	downStatic(gridManagerName, fileName, onlyChecked, suffix) {
        const thDOM = getFakeVisibleTh(gridManagerName, true);
        const $tbody = getTbody(gridManagerName);
        let	trDOM = null;
        // 验证：是否只导出已选中的表格
        if (onlyChecked) {
            trDOM = jTool('tr[checked="true"]', $tbody);
        } else {
            trDOM = jTool('tr', $tbody);
        }
        // 存储导出的thead
        const thead = [];
        jEach(thDOM, (i, v) => {
            thead.push(`"${v.getElementsByClassName('th-text')[0].textContent || ''}"`);
        });

        // 存储导出的tbody
        let exportHTML = thead.join(',');
        jEach(trDOM, (i, v) => {
            exportHTML += '\r\n';
            const tdDOM = jTool(`td:not([${GM_CREATE}])[${TD_VISIBLE}="visible"]`, v);
            jEach(tdDOM, (i2, v2) => {
                if (i2 !== 0) {
                    exportHTML += ',';
                }
{'fraction_non_alphanumeric': 0.1286922147798397, 'fraction_numerical': 0.0010980564401010212, 'mean_word_length': 2.1138461538461537, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7635247', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2893, 'n_words': 573}