add jsdoc comments

This commit is contained in:
lchen committed 2026-11-18 10:14:21 +08:00
1 parent 6de354ad47
commit 8c1205bc13
24 files changed
+1228 -425

No files matched your search

+110 -64
View File
@@ -6,6 +6,9 @@ import { createElement } from "../functions";
import { createIcon, changeIcon } from "./icon";
import { requestAnimationFrame } from "../ui";
/** @typedef {import('../types').PopupOptions} PopupOptions */
/** @typedef {import('../types').PopupRectParam} PopupRectParam */
const ResizeMods = {
right: 1,
bottom: 2,
@@ -30,58 +33,87 @@ const IconStyle = 'fa-light';
// [ResizeMods.topLeft]: 'nwse-resize'
// }
/**
* @private
* @param {string} px
* @returns {number | undefined}
*/
function trimPx(px) {
if (typeof px !== 'string') {
return px;
}
if (px.endsWith('px')) {
const size = Number(px.substring(0, px.length - 2));
return isNaN(size) ? px : size;
if (!isNaN(size)) {
return size;
}
}
return px;
}
/** @type {import('../types').Popup} */
export class Popup {
_var = {};
// _var.mask;
// _var.option;
// _var.bounds;
// _var.cursor;
_var = {
/** @type {HTMLElement | undefined} */
mask: undefined,
/** @type {PopupOptions | undefined} */
option: undefined,
/** @type {PopupRectParam | undefined} */
bounds: undefined,
cursor: undefined
};
constructor(opts = {}) {
/**
* @param {PopupOptions} [opts]
*/
constructor(opts) {
this._var.option = opts;
}
get container() { return this._var.mask.querySelector('.ui-popup-container') }
get container() { return /** @type {HTMLElement | undefined} */ (this._var.mask?.querySelector('.ui-popup-container')) }
get title() { return this._var.option.title }
get title() { return this._var.option?.title ?? ''; }
set title(title) {
const element = this._var.mask?.querySelector('.ui-popup-container .ui-popup-header .ui-popup-header-title');
if (element != null) {
element.innerText = title;
if (element instanceof HTMLElement) {
if (typeof title === 'string') {
element.innerText = title;
} else {
element.replaceChildren(title);
}
}
if (this._var.option != null) {
this._var.option.title = title;
}
this._var.option.title = title;
}
get loading() { return this._var.mask?.querySelector('.ui-popup-body>.ui-popup-loading')?.style?.visibility === 'visible' }
_getLoadingPanel() {
const panel = this._var.mask?.querySelector('.ui-popup-body>.ui-popup-loading');
if (panel instanceof HTMLElement) {
return panel;
}
return null;
}
get loading() { return this._getLoadingPanel()?.style?.visibility === 'visible' }
set loading(flag) {
let loading = this._var.mask?.querySelector('.ui-popup-body>.ui-popup-loading');
let loading = this._getLoadingPanel();
if (loading == null) {
return;
}
if (flag === false) {
loading.style.visibility = 'hidden';
loading.style.opacity = 0;
loading.style.opacity = '0';
} else {
loading.style.visibility = 'visible';
loading.style.opacity = 1;
loading.style.opacity = '1';
}
}
/** @returns {PopupRectParam | undefined} */
get rect() {
const container = this.container;
if (container == null) {
return null;
return;
}
const style = global.getComputedStyle(container);
const collapsed = container.classList.contains('ui-popup-collapse');
@@ -94,16 +126,19 @@ export class Popup {
height: collapsed === true && bounds != null ? bounds.height : trimPx(style.height)
};
}
/**
* @param {PopupRectParam} r
*/
set rect(r) {
const container = this.container;
if (container == null) {
return;
}
const css = [];
if (!isNaN(r.left)) {
if (r.left != null && !isNaN(r.left)) {
css.push(`left: ${r.left}px`);
}
if (!isNaN(r.top)) {
if (r.top != null && !isNaN(r.top)) {
css.push(`top: ${r.top}px`);
}
const collapse = container.querySelector('.ui-popup-header-icons>.icon-expand');
@@ -115,14 +150,14 @@ export class Popup {
changeIcon(collapse, IconStyle, 'expand-alt');
}
} else {
if (!isNaN(r.width) && r.width > 0) {
if (r.width != null && !isNaN(r.width) && r.width > 0) {
css.push(`width: ${r.width}px`);
}
if (!isNaN(r.height) && r.height > 0) {
if (r.height != null && !isNaN(r.height) && r.height > 0) {
css.push(`height: ${r.height}px`);
}
container.classList.remove('ui-popup-collapse');
this._var.bounds = null;
delete this._var.bounds;
if (collapse != null) {
changeIcon(collapse, IconStyle, 'compress-alt');
}
@@ -136,24 +171,28 @@ export class Popup {
const option = this._var.option;
const mask = this._var.mask;
const doClose = () => {
if (option.persistent) {
mask.style.display = 'none';
if (option?.persistent) {
if (mask != null) {
mask.style.display = 'none';
}
} else {
mask.remove();
this._var.mask = null;
mask?.remove();
delete this._var.mask;
}
}
if (animation) {
mask.classList.add('ui-popup-active');
mask.style.opacity = 0;
if (mask != null) {
mask.classList.add('ui-popup-active');
mask.style.opacity = '0';
}
setTimeout(() => { doClose(); }, 120);
} else {
doClose();
}
if (typeof option.onMasking === 'function') {
if (typeof option?.onMasking === 'function') {
option.onMasking.call(this, false);
}
if (typeof option.resolve === 'function') {
if (typeof option?.resolve === 'function') {
option.resolve.call(this, {
result,
popup: this
@@ -168,18 +207,18 @@ export class Popup {
create() {
const mask = createElement('div', 'ui-popup-mask ui-popup-active');
const option = this._var.option;
if (option.mask === false) {
if (option?.mask === false) {
mask.classList.add('ui-popup-transparent');
} else if (typeof option.onMasking === 'function') {
} else if (typeof option?.onMasking === 'function') {
option.onMasking.call(this, true);
}
if (!isNaN(option.zIndex)) {
if (option?.zIndex != null && !isNaN(option.zIndex)) {
mask.style.zIndex = String(option.zIndex);
}
const container = createElement('div', 'ui-popup-container');
if (option.changeZIndex === true) {
if (option?.changeZIndex === true) {
container.addEventListener('mousedown', () => {
const masks = [...this._var.mask.parentElement.children].filter(e => e.classList.contains('ui-popup-mask'));
const masks = [...this._var.mask?.parentElement?.children].filter(e => e.classList.contains('ui-popup-mask'));
let max = 200;
masks.forEach(m => {
let index;
@@ -195,7 +234,7 @@ export class Popup {
}
});
mask.dataset.zindex = mask.style.zIndex;
mask.style.zIndex = max + 1;
mask.style.zIndex = `${max + 1}`;
});
} else {
@@ -205,17 +244,17 @@ export class Popup {
tabIndex = 0;
}
container.tabIndex = tabIndex + 1;
let content = option.content;
let content = option?.content;
if (!(content instanceof HTMLElement)) {
content = createElement('div', d => d.innerText = content);
}
container.append(
createElement('div', header => {
header.className = 'ui-popup-header';
let title = option.title;
let title = option?.title;
if (!(title instanceof HTMLElement)) {
title = createElement('div', t => {
if (option.movable === false) {
if (option?.movable === false) {
t.className = 'ui-popup-header-title no-move';
} else {
t.className = 'ui-popup-header-title';
@@ -224,7 +263,7 @@ export class Popup {
});
}
header.appendChild(title);
if (option.movable !== false) {
if (option?.movable !== false) {
const move = header; // title.querySelector('.ui-popup-move') ?? title;
move.addEventListener('mousedown', e => {
if (['svg', 'use'].includes(e.target?.tagName)) {
@@ -233,9 +272,10 @@ export class Popup {
if (e.buttons !== 1) {
return;
}
const parent = option.mask === false ? mask.parentElement : mask;
const parent = option?.mask === false ? mask.parentElement : mask;
const x = e.clientX - container.offsetLeft;
const y = e.clientY - container.offsetTop;
/** @type {boolean} */
let moved;
const move = e => {
if (e.buttons === 1) {
@@ -250,7 +290,7 @@ export class Popup {
const up = () => {
parent.removeEventListener('mousemove', move, { passive: false });
parent.removeEventListener('mouseup', up);
if (moved === true && typeof option.onMoveEnded === 'function') {
if (moved === true && typeof option?.onMoveEnded === 'function') {
option.onMoveEnded.call(this);
}
moved = false;
@@ -260,7 +300,7 @@ export class Popup {
}
const icons = createElement('div', icons => {
icons.className = 'ui-popup-header-icons';
if (option.collapsable === true) {
if (option?.collapsable === true) {
const collapse = createIcon(IconStyle, 'compress-alt');
collapse.tabIndex = tabIndex + 2;
collapse.classList.add('icon-expand');
@@ -274,7 +314,7 @@ export class Popup {
const bounds = this._var.bounds;
if (bounds != null) {
container.style.cssText += `width: ${bounds.width}px; height: ${bounds.height}px`;
this._var.bounds = null;
delete this._var.bounds;
}
container.classList.remove('ui-popup-collapse');
changeIcon(collapse, IconStyle, 'compress-alt');
@@ -285,13 +325,13 @@ export class Popup {
container.classList.add('ui-popup-collapse');
changeIcon(collapse, IconStyle, 'expand-alt');
}
if (typeof option.onResizeEnded === 'function') {
if (typeof option?.onResizeEnded === 'function') {
option.onResizeEnded.call(this);
}
});
icons.appendChild(collapse);
}
if (option.closable !== false) {
if (option?.closable !== false) {
const cancel = createIcon(IconStyle, 'times');
cancel.tabIndex = tabIndex + 3;
cancel.addEventListener('keypress', e => {
@@ -309,7 +349,7 @@ export class Popup {
createElement('div', null, createIcon(IconStyle, 'spinner-third'))
))
);
if (Array.isArray(option.buttons) && option.buttons.length > 0) {
if (Array.isArray(option?.buttons) && option.buttons.length > 0) {
tabIndex = Math.max.apply(null, [...container.querySelectorAll('[tabindex]')].map(e => e.tabIndex ?? 0));
container.appendChild(
createElement('div', 'ui-popup-footer', ...option.buttons.map((b, i) => {
@@ -320,7 +360,7 @@ export class Popup {
if (b.isPrimary) {
button.classList.add('primary');
}
if (b.tabIndex > 0) {
if (b.tabIndex != null && b.tabIndex > 0) {
button.tabIndex = b.tabIndex;
} else {
button.tabIndex = tabIndex + i + 1;
@@ -362,7 +402,7 @@ export class Popup {
container.querySelector('.ui-popup-body>.ui-popup-loading').classList.add('ui-popup-loading-content');
}
// resizable
if (option.resizable === true) {
if (option?.resizable === true) {
container.append(
createElement('layer', layer => {
layer.className = 'ui-popup-border ui-popup-border-right';
@@ -430,18 +470,20 @@ export class Popup {
return Promise.resolve(mask);
}
}
if (this._var.option.mask === false) {
if (this._var.option?.mask === false) {
// calculator position
const container = this.container;
container.style.left = String((parent.offsetWidth - container.offsetWidth) / 2) + 'px';
container.style.top = String((parent.offsetHeight - container.offsetHeight) / 2) + 'px';
if (container != null) {
container.style.left = String((parent.offsetWidth - container.offsetWidth) / 2) + 'px';
container.style.top = String((parent.offsetHeight - container.offsetHeight) / 2) + 'px'
};
}
return new Promise(resolve => {
mask.style.display = '';
requestAnimationFrame(() => {
mask.classList.remove('ui-popup-active');
mask.style.opacity = 1;
this.container.focus();
mask.style.opacity = '1';
this.container?.focus();
setTimeout(() => resolve(mask), 120);
});
});
@@ -452,8 +494,11 @@ export class Popup {
return;
}
const container = this.container;
if (container == null) {
return;
}
const option = this._var.option;
if (typeof option.onResizeStarted === 'function') {
if (typeof option?.onResizeStarted === 'function') {
option.onResizeStarted.call(this);
}
const mask = this._var.mask;
@@ -467,13 +512,14 @@ export class Popup {
left: container.offsetLeft,
top: container.offsetTop
};
const minWidth = option.minWidth ?? 200;
const minHeight = option.minHeight ?? 200;
const minWidth = option?.minWidth ?? 200;
const minHeight = option?.minHeight ?? 200;
/** @type {boolean} */
let resized;
const parent = option.mask === false ? mask.parentElement : mask;
const parent = option?.mask === false ? mask.parentElement : mask;
const move = e => {
if (e.buttons !== 1) {
parent.dispatchEvent(new MouseEvent('mouseup'));
parent?.dispatchEvent(new MouseEvent('mouseup'));
return;
}
const offsetX = e.clientX - originalX;
@@ -512,24 +558,24 @@ export class Popup {
y += offsetY;
}
}
if (typeof option.onResizing === 'function') {
if (typeof option?.onResizing === 'function') {
option.onResizing.call(this, x, y, width, height);
} else {
container.style.cssText += `left: ${x}px; top: ${y}px; width: ${width}px; height: ${height}px`;
}
resized = true;
}
parent.addEventListener('mousemove', move, { passive: false });
parent?.addEventListener('mousemove', move, { passive: false });
const up = () => {
parent.removeEventListener('mousemove', move, { passive: false });
parent.removeEventListener('mouseup', up);
// mask.style.cursor = this._var.cursor;
if (resized === true && typeof option.onResizeEnded === 'function') {
if (resized === true && typeof option?.onResizeEnded === 'function') {
option.onResizeEnded.call(this);
}
resized = false;
};
parent.addEventListener('mouseup', up);
parent?.addEventListener('mouseup', up);
}
}