Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored code in public/src/admin/appearance/themes.js to reduce function nesting #536

Open
wants to merge 1 commit into
base: f24
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 92 additions & 75 deletions public/src/admin/appearance/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,108 @@ define('admin/appearance/themes', ['bootbox', 'translator', 'alerts'], function
const Themes = {};

Themes.init = function () {
$('#installed_themes').on('click', function (e) {
const target = $(e.target);
const action = target.attr('data-action');

if (action && action === 'use') {
const parentEl = target.parents('[data-theme]');
const themeType = parentEl.attr('data-type');
const cssSrc = parentEl.attr('data-css');
const themeId = parentEl.attr('data-theme');

if (config['theme:id'] === themeId) {
return;
}
socket.emit('admin.themes.set', {
type: themeType,
id: themeId,
src: cssSrc,
}, function (err) {
if (err) {
return alerts.error(err);
}
config['theme:id'] = themeId;
highlightSelectedTheme(themeId);

alerts.alert({
alert_id: 'admin:theme',
type: 'info',
title: '[[admin/appearance/themes:theme-changed]]',
message: '[[admin/appearance/themes:restart-to-activate]]',
timeout: 5000,
clickfn: function () {
require(['admin/modules/instance'], function (instance) {
instance.rebuildAndRestart();
});
},
});
});
}
});
$('#installed_themes').on('click', clickedTheme);
$('#revert_theme').on('click', clickedRevert);
};

$('#revert_theme').on('click', function () {
if (config['theme:id'] === 'nodebb-theme-harmony') {
function clickedTheme(e) {
const target = $(e.target);
const action = target.attr('data-action');

if (action && action === 'use') {
const parentEl = target.parents('[data-theme]');
const themeType = parentEl.attr('data-type');
const cssSrc = parentEl.attr('data-css');
const themeId = parentEl.attr('data-theme');

if (config['theme:id'] === themeId) {
return;
}
bootbox.confirm('[[admin/appearance/themes:revert-confirm]]', function (confirm) {
if (confirm) {
socket.emit('admin.themes.set', {
type: 'local',
id: 'nodebb-theme-harmony',
}, function (err) {
if (err) {
return alerts.error(err);
}
config['theme:id'] = 'nodebb-theme-harmony';
highlightSelectedTheme('nodebb-theme-harmony');
alerts.alert({
alert_id: 'admin:theme',
type: 'success',
title: '[[admin/appearance/themes:theme-changed]]',
message: '[[admin/appearance/themes:revert-success]]',
timeout: 3500,
});
});
}
});
});
setTheme(themeType, themeId, cssSrc);
}
}

socket.emit('admin.themes.getInstalled', function (err, themes) {
function setTheme(themeType, themeId, cssSrc) {
socket.emit('admin.themes.set', {
type: themeType,
id: themeId,
src: cssSrc,
}, function (err) {
if (err) {
return alerts.error(err);
}
config['theme:id'] = themeId;
highlightSelectedTheme(themeId);
showChange();
});
}

const instListEl = $('#installed_themes');

if (!themes.length) {
instListEl.append($('<li/ >').addClass('no-themes').translateHtml('[[admin/appearance/themes:no-themes]]'));
} else {
app.parseAndTranslate('admin/partials/theme_list', {
themes: themes,
}, function (html) {
instListEl.html(html);
highlightSelectedTheme(config['theme:id']);
function showChange() {
alerts.alert({
alert_id: 'admin:theme',
type: 'info',
title: '[[admin/appearance/themes:theme-changed]]',
message: '[[admin/appearance/themes:restart-to-activate]]',
timeout: 5000,
clickfn: function () {
require(['admin/modules/instance'], function (instance) {
instance.rebuildAndRestart();
});
},
});
}

function clickedRevert() {
console.log('EVELYN');
if (config['theme:id'] === 'nodebb-theme-harmony') {
return;
}
bootbox.confirm('[[admin/appearance/themes:revert-confirm]]', function (confirm) {
if (confirm) {
revertTheme();
}
});
};
}

function revertTheme() {
socket.emit('admin.themes.set', {
type: 'local',
id: 'nodebb-theme-harmony',
}, function (err) {
if (err) {
return alerts.error(err);
}
config['theme:id'] = 'nodebb-theme-harmony';
highlightSelectedTheme('nodebb-theme-harmony');
alerts.alert({
alert_id: 'admin:theme',
type: 'success',
title: '[[admin/appearance/themes:theme-changed]]',
message: '[[admin/appearance/themes:revert-success]]',
timeout: 3500,
});
});
}

socket.emit('admin.themes.getInstalled', function (err, themes) {
if (err) {
return alerts.error(err);
}

const instListEl = $('#installed_themes');

if (!themes.length) {
instListEl.append($('<li/ >').addClass('no-themes').translateHtml('[[admin/appearance/themes:no-themes]]'));
} else {
app.parseAndTranslate('admin/partials/theme_list', {
themes: themes,
}, function (html) {
instListEl.html(html);
highlightSelectedTheme(config['theme:id']);
});
}
});


function highlightSelectedTheme(themeId) {
translator.translate('[[admin/appearance/themes:select-theme]] || [[admin/appearance/themes:current-theme]]', function (text) {
Expand All @@ -116,3 +132,4 @@ define('admin/appearance/themes', ['bootbox', 'translator', 'alerts'], function

return Themes;
});