Skip to content

Commit 932cf0a

Browse files
authored
Tidy JS (hestiacp#3492)
- Move page error message logic in to main JS - Enable ESLint "no-undef" rule - Various tidying/consistency improvements
1 parent d30f96c commit 932cf0a

26 files changed

+959
-301
lines changed

.eslintrc.cjs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ module.exports = {
99
'eslint:recommended',
1010
'plugin:@typescript-eslint/recommended',
1111
'plugin:editorconfig/noconflict',
12+
'plugin:import/recommended',
1213
'prettier',
1314
],
14-
plugins: ['editorconfig', '@typescript-eslint'],
15+
plugins: ['editorconfig', '@typescript-eslint', 'import'],
1516
ignorePatterns: ['*.cjs'],
1617
env: {
1718
browser: true,
1819
es2021: true,
1920
},
2021
globals: {
2122
$: 'readonly',
22-
jQuery: 'readonly',
2323
App: 'readonly',
24+
Hestia: 'readonly',
25+
Alpine: 'readonly',
2426
},
2527
rules: {
26-
// Set those as warnings instead. They should be fixed at some point
2728
'@typescript-eslint/no-unused-vars': [
2829
'error',
2930
{
@@ -32,8 +33,12 @@ module.exports = {
3233
caughtErrorsIgnorePattern: '^_',
3334
},
3435
],
35-
'@typescript-eslint/no-var-requires': 'off',
36-
'no-redeclare': 'off',
37-
'no-undef': 'off',
36+
'import/order': [
37+
'error',
38+
{
39+
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object', 'type'],
40+
},
41+
],
42+
'import/no-unresolved': 'off',
3843
},
3944
};

build.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
/* eslint-env node */
2+
13
// Build JS and CSS using esbuild and PostCSS
2-
import esbuild from 'esbuild';
3-
import postcss from 'postcss';
44
import { promises as fs } from 'node:fs';
55
import path from 'node:path';
6+
import esbuild from 'esbuild';
7+
import postcss from 'postcss';
68
import postcssConfig from './postcss.config.js';
79

810
// Packages to build but exclude from bundle

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
"@typescript-eslint/eslint-plugin": "^5.59.0",
3131
"@typescript-eslint/parser": "^5.59.0",
3232
"cssnano": "^6.0.0",
33-
"esbuild": "^0.17.17",
33+
"esbuild": "^0.17.18",
3434
"eslint": "^8.39.0",
3535
"eslint-config-prettier": "^8.8.0",
3636
"eslint-plugin-editorconfig": "^4.0.2",
37+
"eslint-plugin-import": "^2.27.5",
3738
"husky": "^8.0.3",
3839
"lint-staged": "^13.2.1",
3940
"markdownlint-cli2": "^0.7.0",
@@ -42,11 +43,11 @@
4243
"postcss-path-replace": "^1.0.4",
4344
"postcss-preset-env": "^8.3.2",
4445
"postcss-size": "^4.0.1",
45-
"prettier": "^2.8.7",
46+
"prettier": "^2.8.8",
4647
"prettier-plugin-nginx": "^1.0.3",
4748
"prettier-plugin-sh": "^0.12.8",
4849
"prettier-plugin-sql": "^0.14.0",
49-
"stylelint": "^15.5.0",
50+
"stylelint": "^15.6.0",
5051
"stylelint-config-standard": "^33.0.0",
5152
"typescript": "^5.0.4",
5253
"vitepress": "1.0.0-alpha.73",

web/js/dist/main.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/js/dist/main.min.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/js/pages/add_web.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ App.Actions.WEB.update_ftp_username_hint = function (elm, hint) {
2020
App.Listeners.WEB.keypress_ftp_username = function () {
2121
var ftp_user_inputs = $('.v-ftp-user');
2222
$.each(ftp_user_inputs, function (i, ref) {
23-
var ref = $(ref);
24-
var current_val = ref.val();
23+
var $ref = $(ref);
24+
var current_val = $ref.val();
2525
if (current_val.trim() != '') {
26-
App.Actions.WEB.update_ftp_username_hint(ref, current_val);
26+
App.Actions.WEB.update_ftp_username_hint($ref, current_val);
2727
}
2828

29-
ref.bind('keypress input', function (evt) {
29+
$ref.bind('keypress input', function (evt) {
3030
clearTimeout(window.frp_usr_tmt);
3131
window.frp_usr_tmt = setTimeout(function () {
3232
var elm = $(evt.target);

web/js/pages/edit_web.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ App.Actions.WEB.update_ftp_username_hint = function (elm, hint) {
5454
App.Listeners.WEB.keypress_ftp_username = function () {
5555
var ftp_user_inputs = $('.v-ftp-user');
5656
$.each(ftp_user_inputs, function (i, ref) {
57-
var ref = $(ref);
58-
var current_val = ref.val();
57+
var $ref = $(ref);
58+
var current_val = $ref.val();
5959
if (current_val.trim() != '') {
60-
App.Actions.WEB.update_ftp_username_hint(ref, current_val);
60+
App.Actions.WEB.update_ftp_username_hint($ref, current_val);
6161
}
6262

63-
ref.bind('keypress input', function (evt) {
63+
$ref.bind('keypress input', function (evt) {
6464
clearTimeout(window.frp_usr_tmt);
6565
window.frp_usr_tmt = setTimeout(function () {
6666
var elm = $(evt.target);
@@ -90,13 +90,13 @@ App.Actions.WEB.update_ftp_path_hint = function (elm, hint) {
9090
App.Listeners.WEB.keypress_ftp_path = function () {
9191
var ftp_path_inputs = $('.js-ftp-path');
9292
$.each(ftp_path_inputs, function (i, ref) {
93-
var ref = $(ref);
94-
var current_val = ref.val();
93+
var $ref = $(ref);
94+
var current_val = $ref.val();
9595
if (current_val.trim() != '') {
96-
App.Actions.WEB.update_ftp_path_hint(ref, current_val);
96+
App.Actions.WEB.update_ftp_path_hint($ref, current_val);
9797
}
9898

99-
ref.bind('keypress input', function (evt) {
99+
$ref.bind('keypress input', function (evt) {
100100
clearTimeout(window.frp_usr_tmt);
101101
window.frp_usr_tmt = setTimeout(function () {
102102
var elm = $(evt.target);
@@ -109,34 +109,36 @@ App.Listeners.WEB.keypress_ftp_path = function () {
109109
//
110110
//
111111
App.Actions.WEB.add_ftp_user_form = function () {
112-
var ref = $('#templates').find('.js-ftp-account-nrm').clone(true);
113-
var index = $('.form-container .js-ftp-account').length + 1;
114-
115-
ref.find('input').each(function (i, elm) {
116-
var name = $(elm).attr('name');
117-
var id = $(elm).attr('id');
118-
$(elm).attr('name', name.replace('%INDEX%', index));
112+
const template = $('#templates').find('.js-ftp-account-nrm').clone(true);
113+
const ftpAccounts = $('.form-container .js-ftp-account');
114+
const newIndex = ftpAccounts.length + 1;
115+
116+
template.find('input').each((_, elm) => {
117+
const $elm = $(elm);
118+
const name = $elm.attr('name');
119+
const id = $elm.attr('id');
120+
$elm.attr('name', name.replace('%INDEX%', newIndex));
119121
if (id) {
120-
$(elm).attr('id', id.replace('%INDEX%', index));
122+
$elm.attr('id', id.replace('%INDEX%', newIndex));
121123
}
122124
});
123125

124-
ref
126+
template
125127
.find('input')
126128
.prev('label')
127-
.each(function (i, elm) {
128-
var for_attr = $(elm).attr('for');
129-
$(elm).attr('for', for_attr.replace('%INDEX%', index));
129+
.each((_, elm) => {
130+
const $elm = $(elm);
131+
const forAttr = $elm.attr('for');
132+
$elm.attr('for', forAttr.replace('%INDEX%', newIndex));
130133
});
131134

132-
ref.find('.ftp-user-number').text(index);
133-
134-
$('#ftp_users').append(ref);
135+
template.find('.ftp-user-number').text(newIndex);
136+
$('#ftp_users').append(template);
135137

136-
var index = 1;
137-
$('.form-container .ftp-user-number:visible').each(function (i, o) {
138-
$(o).text(index);
139-
index += 1;
138+
let counter = 1;
139+
$('.form-container .ftp-user-number:visible').each((_, o) => {
140+
$(o).text(counter);
141+
counter += 1;
140142
});
141143
};
142144

web/js/pages/list_rrd.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ async function loadChartJs() {
33
return module.Chart;
44
}
55

6-
async function main() {
6+
async function initCharts() {
77
const Chart = await loadChartJs();
88
const chartCanvases = document.querySelectorAll('.js-rrd-chart');
99

@@ -108,4 +108,4 @@ function getCssVariable(variableName) {
108108
return getComputedStyle(document.documentElement).getPropertyValue(variableName).trim();
109109
}
110110

111-
main();
111+
initCharts();

web/js/src/alpineInit.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import notificationMethods from './notifications.js';
2-
31
// Set up various Alpine things after it's initialized
42
export default function alpineInit() {
53
// Bulk edit forms
@@ -36,6 +34,42 @@ export default function alpineInit() {
3634
});
3735
});
3836

39-
// Register Alpine notifications methods
40-
Alpine.data('notifications', notificationMethods);
37+
// Notifications methods called by the view code
38+
Alpine.data('notifications', () => ({
39+
initialized: false,
40+
open: false,
41+
notifications: [],
42+
toggle() {
43+
this.open = !this.open;
44+
if (!this.initialized) {
45+
this.list();
46+
}
47+
},
48+
async list() {
49+
const token = document.querySelector('#token').getAttribute('token');
50+
const res = await fetch(`/list/notifications/?ajax=1&token=${token}`);
51+
this.initialized = true;
52+
if (!res.ok) {
53+
throw new Error('An error occurred while listing notifications.');
54+
}
55+
56+
this.notifications = Object.values(await res.json());
57+
},
58+
async remove(id) {
59+
const token = document.querySelector('#token').getAttribute('token');
60+
await fetch(`/delete/notification/?delete=1&notification_id=${id}&token=${token}`);
61+
62+
this.notifications = this.notifications.filter((notification) => notification.ID != id);
63+
if (this.notifications.length === 0) {
64+
this.open = false;
65+
}
66+
},
67+
async removeAll() {
68+
const token = document.querySelector('#token').getAttribute('token');
69+
await fetch(`/delete/notification/?delete=1&token=${token}`);
70+
71+
this.notifications = [];
72+
this.open = false;
73+
},
74+
}));
4175
}

web/js/src/confirmationDialog.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createConfirmationDialog } from './helpers.js';
22

33
// Adds listeners to .js-confirm-action links and intercepts them with a confirmation dialog
4-
export default function initConfirmationDialogs() {
4+
export default function handleConfirmationDialogs() {
55
document.querySelectorAll('.js-confirm-action').forEach((triggerLink) => {
66
triggerLink.addEventListener('click', (evt) => {
77
evt.preventDefault();

0 commit comments

Comments
 (0)