Skip to content

Commit 30d0dcb

Browse files
committed
Refactor admin/appearance/themes.js:init to reduce complexity via guard clause and helpers
1 parent ed979eb commit 30d0dcb

1 file changed

Lines changed: 61 additions & 48 deletions

File tree

public/src/admin/appearance/themes.js

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,87 @@
44
define('admin/appearance/themes', ['bootbox', 'translator', 'alerts'], function (bootbox, translator, alerts) {
55
const Themes = {};
66

7+
function getThemeInfoFrom(target) {
8+
const parentEl = target.parents('[data-theme]');
9+
return {
10+
themeType: parentEl.attr('data-type'),
11+
cssSrc: parentEl.attr('data-css'),
12+
themeId: parentEl.attr('data-theme'),
13+
};
14+
}
15+
16+
function applyTheme(themeType, themeId, cssSrc) {
17+
socket.emit('admin.themes.set', { type: themeType, id: themeId, src: cssSrc }, function (err) {
18+
if (err) {
19+
return alerts.error(err);
20+
}
21+
config['theme:id'] = themeId;
22+
highlightSelectedTheme(themeId);
23+
24+
alerts.alert({
25+
alert_id: 'admin:theme',
26+
type: 'info',
27+
title: '[[admin/appearance/themes:theme-changed]]',
28+
message: '[[admin/appearance/themes:restart-to-activate]]',
29+
timeout: 5000,
30+
clickfn: function () {
31+
require(['admin/modules/instance'], function (instance) {
32+
instance.rebuildAndRestart();
33+
});
34+
},
35+
});
36+
});
37+
}
38+
39+
740
Themes.init = function () {
41+
842
$('#installed_themes').on('click', function (e) {
943
const target = $(e.target);
1044
const action = target.attr('data-action');
1145

12-
if (action && action === 'use') {
13-
const parentEl = target.parents('[data-theme]');
14-
const themeType = parentEl.attr('data-type');
15-
const cssSrc = parentEl.attr('data-css');
16-
const themeId = parentEl.attr('data-theme');
46+
// guarding clause to reduce nesting
47+
if (action !== 'use') {
48+
return;
49+
}
50+
51+
const { themeType, cssSrc, themeId } = getThemeInfoFrom(target);
52+
53+
// already selected → no-op
54+
if (config['theme:id'] === themeId) {
55+
return;
56+
}
57+
58+
applyTheme(themeType, themeId, cssSrc);
59+
});
1760

18-
if (config['theme:id'] === themeId) {
61+
$('#revert_theme').on('click', function () {
62+
if (config['theme:id'] === 'nodebb-theme-harmony') {
63+
return;
64+
}
65+
bootbox.confirm('[[admin/appearance/themes:revert-confirm]]', function (confirm) {
66+
if (!confirm) {
1967
return;
2068
}
2169
socket.emit('admin.themes.set', {
22-
type: themeType,
23-
id: themeId,
24-
src: cssSrc,
70+
type: 'local',
71+
id: 'nodebb-theme-harmony',
2572
}, function (err) {
2673
if (err) {
2774
return alerts.error(err);
2875
}
29-
config['theme:id'] = themeId;
30-
highlightSelectedTheme(themeId);
31-
76+
config['theme:id'] = 'nodebb-theme-harmony';
77+
highlightSelectedTheme('nodebb-theme-harmony');
3278
alerts.alert({
3379
alert_id: 'admin:theme',
34-
type: 'info',
80+
type: 'success',
3581
title: '[[admin/appearance/themes:theme-changed]]',
36-
message: '[[admin/appearance/themes:restart-to-activate]]',
37-
timeout: 5000,
38-
clickfn: function () {
39-
require(['admin/modules/instance'], function (instance) {
40-
instance.rebuildAndRestart();
41-
});
42-
},
82+
message: '[[admin/appearance/themes:revert-success]]',
83+
timeout: 3500,
4384
});
4485
});
45-
}
46-
});
47-
48-
$('#revert_theme').on('click', function () {
49-
if (config['theme:id'] === 'nodebb-theme-harmony') {
50-
return;
51-
}
52-
bootbox.confirm('[[admin/appearance/themes:revert-confirm]]', function (confirm) {
53-
if (confirm) {
54-
socket.emit('admin.themes.set', {
55-
type: 'local',
56-
id: 'nodebb-theme-harmony',
57-
}, function (err) {
58-
if (err) {
59-
return alerts.error(err);
60-
}
61-
config['theme:id'] = 'nodebb-theme-harmony';
62-
highlightSelectedTheme('nodebb-theme-harmony');
63-
alerts.alert({
64-
alert_id: 'admin:theme',
65-
type: 'success',
66-
title: '[[admin/appearance/themes:theme-changed]]',
67-
message: '[[admin/appearance/themes:revert-success]]',
68-
timeout: 3500,
69-
});
70-
});
71-
}
7286
});
7387
});
74-
7588
socket.emit('admin.themes.getInstalled', function (err, themes) {
7689
if (err) {
7790
return alerts.error(err);

0 commit comments

Comments
 (0)