2022-09-14 11:26:01 +00:00
|
|
|
import { DomHandler } from 'primevue/utils';
|
2023-07-03 22:20:35 +00:00
|
|
|
import BaseRipple from './BaseRipple';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2023-07-03 22:20:35 +00:00
|
|
|
const Ripple = BaseRipple.extend('ripple', {
|
2024-05-08 15:58:22 +00:00
|
|
|
watch: {
|
|
|
|
'config.ripple'(newValue) {
|
|
|
|
if (newValue) {
|
2024-05-15 06:38:43 +00:00
|
|
|
this.createRipple(this.$host);
|
2024-05-08 15:58:22 +00:00
|
|
|
this.bindEvents(this.$host);
|
2023-06-22 13:52:12 +00:00
|
|
|
|
2024-05-08 15:58:22 +00:00
|
|
|
this.$host.setAttribute('data-pd-ripple', true);
|
2024-05-13 11:17:01 +00:00
|
|
|
this.$host.style['overflow'] = 'hidden';
|
|
|
|
this.$host.style['position'] = 'relative';
|
2024-05-08 15:58:22 +00:00
|
|
|
} else {
|
|
|
|
this.remove(this.$host);
|
2024-05-09 05:54:40 +00:00
|
|
|
this.$host.removeAttribute('data-pd-ripple');
|
2024-05-08 15:58:22 +00:00
|
|
|
}
|
2023-10-10 15:51:18 +00:00
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
unmounted(el) {
|
2023-07-03 22:20:35 +00:00
|
|
|
this.remove(el);
|
|
|
|
},
|
|
|
|
timeout: undefined,
|
|
|
|
methods: {
|
|
|
|
bindEvents(el) {
|
|
|
|
el.addEventListener('mousedown', this.onMouseDown.bind(this));
|
|
|
|
},
|
|
|
|
unbindEvents(el) {
|
|
|
|
el.removeEventListener('mousedown', this.onMouseDown.bind(this));
|
|
|
|
},
|
2024-05-15 06:38:43 +00:00
|
|
|
createRipple(el) {
|
2023-07-03 22:20:35 +00:00
|
|
|
const ink = DomHandler.createElement('span', {
|
|
|
|
role: 'presentation',
|
|
|
|
'aria-hidden': true,
|
|
|
|
'data-p-ink': true,
|
|
|
|
'data-p-ink-active': false,
|
2023-10-10 15:37:42 +00:00
|
|
|
class: !this.isUnstyled() && this.cx('root'),
|
2023-10-10 17:31:13 +00:00
|
|
|
onAnimationEnd: this.onAnimationEnd.bind(this),
|
2024-04-01 15:03:24 +00:00
|
|
|
[this.$attrSelector]: '',
|
2023-07-03 22:20:35 +00:00
|
|
|
'p-bind': this.ptm('root')
|
|
|
|
});
|
|
|
|
|
|
|
|
el.appendChild(ink);
|
|
|
|
|
|
|
|
this.$el = ink;
|
|
|
|
},
|
|
|
|
remove(el) {
|
|
|
|
let ink = this.getInk(el);
|
|
|
|
|
|
|
|
if (ink) {
|
2024-05-13 11:17:01 +00:00
|
|
|
this.$host.style['overflow'] = '';
|
|
|
|
this.$host.style['position'] = '';
|
|
|
|
|
2023-07-03 22:20:35 +00:00
|
|
|
this.unbindEvents(el);
|
|
|
|
ink.removeEventListener('animationend', this.onAnimationEnd);
|
|
|
|
ink.remove();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onMouseDown(event) {
|
|
|
|
let target = event.currentTarget;
|
|
|
|
let ink = this.getInk(target);
|
|
|
|
|
|
|
|
if (!ink || getComputedStyle(ink, null).display === 'none') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-10 15:37:42 +00:00
|
|
|
!this.isUnstyled() && DomHandler.removeClass(ink, 'p-ink-active');
|
2023-07-03 22:20:35 +00:00
|
|
|
ink.setAttribute('data-p-ink-active', 'false');
|
|
|
|
|
|
|
|
if (!DomHandler.getHeight(ink) && !DomHandler.getWidth(ink)) {
|
|
|
|
let d = Math.max(DomHandler.getOuterWidth(target), DomHandler.getOuterHeight(target));
|
|
|
|
|
|
|
|
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-10-10 15:37:42 +00:00
|
|
|
!this.isUnstyled() && DomHandler.addClass(ink, 'p-ink-active');
|
2023-07-03 22:20:35 +00:00
|
|
|
ink.setAttribute('data-p-ink-active', 'true');
|
|
|
|
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
if (ink) {
|
2023-10-10 15:37:42 +00:00
|
|
|
!this.isUnstyled() && DomHandler.removeClass(ink, 'p-ink-active');
|
2023-07-03 22:20:35 +00:00
|
|
|
ink.setAttribute('data-p-ink-active', 'false');
|
|
|
|
}
|
|
|
|
}, 401);
|
|
|
|
},
|
|
|
|
onAnimationEnd(event) {
|
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
|
|
|
|
2023-10-10 15:37:42 +00:00
|
|
|
!this.isUnstyled() && DomHandler.removeClass(event.currentTarget, 'p-ink-active');
|
2023-07-03 22:20:35 +00:00
|
|
|
event.currentTarget.setAttribute('data-p-ink-active', 'false');
|
|
|
|
},
|
|
|
|
getInk(el) {
|
2023-07-07 12:00:11 +00:00
|
|
|
return el && el.children ? [...el.children].find((child) => DomHandler.getAttribute(child, 'data-pc-name') === 'ripple') : undefined;
|
2023-07-03 22:20:35 +00:00
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
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;
|