primevue-mirror/components/badgedirective/BadgeDirective.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-12-21 12:48:42 +00:00
import { DomHandler, UniqueComponentId } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
const BadgeDirective = {
2022-12-21 13:11:13 +00:00
mounted(el, options) {
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;
let badge = document.createElement('span');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
badge.id = id;
badge.className = 'p-badge p-component';
for (let modifier in options.modifiers) {
DomHandler.addClass(badge, 'p-badge-' + modifier);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (options.value != null) {
badge.appendChild(document.createTextNode(options.value));
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (String(options.value).length === 1) {
DomHandler.addClass(badge, 'p-badge-no-gutter');
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
DomHandler.addClass(badge, 'p-badge-dot');
}
DomHandler.addClass(el, 'p-overlay-badge');
el.appendChild(badge);
},
updated(el, options) {
DomHandler.addClass(el, 'p-overlay-badge');
if (options.oldValue !== options.value) {
let badge = document.getElementById(el.$_pbadgeId);
if (options.value) {
if (DomHandler.hasClass(badge, 'p-badge-dot')) {
DomHandler.removeClass(badge, 'p-badge-dot');
}
2022-09-14 11:26:01 +00:00
if (String(options.value).length === 1) DomHandler.addClass(badge, 'p-badge-no-gutter');
else DomHandler.removeClass(badge, 'p-badge-no-gutter');
} else if (!options.value && !DomHandler.hasClass(badge, 'p-badge-dot')) {
2022-09-06 12:03:37 +00:00
DomHandler.addClass(badge, 'p-badge-dot');
}
badge.innerHTML = '';
badge.appendChild(document.createTextNode(options.value));
}
}
};
export default BadgeDirective;