Skip to content

Commit

Permalink
feat: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Oct 5, 2024
1 parent a7b483a commit 6239f0e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { pagination } from './pagination.js';

document.addEventListener('DOMContentLoaded', () => {
audio.init();
theme.check();
theme.init();
pagination.init();

guest.init();
Expand Down
3 changes: 1 addition & 2 deletions js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { pagination } from './pagination.js';

document.addEventListener('DOMContentLoaded', () => {
admin.init();

theme.check();
theme.init();
pagination.init();

window.like = like;
Expand Down
2 changes: 1 addition & 1 deletion js/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export const guest = (() => {
};

const init = () => {
countDownDate();

if (session.isAdmin()) {
storage('user').clear();
Expand Down Expand Up @@ -148,6 +147,7 @@ export const guest = (() => {
}

session.guest();
countDownDate();
};

return {
Expand Down
44 changes: 19 additions & 25 deletions js/theme.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { storage } from './storage.js';

export const THEME_DARK = 'dark';
export const THEME_LIGHT = 'light';
export const THEME_BS_DATA = 'data-bs-theme';

export const theme = (() => {

const THEME_DARK = 'dark';
const THEME_LIGHT = 'light';

const theme = storage('theme');

const onLight = () => {
theme.set('active', THEME_LIGHT);
document.documentElement.setAttribute('data-bs-theme', THEME_LIGHT);

const elements = document.querySelectorAll('.text-light, .btn-theme-light, .bg-dark, .bg-black, .bg-theme-dark, .color-theme-black, .btn-outline-light, .bg-cover-black');
elements.forEach((element) => {
if (element.classList.contains('text-light')) {
Expand Down Expand Up @@ -54,6 +56,9 @@ export const theme = (() => {
};

const onDark = () => {
theme.set('active', THEME_DARK);
document.documentElement.setAttribute('data-bs-theme', THEME_DARK);

const elements = document.querySelectorAll('.text-dark, .btn-theme-dark, .bg-light, .bg-white, .bg-theme-light, .color-theme-white, .btn-outline-dark, .bg-cover-white');
elements.forEach((element) => {
if (element.classList.contains('text-dark')) {
Expand Down Expand Up @@ -111,16 +116,16 @@ export const theme = (() => {
const change = () => {
if (isDarkMode()) {
onLight();
document.documentElement.setAttribute(THEME_BS_DATA, THEME_LIGHT);
theme.set('active', THEME_LIGHT);
} else {
onDark();
document.documentElement.setAttribute(THEME_BS_DATA, THEME_DARK);
theme.set('active', THEME_DARK);
}
};

const check = () => {
const showButtonChangeTheme = () => {
document.getElementById('button-theme').style.display = 'block';
};

const init = () => {
if (!theme.has('active')) {
theme.set('active', THEME_LIGHT);

Expand All @@ -129,32 +134,21 @@ export const theme = (() => {
}
}

const toggle = document.getElementById('darkMode');

if (isDarkMode()) {
onDark();
document.documentElement.setAttribute(THEME_BS_DATA, THEME_DARK);
theme.set('active', THEME_DARK);
if (toggle) {
toggle.checked = true;
}
} else {
onLight();
document.documentElement.setAttribute(THEME_BS_DATA, THEME_LIGHT);
theme.set('active', THEME_LIGHT);
if (toggle) {
toggle.checked = false;
}
}
};

const showButtonChangeTheme = () => {
document.getElementById('button-theme').style.display = 'block';
const toggle = document.getElementById('darkMode');
if (toggle) {
toggle.checked = isDarkMode();
}
};

return {
change,
check,
init,
isDarkMode,
showButtonChangeTheme
};
Expand Down

0 comments on commit 6239f0e

Please sign in to comment.