2023-06-22 13:52:12 +00:00
|
|
|
import { BaseDirective } from 'primevue/basedirective';
|
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);
|
|
|
|
}
|
|
|
|
|
2023-06-22 13:52:12 +00:00
|
|
|
function create(el, binding) {
|
2022-09-06 12:03:37 +00:00
|
|
|
let ink = document.createElement('span');
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2023-06-22 13:52:12 +00:00
|
|
|
!el.$_prippleUnstyled && (ink.className = 'p-ink');
|
|
|
|
ink.setAttribute('data-pc-name', 'ripple');
|
|
|
|
ink.setAttribute('data-pc-section', 'root');
|
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');
|
2023-05-19 10:32:44 +00:00
|
|
|
ink.setAttribute('data-p-ink', 'true');
|
2023-06-22 13:52:12 +00:00
|
|
|
ink.setAttribute('data-p-ink-active', 'false');
|
2022-09-06 12:03:37 +00:00
|
|
|
el.appendChild(ink);
|
2023-06-23 09:34:16 +00:00
|
|
|
el.$pEl = ink;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
ink.addEventListener('animationend', onAnimationEnd);
|
2023-06-22 13:52:12 +00:00
|
|
|
|
|
|
|
BaseDirective.handleCSS('ripple', ink, binding);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-22 13:52:12 +00:00
|
|
|
!target.$_prippleUnstyled && DomHandler.removeClass(ink, 'p-ink-active');
|
|
|
|
ink.setAttribute('data-p-ink-active', 'false');
|
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';
|
2023-06-22 13:52:12 +00:00
|
|
|
|
|
|
|
!target.$_prippleUnstyled && DomHandler.addClass(ink, 'p-ink-active');
|
|
|
|
ink.setAttribute('data-p-ink-active', 'true');
|
2022-12-08 11:04:25 +00:00
|
|
|
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
if (ink) {
|
2023-06-22 13:52:12 +00:00
|
|
|
!target.$_prippleUnstyled && DomHandler.removeClass(ink, 'p-ink-active');
|
|
|
|
ink.setAttribute('data-p-ink-active', 'false');
|
2022-12-08 11:04:25 +00:00
|
|
|
}
|
|
|
|
}, 401);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onAnimationEnd(event) {
|
2022-12-08 11:04:25 +00:00
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
2023-06-22 13:52:12 +00:00
|
|
|
!event.currentTarget.$_prippleUnstyled && DomHandler.removeClass(event.currentTarget, 'p-ink-active');
|
|
|
|
event.currentTarget.setAttribute('data-p-ink-active', 'false');
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getInk(el) {
|
|
|
|
for (let i = 0; i < el.children.length; i++) {
|
2023-06-22 13:52:12 +00:00
|
|
|
if (el.children[i].getAttribute('data-pc-name') === 'ripple') {
|
2022-09-06 12:03:37 +00:00
|
|
|
return el.children[i];
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-06-22 13:52:12 +00:00
|
|
|
const Ripple = BaseDirective.extend('ripple', {
|
2022-09-06 12:03:37 +00:00
|
|
|
mounted(el, binding) {
|
2023-06-22 13:52:12 +00:00
|
|
|
const primevue = binding.instance.$primevue;
|
|
|
|
|
|
|
|
if (primevue && primevue.config && primevue.config.ripple) {
|
|
|
|
el.$_prippleUnstyled = primevue.config.unstyled || false;
|
|
|
|
|
|
|
|
create(el, binding);
|
2022-09-06 12:03:37 +00:00
|
|
|
bindEvents(el);
|
|
|
|
}
|
|
|
|
},
|
2023-06-22 13:52:12 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
unmounted(el) {
|
|
|
|
remove(el);
|
|
|
|
}
|
2023-06-22 13:52:12 +00:00
|
|
|
});
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
export default Ripple;
|