- md5
- be0b56e376c3538d45116da294001759
- sha1
- e941391fefa84e081530d78fff35fdb18134da45
- sha256
- 524d60c2d0c6619d92485d186199f71b93b47d6393de2f0ccab2a3c630cbfa4d
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
/**
* @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
window.customElements.define('joomla-toolbar-button', class extends HTMLElement {
// Attribute getters
get task() {
return this.getAttribute('task');
}
get listSelection() {
return this.hasAttribute('list-selection');
}
get form() {
return this.getAttribute('form');
}
get formValidation() {
return this.hasAttribute('form-validation');
}
get confirmMessage() {
return this.getAttribute('confirm-message');
}
/**
* Lifecycle
*/
constructor() {
super();
if (!Joomla) {
throw new Error('Joomla API is not properly initiated');
}
this.onChange = this.onChange.bind(this);
this.executeTask = this.executeTask.bind(this);
}
/**
* Lifecycle
*/
connectedCallback() {
// We need a button to support button behavior,
// because we cannot currently extend HTMLButtonElement
this.buttonElement = this.querySelector('button, a');
this.buttonElement.addEventListener('click', this.executeTask); // Check whether we have a form
const formSelector = this.form || 'adminForm';
this.formElement = document.getElementById(formSelector);
this.disabled = false; // If list selection is required, set button to disabled by default
if (this.listSelection) {
this.setDisabled(true);
}
if (this.listSelection) {
if (!this.formElement) {
throw new Error(`The form "${formSelector}" is required to perform the task, but the form was not found on the page.`);
} // Watch on list selection
this.formElement.boxchecked.addEventListener('change', this.onChange);
}
}
/**
* Lifecycle
*/
disconnectedCallback() {
if (this.formElement.boxchecked) {
this.formElement.boxchecked.removeEventListener('change', this.onChange);
}
this.buttonElement.removeEventListener('click', this.executeTask);
}
onChange({
target
}) {
// Check whether we have selected something
this.setDisabled(target.value < 1);
}
setDisabled(disabled) {
// Make sure we have a boolean value
this.disabled = !!disabled; // Switch attribute for native element
// An anchor does not support "disabled" attribute, so use class
if (this.buttonElement) {
if (this.disabled) {
if (this.buttonElement.nodeName === 'BUTTON') {
this.buttonElement.disabled = true;
} else {
this.buttonElement.classList.add('disabled');
}
} else if (this.buttonElement.nodeName === 'BUTTON') {
this.buttonElement.disabled = false;
} else {
this.buttonElement.classList.remove('disabled');
}
}
}
executeTask() {
if (this.disabled) {
return false;
} // eslint-disable-next-line no-restricted-globals
if (this.confirmMessage && !confirm(this.confirmMessage)) {
return false;
}
if (this.task) {
Joomla.submitbutton(this.task, this.form, this.formValidation);
}
return true;
}
});
The same content is in 68 releases, Joomla 4.0.0-rc1 to 4.2.9. See them all, or every version of this file.