Skip to content

Commit f68e2cb

Browse files
committed
Refactor home.js file
Extracts GitInfo and ThemesManager into modules. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 431b594 commit f68e2cb

File tree

3 files changed

+169
-160
lines changed

3 files changed

+169
-160
lines changed

js/src/home.js

Lines changed: 11 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,18 @@
1-
import $ from 'jquery';
2-
3-
const GitInfo = {
4-
/**
5-
* Version string to integer conversion.
6-
* @param {string} str
7-
* @return {number | false}
8-
*/
9-
parseVersionString: str => {
10-
if (typeof(str) !== 'string') {
11-
return false;
12-
}
13-
let add = 0;
14-
// Parse possible alpha/beta/rc/
15-
const state = str.split('-');
16-
if (state.length >= 2) {
17-
if (state[1].startsWith('rc')) {
18-
add = - 20 - parseInt(state[1].substring(2), 10);
19-
} else if (state[1].startsWith('beta')) {
20-
add = - 40 - parseInt(state[1].substring(4), 10);
21-
} else if (state[1].startsWith('alpha')) {
22-
add = - 60 - parseInt(state[1].substring(5), 10);
23-
} else if (state[1].startsWith('dev')) {
24-
/* We don't handle dev, it's git snapshot */
25-
add = 0;
26-
}
27-
}
28-
// Parse version
29-
const x = str.split('.');
30-
// Use 0 for non existing parts
31-
const maj = parseInt(x[0], 10) || 0;
32-
const min = parseInt(x[1], 10) || 0;
33-
const pat = parseInt(x[2], 10) || 0;
34-
const hotfix = parseInt(x[3], 10) || 0;
35-
return maj * 100000000 + min * 1000000 + pat * 10000 + hotfix * 100 + add;
36-
},
37-
38-
/**
39-
* Indicates current available version on main page.
40-
* @param {object} data
41-
*/
42-
currentVersion: data => {
43-
if (data && data.version && data.date) {
44-
const current = GitInfo.parseVersionString($('span.version').text());
45-
const latest = GitInfo.parseVersionString(data.version);
46-
const url = './url.php?url=https://www.phpmyadmin.net/files/' + Functions.escapeHtml(encodeURIComponent(data.version)) + '/';
47-
let versionInformationMessage = document.createElement('span');
48-
versionInformationMessage.className = 'latest';
49-
const versionInformationMessageLink = document.createElement('a');
50-
versionInformationMessageLink.href = url;
51-
versionInformationMessageLink.className = 'disableAjax';
52-
versionInformationMessageLink.target = '_blank';
53-
versionInformationMessageLink.rel = 'noopener noreferrer';
54-
const versionInformationMessageLinkText = document.createTextNode(data.version);
55-
versionInformationMessageLink.appendChild(versionInformationMessageLinkText);
56-
const prefixMessage = document.createTextNode(window.Messages.strLatestAvailable + ' ');
57-
versionInformationMessage.appendChild(prefixMessage);
58-
versionInformationMessage.appendChild(versionInformationMessageLink);
59-
if (latest > current) {
60-
const message = Functions.sprintf(
61-
window.Messages.strNewerVersion,
62-
Functions.escapeHtml(data.version),
63-
Functions.escapeHtml(data.date)
64-
);
65-
let htmlClass = 'alert alert-primary';
66-
if (Math.floor(latest / 10000) === Math.floor(current / 10000)) {
67-
/* Security update */
68-
htmlClass = 'alert alert-danger';
69-
}
70-
$('#newer_version_notice').remove();
71-
const mainContainerDiv = document.createElement('div');
72-
mainContainerDiv.id = 'newer_version_notice';
73-
mainContainerDiv.className = htmlClass;
74-
const mainContainerDivLink = document.createElement('a');
75-
mainContainerDivLink.href = url;
76-
mainContainerDivLink.className = 'disableAjax';
77-
mainContainerDivLink.target = '_blank';
78-
mainContainerDivLink.rel = 'noopener noreferrer';
79-
const mainContainerDivLinkText = document.createTextNode(message);
80-
mainContainerDivLink.appendChild(mainContainerDivLinkText);
81-
mainContainerDiv.appendChild(mainContainerDivLink);
82-
$('#maincontainer').append($(mainContainerDiv));
83-
}
84-
if (latest === current) {
85-
versionInformationMessage = document.createTextNode(' (' + window.Messages.strUpToDate + ')');
86-
}
87-
/* Remove extra whitespace */
88-
const versionInfo = $('#li_pma_version').contents().get(2);
89-
if (typeof versionInfo !== 'undefined') {
90-
versionInfo.textContent = versionInfo.textContent.trim();
91-
}
92-
const $liPmaVersion = $('#li_pma_version');
93-
$liPmaVersion.find('span.latest').remove();
94-
$liPmaVersion.append($(versionInformationMessage));
95-
}
96-
},
97-
98-
/**
99-
* Loads Git revision data from ajax for index.php
100-
*/
101-
displayGitRevision: () => {
102-
$('#is_git_revision').remove();
103-
$('#li_pma_version_git').remove();
104-
$.get(
105-
'index.php?route=/git-revision',
106-
{
107-
'server': window.CommonParams.get('server'),
108-
'ajax_request': true,
109-
'no_debug': true
110-
},
111-
data => {
112-
if (typeof data !== 'undefined' && data.success === true) {
113-
$(data.message).insertAfter('#li_pma_version');
114-
}
115-
}
116-
);
117-
},
118-
119-
/**
120-
* Load version information asynchronously.
121-
*/
122-
loadVersion: () => {
123-
if ($('li.jsversioncheck').length === 0) {
124-
return;
125-
}
126-
127-
$.ajax({
128-
dataType: 'json',
129-
url: 'index.php?route=/version-check',
130-
method: 'POST',
131-
data: {
132-
'server': window.CommonParams.get('server')
133-
},
134-
success: GitInfo.currentVersion
135-
});
136-
},
137-
138-
showVersion: () => {
139-
GitInfo.loadVersion();
140-
if ($('#is_git_revision').length === 0) {
141-
return;
142-
}
143-
144-
setTimeout(GitInfo.displayGitRevision, 10);
145-
}
146-
};
147-
148-
/**
149-
* @implements EventListener
150-
*/
151-
const ThemesManager = {
152-
handleEvent: () => {
153-
$.get('index.php?route=/themes', data => {
154-
$('#themesModal .modal-body').html(data.themes);
155-
});
156-
}
157-
};
1+
import { showGitVersion } from './modules/git-info.js';
2+
import { ThemesManager } from './modules/themes-manager.js';
1583

1594
window.AJAX.registerTeardown('home.js', () => {
160-
$('#themesModal').off('show.bs.modal');
5+
const themesModal = document.getElementById('themesModal');
6+
if (themesModal) {
7+
themesModal.removeEventListener('show.bs.modal', ThemesManager.handleEvent);
8+
}
1619
});
16210

16311
window.AJAX.registerOnload('home.js', () => {
164-
$('#themesModal').on('show.bs.modal', ThemesManager.handleEvent);
12+
const themesModal = document.getElementById('themesModal');
13+
if (themesModal) {
14+
themesModal.addEventListener('show.bs.modal', ThemesManager.handleEvent);
15+
}
16516

166-
GitInfo.showVersion();
17+
showGitVersion();
16718
});

js/src/modules/git-info.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import $ from 'jquery';
2+
3+
const GitInfo = {
4+
/**
5+
* Version string to integer conversion.
6+
* @param {string} str
7+
* @return {number | false}
8+
*/
9+
parseVersionString: str => {
10+
if (typeof(str) !== 'string') {
11+
return false;
12+
}
13+
let add = 0;
14+
// Parse possible alpha/beta/rc/
15+
const state = str.split('-');
16+
if (state.length >= 2) {
17+
if (state[1].startsWith('rc')) {
18+
add = - 20 - parseInt(state[1].substring(2), 10);
19+
} else if (state[1].startsWith('beta')) {
20+
add = - 40 - parseInt(state[1].substring(4), 10);
21+
} else if (state[1].startsWith('alpha')) {
22+
add = - 60 - parseInt(state[1].substring(5), 10);
23+
} else if (state[1].startsWith('dev')) {
24+
/* We don't handle dev, it's git snapshot */
25+
add = 0;
26+
}
27+
}
28+
// Parse version
29+
const x = str.split('.');
30+
// Use 0 for non existing parts
31+
const maj = parseInt(x[0], 10) || 0;
32+
const min = parseInt(x[1], 10) || 0;
33+
const pat = parseInt(x[2], 10) || 0;
34+
const hotfix = parseInt(x[3], 10) || 0;
35+
return maj * 100000000 + min * 1000000 + pat * 10000 + hotfix * 100 + add;
36+
},
37+
38+
/**
39+
* Indicates current available version on main page.
40+
* @param {object} data
41+
*/
42+
currentVersion: data => {
43+
if (data && data.version && data.date) {
44+
const current = GitInfo.parseVersionString($('span.version').text());
45+
const latest = GitInfo.parseVersionString(data.version);
46+
const url = './url.php?url=https://www.phpmyadmin.net/files/' + Functions.escapeHtml(encodeURIComponent(data.version)) + '/';
47+
let versionInformationMessage = document.createElement('span');
48+
versionInformationMessage.className = 'latest';
49+
const versionInformationMessageLink = document.createElement('a');
50+
versionInformationMessageLink.href = url;
51+
versionInformationMessageLink.className = 'disableAjax';
52+
versionInformationMessageLink.target = '_blank';
53+
versionInformationMessageLink.rel = 'noopener noreferrer';
54+
const versionInformationMessageLinkText = document.createTextNode(data.version);
55+
versionInformationMessageLink.appendChild(versionInformationMessageLinkText);
56+
const prefixMessage = document.createTextNode(window.Messages.strLatestAvailable + ' ');
57+
versionInformationMessage.appendChild(prefixMessage);
58+
versionInformationMessage.appendChild(versionInformationMessageLink);
59+
if (latest > current) {
60+
const message = Functions.sprintf(
61+
window.Messages.strNewerVersion,
62+
Functions.escapeHtml(data.version),
63+
Functions.escapeHtml(data.date)
64+
);
65+
let htmlClass = 'alert alert-primary';
66+
if (Math.floor(latest / 10000) === Math.floor(current / 10000)) {
67+
/* Security update */
68+
htmlClass = 'alert alert-danger';
69+
}
70+
$('#newer_version_notice').remove();
71+
const mainContainerDiv = document.createElement('div');
72+
mainContainerDiv.id = 'newer_version_notice';
73+
mainContainerDiv.className = htmlClass;
74+
const mainContainerDivLink = document.createElement('a');
75+
mainContainerDivLink.href = url;
76+
mainContainerDivLink.className = 'disableAjax';
77+
mainContainerDivLink.target = '_blank';
78+
mainContainerDivLink.rel = 'noopener noreferrer';
79+
const mainContainerDivLinkText = document.createTextNode(message);
80+
mainContainerDivLink.appendChild(mainContainerDivLinkText);
81+
mainContainerDiv.appendChild(mainContainerDivLink);
82+
$('#maincontainer').append($(mainContainerDiv));
83+
}
84+
if (latest === current) {
85+
versionInformationMessage = document.createTextNode(' (' + window.Messages.strUpToDate + ')');
86+
}
87+
/* Remove extra whitespace */
88+
const versionInfo = $('#li_pma_version').contents().get(2);
89+
if (typeof versionInfo !== 'undefined') {
90+
versionInfo.textContent = versionInfo.textContent.trim();
91+
}
92+
const $liPmaVersion = $('#li_pma_version');
93+
$liPmaVersion.find('span.latest').remove();
94+
$liPmaVersion.append($(versionInformationMessage));
95+
}
96+
},
97+
98+
/**
99+
* Loads Git revision data from ajax for index.php
100+
*/
101+
displayGitRevision: () => {
102+
$('#is_git_revision').remove();
103+
$('#li_pma_version_git').remove();
104+
$.get(
105+
'index.php?route=/git-revision',
106+
{
107+
'server': window.CommonParams.get('server'),
108+
'ajax_request': true,
109+
'no_debug': true
110+
},
111+
data => {
112+
if (typeof data !== 'undefined' && data.success === true) {
113+
$(data.message).insertAfter('#li_pma_version');
114+
}
115+
}
116+
);
117+
},
118+
119+
/**
120+
* Load version information asynchronously.
121+
*/
122+
loadVersion: () => {
123+
if ($('li.jsversioncheck').length === 0) {
124+
return;
125+
}
126+
127+
$.ajax({
128+
dataType: 'json',
129+
url: 'index.php?route=/version-check',
130+
method: 'POST',
131+
data: {
132+
'server': window.CommonParams.get('server')
133+
},
134+
success: GitInfo.currentVersion
135+
});
136+
}
137+
};
138+
139+
export function showGitVersion () {
140+
GitInfo.loadVersion();
141+
if ($('#is_git_revision').length === 0) {
142+
return;
143+
}
144+
145+
setTimeout(GitInfo.displayGitRevision, 10);
146+
}

js/src/modules/themes-manager.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import $ from 'jquery';
2+
3+
/**
4+
* @implements EventListener
5+
*/
6+
export const ThemesManager = {
7+
handleEvent: () => {
8+
$.get('index.php?route=/themes', data => {
9+
$('#themesModal .modal-body').html(data.themes);
10+
});
11+
}
12+
};

0 commit comments

Comments
 (0)