Files
pricing_review/造价审核/app/tools/_patch_combo_free.py

96 lines
4.6 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""一次性补丁:common.js 组合框扩展(自由文本自动完成 + 弹层互斥 + flex 槽位复制)。"""
import io
p = r"D:\Pricing review‌\造价审核\app\ui\common.js"
src = io.open(p, encoding="utf-8").read()
# ① comboAttach:flex 槽位复制(弹框里 select 常带 style="flex:1",隐藏后槽位要交给 wrap)
a = """ wrap.append(inp, clr);
sel.style.display = 'none';"""
b = """ wrap.append(inp, clr);
if (sel.style.flex) { wrap.style.flex = sel.style.flex; sel.style.flex = ''; } // 弹框布局槽位交接
sel.style.display = 'none';"""
assert a in src, "flex copy"
src = src.replace(a, b, 1)
# ② openPanel 改为互斥收起全部弹层
a = """ const openPanel = () => {
COMBOS.forEach(c => { if (c.panel !== panel) c.close(); }); // 同屏只开一个
const r = inp.getBoundingClientRect();"""
b = """ const openPanel = () => {
comboCloseAllPanels(panel); // 同屏只开一个(含自由输入弹层)
const r = inp.getBoundingClientRect();"""
assert a in src, "openPanel"
src = src.replace(a, b, 1)
# ③ 新增 comboCloseAllPanels + comboFree(自由文本自动完成),插在 comboSyncAll 之前
a = """/* 程序性改值(联动/重置/回填)之后调用:把各组合框的显示刷新为其 select 的当前值 */
function comboSyncAll(){ COMBOS.forEach(c => c.refresh()); }"""
b = """const FREE_PANELS = [];
/* 收起全部弹层(组合框 + 自由输入),except 除外 */
function comboCloseAllPanels(except){
COMBOS.forEach(c => { if (c.panel !== except) c.panel.style.display = 'none'; });
FREE_PANELS.forEach(p => { if (p !== except) p.style.display = 'none'; });
}
/* 自由文本自动完成(2026-10-08):输入框 + 历史项目建议下拉。
与 comboAttach 的差别:**输入什么就是什么**(新值可直接用,不下拉也能填),
下拉只是来自历史数据的建议(点选回填)。用于导入/打开工程的 建设单位 等。 */
function comboFree(inp, getOptions){
if (inp._cfree) return;
const panel = document.createElement('div');
panel.className = 'combo-panel';
document.body.appendChild(panel);
FREE_PANELS.push(panel);
let items = [], hit = -1;
const closePanel = () => { panel.style.display = 'none'; };
const openPanel = () => {
comboCloseAllPanels(panel);
const k = inp.value.trim().toLowerCase();
const all = getOptions() || [];
items = all.filter(o => !k || o.toLowerCase().indexOf(k) >= 0);
hit = items.length ? 0 : -1;
panel.innerHTML = items.map((o, i) =>
'<div class="combo-it' + (i === hit ? ' on' : '') + '" data-i="' + i + '">' + esc(o) + '</div>').join('')
|| '<div class="combo-none">没有匹配项(可直接使用输入的内容)</div>';
const r = inp.getBoundingClientRect();
panel.style.left = Math.max(6, Math.min(r.left, window.innerWidth - 320)) + 'px';
panel.style.top = (r.bottom + 4) + 'px';
panel.style.minWidth = r.width + 'px';
panel.style.display = 'block';
};
inp.addEventListener('focus', openPanel);
inp.addEventListener('input', openPanel);
inp.addEventListener('blur', () => setTimeout(closePanel, 150)); // 自由文本:还原不发生,输入保留
inp.addEventListener('keydown', e => {
const open = panel.style.display === 'block';
if (!open) { if (e.key === 'Enter') { openPanel(); e.preventDefault(); } return; }
if (e.key === 'ArrowDown') { hit = Math.min(items.length - 1, hit + 1); e.preventDefault(); }
else if (e.key === 'ArrowUp') { hit = Math.max(0, hit - 1); e.preventDefault(); }
else if (e.key === 'Enter') {
e.preventDefault();
if (hit >= 0 && items[hit]) inp.value = items[hit];
closePanel();
return;
}
else if (e.key === 'Escape') { closePanel(); return; }
else return;
panel.querySelectorAll('.combo-it').forEach((el, i) => el.classList.toggle('on', i === hit));
const on = panel.querySelector('.combo-it.on');
if (on) on.scrollIntoView({ block: 'nearest' });
});
panel.addEventListener('mousedown', e => {
const it = e.target.closest('.combo-it');
if (it) { e.preventDefault(); inp.value = items[+it.dataset.i] || inp.value; closePanel(); }
});
inp._cfree = { panel, close: closePanel };
return inp;
}
/* 程序性改值(联动/重置/回填)之后调用:把各组合框的显示刷新为其 select 的当前值 */
function comboSyncAll(){ COMBOS.forEach(c => c.refresh()); }"""
assert a in src, "syncAll anchor"
src = src.replace(a, b, 1)
io.open(p, "w", encoding="utf-8", newline="\n").write(src)
print("common.js extended")