primevue-mirror/components/lib/styleclass/StyleClass.js

179 lines
5.6 KiB
JavaScript
Raw Normal View History

2023-06-22 13:55:50 +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
function bind(el, binding) {
2023-06-22 13:55:50 +00:00
const target = resolveTarget(el, binding);
2023-06-23 09:34:16 +00:00
el.$pEl = target;
2022-09-06 12:03:37 +00:00
2023-06-22 13:55:50 +00:00
el.$_pstyleclass_clicklistener = () => {
2022-09-06 12:03:37 +00:00
if (binding.value.toggleClass) {
2022-09-14 11:26:01 +00:00
if (DomHandler.hasClass(target, binding.value.toggleClass)) DomHandler.removeClass(target, binding.value.toggleClass);
else DomHandler.addClass(target, binding.value.toggleClass);
} else {
if (target.offsetParent === null) enter(target, el, binding);
else leave(target, binding);
2022-09-06 12:03:37 +00:00
}
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
el.addEventListener('click', el.$_pstyleclass_clicklistener);
}
function unbind(el) {
if (el.$_pstyleclass_clicklistener) {
2022-12-08 11:04:25 +00:00
el.removeEventListener('click', el.$_pstyleclass_clicklistener);
2022-09-06 12:03:37 +00:00
el.$_pstyleclass_clicklistener = null;
}
unbindDocumentListener(el);
}
function enter(target, el, binding) {
if (binding.value.enterActiveClass) {
if (!target.$_pstyleclass_animating) {
target.$_pstyleclass_animating = true;
if (binding.value.enterActiveClass === 'slidedown') {
target.style.height = '0px';
DomHandler.removeClass(target, 'hidden');
target.style.maxHeight = target.scrollHeight + 'px';
DomHandler.addClass(target, 'hidden');
target.style.height = '';
}
DomHandler.addClass(target, binding.value.enterActiveClass);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (binding.value.enterClass) {
DomHandler.removeClass(target, binding.value.enterClass);
}
target.$p_styleclass_enterlistener = () => {
DomHandler.removeClass(target, binding.value.enterActiveClass);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (binding.value.enterToClass) {
DomHandler.addClass(target, binding.value.enterToClass);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
target.removeEventListener('animationend', target.$p_styleclass_enterlistener);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (binding.value.enterActiveClass === 'slidedown') {
target.style.maxHeight = '';
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
target.$_pstyleclass_animating = false;
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
target.addEventListener('animationend', target.$p_styleclass_enterlistener);
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
if (binding.value.enterClass) {
DomHandler.removeClass(target, binding.value.enterClass);
}
if (binding.value.enterToClass) {
DomHandler.addClass(target, binding.value.enterToClass);
}
}
if (binding.value.hideOnOutsideClick) {
bindDocumentListener(target, el, binding);
}
}
function leave(target, binding) {
if (binding.value.leaveActiveClass) {
if (!target.$_pstyleclass_animating) {
target.$_pstyleclass_animating = true;
DomHandler.addClass(target, binding.value.leaveActiveClass);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (binding.value.leaveClass) {
DomHandler.removeClass(target, binding.value.leaveClass);
}
target.$p_styleclass_leavelistener = () => {
DomHandler.removeClass(target, binding.value.leaveActiveClass);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (binding.value.leaveToClass) {
DomHandler.addClass(target, binding.value.leaveToClass);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
target.removeEventListener('animationend', target.$p_styleclass_leavelistener);
target.$_pstyleclass_animating = false;
};
target.addEventListener('animationend', target.$p_styleclass_leavelistener);
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
if (binding.value.leaveClass) {
DomHandler.removeClass(target, binding.value.leaveClass);
}
if (binding.value.leaveToClass) {
DomHandler.addClass(target, binding.value.leaveToClass);
}
}
if (binding.value.hideOnOutsideClick) {
unbindDocumentListener(target);
}
}
function resolveTarget(el, binding) {
switch (binding.value.selector) {
case '@next':
return el.nextElementSibling;
case '@prev':
return el.previousElementSibling;
case '@parent':
return el.parentElement;
case '@grandparent':
return el.parentElement.parentElement;
default:
return document.querySelector(binding.value.selector);
}
}
function bindDocumentListener(target, el, binding) {
if (!target.$p_styleclass_documentlistener) {
target.$p_styleclass_documentlistener = (event) => {
if (!isVisible(target) || getComputedStyle(target).getPropertyValue('position') === 'static') {
unbindDocumentListener(target);
2022-09-14 11:26:01 +00:00
} else if (isOutsideClick(event, target, el)) {
2022-09-06 12:03:37 +00:00
leave(target, binding);
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
target.ownerDocument.addEventListener('click', target.$p_styleclass_documentlistener);
}
}
function unbindDocumentListener(target) {
if (target.$p_styleclass_documentlistener) {
target.ownerDocument.removeEventListener('click', target.$p_styleclass_documentlistener);
target.$p_styleclass_documentlistener = null;
}
}
function isVisible(target) {
return target.offsetParent !== null;
}
function isOutsideClick(event, target, el) {
return !el.isSameNode(event.target) && !el.contains(event.target) && !target.contains(event.target);
}
2023-06-22 13:55:50 +00:00
const StyleClass = BaseDirective.extend('styleclass', {
2022-09-06 12:03:37 +00:00
mounted(el, binding) {
bind(el, binding);
},
unmounted(el) {
unbind(el);
}
2023-06-22 13:55:50 +00:00
});
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
export default StyleClass;