- md5
- 86c37e485517df9ff969d015c26ebac3
- sha1
- ebea7184c4dadacf4983e40449b2e19061705bad
- sha256
- 7fd0c16befb533543d4519a50d892c5ec5e8487dfd1a6c2e31626fbd49a3c3bd
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
/**
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
window.customElements.define('joomla-field-permissions', class extends HTMLElement {
constructor() {
super();
if (!Joomla) {
throw new Error('Joomla API is not properly initiated');
}
if (!this.getAttribute('data-uri')) {
throw new Error('No valid url for validation');
}
this.query = window.location.search.substring(1);
this.buttons = '';
this.buttonDataSelector = 'data-onchange-task';
this.onDropdownChange = this.onDropdownChange.bind(this);
this.getUrlParam = this.getUrlParam.bind(this);
this.component = this.getUrlParam('component');
this.extension = this.getUrlParam('extension');
this.option = this.getUrlParam('option');
this.view = this.getUrlParam('view');
this.asset = 'not';
this.context = '';
}
/**
* Lifecycle
*/
connectedCallback() {
this.buttons = [].slice.call(document.querySelectorAll(`[${this.buttonDataSelector}]`));
if (this.buttons) {
this.buttons.forEach(button => {
button.addEventListener('change', this.onDropdownChange);
});
}
}
/**
* Lifecycle
*/
disconnectedCallback() {
if (this.buttons) {
this.buttons.forEach(button => {
button.removeEventListener('change', this.onDropdownChange);
});
}
}
/**
* Lifecycle
*/
onDropdownChange(event) {
event.preventDefault();
const task = event.target.getAttribute(this.buttonDataSelector);
if (task === 'permissions.apply') {
this.sendPermissions(event);
}
}
sendPermissions(event) {
const {
target
} = event; // Set the icon while storing the values
const icon = document.getElementById(`icon_${target.id}`);
icon.removeAttribute('class');
icon.setAttribute('class', 'joomla-icon joomla-field-permissions__spinner'); // Get values add prepare GET-Parameter
const {
value
} = target;
if (document.getElementById('jform_context')) {
this.context = document.getElementById('jform_context').value;
[this.context] = this.context.split('.');
}
if (this.option === 'com_config' && !this.component && !this.extension) {
this.asset = 'root.1';
} else if (!this.extension && this.view === 'component') {
this.asset = this.component;
} else if (this.context) {
if (this.view === 'group') {
this.asset = `${this.context}.fieldgroup.${this.getUrlParam('id')}`;
} else {
this.asset = `${this.context}.field.{this.getUrlParam('id')}`;
}
this.title = document.getElementById('jform_title').value;
} else if (this.extension && this.view) {
this.asset = `${this.extension}.${this.view}.${this.getUrlParam('id')}`;
this.title = document.getElementById('jform_title').value;
} else if (!this.extension && this.view) {
this.asset = `${this.option}.${this.view}.${this.getUrlParam('id')}`;
this.title = document.getElementById('jform_title').value;
}
const id = target.id.replace('jform_rules_', '');
const lastUnderscoreIndex = id.lastIndexOf('_');
const permissionData = {
comp: this.asset,
action: id.substring(0, lastUnderscoreIndex),
rule: id.substring(lastUnderscoreIndex + 1),
value,
title: this.title
}; // Remove JS messages, if they exist.
Joomla.removeMessages(); // Ajax request
Joomla.request({
url: this.getAttribute('data-uri'),
method: 'POST',
data: JSON.stringify(permissionData),
perform: true,
headers: {
'Content-Type': 'application/json'
},
onSuccess: data => {
let response;
try {
response = JSON.parse(data);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
icon.removeAttribute('class'); // Check if everything is OK
if (response.data && response.data.result) {
icon.setAttribute('class', 'joomla-icon joomla-field-permissions__allowed');
const badgeSpan = target.parentNode.parentNode.nextElementSibling.querySelector('span');
badgeSpan.removeAttribute('class');
badgeSpan.setAttribute('class', response.data.class);
badgeSpan.innerHTML = Joomla.sanitizeHtml(response.data.text);
} // Render messages, if any. There are only message in case of errors.
if (typeof response.messages === 'object' && response.messages !== null) {
Joomla.renderMessages(response.messages);
if (response.data && response.data.result) {
icon.setAttribute('class', 'joomla-icon joomla-field-permissions__allowed');
} else {
icon.setAttribute('class', 'joomla-icon joomla-field-permissions__denied');
}
}
},
onError: xhr => {
// Remove the spinning icon.
icon.removeAttribute('style');
Joomla.renderMessages(Joomla.ajaxErrorsMessages(xhr, xhr.statusText));
icon.setAttribute('class', 'joomla-icon joomla-field-permissions__denied');
}
});
}
getUrlParam(variable) {
const vars = this.query.split('&');
let i = 0;
for (i; i < vars.length; i += 1) {
const pair = vars[i].split('=');
if (pair[0] === variable) {
return pair[1];
}
}
return false;
}
});
The same content is in 67 releases, Joomla 4.0.0-rc2 to 4.2.9. See them all, or every version of this file.