[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/com_scheduler/js/ -> admin-view-run-test-task.js (source)

   1  /**
   2   * @copyright   (C) 2021 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   * Provides the manual-run functionality for tasks over the com_scheduler administrator backend.
   8   *
   9   * @package  Joomla.Components
  10   * @subpackage Scheduler.Tasks
  11   *
  12   * @since    4.1.0
  13   */
  14  if (!window.Joomla) {
  15    throw new Error('Joomla API was not properly initialised');
  16  }
  17  
  18  const initRunner = () => {
  19    const paths = Joomla.getOptions('system.paths');
  20    const token = Joomla.getOptions('com_scheduler.test-task.token');
  21    const uri = `$paths ? `$paths.base}/index.php` : window.location.pathname}?option=com_ajax&format=json&plugin=RunSchedulerTest&group=system&id=%d$token ? `&$token}=1` : ''}`;
  22    const modal = document.getElementById('scheduler-test-modal'); // Task output template
  23  
  24    const template = `
  25      <h4 class="scheduler-headline">$Joomla.Text._('COM_SCHEDULER_TEST_RUN_TASK')}</h4>
  26      <div>$Joomla.Text._('COM_SCHEDULER_TEST_RUN_STATUS_STARTED')}</div>
  27      <div class="mt-3 text-center"><span class="fa fa-spinner fa-spin fa-lg"></span></div>
  28    `;
  29  
  30    const sanitiseTaskOutput = text => text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br>$2'); // Trigger the task through a GET request, populate the modal with output on completion.
  31  
  32  
  33    const triggerTaskAndShowOutput = e => {
  34      const button = e.relatedTarget;
  35      const id = parseInt(button.dataset.id, 10);
  36      const {
  37        title
  38      } = button.dataset;
  39      modal.querySelector('.modal-title').innerHTML = Joomla.Text._('COM_SCHEDULER_TEST_RUN_TITLE').replace('%d', id.toString());
  40      modal.querySelector('.modal-body > div').innerHTML = template.replace('%s', title);
  41      Joomla.request({
  42        url: uri.replace('%d', id.toString()),
  43        onSuccess: (data, xhr) => {
  44          [].slice.call(modal.querySelectorAll('.modal-body > div > div')).forEach(el => {
  45            el.parentNode.removeChild(el);
  46          });
  47          const output = JSON.parse(data);
  48  
  49          if (output && output.success && output.data) {
  50            modal.querySelector('.modal-body > div').innerHTML += `<div>$Joomla.Text._('COM_SCHEDULER_TEST_RUN_STATUS_COMPLETED')}</div>`;
  51  
  52            if (output.data.duration > 0) {
  53              modal.querySelector('.modal-body > div').innerHTML += `<div>$Joomla.Text._('COM_SCHEDULER_TEST_RUN_DURATION').replace('%s', output.data.duration.toFixed(2))}</div>`;
  54            }
  55  
  56            if (output.data.output) {
  57              const result = Joomla.sanitizeHtml(output.data.output, null, sanitiseTaskOutput); // Can use an indication for non-0 exit codes
  58  
  59              modal.querySelector('.modal-body > div').innerHTML += `<div>$Joomla.Text._('COM_SCHEDULER_TEST_RUN_OUTPUT').replace('%s', result)}</div>`;
  60            }
  61          } else {
  62            modal.querySelector('.modal-body > div').innerHTML += `<div>$Joomla.Text._('COM_SCHEDULER_TEST_RUN_STATUS_TERMINATED')}</div>`;
  63            modal.querySelector('.modal-body > div').innerHTML += `<div>$Joomla.Text._('COM_SCHEDULER_TEST_RUN_OUTPUT').replace('%s', Joomla.Text._('JLIB_JS_AJAX_ERROR_OTHER').replace('%s', xhr.status))}</div>`;
  64          }
  65        },
  66        onError: xhr => {
  67          modal.querySelector('.modal-body > div').innerHTML += `<div>$Joomla.Text._('COM_SCHEDULER_TEST_RUN_STATUS_TERMINATED')}</div>`;
  68          const msg = Joomla.ajaxErrorsMessages(xhr);
  69          modal.querySelector('.modal-body > div').innerHTML += `<div>$Joomla.Text._('COM_SCHEDULER_TEST_RUN_OUTPUT').replace('%s', msg.error)}</div>`;
  70        }
  71      });
  72    };
  73  
  74    const reloadOnClose = () => {
  75      window.location.href = `$paths ? `$paths.base}/index.php` : window.location.pathname}?option=com_scheduler&view=tasks`;
  76    };
  77  
  78    modal.addEventListener('show.bs.modal', triggerTaskAndShowOutput);
  79    modal.addEventListener('hidden.bs.modal', reloadOnClose);
  80    document.removeEventListener('DOMContentLoaded', initRunner);
  81  };
  82  
  83  document.addEventListener('DOMContentLoaded', initRunner);


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