[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/mod_sampledata/js/ -> sampledata-process.js (source)

   1  /**
   2   * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   3   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   4   */
   5  const SampleData = {
   6    inProgress: false
   7  };
   8  
   9  const sampledataAjax = (type, steps, step) => {
  10    // Get variables
  11    const baseUrl = `index.php?option=com_ajax&format=json&group=sampledata&$Joomla.getOptions('csrf.token')}=1`;
  12    const options = Joomla.getOptions('sample-data'); // Create list
  13  
  14    const list = document.createElement('div');
  15    list.classList.add(`sampledata-steps-$type}-$step}`);
  16    list.setAttribute('role', 'region');
  17    list.setAttribute('aria-live', 'polite'); // Create paragraph
  18  
  19    const para = document.createElement('p');
  20    para.classList.add('loader-image');
  21    para.classList.add('text-center'); // Create image
  22  
  23    const img = document.createElement('img');
  24    img.setAttribute('src', options.icon);
  25    img.setAttribute('width', 30);
  26    img.setAttribute('height', 30); // Append everything
  27  
  28    para.appendChild(img);
  29    list.appendChild(para);
  30    document.querySelector(`.sampledata-progress-$type}`).appendChild(list);
  31    Joomla.request({
  32      url: `$baseUrl}&type=$type}&plugin=SampledataApplyStep$step}&step=$step}`,
  33      method: 'GET',
  34      perform: true,
  35      onSuccess: resp => {
  36        // Remove loader image
  37        const loader = list.querySelector('.loader-image');
  38        loader.parentNode.removeChild(loader);
  39        let response = {};
  40  
  41        try {
  42          response = JSON.parse(resp);
  43        } catch (e) {
  44          Joomla.renderMessages({
  45            error: [Joomla.Text._('MOD_SAMPLEDATA_INVALID_RESPONSE')]
  46          }, `.sampledata-steps-$type}-$step}`);
  47          SampleData.inProgress = false;
  48          return;
  49        }
  50  
  51        let progressClass = '';
  52        let success;
  53  
  54        if (response.success && response.data && response.data.length > 0) {
  55          const progress = document.querySelector(`.sampledata-progress-$type} .progress-bar`); // Display all messages that we got
  56  
  57          response.data.forEach(value => {
  58            if (value === null) {
  59              return;
  60            } // eslint-disable-next-line prefer-destructuring
  61  
  62  
  63            success = value.success;
  64            progressClass = success ? 'bg-success' : 'bg-danger'; // Display success alert
  65  
  66            if (success) {
  67              Joomla.renderMessages({
  68                message: [value.message]
  69              }, `.sampledata-steps-$type}-$step}`, false, 3000);
  70            } else {
  71              Joomla.renderMessages({
  72                error: [value.message]
  73              }, `.sampledata-steps-$type}-$step}`, false);
  74            }
  75          }); // Update progress
  76  
  77          progress.innerText = `$step}/$steps}`;
  78          progress.style.width = `$step / steps * 100}%`;
  79          progress.setAttribute('aria-valuemin', 0);
  80          progress.setAttribute('aria-valuemax', 100);
  81          progress.setAttribute('aria-valuenow', step / steps * 100);
  82          progress.classList.add(progressClass); // Move on next step
  83  
  84          if (success && step <= steps) {
  85            const stepNew = step + 1;
  86  
  87            if (stepNew <= steps) {
  88              sampledataAjax(type, steps, stepNew);
  89            } else {
  90              const bar = document.querySelector(`.sampledata-progress-$type}`);
  91              bar.parentNode.removeChild(bar);
  92              Joomla.renderMessages({
  93                message: [Joomla.Text._('MOD_SAMPLEDATA_COMPLETED')]
  94              });
  95              window.scroll({
  96                top: 0,
  97                left: 0,
  98                behavior: 'smooth'
  99              });
 100              SampleData.inProgress = false;
 101            }
 102          }
 103        } else {
 104          // Display error alert
 105          Joomla.renderMessages({
 106            error: [Joomla.Text._('MOD_SAMPLEDATA_INVALID_RESPONSE')]
 107          });
 108          window.scroll({
 109            top: 0,
 110            left: 0,
 111            behavior: 'smooth'
 112          });
 113          SampleData.inProgress = false;
 114        }
 115      },
 116      onError: () => {
 117        Joomla.renderMessages({
 118          error: ['Something went wrong! Please close and reopen the browser and try again!']
 119        });
 120        window.scroll({
 121          top: 0,
 122          left: 0,
 123          behavior: 'smooth'
 124        });
 125        SampleData.inProgress = false;
 126      }
 127    });
 128  };
 129  
 130  const sampledataApply = element => {
 131    const type = element.getAttribute('data-type');
 132    const steps = element.getAttribute('data-steps'); // Check whether the work in progress or we already processed with current item
 133  
 134    if (SampleData.inProgress) {
 135      return;
 136    }
 137  
 138    if (element.getAttribute('data-processed')) {
 139      alert(Joomla.Text._('MOD_SAMPLEDATA_ITEM_ALREADY_PROCESSED'));
 140      SampleData.inProgress = false;
 141      return;
 142    } // Make sure that use run this not by random clicking on the page links
 143    // @todo use the CE Modal here
 144  
 145  
 146    if (!window.confirm(Joomla.Text._('MOD_SAMPLEDATA_CONFIRM_START'))) {
 147      // eslint-disable-next-line consistent-return
 148      return false;
 149    } // Turn on the progress container
 150  
 151  
 152    const progressElements = [].slice.call(document.querySelectorAll(`.sampledata-progress-$type}`));
 153    progressElements.forEach(progressElement => {
 154      progressElement.classList.remove('d-none');
 155    });
 156    element.getAttribute('data-processed', true);
 157    SampleData.inProgress = true;
 158    sampledataAjax(type, steps, 1); // eslint-disable-next-line consistent-return
 159  
 160    return false;
 161  };
 162  
 163  const sampleDataWrapper = document.getElementById('sample-data-wrapper');
 164  
 165  if (sampleDataWrapper) {
 166    const links = [].slice.call(sampleDataWrapper.querySelectorAll('.apply-sample-data'));
 167    links.forEach(link => {
 168      link.addEventListener('click', ({
 169        currentTarget
 170      }) => sampledataApply(currentTarget));
 171    });
 172  }


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