first commit
This commit is contained in:
58
media/mod_quickicon/js/quickicon.js
Normal file
58
media/mod_quickicon/js/quickicon.js
Normal file
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
/**
|
||||
* Every quickicon with an ajax request url loads data and set them into the counter element
|
||||
* Also the data name is set as singular or plural.
|
||||
* A SR-only text is added
|
||||
* The class pulse gets 'warning', 'success' or 'error', depending on the retrieved data.
|
||||
*/
|
||||
if (!Joomla) {
|
||||
throw new Error('Joomla API was not properly initialized');
|
||||
}
|
||||
Array.from(document.querySelectorAll('.quickicon')).forEach(quickicon => {
|
||||
const counter = quickicon.querySelector('.quickicon-amount');
|
||||
if (!counter) {
|
||||
return;
|
||||
}
|
||||
if (counter.dataset.url) {
|
||||
Joomla.request({
|
||||
url: counter.dataset.url,
|
||||
method: 'GET',
|
||||
onSuccess: resp => {
|
||||
let response;
|
||||
try {
|
||||
response = JSON.parse(resp);
|
||||
} catch (error) {
|
||||
quickicon.classList.add('error');
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(response, 'data')) {
|
||||
const name = quickicon.querySelector('.quickicon-name');
|
||||
const nameSpan = document.createElement('span');
|
||||
quickicon.classList.add(response.data > 0 ? 'warning' : 'success');
|
||||
|
||||
// Set name in singular or plural
|
||||
if (response.data.name && name) {
|
||||
nameSpan.textContent = response.data.name;
|
||||
name.replaceChild(nameSpan, name.firstChild);
|
||||
}
|
||||
|
||||
// Set amount of number into counter span
|
||||
counter.textContent = `\u200E${response.data.amount}`;
|
||||
|
||||
// Insert screenreader text
|
||||
const sronly = quickicon.querySelector('.quickicon-sr-desc');
|
||||
if (response.data.sronly && sronly) {
|
||||
sronly.textContent = response.data.sronly;
|
||||
}
|
||||
} else {
|
||||
quickicon.classList.add('error');
|
||||
}
|
||||
},
|
||||
onError: () => {
|
||||
quickicon.classList.add('error');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
1
media/mod_quickicon/js/quickicon.min.js
vendored
Normal file
1
media/mod_quickicon/js/quickicon.min.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
if(!Joomla)throw new Error("Joomla API was not properly initialized");Array.from(document.querySelectorAll(".quickicon")).forEach((t=>{const e=t.querySelector(".quickicon-amount");e&&e.dataset.url&&Joomla.request({url:e.dataset.url,method:"GET",onSuccess:a=>{let r;try{r=JSON.parse(a)}catch(e){t.classList.add("error")}if(Object.prototype.hasOwnProperty.call(r,"data")){const a=t.querySelector(".quickicon-name"),o=document.createElement("span");t.classList.add(r.data>0?"warning":"success"),r.data.name&&a&&(o.textContent=r.data.name,a.replaceChild(o,a.firstChild)),e.textContent=`${r.data.amount}`;const c=t.querySelector(".quickicon-sr-desc");r.data.sronly&&c&&(c.textContent=r.data.sronly)}else t.classList.add("error")},onError:()=>{t.classList.add("error")}})}));
|
||||
BIN
media/mod_quickicon/js/quickicon.min.js.gz
Normal file
BIN
media/mod_quickicon/js/quickicon.min.js.gz
Normal file
Binary file not shown.
Reference in New Issue
Block a user