33
0
mirror of https://github.com/joomla-extensions/patchtester.git synced 2024-11-14 09:14:11 +00:00
patchtester/media/com_patchtester/js/fetcher.js

130 lines
5.2 KiB
JavaScript
Raw Normal View History

2016-03-16 05:18:16 +00:00
/**
* 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.
2016-03-16 05:18:16 +00:00
* @license GNU General Public License version 2 or later
*/
2016-03-16 05:18:16 +00:00
if (typeof jQuery === 'undefined') {
throw new Error('PatchFetcher JavaScript requires jQuery')
}
2016-03-16 05:18:16 +00:00
if (typeof Joomla === 'undefined') {
throw new Error('PatchFetcher JavaScript requires the Joomla core JavaScript API')
}
!function (jQuery, Joomla, window) {
'use strict';
/**
* Initialize the PatchFetcher object
*
* @constructor
*/
var PatchFetcher = function () {
var offset = null,
progress = null,
2017-06-18 17:21:43 +00:00
path = 'index.php?option=com_patchtester&tmpl=component&format=json',
lastPage = null,
progressBar = jQuery('#progress-bar');
2016-03-16 05:18:16 +00:00
var initialize = function () {
offset = 0;
progress = 0;
path = path + '&' + jQuery('#patchtester-token').attr('name') + '=1';
getRequest('startfetch');
};
2016-03-16 05:18:16 +00:00
var getRequest = function (task) {
jQuery.ajax({
type: 'GET',
url: path,
data: 'task=' + task,
dataType: 'json',
success: function (response, textStatus, xhr) {
try {
if (response === null) {
throw textStatus;
}
2017-06-18 17:21:43 +00:00
2016-03-16 05:18:16 +00:00
if (response.error) {
throw response;
}
2017-06-18 17:21:43 +00:00
// 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;
}
// Update the progress bar if we have the data to do so
if (typeof response.data.page !== 'undefined') {
progress = (response.data.page / 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);
2017-06-18 17:21:43 +00:00
}
}
2016-03-16 05:18:16 +00:00
jQuery('#patchtester-progress-message').html(response.message);
2016-03-16 05:18:16 +00:00
if (response.data.header) {
jQuery('#patchtester-progress-header').html(response.data.header);
}
2016-03-16 05:18:16 +00:00
if (!response.data.complete) {
// Send another request
getRequest('fetch');
2017-06-18 17:21:43 +00:00
} else {
jQuery('#progress').remove();
2016-03-16 05:18:16 +00:00
}
} catch (error) {
try {
if (response.error) {
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');
}
jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED'));
jQuery('#patchtester-progress-message').html(error);
2017-06-18 17:21:43 +00:00
jQuery('#progress').remove();
2016-03-16 05:18:16 +00:00
}
}
return true;
},
error: function (jqXHR, textStatus, errorThrown) {
2017-06-18 17:21:43 +00:00
var json = (typeof jqXHR === 'object' && jqXHR.responseText) ? jqXHR.responseText : null;
jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED'));
2016-03-16 05:18:16 +00:00
jQuery('#patchtester-progress-message').html(json);
2017-06-18 17:21:43 +00:00
jQuery('#progress').remove();
}
2016-03-16 05:18:16 +00:00
});
}
2016-03-16 05:18:16 +00:00
initialize();
};
2016-03-16 05:18:16 +00:00
jQuery(function () {
new PatchFetcher();
2017-06-18 17:21:43 +00:00
if (typeof window.parent.SqueezeBox === 'object') {
2016-03-16 05:18:16 +00:00
jQuery(window.parent.SqueezeBox).on('close', function () {
window.parent.location.reload(true);
});
}
});
}(jQuery, Joomla, window);