(function () { 'use strict'; /** * @copyright (C) 2021 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt */ /** * Provides the manual-run functionality for tasks over the com_scheduler administrator backend. * * @package Joomla.Components * @subpackage Scheduler.Tasks * * @since 4.1.0 */ if (!window.Joomla) { throw new Error('Joomla API was not properly initialised'); } var initRunner = function initRunner() { var paths = Joomla.getOptions('system.paths'); var token = Joomla.getOptions('com_scheduler.test-task.token'); var uri = (paths ? paths.base + "/index.php" : window.location.pathname) + "?option=com_ajax&format=json&plugin=RunSchedulerTest&group=system&id=%d" + (token ? "&" + token + "=1" : ''); var modal = document.getElementById('scheduler-test-modal'); // Task output template var template = "\n

" + Joomla.Text._('COM_SCHEDULER_TEST_RUN_TASK') + "

\n
" + Joomla.Text._('COM_SCHEDULER_TEST_RUN_STATUS_STARTED') + "
\n
\n "; var sanitiseTaskOutput = function sanitiseTaskOutput(text) { return text.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1
$2'); }; // Trigger the task through a GET request, populate the modal with output on completion. var triggerTaskAndShowOutput = function triggerTaskAndShowOutput(e) { var button = e.relatedTarget; var id = parseInt(button.dataset.id, 10); var title = button.dataset.title; modal.querySelector('.modal-title').innerHTML = Joomla.Text._('COM_SCHEDULER_TEST_RUN_TITLE').replace('%d', id.toString()); modal.querySelector('.modal-body > div').innerHTML = template.replace('%s', title); Joomla.request({ url: uri.replace('%d', id.toString()), onSuccess: function onSuccess(data, xhr) { [].slice.call(modal.querySelectorAll('.modal-body > div > div')).forEach(function (el) { el.parentNode.removeChild(el); }); var output = JSON.parse(data); if (output && output.success && output.data) { modal.querySelector('.modal-body > div').innerHTML += "
" + Joomla.Text._('COM_SCHEDULER_TEST_RUN_STATUS_COMPLETED') + "
"; if (output.data.duration > 0) { modal.querySelector('.modal-body > div').innerHTML += "
" + Joomla.Text._('COM_SCHEDULER_TEST_RUN_DURATION').replace('%s', output.data.duration.toFixed(2)) + "
"; } if (output.data.output) { var result = Joomla.sanitizeHtml(output.data.output, null, sanitiseTaskOutput); // Can use an indication for non-0 exit codes modal.querySelector('.modal-body > div').innerHTML += "
" + Joomla.Text._('COM_SCHEDULER_TEST_RUN_OUTPUT').replace('%s', result) + "
"; } } else { modal.querySelector('.modal-body > div').innerHTML += "
" + Joomla.Text._('COM_SCHEDULER_TEST_RUN_STATUS_TERMINATED') + "
"; modal.querySelector('.modal-body > div').innerHTML += "
" + Joomla.Text._('COM_SCHEDULER_TEST_RUN_OUTPUT').replace('%s', Joomla.Text._('JLIB_JS_AJAX_ERROR_OTHER').replace('%s', xhr.status)) + "
"; } }, onError: function onError(xhr) { modal.querySelector('.modal-body > div').innerHTML += "
" + Joomla.Text._('COM_SCHEDULER_TEST_RUN_STATUS_TERMINATED') + "
"; var msg = Joomla.ajaxErrorsMessages(xhr); modal.querySelector('.modal-body > div').innerHTML += "
" + Joomla.Text._('COM_SCHEDULER_TEST_RUN_OUTPUT').replace('%s', msg.error) + "
"; } }); }; var reloadOnClose = function reloadOnClose() { window.location.href = (paths ? paths.base + "/index.php" : window.location.pathname) + "?option=com_scheduler&view=tasks"; }; modal.addEventListener('show.bs.modal', triggerTaskAndShowOutput); modal.addEventListener('hidden.bs.modal', reloadOnClose); document.removeEventListener('DOMContentLoaded', initRunner); }; document.addEventListener('DOMContentLoaded', initRunner); })();