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

Refactor code to reduce nesting in public/src/admin/appearance/skins.js #122

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Binary file added dump.rdb
Binary file not shown.
187 changes: 97 additions & 90 deletions public/src/admin/appearance/skins.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';


// refacter to fix nested callbacks 4 deep
define('admin/appearance/skins', [
'translator', 'alerts', 'settings', 'hooks', 'slugify',
], function (translator, alerts, settings, hooks, slugify) {
Expand All @@ -11,118 +11,125 @@ define('admin/appearance/skins', [
$.ajax({
method: 'get',
url: 'https://bootswatch.com/api/5.json',
}).done((bsData) => {
hooks.on('action:settings.sorted-list.loaded', (data) => {
if (data.hash === 'custom-skins') {
// slugify all custom-skin ids after load
$('.custom-skin-settings [data-type="list"] [data-theme]').each((i, el) => {
$(el).attr('data-theme', slugify($(el).attr('data-theme')));
});
highlightSelectedTheme(app.config.bootswatchSkin);
}
});
settings.load('custom-skins', $('.custom-skin-settings'));
Skins.render(bsData);
});
}).done(handleBootswatchData);

$('#save-custom-skins').on('click', function () {
settings.save('custom-skins', $('.custom-skin-settings'), function () {
alerts.success('[[admin/appearance/skins:save-custom-skins-success]]');
});
return false;
});
$('#save-custom-skins').on('click', handleSaveCustomSkins);
$('#skins').on('click', handleSkinClick);
};

function handleBootswatchData(bsData) {
hooks.on('action:settings.sorted-list.loaded', handleSettingsLoaded);
settings.load('custom-skins', $('.custom-skin-settings'));
Skins.render(bsData);
}

function handleSettingsLoaded(data) {
if (data.hash === 'custom-skins') {
slugifyThemes();
highlightSelectedTheme(app.config.bootswatchSkin);
}
}

$('#skins').on('click', function (e) {
let target = $(e.target);
function slugifyThemes() {
$('.custom-skin-settings [data-type="list"] [data-theme]').each((i, el) => {
const theme = $(el).attr('data-theme');
$(el).attr('data-theme', slugify(theme));
});
}

if (!target.attr('data-action')) {
target = target.parents('[data-action]');
}
function handleSaveCustomSkins() {
settings.save('custom-skins', $('.custom-skin-settings'), function () {
alerts.success('[[admin/appearance/skins:save-custom-skins-success]]');
});
return false;
}

const action = target.attr('data-action');

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

socket.emit('admin.themes.set', {
type: 'bootswatch',
id: themeId,
src: cssSrc,
}, function (err) {
if (err) {
return alerts.error(err);
}
highlightSelectedTheme(themeId);

alerts.alert({
alert_id: 'admin:theme',
type: 'info',
title: '[[admin/appearance/skins:skin-updated]]',
message: themeId ? ('[[admin/appearance/skins:applied-success, ' + themeName + ']]') : '[[admin/appearance/skins:revert-success]]',
timeout: 5000,
});
});
function handleSkinClick(e) {
let target = $(e.target);
if (!target.attr('data-action')) {
target = target.parents('[data-action]');
}
const action = target.attr('data-action');
if (action && action === 'use') {
applySkin(target);
}
}

function applySkin(target) {
const parentEl = target.parents('[data-theme]');
const cssSrc = parentEl.attr('data-css');
const themeId = parentEl.attr('data-theme');
const themeName = parentEl.attr('data-theme-name');

socket.emit('admin.themes.set', {
type: 'bootswatch',
id: themeId,
src: cssSrc,
}, function (err) {
if (err) {
return alerts.error(err);
}
highlightSelectedTheme(themeId);

alerts.alert({
alert_id: 'admin:theme',
type: 'info',
title: '[[admin/appearance/skins:skin-updated]]',
message: themeId ? ('[[admin/appearance/skins:applied-success, ' + themeName + ']]') : '[[admin/appearance/skins:revert-success]]',
timeout: 5000,
});
});
};
}

Skins.render = function (bootswatch) {
const themeContainer = $('#bootstrap_themes');

app.parseAndTranslate('admin/partials/theme_list', {
themes: bootswatch.themes.map(function (theme) {
return {
type: 'bootswatch',
id: theme.name.toLowerCase(),
name: theme.name,
description: theme.description,
screenshot_url: theme.thumbnail,
url: theme.preview,
css: theme.cssCdn,
skin: true,
};
}),
themes: bootswatch.themes.map(mapThemes),
showRevert: true,
}, function (html) {
themeContainer.html(html);

highlightSelectedTheme(app.config.bootswatchSkin);
});
};

function mapThemes(theme) {
return {
type: 'bootswatch',
id: theme.name.toLowerCase(),
name: theme.name,
description: theme.description,
screenshot_url: theme.thumbnail,
url: theme.preview,
css: theme.cssCdn,
skin: true,
};
}

function highlightSelectedTheme(themeId) {
translator.translate('[[admin/appearance/skins:select-skin]] || [[admin/appearance/skins:current-skin]]', function (text) {
text = text.split(' || ');
const select = text[0];
const current = text[1];

$('[data-theme]')
.removeClass('selected')
.find('[data-action="use"]').each(function () {
if ($(this).parents('[data-theme]').attr('data-theme')) {
$(this)
.html(select)
.removeClass('btn-success')
.addClass('btn-primary');
}
});

if (!themeId) {
return;
const [select, current] = text.split(' || ');
resetThemeSelection(select);
if (themeId) {
setActiveTheme(themeId, current);
}

$('[data-theme="' + themeId + '"]')
.addClass('selected')
.find('[data-action="use"]')
.html(current)
.removeClass('btn-primary')
.addClass('btn-success');
});
}

function resetThemeSelection(select) {
$('[data-theme]').removeClass('selected')
.find('[data-action="use"]').each(function () {
const theme = $(this).parents('[data-theme]').attr('data-theme');
if (theme) {
$(this).html(select).removeClass('btn-success').addClass('btn-primary');
}
});
}

function setActiveTheme(themeId, current) {
$(`[data-theme="${themeId}"]`).addClass('selected')
.find('[data-action="use"]').html(current)
.removeClass('btn-primary')
.addClass('btn-success');
}
return Skins;
});
Loading