- md5
- 0fb9dc1a8374f36988919464c6626c78
- sha1
- cb01df23da0b6366e2e334496307b96f2caa069d
- sha256
- a363826fc469674f238173d6f144790fe772bf8139c4d54d36cb40da42f01d77
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
(() => {
class JoomlaAlertElement extends HTMLElement {
/* Attributes to monitor */
static get observedAttributes() { return ['type', 'dismiss', 'acknowledge', 'href']; }
get type() { return this.getAttribute('type'); }
set type(value) { return this.setAttribute('type', value); }
get dismiss() { return this.getAttribute('dismiss'); }
get acknowledge() { return this.getAttribute('acknowledge'); }
get href() { return this.getAttribute('href'); }
/* Lifecycle, element appended to the DOM */
connectedCallback() {
this.setAttribute('role', 'alert');
this.classList.add('joomla-alert--show');
// Default to info
if (!this.type || ['info', 'warning', 'danger', 'success'].indexOf(this.type) === -1) {
this.setAttribute('type', 'info');
}
// Append button
if ((this.hasAttribute('dismiss') || this.hasAttribute('acknowledge')) || ((this.hasAttribute('href') && this.getAttribute('href') !== '')
&& !this.querySelector('button.joomla-alert--close') && !this.querySelector('button.joomla-alert-button--close'))) {
this.appendCloseButton();
}
this.dispatchCustomEvent('joomla.alert.show');
const closeButton = this.querySelector('button.joomla-alert--close') || this.querySelector('button.joomla-alert-button--close');
if (closeButton) {
closeButton.focus();
}
}
/* Lifecycle, element removed from the DOM */
disconnectedCallback() {
this.removeEventListener('joomla.alert.show', this);
this.removeEventListener('joomla.alert.close', this);
this.removeEventListener('joomla.alert.closed', this);
if (this.firstChild.tagName && this.firstChild.tagName.toLowerCase() === 'button') {
this.firstChild.removeEventListener('click', this);
}
}
/* Respond to attribute changes */
attributeChangedCallback(attr, oldValue, newValue) {
switch (attr) {
case 'type':
if (!newValue || (newValue && ['info', 'warning', 'danger', 'success'].indexOf(newValue) === -1)) {
this.type = 'info';
}
break;
case 'dismiss':
case 'acknowledge':
if (!newValue || newValue === 'true') {
this.appendCloseButton();
} else {
this.removeCloseButton();
}
break;
case 'href':
if (!newValue || newValue === '') {
this.removeCloseButton();
} else if (!this.querySelector('button.joomla-alert-button--close')) {
this.appendCloseButton();
}
break;
default:
break;
}
}
/* Method to close the alert */
close() {
this.dispatchCustomEvent('joomla.alert.close');
this.addEventListener('transitionend', () => {
this.dispatchCustomEvent('joomla.alert.closed');
this.parentNode.removeChild(this);
}, false);
this.classList.remove('joomla-alert--show');
}
/* Method to dispatch events */
dispatchCustomEvent(eventName) {
const OriginalCustomEvent = new CustomEvent(eventName);
OriginalCustomEvent.relatedTarget = this;
this.dispatchEvent(OriginalCustomEvent);
this.removeEventListener(eventName, this);
}
/* Method to create the close button */
appendCloseButton() {
if (this.querySelector('button.joomla-alert--close') || this.querySelector('button.joomla-alert-button--close')) {
return;
}
const self = this;
const closeButton = document.createElement('button');
if (this.hasAttribute('dismiss')) {
closeButton.classList.add('joomla-alert--close');
closeButton.innerHTML = '<span aria-hidden="true">×</span>';
closeButton.setAttribute('aria-label', this.getText('JCLOSE', 'Close'));
} else {
closeButton.classList.add('joomla-alert-button--close');
if (this.hasAttribute('acknowledge')) {
closeButton.innerHTML = this.getText('JOK', 'ok');
} else {
closeButton.innerHTML = this.getText('JOPEN', 'Open');
}
}
if (this.firstChild) {
this.insertBefore(closeButton, this.firstChild);
} else {
this.appendChild(closeButton);
}
/* Add the required listener */
if (closeButton) {
if (!this.href) {
closeButton.addEventListener('click', () => {
self.dispatchCustomEvent('joomla.alert.buttonClicked');
if (self.getAttribute('data-callback')) {
window[self.getAttribute('data-callback')]();
self.close();
} else {
self.close();
}
});
} else {
closeButton.addEventListener('click', () => {
self.dispatchCustomEvent('joomla.alert.buttonClicked');
window.location.href = self.href;
self.close();
});
}
}
if (this.hasAttribute('auto-dismiss')) {
setTimeout(() => {
self.dispatchCustomEvent('joomla.alert.buttonClicked');
if (self.hasAttribute('data-callback')) {
window[self.getAttribute('data-callback')]();
} else {
self.close();
}
}, parseInt(self.getAttribute('auto-dismiss'), 10) ? self.getAttribute('auto-dismiss') : 3000);
}
}
/* Method to remove the close button */
removeCloseButton() {
const button = this.querySelector('button');
if (button) {
button.removeEventListener('click', this);
button.parentNode.removeChild(button);
}
}
/* Method to get the translated text */
getText(str, fallback) {
// TODO: Remove coupling to Joomla CMS Core JS here
/* eslint-disable-next-line no-undef */
return (window.Joomla && Joomla.JText && Joomla.JText._ && typeof Joomla.JText._ === 'function' && Joomla.JText._(str)) ? Joomla.JText._(str) : fallback;
}
}
customElements.define('joomla-alert', JoomlaAlertElement);
})();
The same content is in 5 releases, Joomla 4.0.0-alpha6 to 4.0.0-alpha10. See them all, or every version of this file.