import UUID from "uuidjs"; import { DATA_UID } from "../constant/context"; /** * 根据设备类型生成设备序列号:全局唯一 * @param type */ export function genSerialNumber(): string { return UUID.generate(); } /** * 动态加载script * @param url * @param resolve */ export function insertScript(url: string, resolve: Function) { let head = document.getElementsByTagName("head")[0]; if (!head) { throw Error("页面无head标签"); } let script = document.createElement("script"); script.type = "text/javascript"; script.charset = "utf-8"; script.async = true; script.src = url; script.onload = () => resolve(); head.appendChild(script); } /** * 生成样式字符串数组 * @param style */ export function genStyleList(style: Record): string[] { return Object.keys(style || {}).map((key) => { return `${key}: ${style[key]}`; }); } /** * 通过 * @param uid */ export function getComponentByUid( uid: string, list: T[] ): T | undefined { let find = (list: T[] | undefined): T | undefined => { if (!list || list.length === 0) { return undefined; } for (let i = list.length - 1; i >= 0; i--) { if (list[i].$options.uid === uid) { return list[i]; } let component = find(list[i].$children); if (component) { return component; } } }; return find(list); } /** * 通过html节点获取最近的自定义组件实例 * @param node * @param list */ export function getNearestComponentByHtmlElement(node: HTMLElement, list: T[]): T | undefined { let getUid = (node: HTMLElement | null): string | null => { if (!node) { return null; } let uid = node.getAttribute(DATA_UID); if (uid) { return uid; } return getUid(node.parentElement); }; let uid = getUid(node); if (!uid) { return; } else { return getComponentByUid(uid, list); } }