|
4 | 4 | define('admin/appearance/themes', ['bootbox', 'translator', 'alerts'], function (bootbox, translator, alerts) { |
5 | 5 | const Themes = {}; |
6 | 6 |
|
| 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 | + |
7 | 40 | Themes.init = function () { |
| 41 | + |
8 | 42 | $('#installed_themes').on('click', function (e) { |
9 | 43 | const target = $(e.target); |
10 | 44 | const action = target.attr('data-action'); |
11 | 45 |
|
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 | + }); |
17 | 60 |
|
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) { |
19 | 67 | return; |
20 | 68 | } |
21 | 69 | socket.emit('admin.themes.set', { |
22 | | - type: themeType, |
23 | | - id: themeId, |
24 | | - src: cssSrc, |
| 70 | + type: 'local', |
| 71 | + id: 'nodebb-theme-harmony', |
25 | 72 | }, function (err) { |
26 | 73 | if (err) { |
27 | 74 | return alerts.error(err); |
28 | 75 | } |
29 | | - config['theme:id'] = themeId; |
30 | | - highlightSelectedTheme(themeId); |
31 | | - |
| 76 | + config['theme:id'] = 'nodebb-theme-harmony'; |
| 77 | + highlightSelectedTheme('nodebb-theme-harmony'); |
32 | 78 | alerts.alert({ |
33 | 79 | alert_id: 'admin:theme', |
34 | | - type: 'info', |
| 80 | + type: 'success', |
35 | 81 | 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, |
43 | 84 | }); |
44 | 85 | }); |
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 | | - } |
72 | 86 | }); |
73 | 87 | }); |
74 | | - |
75 | 88 | socket.emit('admin.themes.getInstalled', function (err, themes) { |
76 | 89 | if (err) { |
77 | 90 | return alerts.error(err); |
|
0 commit comments