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

174 lines
6.6 KiB
JavaScript
Raw Normal View History

2022-09-14 11:26:01 +00:00
import { DomHandler } from 'primevue/utils';
import BaseStyleClass from './BaseStyleClass';
2022-09-06 12:03:37 +00:00
const StyleClass = BaseStyleClass.extend('styleclass', {
mounted(el, binding) {
2023-07-07 12:00:11 +00:00
el.setAttribute('data-pd-styleclass', true);
this.bind(el, binding);
},
unmounted(el) {
this.unbind(el);
},
methods: {
bind(el, binding) {
const target = this.resolveTarget(el, binding);
this.$el = target;
el.$_pstyleclass_clicklistener = () => {
if (binding.value.toggleClass) {
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) this.enter(target, el, binding);
else this.leave(target, binding);
}
};
2023-06-22 13:55:50 +00:00
el.addEventListener('click', el.$_pstyleclass_clicklistener);
},
unbind(el) {
if (el.$_pstyleclass_clicklistener) {
el.removeEventListener('click', el.$_pstyleclass_clicklistener);
el.$_pstyleclass_clicklistener = null;
}
2022-09-06 12:03:37 +00:00
this.unbindDocumentListener(el);
},
enter(target, el, binding) {
if (binding.value.enterActiveClass) {
if (!target.$_pstyleclass_animating) {
target.$_pstyleclass_animating = true;
2022-09-14 11:26:01 +00:00
2024-05-20 11:17:48 +00:00
if (binding.value.enterActiveClass.includes('slidedown')) {
target.style.height = '0px';
DomHandler.removeClass(target, 'hidden');
target.style.maxHeight = target.scrollHeight + 'px';
DomHandler.addClass(target, 'hidden');
target.style.height = '';
}
2022-09-06 12:03:37 +00:00
DomHandler.addClass(target, binding.value.enterActiveClass);
2022-09-06 12:03:37 +00:00
if (binding.value.enterFromClass) {
DomHandler.removeClass(target, binding.value.enterFromClass);
}
target.$p_styleclass_enterlistener = () => {
DomHandler.removeClass(target, binding.value.enterActiveClass);
2022-09-06 12:03:37 +00:00
if (binding.value.enterToClass) {
DomHandler.addClass(target, binding.value.enterToClass);
}
2022-09-06 12:03:37 +00:00
target.removeEventListener('animationend', target.$p_styleclass_enterlistener);
2022-09-14 11:26:01 +00:00
2024-05-20 11:17:48 +00:00
if (binding.value.enterActiveClass.includes('slidedown')) {
target.style.maxHeight = '';
}
2022-09-06 12:03:37 +00:00
target.$_pstyleclass_animating = false;
};
2022-09-14 11:26:01 +00:00
target.addEventListener('animationend', target.$p_styleclass_enterlistener);
2022-09-06 12:03:37 +00:00
}
} else {
if (binding.value.enterFromClass) {
DomHandler.removeClass(target, binding.value.enterFromClass);
}
if (binding.value.enterToClass) {
DomHandler.addClass(target, binding.value.enterToClass);
}
}
2022-09-14 11:26:01 +00:00
if (binding.value.hideOnOutsideClick) {
this.bindDocumentListener(target, el, binding);
}
},
leave(target, binding) {
if (binding.value.leaveActiveClass) {
if (!target.$_pstyleclass_animating) {
target.$_pstyleclass_animating = true;
DomHandler.addClass(target, binding.value.leaveActiveClass);
2022-09-06 12:03:37 +00:00
if (binding.value.leaveFromClass) {
DomHandler.removeClass(target, binding.value.leaveFromClass);
}
target.$p_styleclass_leavelistener = () => {
DomHandler.removeClass(target, binding.value.leaveActiveClass);
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
target.removeEventListener('animationend', target.$p_styleclass_leavelistener);
target.$_pstyleclass_animating = false;
};
2022-09-06 12:03:37 +00:00
target.addEventListener('animationend', target.$p_styleclass_leavelistener);
}
} else {
if (binding.value.leaveFromClass) {
DomHandler.removeClass(target, binding.value.leaveFromClass);
}
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
if (binding.value.hideOnOutsideClick) {
this.unbindDocumentListener(target);
}
},
resolveTarget(el, binding) {
switch (binding.value.selector) {
case '@next':
return el.nextElementSibling;
2022-09-06 12:03:37 +00:00
case '@prev':
return el.previousElementSibling;
2022-09-06 12:03:37 +00:00
case '@parent':
return el.parentElement;
2022-09-06 12:03:37 +00:00
case '@grandparent':
return el.parentElement.parentElement;
2022-09-06 12:03:37 +00:00
default:
return document.querySelector(binding.value.selector);
}
},
bindDocumentListener(target, el, binding) {
if (!target.$p_styleclass_documentlistener) {
target.$p_styleclass_documentlistener = (event) => {
if (!this.isVisible(target) || getComputedStyle(target).getPropertyValue('position') === 'static') {
this.unbindDocumentListener(target);
2023-07-07 07:45:25 +00:00
} else if (this.isOutsideClick(event, target, el)) {
this.leave(target, binding);
}
};
target.ownerDocument.addEventListener('click', target.$p_styleclass_documentlistener);
}
},
unbindDocumentListener(target) {
if (target.$p_styleclass_documentlistener) {
target.ownerDocument.removeEventListener('click', target.$p_styleclass_documentlistener);
target.$p_styleclass_documentlistener = null;
}
},
isVisible(target) {
return target.offsetParent !== null;
},
isOutsideClick(event, target, el) {
return !el.isSameNode(event.target) && !el.contains(event.target) && !target.contains(event.target);
}
2022-09-06 12:03:37 +00:00
}
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;