初版:上传接口(从线上实例导出,适配容器端口)
This commit is contained in:
@@ -0,0 +1,970 @@
|
||||
/**
|
||||
* MorphCard — 胶囊按钮 → 卡片 多状态形变组件(公共库)
|
||||
*
|
||||
* v6:临界阻尼流体弹簧引擎。
|
||||
* 容器位置/尺寸/底色/圆角不再走 CSS transition,而是由一组 ζ=1.0(临界阻尼、无过冲)
|
||||
* 的二阶弹簧逐帧驱动;border-radius 每帧由当前宽高推导:
|
||||
* radius = min( min(w,h)/2, lerp(min(w,h)/2 → 28, 圆角揭示进度) )
|
||||
* 胶囊阶段天然等于高度一半(完整圆端),随轮廓长大连续收敛到卡片圆角,
|
||||
* 与宽高/位置共享同一组弹簧,全程同步、无跳变。
|
||||
* 内容面的透明度/位移/模糊/级联也由面弹簧的当前值推导,可中断、无定时器硬切。
|
||||
*
|
||||
* 用法(任何页面):
|
||||
* <script src="/static/js/morph-card.js"></script>
|
||||
* <script>
|
||||
* const card = MorphCard.init({
|
||||
* // 可选配置(以下为默认值)
|
||||
* successTitle: '提交成功',
|
||||
* successDesc: '您填写的数据已保存。',
|
||||
* doneText: '确定',
|
||||
* errorTitle: '无法提交',
|
||||
* errorDefault: '提交失败,请稍后重试',
|
||||
* backText: '返回修改',
|
||||
* progressLabel: '正在上传',
|
||||
* onExpand: (sourceEl) => {}, // 卡片从来源元素展开时回调
|
||||
* onCollapse: (sourceEl) => {}, // 卡片收回、来源元素交还时回调
|
||||
* });
|
||||
*
|
||||
* card.open('progress', { sourceEl: btn }); // 从某元素形变展开(verify/confirm/progress/success/error)
|
||||
* card.setState('success'); // 已展开时切换内容面(宽高随弹簧平滑过渡)
|
||||
* card.progress({ percent: 68, text: '2.3 MB/s · 剩余 00:04' });
|
||||
* card.success(); // 成功面(对勾笔画 + 粒子)
|
||||
* card.error('具体的错误信息'); // 错误面(不自动收起,用户点"返回修改"关闭)
|
||||
* card.close(); // 收回来源位置
|
||||
*
|
||||
* // 确认卡片:无输入框的二次确认(如"仅可提交一次"提示)。
|
||||
* // onConfirm 返回 true=确认(keepOpen=true 时卡片保持展开由调用方切下一面);
|
||||
* // 返回字符串=错误提示显示在卡片内;取消/backdrop/Esc resolve false。
|
||||
* const ok = await card.confirm({
|
||||
* title: '仅可提交一次', desc: '提交后不能再修改。',
|
||||
* okText: '确认提交', cancelText: '再检查一下',
|
||||
* sourceEl: btn, keepOpen: true,
|
||||
* onConfirm: async () => true,
|
||||
* });
|
||||
*
|
||||
* // 验证卡片:尾号/验证码输入,resolve 输入值;取消 resolve null;
|
||||
* // onSubmit 返回 true=通过;返回字符串=错误提示(卡片内显示并抖动)
|
||||
* const value = await card.verify({
|
||||
* title: '加载上次提交', desc: '输入学号后 2 位…',
|
||||
* maxlength: 2, placeholder: '••', okText: '验证并加载',
|
||||
* inputFilter: v => v.replace(/\D/g, ''),
|
||||
* sourceEl: btn, keepOpen: false,
|
||||
* onSubmit: async (value) => true,
|
||||
* });
|
||||
* </script>
|
||||
*
|
||||
* 依赖:页面主题变量(--accent-bg、--bg-secondary、--text-primary、--border-color 等,
|
||||
* 由 basic.css 提供),缺失时使用内置深色兜底值。
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
if (window.MorphCard) return; // 单例
|
||||
|
||||
/* ---------- 弹簧参数 ---------- */
|
||||
/* 质量=1 二阶弹簧:ω = 2π/response,k = ω²,c = 2ζω。
|
||||
ζ = 1 临界阻尼(高阻尼、无过冲、平滑收敛);
|
||||
response 是"到达目标的特征时间"(秒),不是固定时长。 */
|
||||
const RESPONSE = 0.45;
|
||||
const ZETA = 1.0;
|
||||
const RESPONSE_REVEAL = RESPONSE + 0.12; // 圆角揭示略滞后于轮廓,更像液体
|
||||
const RESPONSE_BG = RESPONSE + 0.08;
|
||||
const RESPONSE_BG_COLLAPSE = 0.16; // 收起时先回到按钮主色,再完成轮廓收缩
|
||||
const FACE_IN = 0.55, FACE_OUT = 0.22; // 内容面弹簧(入/出)
|
||||
const GATE_AT = 0.45; // 展开时轮廓揭示超过该比例后内容才开始入场
|
||||
const CARD_R = 28;
|
||||
const SETTLE_GUARD_MS = 1000; // busy 兜底(约等于弹簧收敛时间)
|
||||
const COLLAPSE_SAFETY_MS = 2000; // 收起兜底(后台标签页 rAF 暂停时也能收尾)
|
||||
|
||||
const FACE_WIDTH = { verify: 300, confirm: 300, progress: 250, success: 320, error: 320 };
|
||||
const CARD_PAD_X = 44;
|
||||
const CARD_PAD_Y = 48;
|
||||
const ACCENT = [23, 23, 23]; // 兜底胶囊底色(正常被 --accent-bg 覆盖读取)
|
||||
const CARD_BG = [255, 255, 255]; // 兜底卡片底色(正常被 --bg-secondary 覆盖读取)
|
||||
|
||||
const reduceMotion = window.matchMedia && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||||
|
||||
/* ---------- 弹簧引擎 ---------- */
|
||||
const spring = v => ({ x: v, v: 0, t: v });
|
||||
function stepSpring(s, dt, response, zeta) {
|
||||
if (dt <= 0) return;
|
||||
const w = 2 * Math.PI / response, k = w * w, c = 2 * (zeta == null ? ZETA : zeta) * w;
|
||||
const n = Math.max(1, Math.ceil(dt / (1 / 240))); // 固定子步长,保证数值稳定
|
||||
const h = dt / n;
|
||||
for (let i = 0; i < n; i++) {
|
||||
s.v += (-k * (s.x - s.t) - c * s.v) * h;
|
||||
s.x += s.v * h;
|
||||
}
|
||||
}
|
||||
const settledS = s => Math.abs(s.x - s.t) < 0.05 && Math.abs(s.v) < 0.05;
|
||||
const clamp = (v, a, b) => Math.min(b, Math.max(a, v));
|
||||
const lerp = (a, b, t) => a + (b - a) * t;
|
||||
const easeOutCubic = t => 1 - Math.pow(1 - t, 3);
|
||||
|
||||
/* ---------- 样式注入 ----------
|
||||
说明:--um-ease-* / --um-morph-ms 变量保留为外部兼容钩子(页面级 CSS 可引用),
|
||||
组件本体已不再使用 CSS 过渡做形变。 */
|
||||
const CSS = `
|
||||
:root {
|
||||
--um-morph-ms: 560ms;
|
||||
--um-ease-morph: cubic-bezier(0.32, 0.72, 0, 1);
|
||||
--um-ease-out: cubic-bezier(0.23, 1, 0.32, 1);
|
||||
--um-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
|
||||
}
|
||||
body.um-locked { overflow: hidden; }
|
||||
|
||||
.um-backdrop {
|
||||
position: fixed; inset: 0; z-index: 90;
|
||||
background: rgba(8, 10, 14, 0.45);
|
||||
backdrop-filter: blur(10px) saturate(1.1);
|
||||
-webkit-backdrop-filter: blur(10px) saturate(1.1);
|
||||
opacity: 0; pointer-events: none;
|
||||
}
|
||||
.um-backdrop.show { pointer-events: auto; }
|
||||
|
||||
.um-morpher {
|
||||
position: fixed; left: 0; top: 0; z-index: 95; display: none;
|
||||
border: none; border-radius: 999px;
|
||||
background: var(--accent-bg, #171717);
|
||||
overflow: hidden; user-select: none; -webkit-tap-highlight-color: transparent;
|
||||
will-change: transform, width, height;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
/* 胶囊面的 spinner:随圆角揭示进度淡出(JS 逐帧写 opacity) */
|
||||
.um-spinner {
|
||||
position: absolute; left: 50%; top: 50%; width: 22px; height: 22px;
|
||||
margin: -11px 0 0 -11px; border-radius: 50%;
|
||||
border: 2.5px solid color-mix(in srgb, var(--accent-text, #fff) 30%, transparent);
|
||||
border-top-color: var(--accent-text, #fff);
|
||||
animation: um-spin 0.7s linear infinite;
|
||||
pointer-events: none;
|
||||
}
|
||||
@keyframes um-spin { to { transform: rotate(360deg); } }
|
||||
|
||||
/* 多状态面:verify / confirm / progress / success / error
|
||||
透明度/模糊由面弹簧逐帧写入,子元素级联亦由弹簧值推导 */
|
||||
.um-face {
|
||||
position: absolute; inset: 0;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
overflow-y: auto;
|
||||
opacity: 0; pointer-events: none;
|
||||
}
|
||||
|
||||
.um-card-inner {
|
||||
display: flex; flex-direction: column; align-items: center;
|
||||
width: 320px; max-width: 100%; margin: auto;
|
||||
text-align: center;
|
||||
color: var(--text-primary, #171717);
|
||||
}
|
||||
|
||||
.um-badge-wrap { position: relative; width: 76px; height: 76px; }
|
||||
.um-badge {
|
||||
position: absolute; inset: 0; border-radius: 50%;
|
||||
background: linear-gradient(135deg, #34d399, #059669);
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
box-shadow: 0 10px 28px -8px rgba(16, 185, 129, 0.55);
|
||||
transform: scale(0.4); opacity: 0;
|
||||
transition: transform 0.4s var(--um-spring), opacity 0.2s ease;
|
||||
}
|
||||
.um-morpher.um-badge-in .um-badge { transform: scale(1); opacity: 1; }
|
||||
.um-badge-error {
|
||||
background: linear-gradient(135deg, #f87171, #dc2626);
|
||||
box-shadow: 0 10px 28px -8px rgba(220, 38, 38, 0.5);
|
||||
}
|
||||
.um-badge svg { width: 34px; height: 34px; }
|
||||
.um-check {
|
||||
fill: none; stroke: #fff; stroke-width: 2.6;
|
||||
stroke-linecap: round; stroke-linejoin: round;
|
||||
stroke-dasharray: 1; stroke-dashoffset: 1;
|
||||
}
|
||||
.um-morpher.is-done .um-check {
|
||||
stroke-dashoffset: 0;
|
||||
transition: stroke-dashoffset 0.42s cubic-bezier(0.65, 0, 0.35, 1);
|
||||
}
|
||||
.um-ring-pulse {
|
||||
position: absolute; inset: -2px; border-radius: 50%;
|
||||
border: 2px solid #10b981; opacity: 0; pointer-events: none;
|
||||
}
|
||||
.um-morpher.is-done .um-ring-pulse { animation: um-ripple 0.65s ease-out 0.15s both; }
|
||||
@keyframes um-ripple {
|
||||
0% { transform: scale(1); opacity: 0.55; }
|
||||
100% { transform: scale(1.75); opacity: 0; }
|
||||
}
|
||||
|
||||
.um-title {
|
||||
font-family: 'Playfair Display', Georgia, 'Times New Roman', serif;
|
||||
font-size: 24px; font-weight: 500; margin-top: 22px;
|
||||
color: inherit;
|
||||
}
|
||||
.um-desc {
|
||||
font-size: 13.5px; color: var(--text-secondary, #525252);
|
||||
line-height: 1.75; margin-top: 10px;
|
||||
}
|
||||
.um-btn {
|
||||
width: 100%; height: 48px; margin-top: 26px;
|
||||
border: none; border-radius: 999px; cursor: pointer;
|
||||
background: var(--accent-bg, #171717); color: var(--accent-text, #ffffff);
|
||||
font: 600 14px/1 inherit; letter-spacing: 0.02em;
|
||||
transition: transform 0.16s ease-out, opacity 0.15s ease;
|
||||
}
|
||||
.um-btn:active { transform: scale(0.97); }
|
||||
|
||||
/* 验证面 */
|
||||
.um-verify-inner { width: 300px; max-width: 100%; }
|
||||
.um-verify-input {
|
||||
display: block; width: 170px; margin: 22px auto 0; padding: 10px 6px;
|
||||
border: none; border-bottom: 2px solid var(--border-color, #e5e5e5);
|
||||
border-radius: 0; background: transparent; outline: none;
|
||||
text-align: center; letter-spacing: 0.35em; text-indent: 0.35em;
|
||||
font-size: 26px; font-weight: 600; line-height: 1.4;
|
||||
color: var(--text-primary, #171717); font-family: inherit;
|
||||
caret-color: var(--accent-bg, #171717);
|
||||
transition: border-color 0.2s ease;
|
||||
}
|
||||
.um-verify-input:focus { border-color: var(--text-primary, #171717); }
|
||||
.um-verify-input.um-input-error { border-color: var(--error-text, #ef4444); }
|
||||
.um-verify-err {
|
||||
min-height: 20px; margin-top: 10px;
|
||||
font-size: 12px; color: var(--error-text, #ef4444);
|
||||
}
|
||||
.um-verify-actions { display: flex; gap: 10px; width: 100%; margin-top: 8px; }
|
||||
.um-verify-actions .um-btn { flex: 1; width: auto; margin-top: 0; }
|
||||
.um-btn-ghost {
|
||||
background: transparent; color: var(--text-secondary, #525252);
|
||||
border: 1px solid var(--border-color, #e5e5e5);
|
||||
}
|
||||
|
||||
/* 进度面 */
|
||||
.um-progress-inner { width: 250px; max-width: 100%; }
|
||||
.um-progress-label {
|
||||
font-size: 11px; letter-spacing: 0.4em; text-transform: uppercase;
|
||||
color: var(--text-muted, #737373);
|
||||
}
|
||||
.um-progress-ring { position: relative; width: 132px; height: 132px; margin-top: 28px; }
|
||||
.um-progress-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
|
||||
.um-ring-track { fill: none; stroke: var(--border-color, #e5e5e5); stroke-width: 5; }
|
||||
.um-ring-fill {
|
||||
fill: none; stroke: var(--text-primary, #171717); stroke-width: 5; stroke-linecap: round;
|
||||
stroke-dasharray: 100; stroke-dashoffset: 100;
|
||||
transition: stroke-dashoffset 0.25s linear;
|
||||
}
|
||||
.um-progress-percent {
|
||||
position: absolute; inset: 0;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
font-size: 26px; font-weight: 600; color: var(--text-primary, #171717);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.um-progress-sub {
|
||||
margin-top: 24px; font-size: 12.5px; color: var(--text-muted, #737373);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.um-error-inner .um-desc { color: var(--error-text, #ef4444); }
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.um-spinner, .um-badge, .um-ring-pulse, .um-btn, .um-check {
|
||||
transition-duration: 1ms !important;
|
||||
animation-duration: 1ms !important;
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
/* ---------- DOM ---------- */
|
||||
function buildDom(opts) {
|
||||
const style = document.createElement('style');
|
||||
style.textContent = CSS;
|
||||
document.head.appendChild(style);
|
||||
|
||||
const backdrop = document.createElement('div');
|
||||
backdrop.className = 'um-backdrop';
|
||||
backdrop.id = 'umBackdrop';
|
||||
|
||||
const morpher = document.createElement('div');
|
||||
morpher.className = 'um-morpher';
|
||||
morpher.id = 'uploadMorpher';
|
||||
morpher.setAttribute('role', 'dialog');
|
||||
morpher.setAttribute('aria-modal', 'true');
|
||||
morpher.innerHTML = `
|
||||
<span class="um-spinner" aria-hidden="true"></span>
|
||||
|
||||
<div class="um-face um-face-verify">
|
||||
<div class="um-card-inner um-verify-inner">
|
||||
<h3 class="um-title" id="umVerifyTitle">身份验证</h3>
|
||||
<p class="um-desc" id="umVerifyDesc"></p>
|
||||
<input type="text" class="um-verify-input" id="umVerifyInput" autocomplete="off" />
|
||||
<p class="um-verify-err" id="umVerifyError"></p>
|
||||
<div class="um-verify-actions">
|
||||
<button type="button" class="um-btn um-btn-ghost" id="umVerifyCancel">取消</button>
|
||||
<button type="button" class="um-btn" id="umVerifyOk">确认</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="um-face um-face-confirm">
|
||||
<div class="um-card-inner um-confirm-inner">
|
||||
<h3 class="um-title" id="umConfirmTitle">确认提交</h3>
|
||||
<p class="um-desc" id="umConfirmDesc"></p>
|
||||
<p class="um-verify-err" id="umConfirmError"></p>
|
||||
<div class="um-verify-actions">
|
||||
<button type="button" class="um-btn um-btn-ghost" id="umConfirmCancel">取消</button>
|
||||
<button type="button" class="um-btn" id="umConfirmOk">确认提交</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="um-face um-face-progress">
|
||||
<div class="um-card-inner um-progress-inner">
|
||||
<p class="um-progress-label">${opts.progressLabel}</p>
|
||||
<div class="um-progress-ring">
|
||||
<svg viewBox="0 0 120 120">
|
||||
<circle class="um-ring-track" cx="60" cy="60" r="52" pathLength="100"/>
|
||||
<circle class="um-ring-fill" id="umProgressRing" cx="60" cy="60" r="52" pathLength="100"/>
|
||||
</svg>
|
||||
<span class="um-progress-percent" id="umProgressPercent">0%</span>
|
||||
</div>
|
||||
<p class="um-progress-sub" id="umProgressSub">准备中…</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="um-face um-face-success">
|
||||
<div class="um-card-inner">
|
||||
<span class="um-badge-wrap">
|
||||
<span class="um-ring-pulse"></span>
|
||||
<span class="um-badge">
|
||||
<svg viewBox="0 0 24 24"><path class="um-check" d="M5 13l4 4L19 7" pathLength="1"/></svg>
|
||||
</span>
|
||||
</span>
|
||||
<h3 class="um-title">${opts.successTitle}</h3>
|
||||
<p class="um-desc">${opts.successDesc}</p>
|
||||
<button type="button" class="um-btn" id="umDoneBtn">${opts.doneText}</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="um-face um-face-error">
|
||||
<div class="um-card-inner um-error-inner">
|
||||
<span class="um-badge-wrap">
|
||||
<span class="um-badge um-badge-error">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="2.6" stroke-linecap="round">
|
||||
<path d="M12 6v7"/><path d="M12 16.5v.01"/>
|
||||
</svg>
|
||||
</span>
|
||||
</span>
|
||||
<h3 class="um-title">${opts.errorTitle}</h3>
|
||||
<p class="um-desc" id="umErrorText">${opts.errorDefault}</p>
|
||||
<button type="button" class="um-btn" id="umErrorBack">${opts.backText}</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.body.appendChild(backdrop);
|
||||
document.body.appendChild(morpher);
|
||||
|
||||
const faces = {}, inners = {};
|
||||
morpher.querySelectorAll('.um-face').forEach(f => {
|
||||
const name = /um-face-(\w+)/.exec(f.className)[1];
|
||||
faces[name] = f;
|
||||
inners[name] = f.querySelector('.um-card-inner');
|
||||
});
|
||||
return { backdrop, morpher, faces, inners, spinner: morpher.querySelector('.um-spinner') };
|
||||
}
|
||||
|
||||
/* ---------- 组件 ---------- */
|
||||
const state = {
|
||||
open: false, busy: false, collapsing: false,
|
||||
savedRect: null, sourceEl: null, verifyActive: false,
|
||||
activeFace: null, gateOpen: false, fxStage: 0
|
||||
};
|
||||
let dom = null;
|
||||
let opts = null;
|
||||
let measurer = null;
|
||||
|
||||
/* ---------- 弹簧组 ---------- */
|
||||
const box = { x: spring(0), y: spring(0), w: spring(0), h: spring(0), reveal: spring(0), bg: spring(0), op: spring(1) };
|
||||
const backdropS = spring(0);
|
||||
const faceS = {};
|
||||
for (const k of Object.keys(FACE_WIDTH)) faceS[k] = spring(0);
|
||||
|
||||
/* 底色读取:优先页面主题变量,读不到用兜底值。按次缓存,主题切换/下次展开时刷新 */
|
||||
function parseColor(varName, fallback, themeEl) {
|
||||
const v = getComputedStyle(themeEl).getPropertyValue(varName).trim();
|
||||
if (!v) return fallback;
|
||||
const m = v.match(/^#([0-9a-f]{6})$/i) || v.match(/^rgb\(\s*(\d+)[,\s]+(\d+)[,\s]+(\d+)\s*\)$/i);
|
||||
if (m) {
|
||||
return m.length === 2
|
||||
? [parseInt(m[1].slice(0, 2), 16), parseInt(m[1].slice(2, 4), 16), parseInt(m[1].slice(4, 6), 16)]
|
||||
: [+m[1], +m[2], +m[3]];
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
let cachedAccent = null, cachedCard = null, cachedBorder = null;
|
||||
function refreshTheme() {
|
||||
// 页面可在 body 或按钮上覆盖主题变量;从形变来源读取,颜色才能与按钮一致。
|
||||
const themeEl = state.sourceEl?.isConnected ? state.sourceEl : document.body;
|
||||
cachedAccent = parseColor('--accent-bg', ACCENT, themeEl);
|
||||
cachedCard = parseColor('--bg-secondary', CARD_BG, themeEl);
|
||||
cachedBorder = parseColor('--border-color', [229, 229, 229], themeEl);
|
||||
}
|
||||
|
||||
let rafId = null;
|
||||
let lastT = 0;
|
||||
let collapseSafety = null;
|
||||
|
||||
function ensureLoop() {
|
||||
if (rafId == null) { lastT = performance.now(); rafId = requestAnimationFrame(tick); }
|
||||
}
|
||||
|
||||
function allContainerSettled() {
|
||||
return [box.x, box.y, box.w, box.h, box.reveal, box.bg, box.op, backdropS].every(settledS);
|
||||
}
|
||||
function allFacesSettled() {
|
||||
for (const k in faceS) if (!settledS(faceS[k])) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function visibleViewport() {
|
||||
const vv = window.visualViewport;
|
||||
if (vv) {
|
||||
return { top: vv.offsetTop, left: vv.offsetLeft, width: vv.width, height: vv.height };
|
||||
}
|
||||
return { top: 0, left: 0, width: window.innerWidth, height: window.innerHeight };
|
||||
}
|
||||
|
||||
function targetRectFallback() {
|
||||
const w = Math.min(440, window.innerWidth - 32);
|
||||
const h = Math.min(560, window.innerHeight - 48);
|
||||
return { left: (window.innerWidth - w) / 2, top: (window.innerHeight - h) / 2, width: w, height: h };
|
||||
}
|
||||
|
||||
/* 离屏克隆目标状态面,测量其自然内容尺寸,返回在给定视口内居中的目标矩形 */
|
||||
function measureState(stateName, vp) {
|
||||
const face = dom.faces[stateName];
|
||||
if (!face) return targetRectFallback();
|
||||
vp = vp || visibleViewport();
|
||||
|
||||
if (!measurer) {
|
||||
measurer = document.createElement('div');
|
||||
measurer.style.cssText =
|
||||
'position:fixed;left:-10000px;top:0;visibility:hidden;pointer-events:none;' +
|
||||
'display:flex;align-items:center;justify-content:center;';
|
||||
document.body.appendChild(measurer);
|
||||
}
|
||||
|
||||
const clone = dom.inners[stateName].cloneNode(true);
|
||||
clone.removeAttribute('style');
|
||||
clone.querySelectorAll('*').forEach(el => el.removeAttribute('style'));
|
||||
const designWidth = FACE_WIDTH[stateName] || 320;
|
||||
const maxW = Math.min(440, vp.width - 32) - CARD_PAD_X * 2;
|
||||
measurer.style.width = Math.min(designWidth, maxW) + 'px';
|
||||
measurer.innerHTML = '';
|
||||
measurer.appendChild(clone);
|
||||
|
||||
const w = Math.min(measurer.offsetWidth + CARD_PAD_X * 2, vp.width - 32);
|
||||
const h = Math.min(measurer.offsetHeight + CARD_PAD_Y * 2, vp.height - 32);
|
||||
return {
|
||||
left: vp.left + (vp.width - w) / 2,
|
||||
top: vp.top + (vp.height - h) / 2,
|
||||
width: w, height: h
|
||||
};
|
||||
}
|
||||
|
||||
/* 把当前面的目标矩形写入弹簧(软键盘弹起/收起、切面、展开共用) */
|
||||
function retargetTo(stateName) {
|
||||
const t = measureState(stateName, visibleViewport());
|
||||
box.x.t = t.left; box.y.t = t.top; box.w.t = t.width; box.h.t = t.height;
|
||||
}
|
||||
|
||||
/* 软键盘弹起/收起时,让卡片平滑避让到可视区域内(弹簧自然滑移) */
|
||||
if (window.visualViewport) {
|
||||
window.visualViewport.addEventListener('resize', () => {
|
||||
if (!state.open || state.collapsing || !dom) return;
|
||||
if (dom.morpher.style.display !== 'block') return;
|
||||
retargetTo(state.activeFace || 'verify');
|
||||
ensureLoop();
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------- 逐帧写入 ---------- */
|
||||
/* 关键:border-radius 不独立过渡,由当前宽高与揭示进度推导,见文件头注释 */
|
||||
function applyBox() {
|
||||
const el = dom.morpher;
|
||||
const a = cachedAccent, c = cachedCard, bc = cachedBorder;
|
||||
const m = clamp(box.bg.x, 0, 1);
|
||||
el.style.transform = `translate3d(${box.x.x}px, ${box.y.x}px, 0)`;
|
||||
el.style.width = box.w.x + 'px';
|
||||
el.style.height = box.h.x + 'px';
|
||||
const half = Math.min(box.w.x, box.h.x) / 2;
|
||||
const radius = Math.min(half, lerp(half, CARD_R, clamp(box.reveal.x, 0, 1)));
|
||||
el.style.borderRadius = radius + 'px';
|
||||
el.style.backgroundColor =
|
||||
`rgb(${Math.round(lerp(a[0], c[0], m))},${Math.round(lerp(a[1], c[1], m))},${Math.round(lerp(a[2], c[2], m))})`;
|
||||
el.style.boxShadow = m < 0.01 ? 'none' :
|
||||
`0 ${(32 * m).toFixed(1)}px ${(90 * m).toFixed(1)}px ${(-20 * m).toFixed(1)}px rgba(8,10,14,${(0.35 * m).toFixed(3)}), ` +
|
||||
`0 0 0 1px rgba(${bc[0]},${bc[1]},${bc[2]},${m.toFixed(3)})`;
|
||||
el.style.opacity = clamp(box.op.x, 0, 1).toFixed(3);
|
||||
}
|
||||
|
||||
/* 内容面:透明度/模糊/子元素级联全部由面弹簧当前值推导(可中断、无定时器硬切) */
|
||||
function applyFace(name) {
|
||||
const s = faceS[name];
|
||||
const el = dom.faces[name], inner = dom.inners[name];
|
||||
if (!el) return;
|
||||
const r = clamp(s.x, 0, 1);
|
||||
el.style.opacity = r.toFixed(3);
|
||||
el.style.pointerEvents = r > 0.6 ? 'auto' : 'none';
|
||||
inner.style.filter = r < 0.999 ? `blur(${((1 - r) * 6).toFixed(2)}px)` : 'none';
|
||||
const kids = inner.children;
|
||||
for (let i = 0; i < kids.length; i++) {
|
||||
const d = i * 0.09;
|
||||
const e = easeOutCubic(clamp((r - d) / (1 - d), 0, 1));
|
||||
kids[i].style.opacity = e.toFixed(3);
|
||||
kids[i].style.transform = e < 0.999 ? `translateY(${((1 - e) * 14).toFixed(2)}px)` : 'none';
|
||||
}
|
||||
}
|
||||
|
||||
function burst() {
|
||||
const badge = dom.morpher.querySelector('.um-badge-wrap');
|
||||
if (!badge) return;
|
||||
const r = badge.getBoundingClientRect();
|
||||
const cx = r.left + r.width / 2, cy = r.top + r.height / 2;
|
||||
const colors = ['#10b981', '#3b82f6', '#f59e0b', '#ec4899', '#8b5cf6'];
|
||||
for (let i = 0; i < 14; i++) {
|
||||
const dot = document.createElement('span');
|
||||
const size = 4 + Math.random() * 3;
|
||||
const angle = (Math.PI * 2 * i) / 14 + (Math.random() - 0.5) * 0.6;
|
||||
const dist = 46 + Math.random() * 52;
|
||||
dot.style.cssText =
|
||||
'position:fixed;z-index:96;pointer-events:none;border-radius:50%;' +
|
||||
'left:' + (cx - size / 2) + 'px;top:' + (cy - size / 2) + 'px;' +
|
||||
'width:' + size + 'px;height:' + size + 'px;background:' + colors[i % colors.length] + ';';
|
||||
document.body.appendChild(dot);
|
||||
const anim = dot.animate([
|
||||
{ transform: 'translate(0,0) scale(1)', opacity: 1 },
|
||||
{ transform: `translate(${Math.cos(angle) * dist}px, ${Math.sin(angle) * dist}px) scale(0.5)`, opacity: 0 }
|
||||
], { duration: 650 + Math.random() * 300, easing: 'cubic-bezier(0.16, 1, 0.3, 1)', fill: 'forwards' });
|
||||
anim.onfinish = () => dot.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/* 成功/错误徽章与对勾:由面弹簧阈值触发(is-done 同时驱动描画与涟漪) */
|
||||
function tickSuccessFx() {
|
||||
const name = state.activeFace;
|
||||
if ((name !== 'success' && name !== 'error') || state.collapsing) return;
|
||||
const r = faceS[name].x;
|
||||
if (state.fxStage < 1 && r > 0.55) {
|
||||
state.fxStage = 1;
|
||||
dom.morpher.classList.add('um-badge-in');
|
||||
if (name === 'success') dom.morpher.classList.add('is-done');
|
||||
}
|
||||
if (state.fxStage < 2 && name === 'success' && r > 0.85) {
|
||||
state.fxStage = 2;
|
||||
burst();
|
||||
}
|
||||
}
|
||||
function resetFx() {
|
||||
state.fxStage = 0;
|
||||
dom.morpher.classList.remove('is-done', 'um-badge-in');
|
||||
}
|
||||
|
||||
/* ---------- 主循环 ---------- */
|
||||
function tick(now) {
|
||||
const dt = Math.min((now - lastT) / 1000, 0.05);
|
||||
lastT = now;
|
||||
|
||||
/* 展开 Choreography:轮廓揭示超过阈值后内容才开始入场 */
|
||||
if (state.open && !state.gateOpen && box.reveal.x > GATE_AT) {
|
||||
state.gateOpen = true;
|
||||
if (state.activeFace) faceS[state.activeFace].t = 1;
|
||||
}
|
||||
|
||||
stepSpring(box.x, dt, RESPONSE);
|
||||
stepSpring(box.y, dt, RESPONSE);
|
||||
stepSpring(box.w, dt, RESPONSE);
|
||||
stepSpring(box.h, dt, RESPONSE);
|
||||
stepSpring(box.reveal, dt, RESPONSE_REVEAL);
|
||||
stepSpring(box.bg, dt, state.collapsing ? RESPONSE_BG_COLLAPSE : RESPONSE_BG);
|
||||
stepSpring(box.op, dt, 0.3);
|
||||
stepSpring(backdropS, dt, 0.5, 1);
|
||||
for (const k in faceS) stepSpring(faceS[k], dt, faceS[k].t === 1 ? FACE_IN : FACE_OUT);
|
||||
|
||||
applyBox();
|
||||
for (const k in faceS) applyFace(k);
|
||||
dom.backdrop.style.opacity = clamp(backdropS.x, 0, 1).toFixed(3);
|
||||
dom.spinner.style.opacity =
|
||||
(state.collapsing ? 0 : clamp(1 - box.reveal.x * 2.2, 0, 1)).toFixed(3);
|
||||
tickSuccessFx();
|
||||
|
||||
if (state.collapsing && allContainerSettled() && allFacesSettled()) {
|
||||
finishCollapse();
|
||||
}
|
||||
|
||||
const idle = !state.open && !state.collapsing && allContainerSettled() && allFacesSettled();
|
||||
if (idle) { rafId = null; return; } // 停表:完全静止时不再占用 rAF
|
||||
rafId = requestAnimationFrame(tick);
|
||||
}
|
||||
|
||||
/* ---------- 展开 / 切面 / 收起 ---------- */
|
||||
|
||||
/* 从来源元素锁定并展开为卡片 */
|
||||
function expandFrom(sourceEl, stateName) {
|
||||
// 防重入护栏:收起未完成时先强制瞬间完成收起;展开中/已展开时忽略
|
||||
if (state.collapsing) finishCollapse();
|
||||
if (state.open || state.busy) return;
|
||||
|
||||
state.sourceEl = sourceEl || null;
|
||||
const rect = sourceEl ? sourceEl.getBoundingClientRect() : targetRectFallback();
|
||||
state.savedRect = rect;
|
||||
|
||||
if (opts.onExpand) opts.onExpand(sourceEl);
|
||||
if (sourceEl) sourceEl.style.visibility = 'hidden'; // 原位留白,由胶囊接管
|
||||
|
||||
resetFx();
|
||||
dom.morpher.removeAttribute('data-um-state');
|
||||
refreshTheme();
|
||||
box.x.x = box.x.t = rect.left; box.y.x = box.y.t = rect.top;
|
||||
box.w.x = box.w.t = rect.width; box.h.x = box.h.t = rect.height;
|
||||
box.x.v = box.y.v = box.w.v = box.h.v = 0;
|
||||
box.reveal.x = box.reveal.t = 0; box.reveal.v = 0;
|
||||
box.bg.x = box.bg.t = 0; box.bg.v = 0;
|
||||
box.op.x = box.op.t = 1; box.op.v = 0;
|
||||
backdropS.x = backdropS.t = 0; backdropS.v = 0;
|
||||
for (const k in faceS) { faceS[k].x = faceS[k].t = 0; faceS[k].v = 0; }
|
||||
|
||||
dom.morpher.style.display = 'block';
|
||||
applyBox(); // 首帧即胶囊形态
|
||||
dom.backdrop.classList.add('show');
|
||||
document.body.classList.add('um-locked');
|
||||
|
||||
state.gateOpen = reduceMotion;
|
||||
state.open = true;
|
||||
setStateInternal(stateName);
|
||||
|
||||
box.reveal.t = 1; box.bg.t = 1; backdropS.t = 1;
|
||||
if (reduceMotion) snapAll();
|
||||
state.busy = true;
|
||||
setTimeout(() => { state.busy = false; }, SETTLE_GUARD_MS);
|
||||
ensureLoop();
|
||||
}
|
||||
|
||||
/* 切换内容面:面弹簧交叉淡入,宽高随同一组弹簧滑到新内容尺寸 */
|
||||
function setStateInternal(stateName) {
|
||||
state.activeFace = stateName;
|
||||
refreshTheme();
|
||||
dom.morpher.setAttribute('data-um-state', stateName);
|
||||
if (stateName !== 'success') resetFx(); else state.fxStage = 0;
|
||||
for (const k in faceS) {
|
||||
if (k === stateName) {
|
||||
if (state.gateOpen) faceS[k].t = 1; // 展开途中由 gate 统一点火
|
||||
} else {
|
||||
faceS[k].t = 0;
|
||||
}
|
||||
}
|
||||
if (state.open && dom.morpher.style.display === 'block') {
|
||||
retargetTo(stateName);
|
||||
ensureLoop();
|
||||
}
|
||||
}
|
||||
|
||||
/* 收起:内容快速退场 → 弹簧缩回来源位置 → 交还原位 */
|
||||
function collapse(onDone) {
|
||||
const tryStart = () => {
|
||||
if (state.busy || state.collapsing) { setTimeout(tryStart, 100); return; }
|
||||
state.collapsing = true;
|
||||
state.open = false;
|
||||
state.pendingCollapseDone = onDone || null;
|
||||
|
||||
const sourceEl = state.sourceEl;
|
||||
resetFx();
|
||||
dom.morpher.removeAttribute('data-um-state'); // 内容快速退场
|
||||
for (const k in faceS) faceS[k].t = 0;
|
||||
|
||||
if (!sourceEl) {
|
||||
// 无来源元素(如居中展开的错误卡):原位收缩淡出,避免变形为巨型胶囊盖住页面
|
||||
const cx = box.x.x + box.w.x / 2, cy = box.y.x + box.h.x / 2;
|
||||
box.w.t = box.w.x * 0.88; box.h.t = box.h.x * 0.88;
|
||||
box.x.t = cx - box.w.t / 2; box.y.t = cy - box.h.t / 2;
|
||||
box.op.t = 0;
|
||||
backdropS.t = 0;
|
||||
} else {
|
||||
const r = sourceEl.getBoundingClientRect();
|
||||
box.x.t = r.left; box.y.t = r.top; box.w.t = r.width; box.h.t = r.height;
|
||||
box.reveal.t = 0; // 圆角沿同一组弹簧连续回到完整圆端
|
||||
box.bg.t = 0;
|
||||
backdropS.t = 0;
|
||||
}
|
||||
|
||||
clearTimeout(collapseSafety);
|
||||
collapseSafety = setTimeout(() => { if (state.collapsing) finishCollapse(); }, COLLAPSE_SAFETY_MS);
|
||||
ensureLoop();
|
||||
};
|
||||
tryStart();
|
||||
}
|
||||
|
||||
function finishCollapse() {
|
||||
clearTimeout(collapseSafety);
|
||||
collapseSafety = null;
|
||||
const sourceEl = state.sourceEl;
|
||||
dom.morpher.style.cssText = ''; // 回到 CSS 基础形态(display:none)
|
||||
dom.morpher.removeAttribute('data-um-state');
|
||||
resetFx();
|
||||
dom.backdrop.classList.remove('show');
|
||||
dom.backdrop.style.opacity = '';
|
||||
// 弹簧全部归位,供下次展开使用
|
||||
box.op.x = box.op.t = 1; box.op.v = 0;
|
||||
for (const k in faceS) { faceS[k].x = faceS[k].t = 0; faceS[k].v = 0; }
|
||||
backdropS.x = backdropS.t = 0; backdropS.v = 0;
|
||||
if (sourceEl) {
|
||||
if (opts.onCollapse) opts.onCollapse(sourceEl);
|
||||
sourceEl.style.visibility = ''; // 交还原位
|
||||
}
|
||||
state.sourceEl = null;
|
||||
document.body.classList.remove('um-locked');
|
||||
state.collapsing = false;
|
||||
state.gateOpen = false;
|
||||
state.activeFace = null;
|
||||
const done = state.pendingCollapseDone;
|
||||
state.pendingCollapseDone = null;
|
||||
if (done) done();
|
||||
}
|
||||
|
||||
function snapAll() {
|
||||
for (const s of [box.x, box.y, box.w, box.h, box.reveal, box.bg, box.op, backdropS]) { s.x = s.t; s.v = 0; }
|
||||
for (const k in faceS) { faceS[k].x = faceS[k].t; faceS[k].v = 0; }
|
||||
}
|
||||
|
||||
/* ---------- 公开 API ---------- */
|
||||
const api = {
|
||||
isOpen: () => state.open,
|
||||
|
||||
/* 从某元素形变展开为卡片;state: verify / progress / success / error */
|
||||
open(stateName, options) {
|
||||
if (state.open || state.busy || state.collapsing) return;
|
||||
expandFrom(options && options.sourceEl, stateName);
|
||||
},
|
||||
|
||||
/* 已展开时切换内容面(宽高随弹簧平滑过渡) */
|
||||
setState: setStateInternal,
|
||||
|
||||
/* 收回来源位置并交还原元素 */
|
||||
close() {
|
||||
if (!state.open || state.collapsing) return;
|
||||
collapse();
|
||||
},
|
||||
|
||||
/* 进度面:未展开时从提交按钮展开;openProgress 由页面在提交时调用 */
|
||||
openProgress(sourceEl) {
|
||||
if (state.open) { setStateInternal('progress'); return; }
|
||||
expandFrom(sourceEl || null, 'progress');
|
||||
api.progress({ percent: 0, text: '准备中…' });
|
||||
},
|
||||
|
||||
progress(p) {
|
||||
const ring = document.getElementById('umProgressRing');
|
||||
const pct = document.getElementById('umProgressPercent');
|
||||
const sub = document.getElementById('umProgressSub');
|
||||
if (!ring || !pct) return;
|
||||
if (p.percent != null) {
|
||||
const clamped = Math.max(0, Math.min(100, p.percent));
|
||||
ring.style.strokeDashoffset = String(100 - clamped);
|
||||
pct.textContent = Math.round(clamped) + '%';
|
||||
}
|
||||
if (p.text && sub) sub.textContent = p.text;
|
||||
},
|
||||
|
||||
/* 成功面:已展开则原位切换,否则展开 */
|
||||
success(sourceEl) {
|
||||
if (!state.open) {
|
||||
expandFrom(sourceEl || null, 'success');
|
||||
} else {
|
||||
setStateInternal('success');
|
||||
}
|
||||
},
|
||||
|
||||
/* 错误面:已展开则原位切换(不自动收起),否则展开 */
|
||||
error(message, sourceEl) {
|
||||
document.getElementById('umErrorText').textContent = message || opts.errorDefault;
|
||||
if (state.open) {
|
||||
setStateInternal('error');
|
||||
} else {
|
||||
expandFrom(sourceEl || null, 'error');
|
||||
}
|
||||
},
|
||||
|
||||
/* 验证卡片:从来源元素展开,onSubmit 返回 true 关闭(keepOpen=true 时保持展开)
|
||||
并 resolve 输入值;返回字符串则作为错误显示在卡片内;取消 resolve(null)。 */
|
||||
verify({ title, desc, maxlength = 2, placeholder = '••', okText = '确认',
|
||||
inputFilter = null, sourceEl = null, keepOpen = false, onSubmit }) {
|
||||
return new Promise((resolve) => {
|
||||
const input = document.getElementById('umVerifyInput');
|
||||
const errEl = document.getElementById('umVerifyError');
|
||||
const okBtn = document.getElementById('umVerifyOk');
|
||||
const cancelBtn = document.getElementById('umVerifyCancel');
|
||||
|
||||
document.getElementById('umVerifyTitle').textContent = title;
|
||||
document.getElementById('umVerifyDesc').textContent = desc;
|
||||
okBtn.textContent = okText;
|
||||
input.setAttribute('maxlength', maxlength);
|
||||
input.placeholder = placeholder;
|
||||
input.value = '';
|
||||
errEl.textContent = '';
|
||||
input.classList.remove('um-input-error');
|
||||
okBtn.disabled = false; cancelBtn.disabled = false;
|
||||
|
||||
state.verifyActive = true;
|
||||
let settled = false;
|
||||
|
||||
const cleanup = () => {
|
||||
okBtn.onclick = null; cancelBtn.onclick = null; input.onkeydown = null;
|
||||
dom.backdrop.removeEventListener('click', onBackdrop);
|
||||
document.removeEventListener('keydown', onKey);
|
||||
state.verifyActive = false;
|
||||
};
|
||||
const finish = (value) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
cleanup();
|
||||
if (value !== null && keepOpen && state.open) {
|
||||
resolve(value); // 卡片保持展开,由调用方切换到下一状态
|
||||
} else {
|
||||
collapse(() => resolve(value));
|
||||
}
|
||||
};
|
||||
const submit = async () => {
|
||||
if (settled) return;
|
||||
const value = input.value.trim();
|
||||
if (!value || value.length !== maxlength) {
|
||||
errEl.textContent = `请输入 ${maxlength} 位`;
|
||||
input.classList.add('um-input-error');
|
||||
input.focus();
|
||||
return;
|
||||
}
|
||||
okBtn.disabled = true; cancelBtn.disabled = true;
|
||||
let result;
|
||||
try { result = await onSubmit(value); }
|
||||
catch (e) { result = (e && e.message) || '操作失败,请稍后重试'; }
|
||||
if (result === true) { finish(value); }
|
||||
else {
|
||||
errEl.textContent = result || '验证失败';
|
||||
input.classList.add('um-input-error');
|
||||
okBtn.disabled = false; cancelBtn.disabled = false;
|
||||
/* 抖动写在当前内容面上,避免与容器弹簧逐帧写入的 transform 冲突 */
|
||||
const face = dom.faces[state.activeFace] || dom.faces.verify;
|
||||
if (window.gsap) {
|
||||
gsap.to(face, { x: [-5, 5, -5, 5, 0], duration: 0.4, ease: 'power2.inOut', clearProps: 'x' });
|
||||
} else {
|
||||
face.animate(
|
||||
[{ transform: 'translateX(0)' }, { transform: 'translateX(-5px)' }, { transform: 'translateX(5px)' },
|
||||
{ transform: 'translateX(-5px)' }, { transform: 'translateX(5px)' }, { transform: 'translateX(0)' }],
|
||||
{ duration: 400, easing: 'ease-in-out' });
|
||||
}
|
||||
input.focus(); input.select();
|
||||
}
|
||||
};
|
||||
|
||||
const onBackdrop = () => finish(null);
|
||||
const onKey = (e) => { if (e.key === 'Escape') finish(null); };
|
||||
|
||||
okBtn.onclick = submit;
|
||||
cancelBtn.onclick = () => finish(null);
|
||||
input.onkeydown = (e) => { if (e.key === 'Enter') submit(); };
|
||||
input.oninput = inputFilter ? function () { this.value = inputFilter(this.value); } : null;
|
||||
dom.backdrop.addEventListener('click', onBackdrop);
|
||||
document.addEventListener('keydown', onKey);
|
||||
|
||||
expandFrom(sourceEl, 'verify');
|
||||
setTimeout(() => input.focus(), 500);
|
||||
});
|
||||
},
|
||||
|
||||
/* 确认卡片(如"仅可提交一次"提示):从来源元素展开,无输入框。
|
||||
onConfirm 返回 true 关闭(keepOpen=true 保持展开)并 resolve true;
|
||||
返回字符串则在卡片内显示为错误;取消/backdrop/Esc resolve false。 */
|
||||
confirm({ title = '确认提交', desc = '', okText = '确认提交', cancelText = '取消',
|
||||
sourceEl = null, keepOpen = false, onConfirm }) {
|
||||
return new Promise((resolve) => {
|
||||
const errEl = document.getElementById('umConfirmError');
|
||||
const okBtn = document.getElementById('umConfirmOk');
|
||||
const cancelBtn = document.getElementById('umConfirmCancel');
|
||||
|
||||
document.getElementById('umConfirmTitle').textContent = title;
|
||||
document.getElementById('umConfirmDesc').textContent = desc;
|
||||
okBtn.textContent = okText;
|
||||
cancelBtn.textContent = cancelText;
|
||||
errEl.textContent = '';
|
||||
okBtn.disabled = false; cancelBtn.disabled = false;
|
||||
|
||||
let settled = false;
|
||||
|
||||
const cleanup = () => {
|
||||
okBtn.onclick = null; cancelBtn.onclick = null;
|
||||
dom.backdrop.removeEventListener('click', onBackdrop);
|
||||
document.removeEventListener('keydown', onKey);
|
||||
};
|
||||
const finish = (value) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
cleanup();
|
||||
if (value === true && keepOpen && state.open) {
|
||||
resolve(true); // 卡片保持展开,由调用方切换到下一状态
|
||||
} else {
|
||||
collapse(() => resolve(value));
|
||||
}
|
||||
};
|
||||
const submit = async () => {
|
||||
if (settled) return;
|
||||
okBtn.disabled = true; cancelBtn.disabled = true;
|
||||
let result;
|
||||
try { result = onConfirm ? await onConfirm() : true; }
|
||||
catch (e) { result = (e && e.message) || '操作失败,请稍后重试'; }
|
||||
if (result === true) { finish(true); }
|
||||
else {
|
||||
errEl.textContent = result || '操作失败';
|
||||
okBtn.disabled = false; cancelBtn.disabled = false;
|
||||
}
|
||||
};
|
||||
|
||||
const onBackdrop = () => finish(false);
|
||||
const onKey = (e) => { if (e.key === 'Escape') finish(false); };
|
||||
|
||||
okBtn.onclick = submit;
|
||||
cancelBtn.onclick = () => finish(false);
|
||||
dom.backdrop.addEventListener('click', onBackdrop);
|
||||
document.addEventListener('keydown', onKey);
|
||||
|
||||
expandFrom(sourceEl, 'confirm');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
window.MorphCard = {
|
||||
/* 初始化:注入样式与 DOM,返回 API(单例,重复调用返回同一实例) */
|
||||
init(options) {
|
||||
if (dom) return api;
|
||||
opts = Object.assign({
|
||||
successTitle: '提交成功',
|
||||
successDesc: '您填写的数据已保存。',
|
||||
doneText: '确定',
|
||||
errorTitle: '无法提交',
|
||||
errorDefault: '提交失败,请稍后重试',
|
||||
backText: '返回修改',
|
||||
progressLabel: '正在上传',
|
||||
onExpand: null,
|
||||
onCollapse: null
|
||||
}, options || {});
|
||||
|
||||
dom = buildDom(opts);
|
||||
|
||||
document.getElementById('umDoneBtn').addEventListener('click', () => api.close());
|
||||
document.getElementById('umErrorBack').addEventListener('click', () => api.close());
|
||||
dom.backdrop.addEventListener('click', () => {
|
||||
if (state.verifyActive) return; // 验证中由 verify 的取消逻辑处理
|
||||
api.close();
|
||||
});
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Escape' && !state.verifyActive) api.close();
|
||||
});
|
||||
|
||||
return api;
|
||||
}
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user