2023-06-22 13:50:40 +00:00
|
|
|
import { BaseDirective } from 'primevue/basedirective';
|
2022-12-21 12:48:42 +00:00
|
|
|
import { DomHandler, UniqueComponentId } from 'primevue/utils';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2023-06-22 13:50:40 +00:00
|
|
|
const BadgeDirective = BaseDirective.extend('badge', {
|
|
|
|
mounted(el, binding) {
|
2022-09-06 12:03:37 +00:00
|
|
|
const id = UniqueComponentId() + '_badge';
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
el.$_pbadgeId = id;
|
2023-06-22 13:50:40 +00:00
|
|
|
el.$_pbadgeUnstyled = binding.instance.$primevue.config.unstyled || false;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
let badge = document.createElement('span');
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
badge.id = id;
|
2023-06-22 13:50:40 +00:00
|
|
|
!el.$_pbadgeUnstyled && (badge.className = 'p-badge p-component');
|
|
|
|
badge.setAttribute('data-pc-name', 'badge');
|
|
|
|
badge.setAttribute('data-pc-section', 'root');
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2023-06-22 13:50:40 +00:00
|
|
|
for (let modifier in binding.modifiers) {
|
|
|
|
!el.$_pbadgeUnstyled && DomHandler.addClass(badge, 'p-badge-' + modifier);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2023-06-22 13:50:40 +00:00
|
|
|
if (binding.value != null) {
|
|
|
|
if (typeof binding.value === 'object') el.$_badgeValue = binding.value.value;
|
|
|
|
else el.$_badgeValue = binding.value;
|
|
|
|
badge.appendChild(document.createTextNode(el.$_badgeValue));
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2023-06-22 13:50:40 +00:00
|
|
|
if (String(el.$_badgeValue).length === 1 && !el.$_pbadgeUnstyled) {
|
|
|
|
!el.$_pbadgeUnstyled && DomHandler.addClass(badge, 'p-badge-no-gutter');
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
2023-06-22 13:50:40 +00:00
|
|
|
!el.$_pbadgeUnstyled && DomHandler.addClass(badge, 'p-badge-dot');
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 13:50:40 +00:00
|
|
|
!el.$_pbadgeUnstyled && DomHandler.addClass(el, 'p-overlay-badge');
|
|
|
|
el.setAttribute('data-p-overlay-badge', 'true');
|
2022-09-06 12:03:37 +00:00
|
|
|
el.appendChild(badge);
|
2023-06-22 13:50:40 +00:00
|
|
|
el.$_pDirectiveElement = badge;
|
|
|
|
|
|
|
|
BaseDirective.directiveElement = badge;
|
|
|
|
BaseDirective.handleCSS('badge', el, binding);
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2023-06-22 13:50:40 +00:00
|
|
|
updated(el, binding) {
|
|
|
|
!el.$_pbadgeUnstyled && DomHandler.addClass(el, 'p-overlay-badge');
|
|
|
|
el.setAttribute('data-p-overlay-badge', 'true');
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2023-06-22 13:50:40 +00:00
|
|
|
if (binding.oldValue !== binding.value) {
|
2022-09-06 12:03:37 +00:00
|
|
|
let badge = document.getElementById(el.$_pbadgeId);
|
|
|
|
|
2023-06-22 13:50:40 +00:00
|
|
|
if (typeof binding.value === 'object') el.$_badgeValue = binding.value.value;
|
|
|
|
else el.$_badgeValue = binding.value;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2023-06-22 13:50:40 +00:00
|
|
|
if (!el.$_pbadgeUnstyled) {
|
|
|
|
if (el.$_badgeValue) {
|
|
|
|
if (DomHandler.hasClass(badge, 'p-badge-dot')) DomHandler.removeClass(badge, 'p-badge-dot');
|
|
|
|
|
|
|
|
if (el.$_badgeValue.length === 1) DomHandler.addClass(badge, 'p-badge-no-gutter');
|
|
|
|
else DomHandler.removeClass(badge, 'p-badge-no-gutter');
|
|
|
|
} else if (!el.$_badgeValue && !DomHandler.hasClass(badge, 'p-badge-dot')) {
|
|
|
|
DomHandler.addClass(badge, 'p-badge-dot');
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
badge.innerHTML = '';
|
2023-06-22 13:50:40 +00:00
|
|
|
badge.appendChild(document.createTextNode(el.$_badgeValue));
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-22 13:50:40 +00:00
|
|
|
});
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
export default BadgeDirective;
|