Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.4 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2 years ago
import findUpClassName from '../helpers/dom/findUpClassName';
import sequentialDom from '../helpers/sequentialDom';
import IS_TOUCH_SUPPORTED from '../environment/touchSupport';
import rootScope from '../lib/rootScope';
import findUpAsChild from '../helpers/dom/findUpAsChild';
3 years ago
let rippleClickId = 0;
export default function ripple(
2 years ago
elem: HTMLElement,
callback: (id: number) => Promise<boolean | void> = () => Promise.resolve(),
onEnd: (id: number) => void = null,
prepend = false,
attachListenerTo = elem
) {
2 years ago
// return;
3 years ago
if(elem.querySelector('.c-ripple')) return;
elem.classList.add('rp');
2 years ago
const r = document.createElement('div');
3 years ago
r.classList.add('c-ripple');
const isSquare = elem.classList.contains('rp-square');
if(isSquare) {
r.classList.add('is-square');
}
elem[prepend ? 'prepend' : 'append'](r);
let handler: () => void;
2 years ago
// let animationEndPromise: Promise<number>;
3 years ago
const drawRipple = (clientX: number, clientY: number) => {
const startTime = Date.now();
const elem = document.createElement('div');
const clickId = rippleClickId++;
2 years ago
// console.log('ripple drawRipple');
3 years ago
const duration = +window.getComputedStyle(r).getPropertyValue('--ripple-duration').replace('s', '') * 1000;
2 years ago
// console.log('ripple duration', duration);
3 years ago
handler = () => {
2 years ago
// handler = () => animationEndPromise.then((duration) => {
// console.log('ripple animation was:', duration);
3 years ago
2 years ago
// const duration = isSquare || mediaSizes.isMobile ? 200 : 700;
// return;
const elapsedTime = Date.now() - startTime;
3 years ago
const cb = () => {
2 years ago
// console.log('ripple elapsedTime total pre-remove:', Date.now() - startTime);
3 years ago
sequentialDom.mutate(() => {
elem.remove();
});
2 years ago
3 years ago
if(onEnd) onEnd(clickId);
};
if(elapsedTime < duration) {
2 years ago
const delay = Math.max(duration - elapsedTime, duration / 2);
3 years ago
setTimeout(() => elem.classList.add('hiding'), Math.max(delay - duration / 2, 0));
setTimeout(cb, delay);
} else {
elem.classList.add('hiding');
setTimeout(cb, duration / 2);
}
if(!IS_TOUCH_SUPPORTED) {
3 years ago
window.removeEventListener('contextmenu', handler);
}
handler = null;
touchStartFired = false;
};
2 years ago
// });
3 years ago
callback && callback(clickId);
/* callback().then((bad) => {
if(bad) {
span.remove();
return;
} */
2 years ago
// console.log('ripple after promise', Date.now() - startTime);
// console.log('ripple tooSlow:', tooSlow);
/* if(tooSlow) {
3 years ago
span.remove();
return;
} */
2 years ago
window.requestAnimationFrame(() => {
const rect = r.getBoundingClientRect();
elem.classList.add('c-ripple__circle');
3 years ago
2 years ago
const clickX = clientX - rect.left;
const clickY = clientY - rect.top;
3 years ago
2 years ago
const radius = Math.sqrt((Math.abs(clickY - rect.height / 2) + rect.height / 2) ** 2 + (Math.abs(clickX - rect.width / 2) + rect.width / 2) ** 2);
const size = radius;
3 years ago
2 years ago
// center of circle
const x = clickX - size / 2;
const y = clickY - size / 2;
3 years ago
2 years ago
// console.log('ripple click', offsetFromCenter, size, clickX, clickY);
3 years ago
2 years ago
elem.style.width = elem.style.height = size + 'px';
elem.style.left = x + 'px';
elem.style.top = y + 'px';
3 years ago
2 years ago
// нижний код выполняется с задержкой
/* animationEndPromise = new Promise((resolve) => {
3 years ago
span.addEventListener('animationend', () => {
// 713 -> 700
resolve(((Date.now() - startTime) / 100 | 0) * 100);
}, {once: true});
}); */
2 years ago
// нижний код не всегда включает анимацию ПРИ КЛИКЕ НА ТАЧПАД БЕЗ ТАПТИК ЭНЖИНА
/* span.style.display = 'none';
3 years ago
r.append(span);
duration = +window.getComputedStyle(span).getPropertyValue('animation-duration').replace('s', '') * 1000;
span.style.display = ''; */
2 years ago
r.append(elem);
3 years ago
2 years ago
// r.classList.add('active');
// handler();
});
// });
3 years ago
};
const isRippleUnneeded = (e: Event) => e.target !== elem && (
2 years ago
['BUTTON', 'A'].includes((e.target as HTMLElement).tagName) ||
findUpClassName(e.target as HTMLElement, 'c-ripple') !== r
) && (
attachListenerTo === elem ||
!findUpAsChild(e.target, attachListenerTo)
);
3 years ago
// TODO: rename this variable
let touchStartFired = false;
if(IS_TOUCH_SUPPORTED) {
2 years ago
const touchEnd = () => {
3 years ago
handler && handler();
};
2 years ago
attachListenerTo.addEventListener('touchstart', (e) => {
3 years ago
if(!rootScope.settings.animationsEnabled) {
return;
}
2 years ago
// console.log('ripple touchstart', e);
3 years ago
if(e.touches.length > 1 || touchStartFired || isRippleUnneeded(e)) {
return;
}
2 years ago
// console.log('touchstart', e);
3 years ago
touchStartFired = true;
2 years ago
const {clientX, clientY} = e.touches[0];
3 years ago
drawRipple(clientX, clientY);
attachListenerTo.addEventListener('touchend', touchEnd, {once: true});
2 years ago
3 years ago
window.addEventListener('touchmove', (e) => {
e.cancelBubble = true;
e.stopPropagation();
touchEnd();
attachListenerTo.removeEventListener('touchend', touchEnd);
3 years ago
}, {once: true});
}, {passive: true});
} else {
attachListenerTo.addEventListener('mousedown', (e) => {
3 years ago
if(![0, 2].includes(e.button)) { // only left and right buttons
return;
}
if(!rootScope.settings.animationsEnabled) {
return;
}
2 years ago
// console.log('ripple mousedown', e, e.target, findUpClassName(e.target as HTMLElement, 'c-ripple') === r);
3 years ago
if(attachListenerTo.dataset.ripple === '0' || isRippleUnneeded(e)) {
3 years ago
return;
} else if(touchStartFired) {
touchStartFired = false;
return;
}
2 years ago
const {clientX, clientY} = e;
3 years ago
drawRipple(clientX, clientY);
window.addEventListener('mouseup', handler, {once: true, passive: true});
window.addEventListener('contextmenu', handler, {once: true, passive: true});
}, {passive: true});
}
}