[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/mod_quickicon/js/ -> quickicon.js (source)

   1  /**
   2   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   3   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   4   */
   5  
   6  /**
   7   * Every quickicon with an ajax request url loads data and set them into the counter element
   8   * Also the data name is set as singular or plural.
   9   * A SR-only text is added
  10   * The class pulse gets 'warning', 'success' or 'error', depending on the retrieved data.
  11   */
  12  if (!Joomla) {
  13    throw new Error('Joomla API was not properly initialized');
  14  }
  15  
  16  Array.from(document.querySelectorAll('.quickicon')).forEach(quickicon => {
  17    const counter = quickicon.querySelector('.quickicon-amount');
  18  
  19    if (!counter) {
  20      return;
  21    }
  22  
  23    if (counter.dataset.url) {
  24      Joomla.request({
  25        url: counter.dataset.url,
  26        method: 'GET',
  27        onSuccess: resp => {
  28          let response;
  29  
  30          try {
  31            response = JSON.parse(resp);
  32          } catch (error) {
  33            quickicon.classList.add('error');
  34          }
  35  
  36          if (Object.prototype.hasOwnProperty.call(response, 'data')) {
  37            const name = quickicon.querySelector('.quickicon-name');
  38            const nameSpan = document.createElement('span');
  39            quickicon.classList.add(response.data > 0 ? 'warning' : 'success'); // Set name in singular or plural
  40  
  41            if (response.data.name && name) {
  42              nameSpan.textContent = response.data.name;
  43              name.replaceChild(nameSpan, name.firstChild);
  44            } // Set amount of number into counter span
  45  
  46  
  47            counter.textContent = `\u200E$response.data.amount}`; // Insert screenreader text
  48  
  49            const sronly = quickicon.querySelector('.quickicon-sr-desc');
  50  
  51            if (response.data.sronly && sronly) {
  52              sronly.textContent = response.data.sronly;
  53            }
  54          } else {
  55            quickicon.classList.add('error');
  56          }
  57        },
  58        onError: () => {
  59          quickicon.classList.add('error');
  60        }
  61      });
  62    }
  63  });


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer