const prefix = 'tsp-component-Toast'; // 用于hide方法 const componentFlag = 'tsp-component-Toast-flag'; function render(templelte: string, key: number, duration: number, onClose?: () => void): void { const noTouch = function(event: any): void { event.preventDefault(); }; document.body.insertAdjacentHTML('beforeend', templelte); document.getElementById(key.toString()).addEventListener('touchmove', noTouch, false); setTimeout(() => { const el = document.getElementById(key.toString()); el.removeEventListener('touchmove', noTouch, false); el.parentNode.removeChild(el); }, duration); } class Toast { /** * 加载图标的src */ public static loadingSrc: string; /** * 无图标,只有文本 */ public static text(content: string, duration: number = 3000): void { const key = new Date().getTime(); const templelte = `
${content}
`; render(templelte, key, duration); } /** * 成功 */ public static success(content: string, duration: number = 3000): void { const key = new Date().getTime(); // tslint:disable-next-line:max-line-length const templelte = `
${content}
`; render(templelte, key, duration); } /** * 失败 */ public static fail(content: string, duration: number = 3000): void { const key = new Date().getTime(); // tslint:disable-next-line:max-line-length const templelte = `
${content}
`; render(templelte, key, duration); } /** * 警告 */ public static warning(content: string, duration: number = 3000): void { const key = new Date().getTime(); // tslint:disable-next-line:max-line-length const templelte = `
${content}
`; render(templelte, key, duration); } /** * 加载 */ public static loading(content?: string, duration?: number, onClose?: () => void): void { const key = new Date().getTime(); const templelte = `
${content ? content : '请稍等'}
`; render(templelte, key, duration ? duration : 15000); } /** * 隐藏 */ public static hide(selector?: string): void { const el = document.querySelectorAll(`.${selector ? selector : componentFlag}`); const length = el.length; let i; for (i = 0; i < length; i++) { el[i]['style'].display = 'none'; } } } export default Toast;