Skip to content

Commit 8aad204

Browse files
committed
[#364] Cleaned JavaScript to make it work
Signed-off-by: Roland Dalmulder <contact@rolandd.com>
1 parent 8a6c8d3 commit 8aad204

File tree

5 files changed

+69
-25
lines changed

5 files changed

+69
-25
lines changed

administrator/components/com_patchtester/src/Controller/StartfetchController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function execute($task): void
7070

7171
$testsModel = Factory::getApplication()->bootComponent('com_patchtester')->getMVCFactory()->createModel('Tests', 'Administrator', ['ignore_request' => true]);
7272
try {
73-
// Sanity check, ensure there aren't any applied patches
73+
// Sanity check, ensure there aren't any applied patches
7474
if (count($testsModel->getAppliedPatches()) >= 1) {
7575
$response = new JsonResponse(new \Exception(Text::_('COM_PATCHTESTER_ERROR_APPLIED_PATCHES'), 500));
7676
$this->app->sendHeaders();

administrator/components/com_patchtester/tmpl/fetch/default.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
use Joomla\CMS\Factory;
1111
use Joomla\CMS\HTML\HTMLHelper;
1212
use Joomla\CMS\Language\Text;
13+
use Joomla\CMS\WebAsset\WebAssetManager;
1314

1415
// phpcs:disable PSR1.Files.SideEffects
1516
\defined('_JEXEC') or die;
1617
// phpcs:enable PSR1.Files.SideEffects
1718

18-
HTMLHelper::_('jquery.framework');
19+
/** @var WebAssetManager $wa */
20+
$wa = $this->document->getWebAssetManager();
21+
$wa->useScript('com_patchtester.admin-fetcher-modal');
22+
1923
HTMLHelper::_('behavior.core');
20-
HTMLHelper::_('script', 'com_patchtester/fetcher.js', ['version' => 'auto', 'relative' => true]);
24+
//HTMLHelper::_('script', 'com_patchtester/fetcher.js', ['version' => 'auto', 'relative' => true]);
2125
Text::script('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED');
2226

2327
?>

administrator/components/com_patchtester/tmpl/pulls/default.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
use Joomla\CMS\Language\Text;
1212
use Joomla\CMS\Layout\LayoutHelper;
1313
use Joomla\CMS\Router\Route;
14+
use Joomla\CMS\WebAsset\WebAssetManager;
1415

1516
// phpcs:disable PSR1.Files.SideEffects
1617
\defined('_JEXEC') or die;
1718
// phpcs:enable PSR1.Files.SideEffects
1819

19-
/** @var \Joomla\Component\Patchtester\Administrator\View\Pulls\PullsHtmlView $this */
20+
/** @var \Joomla\Component\Patchtester\Administrator\View\Pulls\HtmlView $this */
2021

21-
HTMLHelper::_('stylesheet', 'com_patchtester/octicons.css', ['version' => '3.5.0', 'relative' => true]);
22-
HTMLHelper::_('script', 'com_patchtester/patchtester.js', ['version' => 'auto', 'relative' => true]);
22+
/** @var WebAssetManager $wa */
23+
$wa = $this->document->getWebAssetManager();
24+
$wa->usePreset('com_patchtester.patchtester');
2325

2426
$listOrder = $this->escape($this->state->get('list.ordering'));
2527
$listDirn = $this->escape($this->state->get('list.direction'));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"$schema": "https://developer.joomla.org/schemas/json-schema/web_assets.json",
3+
"name": "com_patchtester",
4+
"version": "4.3.2",
5+
"description": "Joomla CMS",
6+
"license": "GPL-2.0-or-later",
7+
"assets": [
8+
{
9+
"name": "com_patchtester.admin-fetcher-modal",
10+
"type": "script",
11+
"uri": "com_patchtester/fetcher.js",
12+
"attributes": {
13+
"type": "module",
14+
"defer": true
15+
}
16+
},
17+
{
18+
"name": "com_patchtester.patchtester",
19+
"type": "script",
20+
"uri": "com_patchtester/patchtester.js",
21+
"attributes": {
22+
"type": "module",
23+
"defer": true
24+
}
25+
},
26+
{
27+
"name": "com_patchtester.octicons",
28+
"type": "style",
29+
"uri": "com_patchtester/octicons.css"
30+
},
31+
{
32+
"name": "com_patchtester.patchtester",
33+
"type": "preset",
34+
"dependencies": [
35+
"com_patchtester.patchtester#script",
36+
"com_patchtester.octicons#style"
37+
]
38+
}
39+
]
40+
}

media/com_patchtester/js/fetcher.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,38 @@ if (typeof Joomla === 'undefined') {
1010
}
1111

1212
const defaultSettings = {
13-
offset: 0,
14-
progress: 0,
13+
progress: null,
1514
lastPage: null,
1615
baseURL: `${Joomla.getOptions('system.paths').baseFull}index.php?option=com_patchtester&tmpl=component&format=json`,
17-
lastPage: null,
1816
};
1917

2018
class PatchFetcher {
2119
constructor(settings = defaultSettings) {
2220
this.url = new URL(settings.baseURL);
23-
this.offset = settings.offset;
2421
this.progress = settings.progress;
2522
this.lastPage = settings.lastPage;
2623

2724
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}`);
25+
this.url.searchParams.append(document.getElementById('patchtester-token').getAttribute('name'), 1);
3126

3227
this.request('startfetch');
3328
}
3429

35-
request() {
30+
request(task) {
31+
this.url.searchParams.append('task', `${task}.${task}`);
32+
3633
Joomla.request({
37-
url: path.toString(),
34+
url: this.url.toString(),
3835
method: 'GET',
36+
headers: { 'Content-Type': 'application/json' },
3937
perform: true,
40-
data: `task=${task}.${task}`,
41-
4238
onSuccess: (response) => {
39+
response = JSON.parse(response)
4340
try {
4441
if (response === null || response.error || response.success === false) {
4542
throw response;
4643
}
47-
44+
// {"success":true,"message":"Processing page 1 of GitHub data","messages":null,"data":{"complete":false,"header":"Processing data from GitHub"}}
4845
// Store the last page if it is part of this request and not a boolean false
4946
if (typeof response.data.lastPage !== 'undefined' && response.data.lastPage !== false) {
5047
this.lastPage = response.data.lastPage;
@@ -54,17 +51,17 @@ class PatchFetcher {
5451
if (typeof response.data.page !== 'undefined') {
5552
this.progress = (response.data.page / this.lastPage) * 100;
5653

57-
if (progress < 100) {
58-
this.progressBar.style.width = `${progress}%`;
59-
this.progressBar.setAttribute('aria-valuenow', progress);
54+
if (this.progress < 100) {
55+
this.progressBar.style.width = `${this.progress}%`;
56+
this.progressBar.setAttribute('aria-valuenow', this.progress);
6057
} else {
6158
// Both BS2 and BS4 classes are targeted to keep this script simple
6259
this.progressBar.classList.remove(['bar-success', 'bg-success']);
6360
this.progressBar.classList.remove(['bar-warning', 'bg-warning']);
64-
this.progressBar.style.width = `${progress}%`;
61+
this.progressBar.style.width = `${this.progress}%`;
6562
this.progressBar.setAttribute('aria-valuemin', 100);
6663
this.progressBar.setAttribute('aria-valuemax', 200);
67-
this.progressBar.setAttribute('aria-valuenow', progress);
64+
this.progressBar.setAttribute('aria-valuenow', this.progress);
6865
}
6966
}
7067

@@ -75,11 +72,12 @@ class PatchFetcher {
7572
}
7673

7774
if (!response.data.complete) {
78-
// Send another request
75+
this.url.searchParams.append(document.querySelector('#patchtester-token').getAttribute('name'), 1);
76+
this.url.searchParams.append('task', `${task}.${task}`);
7977
this.request('fetch');
8078
} else {
8179
document.getElementById('progress').remove();
82-
document.getElementById('modal-sync button.btn-close', window.parent.document).click();
80+
window.parent.document.querySelector('#modal-sync button.btn-close').click();
8381
}
8482
} catch (error) {
8583
try {

0 commit comments

Comments
 (0)