primevue-mirror/components/lib/badgedirective/BadgeDirective.js

68 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-12-21 12:48:42 +00:00
import { DomHandler, UniqueComponentId } from 'primevue/utils';
import BaseBadgeDirective from './BaseBadgeDirective';
2022-09-06 12:03:37 +00:00
const BadgeDirective = BaseBadgeDirective.extend('badge', {
2023-06-22 13:50:40 +00:00
mounted(el, binding) {
2022-09-06 12:03:37 +00:00
const id = UniqueComponentId() + '_badge';
2022-09-14 11:26:01 +00:00
el.$_pbadgeUnstyled = binding.instance.$primevue.config.unstyled || binding.value?.unstyled || false;
2022-09-06 12:03:37 +00:00
const badge = DomHandler.createElement('span', {
id,
class: !el.$_pbadgeUnstyled && this.cx('root'),
'p-bind': this.ptm('root')
});
2022-09-14 11:26:01 +00:00
el.$_pbadgeId = badge.getAttribute('id');
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
this.$el = badge;
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;