Files
ui-lib/lib/ui.js
T
2026-11-18 10:54:43 +08:00

221 lines
5.2 KiB
JavaScript

import './ui/css/variables/definition.scss';
import './ui/css/common.scss';
import { createElement } from "./functions";
import { createIcon, changeIcon, resolveIcon } from "./ui/icon";
import { createCheckbox, createRadiobox, resolveCheckbox } from "./ui/checkbox";
import { setTooltip, resolveTooltip } from "./ui/tooltip";
import { createTab } from "./ui/tab";
import { Dropdown } from "./ui/dropdown";
import { Grid } from "./ui/grid/grid";
import { GridColumn, GridInputColumn, GridDropdownColumn, GridCheckboxColumn, GridIconColumn, GridTextColumn, GridDateColumn } from './ui/grid/column';
import { Popup, createPopup, resolvePopup, showAlert, showInput, showConfirm } from "./ui/popup";
import { createPicture, createAudio, createVideo, createFile, createVideoList } from './ui/media';
import { validation, convertCssStyle } from './ui/extension';
import { createDateInput, toDateValue, getFormatter, formatDate, setDateValue, getDateValue, DateSelector } from './ui/date';
import * as utility from './utility';
/**
* @param {FrameRequestCallback | TimerHandler} callback
* @returns {number}
*/
function requestAnimationFrame(callback) {
if (typeof utility.global.requestAnimationFrame === 'function') {
return utility.global.requestAnimationFrame(/** @type {FrameRequestCallback} */(callback));
}
return setTimeout(callback, 0);
}
/**
* @private
* @returns {number}
*/
function scrollLeft() {
const n = document.documentElement;
return (utility.global.scrollX || n.scrollLeft) - (n.clientLeft || 0);
}
/**
* @private
* @returns {number}
*/
function scrollTop() {
const n = document.documentElement;
return (utility.global.scrollY || n.scrollTop) - (n.clientTop || 0);
}
/**
* @param {HTMLElement} e
* @returns {DOMRectReadOnly}
*/
function offset(e) {
const rect = e.getBoundingClientRect();
return new DOMRectReadOnly(
rect.left + scrollLeft(),
rect.top + scrollTop(),
rect.width,
rect.height
);
}
class OptionBase {
_option;
/** @type {import('./types').GetTextCallback} */
r;
/**
* @param {import('./types').OptionParamBase} opt
*/
constructor(opt) {
this._option = opt;
const getText = opt.getText;
if (typeof getText === 'function') {
this.r = getText;
} else if (typeof utility.global.GetTextByKey === 'function') {
this.r = utility.global.GetTextByKey;
} else {
this.r = utility.r;
}
}
}
/**
* @private
* @param {HTMLDivElement} wrapper
* @param {(auto?: boolean) => void} [onDismissed]
* @param {boolean} [auto]
*/
function closeNotify(wrapper, onDismissed, auto) {
wrapper.classList.remove('active');
setTimeout(() => {
wrapper.remove();
if (typeof onDismissed === 'function') {
onDismissed(auto);
}
}, 120);
}
/**
* @param {import('./types').NotifyOption} [opts]
* @returns {HTMLDivElement | undefined}
*/
function notify(opts) {
const message = opts?.message;
if (message == null) {
return;
}
/** @type {number} */
let timer;
const close = createIcon('fa-light', 'times');
close.classList.add('ui-notify-close');
close.addEventListener('click', () => {
timer && clearTimeout(timer);
closeNotify(wrapper, opts?.onDismissed);
});
let type;
let typeClass;
switch (opts?.type) {
case 'warning':
type = 'exclamation-circle';
typeClass = 'warning';
break;
case 'error':
type = 'times-circle';
typeClass = 'error';
break;
default:
type = opts?.type ?? 'check-circle';
typeClass = 'success';
break;
}
const icon = createIcon('fa-solid', type);
icon.classList.add('ui-notify-type', typeClass);
const title = opts?.title;
const wrapper = createElement('div', 'ui-notify',
utility.nullOrEmpty(title) ?
createElement('div', 'ui-notify-single',
icon,
createElement('span', 'ui-notify-message', message),
close
) :
createElement('div', 'ui-notify-content',
createElement('div', 'ui-notify-header',
icon,
createElement('h2', 'ui-notify-title', title),
close
),
createElement('span', 'ui-notify-message', message)
)
);
if (opts?.persistent !== true) {
timer = setTimeout(() => {
closeNotify(wrapper, opts?.onDismissed, true);
}, opts?.timeout || 3000);
}
(opts?.parent ?? document.body).appendChild(wrapper);
setTimeout(() => wrapper.classList.add('active'), 10);
return wrapper;
}
export {
createElement,
// icon
createIcon,
changeIcon,
resolveIcon,
// checkbox
createCheckbox,
createRadiobox,
resolveCheckbox,
// tooltip
setTooltip,
resolveTooltip,
// tab
createTab,
// dropdown
Dropdown,
// grid
Grid,
GridColumn,
GridInputColumn,
GridDropdownColumn,
GridCheckboxColumn,
GridIconColumn,
GridTextColumn,
GridDateColumn,
// popup
Popup,
createPopup,
resolvePopup,
showAlert,
showInput,
showConfirm,
// dateSelector
createDateInput,
toDateValue,
getFormatter,
formatDate,
setDateValue,
getDateValue,
DateSelector,
// media
createPicture,
createAudio,
createVideo,
createFile,
createVideoList,
// extension
validation,
convertCssStyle,
// utility
utility,
// functions
requestAnimationFrame,
offset,
// base classes
OptionBase,
// notify
notify
}