build / build-and-test (push) Successful in 3m17s
根因有两处:
1) 小键盘在 HTML 里是 <form> 的兄弟节点(不在表单内部),JS 用 pad.closest('form') 拿不到表单,
于是整个按键逻辑直接 return —— 按键既不写入输入框,也没有任何反馈;
2) 原生按钮在 mousedown 时会获得焦点,把输入框的焦点顶掉。
修复:
- 改为先定位输入框,再用 input.form 反查表单,不再依赖 DOM 嵌套关系
- mousedown 阻止默认行为,按钮不再抢焦点;pointerdown / click 后统一把焦点与光标送回输入框末尾
- 未聚焦时点键盘会自动聚焦输入框;退格、确认逻辑不变
验证(真实 Chrome + CDP 真鼠标点击,不是单元模拟):
- F-01/02 点 1 后焦点仍在、值为 1
- F-03/04 连点 1/2/3 得到 123,焦点保持
- F-05/06 退格得到 12,焦点保持
- F-07/08 输入框失焦后点键盘,自动聚焦并写入
- 截图 shots/v161-keypad-focus.png
- 119 项验收回归全绿;版本号 1.6.1
201 lines
7.7 KiB
JavaScript
201 lines
7.7 KiB
JavaScript
/* 代打台 · 交互增强(无框架,全部渐进增强) */
|
|
(function () {
|
|
'use strict';
|
|
|
|
/* 提示条自动淡出 */
|
|
document.querySelectorAll('[data-flash]').forEach(function (el) {
|
|
setTimeout(function () {
|
|
el.classList.add('hide');
|
|
setTimeout(function () { el.remove(); }, 450);
|
|
}, 4200);
|
|
});
|
|
|
|
/* 按 / 聚焦搜索框 */
|
|
document.addEventListener('keydown', function (e) {
|
|
if (e.key !== '/' || e.metaKey || e.ctrlKey || e.altKey) return;
|
|
var tag = (document.activeElement && document.activeElement.tagName) || '';
|
|
if (/^(INPUT|TEXTAREA|SELECT)$/.test(tag)) return;
|
|
var s = document.querySelector('[data-search]');
|
|
if (s) { e.preventDefault(); s.focus(); s.select(); }
|
|
});
|
|
|
|
/* 危险操作确认 */
|
|
document.querySelectorAll('form[data-confirm]').forEach(function (f) {
|
|
f.addEventListener('submit', function (e) {
|
|
if (!window.confirm(f.getAttribute('data-confirm'))) e.preventDefault();
|
|
});
|
|
});
|
|
|
|
/* 追加时长面板:点日历挑终止日 + 实时反算 */
|
|
function parseDay(s) { return new Date(s + 'T00:00:00'); }
|
|
function diffDays(a, b) { return Math.round((b.getTime() - a.getTime()) / 86400000); }
|
|
|
|
document.querySelectorAll('form[data-extend]').forEach(function (form) {
|
|
var base = parseDay(form.getAttribute('data-base'));
|
|
var today = parseDay(form.getAttribute('data-today'));
|
|
var input = form.querySelector('[data-end-input]');
|
|
var endLabel = form.querySelector('[data-end-label]');
|
|
var daysEl = form.querySelector('[data-days]');
|
|
var remainEl = form.querySelector('[data-remain]');
|
|
var buttons = form.querySelectorAll('.miniday');
|
|
if (!input) return;
|
|
|
|
function paint(date) {
|
|
if (!date) return;
|
|
var d = parseDay(date);
|
|
input.value = date;
|
|
if (endLabel) endLabel.textContent = date;
|
|
var days = diffDays(base, d) + 1;
|
|
if (daysEl) daysEl.textContent = days > 0 ? days : 0;
|
|
var remain = diffDays(today, d) + 1;
|
|
if (remainEl) remainEl.textContent = remain > 0 ? remain : 0;
|
|
buttons.forEach(function (b) {
|
|
b.classList.toggle('picked', b.getAttribute('data-date') === date);
|
|
});
|
|
}
|
|
|
|
buttons.forEach(function (b) {
|
|
b.addEventListener('click', function () {
|
|
if (!b.disabled) paint(b.getAttribute('data-date'));
|
|
});
|
|
});
|
|
input.addEventListener('change', function () { paint(input.value); });
|
|
if (input.value) paint(input.value);
|
|
});
|
|
|
|
/* 新订阅表单:按套餐类型切换「天数 / 次数 / 终止日」 */
|
|
document.querySelectorAll('form[data-sub-form]').forEach(function (form) {
|
|
var sel = form.querySelector('[data-pkg-select]');
|
|
var daysWrap = form.querySelector('[data-days-wrap]');
|
|
var countWrap = form.querySelector('[data-count-wrap]');
|
|
var endWrap = form.querySelector('[data-end-wrap]');
|
|
if (!sel) return;
|
|
|
|
function sync() {
|
|
var opt = sel.options[sel.selectedIndex];
|
|
var unit = opt && opt.getAttribute('data-unit');
|
|
var amount = opt && opt.getAttribute('data-amount');
|
|
if (unit === 'count') {
|
|
if (daysWrap) daysWrap.hidden = true;
|
|
if (countWrap) { countWrap.hidden = false; if (amount) countWrap.querySelector('input').value = amount; }
|
|
if (endWrap) endWrap.hidden = true;
|
|
} else if (unit === 'day') {
|
|
if (daysWrap) { daysWrap.hidden = false; if (amount) daysWrap.querySelector('input').value = amount; }
|
|
if (countWrap) countWrap.hidden = true;
|
|
if (endWrap) endWrap.hidden = false;
|
|
} else {
|
|
if (daysWrap) daysWrap.hidden = false;
|
|
if (countWrap) countWrap.hidden = true;
|
|
if (endWrap) endWrap.hidden = true;
|
|
}
|
|
}
|
|
sel.addEventListener('change', sync);
|
|
sync();
|
|
});
|
|
|
|
/* 套餐表单:切换默认量单位文案 */
|
|
document.querySelectorAll('form[data-package-form]').forEach(function (form) {
|
|
var label = form.querySelector('[data-unit-label]');
|
|
var radios = form.querySelectorAll('input[name="unit"]');
|
|
function sync() {
|
|
var v = 'day';
|
|
radios.forEach(function (r) { if (r.checked) v = r.value; });
|
|
if (label) label.textContent = v === 'count' ? '次' : '天';
|
|
}
|
|
radios.forEach(function (r) { r.addEventListener('change', sync); });
|
|
sync();
|
|
});
|
|
})();
|
|
|
|
/* ---------- 亮色 / 暗色切换 ----------
|
|
偏好写 cookie,服务端首屏直接带 data-theme,不闪。
|
|
没有 cookie 时跟随系统(由 CSS 的 prefers-color-scheme 决定)。 */
|
|
(function () {
|
|
'use strict';
|
|
var KEY = 'daili_theme';
|
|
var btns = document.querySelectorAll('[data-theme-toggle]');
|
|
if (!btns.length) return;
|
|
|
|
function effective() {
|
|
var attr = document.documentElement.getAttribute('data-theme');
|
|
if (attr === 'light' || attr === 'dark') return attr;
|
|
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
}
|
|
|
|
Array.prototype.forEach.call(btns, function (btn) {
|
|
btn.addEventListener('click', function () {
|
|
var next = effective() === 'dark' ? 'light' : 'dark';
|
|
document.documentElement.setAttribute('data-theme', next);
|
|
document.cookie = KEY + '=' + next + '; path=/; max-age=31536000; SameSite=Lax';
|
|
});
|
|
});
|
|
})();
|
|
|
|
/* ---------- 触摸小键盘 + 抽屉时钟 + 更多菜单 ---------- */
|
|
(function () {
|
|
'use strict';
|
|
|
|
/* 时钟:精确到秒 */
|
|
var clocks = document.querySelectorAll('[data-clock]');
|
|
function tick() {
|
|
var d = new Date();
|
|
var pad = function (n) { return n < 10 ? '0' + n : '' + n; };
|
|
var t = pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds());
|
|
Array.prototype.forEach.call(clocks, function (el) { el.textContent = t; });
|
|
}
|
|
if (clocks.length) { tick(); setInterval(tick, 1000); }
|
|
|
|
/* 小键盘:输入手机尾号
|
|
关键点:鼠标按下时阻止默认行为,否则按钮会先抢走输入框焦点;
|
|
每次按键后再把焦点和光标送回输入框末尾。 */
|
|
Array.prototype.forEach.call(document.querySelectorAll('[data-keypad]'), function (pad) {
|
|
// 键盘是表单的兄弟节点(不在 <form> 内),所以先找输入框,再用 input.form 反查表单
|
|
var scope = pad.closest('.drawer-block') || pad.parentElement || document;
|
|
var input = scope.querySelector('input[name="tail"]');
|
|
if (!input) { return; }
|
|
var form = input.form;
|
|
if (!form) { return; }
|
|
|
|
function keepFocus() {
|
|
try { input.focus({ preventScroll: true }); } catch (err) { input.focus(); }
|
|
var end = input.value.length;
|
|
try { input.setSelectionRange(end, end); } catch (err) { /* number 类型时忽略 */ }
|
|
}
|
|
|
|
// mousedown 会触发“按钮获得焦点”,先拦下来;pointerdown 再兜底一次
|
|
pad.addEventListener('mousedown', function (e) {
|
|
e.preventDefault();
|
|
keepFocus();
|
|
});
|
|
pad.addEventListener('pointerdown', function () {
|
|
// 这里不能 preventDefault:会连带取消后面的 click 事件
|
|
keepFocus();
|
|
});
|
|
|
|
pad.addEventListener('click', function (e) {
|
|
var btn = e.target.closest('button[data-key]');
|
|
if (!btn) return;
|
|
var key = btn.getAttribute('data-key');
|
|
if (key === 'back') {
|
|
input.value = input.value.slice(0, -1);
|
|
} else if (key === 'ok') {
|
|
if (input.value) {
|
|
if (form.requestSubmit) { form.requestSubmit(); } else { form.submit(); }
|
|
}
|
|
return;
|
|
} else if (input.value.length < 8) {
|
|
input.value += key;
|
|
}
|
|
keepFocus();
|
|
});
|
|
});
|
|
|
|
/* 更多菜单:点开一个时收起其它,点空白处关闭 */
|
|
document.addEventListener('click', function (e) {
|
|
var opened = document.querySelectorAll('.more-actions[open]');
|
|
Array.prototype.forEach.call(opened, function (d) {
|
|
if (!d.contains(e.target)) { d.removeAttribute('open'); }
|
|
});
|
|
});
|
|
})();
|