Skip to content

Commit

Permalink
refactor: add JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Dec 19, 2024
1 parent 5df2447 commit 5f39a96
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 27 deletions.
3 changes: 3 additions & 0 deletions js/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { confetti } from './confetti.js';

export const guest = (() => {

/**
* @type {ReturnType<typeof storage>|null}
*/
let information = null;

const countDownDate = () => {
Expand Down
6 changes: 4 additions & 2 deletions js/like.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { dto } from './dto.js';
import { offline } from './offline.js';
import { storage } from './storage.js';
import { session } from './session.js';
import { confetti } from './confetti.js';
import { request, HTTP_PATCH, HTTP_POST } from './request.js';

export const like = (() => {

/**
* @type {ReturnType<typeof storage>|null}
*/
let likes = null;

const like = async (button) => {
Expand Down Expand Up @@ -101,7 +103,7 @@ export const like = (() => {
};

const tapTap = async (div) => {
if (!offline.isOnline()) {
if (!navigator.onLine) {
return;
}

Expand Down
8 changes: 8 additions & 0 deletions js/navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import { bootstrap } from './bootstrap.js';

export const navbar = (() => {

/**
* @param {HTMLElement} btn
* @returns {void}
*/
const buttonNavHome = (btn) => {
bootstrap.Tab.getOrCreateInstance(document.getElementById('button-home')).show();
btn.classList.add('active');
document.getElementById('button-mobile-setting').classList.remove('active');
};

/**
* @param {HTMLElement} btn
* @returns {void}
*/
const buttonNavSetting = (btn) => {
bootstrap.Tab.getOrCreateInstance(document.getElementById('button-setting')).show();
btn.classList.add('active');
Expand Down
3 changes: 3 additions & 0 deletions js/offline.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export const offline = (() => {

/**
* @type {HTMLElement|null}
*/
let alert = null;
let online = true;

Expand Down
7 changes: 7 additions & 0 deletions js/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { guest } from './guest.js';

export const progress = (() => {

/**
* @type {HTMLElement|null}
*/
let info = null;

/**
* @type {HTMLElement|null}
*/
let bar = null;

let total = 0;
Expand Down
2 changes: 1 addition & 1 deletion js/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { request, HTTP_POST, HTTP_GET } from './request.js';
export const session = (() => {

/**
* @type {ReturnType<typeof storage>}
* @type {ReturnType<typeof storage>|null}
*/
let ses = null;

Expand Down
44 changes: 24 additions & 20 deletions js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,7 @@ export const theme = (() => {
}
};

const spyTop = () => {
const observerTop = new IntersectionObserver((es) => {
es.filter((e) => e.isIntersecting).forEach((e) => {
const themeColor = ['bg-black', 'bg-white'].some((i) => e.target.classList.contains(i))
? isDarkMode(themeDark[0], themeLight[0])
: isDarkMode(themeDark[1], themeLight[1]);

metaTheme.setAttribute('content', themeColor);
});
}, {
rootMargin: '0% 0% -95% 0%',
});

document.querySelectorAll('section').forEach((e) => observerTop.observe(e));
};

const init = () => {
theme = storage('theme');
metaTheme = document.querySelector('meta[name="theme-color"]');

const initObserver = () => {
observerLight = new IntersectionObserver((es, o) => {

es.filter((e) => e.isIntersecting).forEach((e) => toLight(e.target));
Expand All @@ -184,6 +165,29 @@ export const theme = (() => {
const now = metaTheme.getAttribute('content');
metaTheme.setAttribute('content', themeLight.some((i) => i === now) ? themeColors[now] : now);
});
};

const spyTop = () => {
const observerTop = new IntersectionObserver((es) => {
es.filter((e) => e.isIntersecting).forEach((e) => {
const themeColor = ['bg-black', 'bg-white'].some((i) => e.target.classList.contains(i))
? isDarkMode(themeDark[0], themeLight[0])
: isDarkMode(themeDark[1], themeLight[1]);

metaTheme.setAttribute('content', themeColor);
});
}, {
rootMargin: '0% 0% -95% 0%',
});

document.querySelectorAll('section').forEach((e) => observerTop.observe(e));
};

const init = () => {
theme = storage('theme');
metaTheme = document.querySelector('meta[name="theme-color"]');

initObserver();

if (!theme.has('active')) {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
Expand Down
43 changes: 39 additions & 4 deletions js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ import { bootstrap } from './bootstrap.js';

export const util = (() => {

/**
* @param {string} id
* @param {number} speed
* @returns {void}
*/
const opacity = (id, speed = 0.01) => {
const element = document.getElementById(id);
let op = parseInt(element.style.opacity);
const el = document.getElementById(id);
let op = parseInt(el.style.opacity);

let clear = null;
const callback = () => {
if (op > 0) {
element.style.opacity = op.toFixed(3);
el.style.opacity = op.toFixed(3);
op -= speed;
return;
}

clearInterval(clear);
clear = null;
element.remove();
el.remove();
};

clear = setInterval(callback, 10);
};

/**
* @param {string} unsafe
* @returns {string}
*/
const escapeHtml = (unsafe) => {
return String(unsafe)
.replace(/&/g, '&amp;')
Expand All @@ -32,6 +41,11 @@ export const util = (() => {
.replace(/'/g, '&#039;');
};

/**
* @param {function} callback
* @param {number} timeout
* @returns {void}
*/
const timeOut = (callback, timeout) => {
let clear = null;
const c = () => {
Expand Down Expand Up @@ -72,8 +86,18 @@ export const util = (() => {
};
};

/**
* @param {HTMLElement} svg
* @param {number} timeout
* @param {string} classes
* @returns {void}
*/
const animate = (svg, timeout, classes) => timeOut(() => svg.classList.add(classes), timeout);

/**
* @param {HTMLImageElement} img
* @returns {void}
*/
const modal = (img) => {
document.getElementById('show-modal-image').src = img.src;
bootstrap.Modal.getOrCreateInstance('#modal-image').show();
Expand Down Expand Up @@ -106,18 +130,29 @@ export const util = (() => {
}, timeout);
};

/**
* @param {string} str
* @returns {string}
*/
const base64Encode = (str) => {
const encoder = new TextEncoder();
const encodedBytes = encoder.encode(str);
return btoa(String.fromCharCode(...encodedBytes));
};

/**
* @param {string} str
* @returns {string}
*/
const base64Decode = (str) => {
const decoder = new TextDecoder();
const decodedBytes = Uint8Array.from(window.atob(str), (c) => c.charCodeAt(0));
return decoder.decode(decodedBytes);
};

/**
* @returns {void}
*/
const closeInformation = () => storage('information').set('info', true);

return {
Expand Down

0 comments on commit 5f39a96

Please sign in to comment.