xframelib
Version:
积累的前端开发基础库,来源于项目和服务于项目。
382 lines (381 loc) • 11.8 kB
TypeScript
/**
* 常用H5相关操作
*/
export default class H5Tool {
/**
* HTML元素事件监听
* @param element Window或HTMLElement
* @param type 事件名称
* @param handler 处理函数
*/
static addHandler(element: Window | HTMLElement | any, type: string, handler: Function): void;
/**
* 元素移除事件
* @param element
* @param type
* @param handler
*/
static removeHandler(element: Window | HTMLElement | any, type: string, handler: Function): void;
/**
* 窗体变化事件
* @param handlerFunc 窗体变化处理函数
*/
static windowResizeHandler(handlerFunc: Function): void;
static offWindowResizeHandler(handlerFunc: Function): void;
/**
* 是否支持全屏操作
* @returns true/false
*/
static fullscreenEnabled(): boolean;
/**
* 全屏
* @param el HTML元素
* @param isfull 是否全屏
*/
static fullScreen(element: any | Element, isfull: boolean): void;
/**
* 进入全屏
* @param element 全屏要素容器,默认为整个页面
*/
static requestFullScreen(el?: any | Element): void;
/**
* 退出全屏
*/
static exitFullScreen(): void;
/**
* 获取全屏的容器元素
* @returns 全屏的容器元素
*/
static fullScreenElement(): Element | HTMLElement | Document | null;
/**
* 是否是全屏
* @returns 全屏状态
*/
static isFullScreen(): boolean;
/**
* 监听全屏变化状态
* @param handlerFunc 全屏状态变化的处理函数
*/
static onFullScreenChanged(handlerFunc: (isfullscreen: boolean) => any): void;
/**
* 解决对象Json化循环引用问题
* @param key
* @param value
* @returns
*/
static stringifyCircularHandler(key: string, value: any): any;
/**
* 对象Json化——解决循环引用问题
* @param obj 传入对象参数
* @returns 无循环引用的新对象
*/
static jsonStringify(obj: any): string;
/**
* 去掉JSON循环引用关系
* @param obj 传入对象参数
* @returns 无循环引用的新对象
*/
static jsonParse(obj: any): any;
/**
* 获取input File的对象的数据路径
* @param file File对象
* @returns
*/
static getObjectURL(file: any): string | undefined;
/**
* 获取文件的短MD5摘要
* @param fileObj file对象
* @param handleMd5Result 处理文件MD5结果
*/
static getFileShortMD5(fileObj: File, handleMd5Result: (data: {
isOK: boolean;
data: string;
}) => void): any;
/**
* 获取文件完整的MD5摘要
* @param fileObj
* @param handleMd5Result
*/
static getFileMD5(fileObj: File, handleMd5Result: (data: {
isOK: boolean;
data: string;
}) => void): any;
/**
* 提供MD5值计算
* @param content 字符串
* @param needRawHash 是否需要是二进制格式的结果,默认为false
* @returns MD5值
*/
static MD5(content: string, needRawHash?: boolean): string;
/**
* 复制文本 old
* @param text
*/
static copyTextByCommand(text: string): Promise<unknown>;
/**
* 复制文本,默认使用clipboard
* @param textContent
* @returns
*/
static copyText: (textContent: string) => Promise<unknown>;
static copyElementTextByCommand(id: string): boolean;
/**
* 根据元素ID,复制元素里的内容,默认使用Clipboard
* @param elementID HTML元素ID
* @returns
*/
static copyElementText(elementID: string): boolean | Promise<boolean>;
/**
* 设置是否开启网站灰模式
* @param isGray 是否暗灰模式
*/
static setGrayMode(isGray: boolean): void;
/**
* 开关 HTML元素的Style类名
* @param flag
* @param clsName
* @param target
*/
static toggleClass(flag: boolean, clsName: string, target?: HTMLElement): void;
/**
* 设置 css 变量(全局)
* @param prop
* @param val
* @param dom
*/
static setCssVar(prop: string, val: any, dom?: HTMLElement): void;
/**
* 主动触发Windows的resize事件
*/
static dispatchWindowResize(): void;
/**
* 主动触发HTML元素的事件
* @param element HTML元素
* @param evtName 事件名
* @param evtHandler 事件额外的处理函数
*/
static dispatchElementEvent(element: HTMLElement | string, evtName?: string, evtHandler?: Function): void;
/**
* 生成范围随机数
* @Min 最小值
* @Max 最大值(包括)
*/
static getRandomNum(Min: any, Max: any): number;
/**
* 对象融合
* @param dest 目标对象
* @param sources 要合并进去的来源对象
* @returns 目标对象dest
*/
static merge(dest?: any, sources?: any): any;
/**
* @function setOptions(obj: Object, options: Object): Object
* Merges the given properties to the `options` of the `obj` object, returning the resulting options. See `Class options`.
* @param {*} obj
* @param {*} options
*/
static setOptions(obj: any, options: any): any;
/**
* 格式化小数位数
* @param num
* @param digits
* @returns {number}
*/
static formatNum(num: any, digits: any): number;
/**
* 去除空格
* @param {*} str
*/
static trim(str: any): any;
/**
* 去掉空白,按空格进行分割字符串
* @param {*} str
*/
static splitWords(str: string): any;
/**
* Data URI string containing a base64-encoded empty GIF image.
* Used as a hack to free memory from unused images on WebKit-powered
* mobile devices (by setting image `src` to this string).
* @returns {string}
*/
static emptyImageUrl(): string;
/**
* Creates a debounced function that delays invoking `fn` until after `delay`
* @param fn
* @param delay
* @returns {function(): void}
*/
static debounce(fn: any, delay: any): () => void;
/**
* Creates a throttled function that only invokes `fn` at most once per
* @param fn
* @param delay
* @returns {function(): void}
*/
static throttle(fn: any, delay: any): () => false | undefined;
/**
*
* @param dataUrl
* @returns {Blob}
*/
static dataURLtoBlob(dataUrl: string): Blob | undefined;
/**
* Returns an element given its DOM id, or returns the element itself
* if it was passed directly.
* @param id
* @returns {HTMLElement|*}
*/
static getElement(id: string | HTMLElement): HTMLElement | null;
/**
* Returns the value for a certain style attribute on an element,
* including computed values or values set through CSS.
* @param el
* @param style
* @returns {null|*}
*/
static getStyle(el: HTMLElement, style: string): CSSStyleDeclaration;
/**
* Creates an HTML element with `tagName`, sets its class to `className`, and optionally appends it to `container` element.
* @param tagName
* @param className
* @param container
*/
static create(tagName: string, className: string, container?: HTMLElement): HTMLElement;
/**
* Removes `el` from its parent element
* @param {*} el
*/
static removeElement(el: HTMLElement | any): void;
/**
* Removes all of `el`'s children elements from `el`
* @param {*} el
*/
static emptyElement(el: HTMLElement | any): void;
/**
* Returns `true` if the element's class attribute contains `name`.
* @param {*} el
* @param {*} name
*/
static hasClass(el: HTMLElement | any, name: string): boolean;
/**
* @function Adds `name` to the element's class attribute.
* @param {*} el
* @param {*} name
*/
static addClass(el: HTMLElement | any, name: string): void;
/**
* @function Removes `name` from the element's class attribute.
* @param {*} el
* @param {*} name
*/
static removeClass(el: HTMLElement | any, name: string): void;
/**
* Sets the element's class.
* @param {*} el
* @param {*} name
*/
static setClass(el: HTMLElement | any, name: string): void;
/**
* @function Returns the element's class.
* @param {*} el
*/
static getClass(el: HTMLElement | any): any;
/**
* Creates svg
* @param width
* @param height
* @param path
* @param container
* @returns {SVGElement}
*/
static createSvgElement(width: any, height: any, path: any, container: any): SVGElement;
/**
* Parses string to Dom
* @param domStr
* @param withWrapper
* @param className
* @returns {HTMLDivElement|NodeListOf<ChildNode>}
*/
static parseDom(domStr: string, withWrapper?: boolean, className?: string): HTMLDivElement | NodeListOf<ChildNode>;
/**
* Creates video
* @param url
* @param className
* @param container
* @returns {HTMLElement}
*/
static createVideoHTML(url: string, className: string, container?: HTMLElement): HTMLElement;
/**
* 以Promise方式读前端文件
* @param file 文件或BLob
* @param type 类型'FileBytes'|'ArrayBuffer'|'Text'|'BinaryString'|'DataURL',默认为ArrayBuffer
* @param encoding 文本编码,默认为UTF-8
* @returns
*/
static readFilePromise(file: File | Blob, type?: 'FileBytes' | 'ArrayBuffer' | 'Text' | 'BinaryString' | 'DataURL', encoding?: string): Promise<string | ArrayBuffer>;
/**
* 读取前端文件,为bytes二进制数组(base64编码的)
* @param file
* @returns
*/
static readFileBytes(file: File | Blob): Promise<any>;
/**
* 将链式异步方法(最后一个参数为cb回调函数),改为promise异步方法
* @param asyncFun
* @param args 正常业务参数
* @returns
*/
static promisify(asyncFun: Function, ...args: any[]): () => Promise<unknown>;
/**
* 异步等待多少ms
* @param ms 多少毫秒
* @returns
*/
static wait(ms: number): Promise<unknown>;
/**
* 停止冒泡事件传递
* @param e
*/
static blockEvent(e: any): void;
/**
* 给DIV对象绑定拖拽文件事件
* @param ele id或classname或 HtmlElement对象
* @param onDropFile 接收文件拖拽列表_回调函数
*/
static bindDropFileHanlder(ele: string | Element, onDropFile: (fileList: FileList) => {}): void;
/**
* 获取正在输入或编辑内容的Element
* @returns input等
*/
static getInputElement(): Element | null;
/**
* 将粘贴的文本,保存为文件
* @param content 粘贴文本内容
* @returns
*/
private static guessPasteTextToFileName;
private static pasteEventHanlder;
/**
* 使用document
* 绑定粘贴事件(粘贴字符串、URL或文件)
* 不是任意DIV都可以绑定粘贴
* 如果 div 元素不可编辑,通常情况下是无法触发 paste 事件的。只有设置了 contenteditable="true" 的元素,才会在用户粘贴内容时触发该事件.
* @param onPasteCallback 粘贴的回调函数
*/
static onPasteHandler(onPasteCallback: (pasteContent: string | Array<File> | undefined) => {}): void;
/**
* 移除document粘贴处理事件
*/
static offPasteHandler(): void;
/**
* 动态写入引入js文件
* @param url
* @param initAction
*/
static loadScript(url: string): void;
/**
* 动态加载JS代码片段
* @param jscode JS代码
*/
static loadJsCode(jscode: string): void;
}