分离部分工具函数

This commit is contained in:
Harvey Zack committed 2018-11-23 09:45:36 +08:00
1 parent d6e35781ca
commit 748ce304e6
15 files changed
+244 -417

No files matched your search

+3 -81
View File
@@ -1,21 +1,6 @@
export const instances = [];
export class ArtPlayerError extends Error {
constructor(message, context) {
super(message);
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, context || this.constructor);
}
this.name = 'ArtPlayerError';
}
}
export function errorHandle(condition, msg) {
const isFun = typeof condition === 'function';
if (isFun ? !isFun() : !condition) {
throw new ArtPlayerError(msg);
}
}
export * from './error';
export * from './dom';
export * from './subtitle';
export function clamp(num, a, b) {
return Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
@@ -37,44 +22,6 @@ export function getExt(url) {
.pop();
}
export function append(parent, child) {
if (child instanceof Element) {
parent.appendChild(child);
} else {
parent.insertAdjacentHTML('beforeend', child);
}
return parent.lastElementChild;
}
export function insertByIndex(parent, child, index) {
const childs = Array.from(parent.children);
child.dataset.index = index;
const nextChild = childs.find(item => Number(item.dataset.index) >= Number(index));
if (nextChild) {
nextChild.insertAdjacentElement('beforebegin', child);
} else {
append(parent, child);
}
return child;
}
export function setStyle(element, key, value) {
element.style[key] = value;
return element;
}
export function setStyles(element, styles) {
Object.keys(styles).forEach(key => {
setStyle(element, key, styles[key]);
});
return element;
}
export function getStyle(element, key, numberType = true) {
const value = window.getComputedStyle(element, null).getPropertyValue(key);
return numberType ? parseFloat(value) : value;
}
export function secondToTime(second) {
const add0 = num => (num < 10 ? `0${num}` : String(num));
const hour = Math.floor(second / 3600);
@@ -83,24 +30,10 @@ export function secondToTime(second) {
return (hour > 0 ? [hour, min, sec] : [min, sec]).map(add0).join(':');
}
export function tooltip(target, msg, pos = 'up') {
target.setAttribute('data-balloon', msg);
target.setAttribute('data-balloon-pos', pos);
}
export function sleep(ms = 0) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export function sublings(target) {
return Array.from(target.parentElement.children).filter(item => item !== target);
}
export function inverseClass(target, className) {
sublings(target).forEach(item => item.classList.remove(className));
target.classList.add(className);
}
export function debounce(func, wait, context) {
let timeout;
function fn(...args) {
@@ -118,14 +51,3 @@ export function debounce(func, wait, context) {
return fn;
}
export function show(target, display) {
target.classList.remove('art-hide');
if (display) {
target.style.display = display;
}
}
export function hide(target) {
target.classList.add('art-hide');
}