Skip to content

Commit 192e86d

Browse files
authored
Update fetcher.js
1 parent 13d174d commit 192e86d

File tree

1 file changed

+100
-122
lines changed

1 file changed

+100
-122
lines changed
Lines changed: 100 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,112 @@
11
/**
22
* Patch testing component for the Joomla! CMS
33
*
4-
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2018 Open Source Matters, Inc. All rights reserved.
4+
* @copyright Copyright (C) 2023 Open Source Matters, Inc. All rights reserved.
55
* @license GNU General Public License version 2 or later
66
*/
77

8-
if (typeof jQuery === 'undefined') {
9-
throw new Error('PatchFetcher JavaScript requires jQuery')
10-
}
11-
128
if (typeof Joomla === 'undefined') {
139
throw new Error('PatchFetcher JavaScript requires the Joomla core JavaScript API')
1410
}
1511

16-
!function (jQuery, Joomla, window) {
17-
'use strict';
18-
19-
/**
20-
* Initialize the PatchFetcher object
21-
*
22-
* @constructor
23-
*/
24-
var PatchFetcher = function () {
25-
var offset = null,
26-
progress = null,
27-
path = 'index.php?option=com_patchtester&tmpl=component&format=json',
28-
lastPage = null,
29-
progressBar = jQuery('#progress-bar');
30-
31-
var initialize = function () {
32-
offset = 0;
33-
progress = 0;
34-
path = path + '&' + jQuery('#patchtester-token').attr('name') + '=1';
35-
36-
getRequest('startfetch');
37-
};
38-
39-
var getRequest = function (task) {
40-
jQuery.ajax({
41-
type: 'GET',
42-
url: path,
43-
data: `task=${task}.${task}`,
44-
dataType: 'json',
45-
success: function (response, textStatus, xhr) {
46-
try {
47-
if (response === null) {
48-
throw textStatus;
49-
}
50-
51-
if (response.error) {
52-
throw response;
53-
}
54-
55-
if (response.success === false) {
56-
throw response;
57-
}
58-
59-
// Store the last page if it is part of this request and not a boolean false
60-
if (typeof response.data.lastPage !== 'undefined' && response.data.lastPage !== false) {
61-
lastPage = response.data.lastPage;
62-
}
63-
64-
// Update the progress bar if we have the data to do so
65-
if (typeof response.data.page !== 'undefined') {
66-
progress = (response.data.page / lastPage) * 100;
67-
68-
if (progress < 100) {
69-
progressBar.css('width', progress + '%').attr('aria-valuenow', progress);
70-
} else {
71-
// Both BS2 and BS4 classes are targeted to keep this script simple
72-
progressBar
73-
.removeClass('bar-success bg-success')
74-
.addClass('bar-warning bg-warning')
75-
.css('width', progress + '%')
76-
.attr('aria-valuemin', 100)
77-
.attr('aria-valuemax', 200)
78-
.attr('aria-valuenow', progress);
79-
}
80-
}
81-
82-
jQuery('#patchtester-progress-message').html(response.message);
83-
84-
if (response.data.header) {
85-
jQuery('#patchtester-progress-header').html(response.data.header);
86-
}
87-
88-
if (!response.data.complete) {
89-
// Send another request
90-
getRequest('fetch');
91-
} else {
92-
jQuery('#progress').remove();
93-
jQuery('#modal-sync button.btn-close', window.parent.document).trigger('click');
94-
}
95-
} catch (error) {
96-
try {
97-
if (response.error || response.success === false) {
98-
jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED'));
99-
jQuery('#patchtester-progress-message').html(response.message);
100-
}
101-
} catch (ignore) {
102-
if (error === '') {
103-
error = Joomla.JText._('COM_PATCHTESTER_NO_ERROR_RETURNED');
104-
}
105-
106-
jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED'));
107-
jQuery('#patchtester-progress-message').html(error);
108-
jQuery('#progress').remove();
109-
}
110-
}
111-
return true;
112-
},
113-
error: function (jqXHR, textStatus, errorThrown) {
114-
var json = (typeof jqXHR === 'object' && jqXHR.responseText) ? jqXHR.responseText : null;
115-
jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED'));
116-
jQuery('#patchtester-progress-message').html(json);
117-
jQuery('#progress').remove();
118-
}
119-
});
120-
};
121-
122-
initialize();
123-
};
124-
125-
jQuery(function () {
126-
new PatchFetcher();
127-
128-
if (typeof window.parent.SqueezeBox === 'object') {
129-
jQuery(window.parent.SqueezeBox).on('close', function () {
130-
window.parent.location.reload(true);
131-
});
12+
const defaultSettings = {
13+
offset: 0,
14+
progress: 0,
15+
lastPage: null,
16+
baseURL: `${Joomla.getOptions('system.paths').baseFull}index.php?option=com_patchtester&tmpl=component&format=json`,
17+
lastPage: null,
18+
};
19+
20+
class PatchFetcher {
21+
constructor(settings = defaultSettings) {
22+
this.url = new URL(settings.baseURL);
23+
this.offset = settings.offset;
24+
this.progress = settings.progress;
25+
this.lastPage = settings.lastPage;
26+
27+
this.progressBar = document.getElementById('progress-bar');
28+
29+
this.url.searchParams.append(document.querySelector('#patchtester-token').getAttribute('name'), 1);
30+
this.url.searchParams.append('task', `${task}.${task}`);
31+
32+
this.request('startfetch');
33+
}
34+
35+
request() {
36+
Joomla.request({
37+
url: path.toString(),
38+
method: 'GET',
39+
perform: true,
40+
data: `task=${task}.${task}`,
41+
42+
onSuccess: (response) => {
43+
try {
44+
if (response === null || response.error || response.success === false) {
45+
throw response;
46+
}
47+
48+
// Store the last page if it is part of this request and not a boolean false
49+
if (typeof response.data.lastPage !== 'undefined' && response.data.lastPage !== false) {
50+
this.lastPage = response.data.lastPage;
51+
}
52+
53+
// Update the progress bar if we have the data to do so
54+
if (typeof response.data.page !== 'undefined') {
55+
this.progress = (response.data.page / this.lastPage) * 100;
56+
57+
if (progress < 100) {
58+
this.progressBar.style.width = `${progress}%`;
59+
this.progressBar.setAttribute('aria-valuenow', progress);
60+
} else {
61+
// Both BS2 and BS4 classes are targeted to keep this script simple
62+
this.progressBar.classList.remove(['bar-success', 'bg-success']);
63+
this.progressBar.classList.remove(['bar-warning', 'bg-warning']);
64+
this.progressBar.style.width = `${progress}%`;
65+
this.progressBar.setAttribute('aria-valuemin', 100);
66+
this.progressBar.setAttribute('aria-valuemax', 200);
67+
this.progressBar.setAttribute('aria-valuenow', progress);
68+
}
69+
}
70+
71+
document.getElementById('patchtester-progress-message').innerHTML = Joomla.sanitizeHtml(response.message);
72+
73+
if (response.data.header) {
74+
document.getElementById('patchtester-progress-header').innerHTML = Joomla.sanitizeHtml(response.data.header);
75+
}
76+
77+
if (!response.data.complete) {
78+
// Send another request
79+
this.request('fetch');
80+
} else {
81+
document.getElementById('rogress').remove();
82+
document.getElementById('modal-sync button.btn-close', window.parent.document).click();
83+
}
84+
} catch (error) {
85+
try {
86+
if (response.error || response.success === false) {
87+
document.getElementById('patchtester-progress-header').innerText(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED'));
88+
document.getElementById('patchtester-progress-message').innerHTML = Joomla.sanitizeHtml(response.message);
89+
}
90+
} catch (ignore) {
91+
if (error === '') {
92+
error = Joomla.JText._('COM_PATCHTESTER_NO_ERROR_RETURNED');
93+
}
94+
95+
document.getElementById('patchtester-progress-header').innerText(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED'));
96+
document.getElementById('patchtester-progress-message').innerHTML = Joomla.sanitizeHtml(error);
97+
document.getElementById('progress').remove();
98+
}
13299
}
100+
return true;
101+
},
102+
onError: (jqXHR) => {
103+
const json = (typeof jqXHR === 'object' && jqXHR.responseText) ? jqXHR.responseText : null;
104+
document.getElementById('patchtester-progress-header').innerText(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED'));
105+
document.getElementById('patchtester-progress-message').innerHTML = Joomla.sanitizeHtml(json);
106+
document.getElementById('progress').remove();
107+
}
133108
});
134-
}(jQuery, Joomla, window);
109+
}
110+
}
111+
112+
new PatchFetcher();

0 commit comments

Comments
 (0)