primevue-mirror/components/ripple/Ripple.js

97 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-09-14 11:26:01 +00:00
import { DomHandler } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
let timeout;
2022-09-06 12:03:37 +00:00
function bindEvents(el) {
el.addEventListener('mousedown', onMouseDown);
}
function unbindEvents(el) {
el.removeEventListener('mousedown', onMouseDown);
}
function create(el) {
let ink = document.createElement('span');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
ink.className = 'p-ink';
2022-09-14 11:26:01 +00:00
ink.setAttribute('role', 'presentation');
2022-12-08 11:04:25 +00:00
ink.setAttribute('aria-hidden', 'true');
2022-09-06 12:03:37 +00:00
el.appendChild(ink);
ink.addEventListener('animationend', onAnimationEnd);
}
function remove(el) {
let ink = getInk(el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (ink) {
unbindEvents(el);
ink.removeEventListener('animationend', onAnimationEnd);
ink.remove();
}
}
function onMouseDown(event) {
let target = event.currentTarget;
let ink = getInk(target);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (!ink || getComputedStyle(ink, null).display === 'none') {
return;
}
DomHandler.removeClass(ink, 'p-ink-active');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (!DomHandler.getHeight(ink) && !DomHandler.getWidth(ink)) {
let d = Math.max(DomHandler.getOuterWidth(target), DomHandler.getOuterHeight(target));
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
ink.style.height = d + 'px';
ink.style.width = d + 'px';
}
let offset = DomHandler.getOffset(target);
let x = event.pageX - offset.left + document.body.scrollTop - DomHandler.getWidth(ink) / 2;
let y = event.pageY - offset.top + document.body.scrollLeft - DomHandler.getHeight(ink) / 2;
ink.style.top = y + 'px';
ink.style.left = x + 'px';
DomHandler.addClass(ink, 'p-ink-active');
2022-12-08 11:04:25 +00:00
timeout = setTimeout(() => {
if (ink) {
DomHandler.removeClass(ink, 'p-ink-active');
}
}, 401);
2022-09-06 12:03:37 +00:00
}
function onAnimationEnd(event) {
2022-12-08 11:04:25 +00:00
if (timeout) {
clearTimeout(timeout);
}
2022-09-06 12:03:37 +00:00
DomHandler.removeClass(event.currentTarget, 'p-ink-active');
}
function getInk(el) {
for (let i = 0; i < el.children.length; i++) {
if (typeof el.children[i].className === 'string' && el.children[i].className.indexOf('p-ink') !== -1) {
return el.children[i];
}
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return null;
}
const Ripple = {
mounted(el, binding) {
if (binding.instance.$primevue && binding.instance.$primevue.config && binding.instance.$primevue.config.ripple) {
create(el);
bindEvents(el);
}
},
unmounted(el) {
remove(el);
}
};
2022-09-14 11:26:01 +00:00
export default Ripple;