diff --git a/media/com_patchtester/js/fetcher.js b/media/com_patchtester/js/fetcher.js index 77a25ea..6d1fda1 100644 --- a/media/com_patchtester/js/fetcher.js +++ b/media/com_patchtester/js/fetcher.js @@ -1,134 +1,112 @@ /** * Patch testing component for the Joomla! CMS * - * @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2018 Open Source Matters, Inc. All rights reserved. + * @copyright Copyright (C) 2023 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later */ -if (typeof jQuery === 'undefined') { - throw new Error('PatchFetcher JavaScript requires jQuery') -} - if (typeof Joomla === 'undefined') { throw new Error('PatchFetcher JavaScript requires the Joomla core JavaScript API') } -!function (jQuery, Joomla, window) { - 'use strict'; +const defaultSettings = { + offset: 0, + progress: 0, + lastPage: null, + baseURL: `${Joomla.getOptions('system.paths').baseFull}index.php?option=com_patchtester&tmpl=component&format=json`, + lastPage: null, +}; - /** - * Initialize the PatchFetcher object - * - * @constructor - */ - var PatchFetcher = function () { - var offset = null, - progress = null, - path = 'index.php?option=com_patchtester&tmpl=component&format=json', - lastPage = null, - progressBar = jQuery('#progress-bar'); +class PatchFetcher { + constructor(settings = defaultSettings) { + this.url = new URL(settings.baseURL); + this.offset = settings.offset; + this.progress = settings.progress; + this.lastPage = settings.lastPage; - var initialize = function () { - offset = 0; - progress = 0; - path = path + '&' + jQuery('#patchtester-token').attr('name') + '=1'; + this.progressBar = document.getElementById('progress-bar'); - getRequest('startfetch'); - }; + this.url.searchParams.append(document.querySelector('#patchtester-token').getAttribute('name'), 1); + this.url.searchParams.append('task', `${task}.${task}`); - var getRequest = function (task) { - jQuery.ajax({ - type: 'GET', - url: path, - data: `task=${task}.${task}`, - dataType: 'json', - success: function (response, textStatus, xhr) { - try { - if (response === null) { - throw textStatus; - } + this.request('startfetch'); + } - if (response.error) { - throw response; - } + request() { + Joomla.request({ + url: path.toString(), + method: 'GET', + perform: true, + data: `task=${task}.${task}`, - if (response.success === false) { - throw response; - } + onSuccess: (response) => { + try { + if (response === null || response.error || response.success === false) { + throw response; + } - // Store the last page if it is part of this request and not a boolean false - if (typeof response.data.lastPage !== 'undefined' && response.data.lastPage !== false) { - lastPage = response.data.lastPage; - } + // Store the last page if it is part of this request and not a boolean false + if (typeof response.data.lastPage !== 'undefined' && response.data.lastPage !== false) { + this.lastPage = response.data.lastPage; + } - // Update the progress bar if we have the data to do so - if (typeof response.data.page !== 'undefined') { - progress = (response.data.page / lastPage) * 100; + // Update the progress bar if we have the data to do so + if (typeof response.data.page !== 'undefined') { + this.progress = (response.data.page / this.lastPage) * 100; - if (progress < 100) { - progressBar.css('width', progress + '%').attr('aria-valuenow', progress); - } else { - // Both BS2 and BS4 classes are targeted to keep this script simple - progressBar - .removeClass('bar-success bg-success') - .addClass('bar-warning bg-warning') - .css('width', progress + '%') - .attr('aria-valuemin', 100) - .attr('aria-valuemax', 200) - .attr('aria-valuenow', progress); - } - } + if (progress < 100) { + this.progressBar.style.width = `${progress}%`; + this.progressBar.setAttribute('aria-valuenow', progress); + } else { + // Both BS2 and BS4 classes are targeted to keep this script simple + this.progressBar.classList.remove(['bar-success', 'bg-success']); + this.progressBar.classList.remove(['bar-warning', 'bg-warning']); + this.progressBar.style.width = `${progress}%`; + this.progressBar.setAttribute('aria-valuemin', 100); + this.progressBar.setAttribute('aria-valuemax', 200); + this.progressBar.setAttribute('aria-valuenow', progress); + } + } - jQuery('#patchtester-progress-message').html(response.message); + document.getElementById('patchtester-progress-message').innerHTML = Joomla.sanitizeHtml(response.message); - if (response.data.header) { - jQuery('#patchtester-progress-header').html(response.data.header); - } + if (response.data.header) { + document.getElementById('patchtester-progress-header').innerHTML = Joomla.sanitizeHtml(response.data.header); + } - if (!response.data.complete) { - // Send another request - getRequest('fetch'); - } else { - jQuery('#progress').remove(); - jQuery('#modal-sync button.btn-close', window.parent.document).trigger('click'); - } - } catch (error) { - try { - if (response.error || response.success === false) { - jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); - jQuery('#patchtester-progress-message').html(response.message); - } - } catch (ignore) { - if (error === '') { - error = Joomla.JText._('COM_PATCHTESTER_NO_ERROR_RETURNED'); - } + if (!response.data.complete) { + // Send another request + this.request('fetch'); + } else { + document.getElementById('rogress').remove(); + document.getElementById('modal-sync button.btn-close', window.parent.document).click(); + } + } catch (error) { + try { + if (response.error || response.success === false) { + document.getElementById('patchtester-progress-header').innerText(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); + document.getElementById('patchtester-progress-message').innerHTML = Joomla.sanitizeHtml(response.message); + } + } catch (ignore) { + if (error === '') { + error = Joomla.JText._('COM_PATCHTESTER_NO_ERROR_RETURNED'); + } - jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); - jQuery('#patchtester-progress-message').html(error); - jQuery('#progress').remove(); - } - } - return true; - }, - error: function (jqXHR, textStatus, errorThrown) { - var json = (typeof jqXHR === 'object' && jqXHR.responseText) ? jqXHR.responseText : null; - jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); - jQuery('#patchtester-progress-message').html(json); - jQuery('#progress').remove(); - } - }); - }; - - initialize(); - }; - - jQuery(function () { - new PatchFetcher(); - - if (typeof window.parent.SqueezeBox === 'object') { - jQuery(window.parent.SqueezeBox).on('close', function () { - window.parent.location.reload(true); - }); + document.getElementById('patchtester-progress-header').innerText(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); + document.getElementById('patchtester-progress-message').innerHTML = Joomla.sanitizeHtml(error); + document.getElementById('progress').remove(); + } } + return true; + }, + onError: (jqXHR) => { + const json = (typeof jqXHR === 'object' && jqXHR.responseText) ? jqXHR.responseText : null; + document.getElementById('patchtester-progress-header').innerText(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); + document.getElementById('patchtester-progress-message').innerHTML = Joomla.sanitizeHtml(json); + document.getElementById('progress').remove(); + } }); -}(jQuery, Joomla, window); + } +} + +new PatchFetcher();