@siberiaweb/components
Version:
60 lines (50 loc) • 1.82 kB
text/typescript
/**
* Утилиты для HTML-элемента.
*/
export default class HTMLElementUtils {
/**
* Создание идентификатора. Используется алгоритм для генерации uuid v4.
*/
public static createId(): string {
let id: string = "";
for ( let i: number = 0; i < 32; i++ ) {
if ( ( i === 8 ) || ( i === 12 ) || ( i === 16 ) || ( i === 20 ) ) {
id += "-";
}
let random: number = Math.random() * 16 | 0;
id += ( i === 12 ? 4 : ( i === 16 ? ( random & 3 | 8 ) : random ) ).toString( 16 );
}
return id;
}
/**
* Ожидание завершения анимации.
*
* @param element Элемент.
*/
public static waitForAnimation(
element: HTMLElement
): Promise< void > {
return new Promise<void>( resolve => {
/**
* Обработка завершения анимации.
*
* @param event Событие.
*/
function animationEndEventHandler(
event: AnimationEvent
) {
if ( ( event.target instanceof Node ) && element.isEqualNode( event.target ) ) {
element.removeEventListener( "animationend", animationEndEventHandler );
resolve();
}
}
let animationDuration: string = getComputedStyle( element ).animationDuration;
if ( ( animationDuration.length === 0 ) || ( animationDuration === "0s" ) ) {
resolve();
}
else {
element.addEventListener( "animationend", animationEndEventHandler );
}
} );
}
}