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

98 lines
4.0 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 -*-
"""一次性补丁:comboFree 补 × 清空(有值显示)+ 输入框内联样式。"""
import io
p = r"D:\Pricing review‌\造价审核\app\ui\common.js"
src = io.open(p, encoding="utf-8").read()
a = """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'; };"""
b = """function comboFree(inp, getOptions){
if (inp._cfree) return;
const wrap = document.createElement('span');
wrap.className = 'combo'; wrap.dataset.for = inp.id || '';
inp.parentNode.insertBefore(wrap, inp);
wrap.append(inp);
// 关键样式内联钉死(防页面本地 .combo input 样式覆盖);× 清空:有值才显示
inp.style.padding = '6px 24px 6px 10px';
inp.style.boxSizing = 'border-box';
inp.style.width = '100%';
const clr = document.createElement('span');
clr.className = 'combo-x'; clr.textContent = '×'; clr.title = '清空';
clr.style.display = 'none';
wrap.append(clr);
const syncX = () => { clr.style.display = inp.value ? 'block' : 'none'; };
inp.addEventListener('input', syncX);
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'; };"""
assert a in src, "comboFree head"
src = src.replace(a, b, 1)
# 事件:输入/选择/清空后同步 × 显隐
a = """ inp.addEventListener('focus', openPanel);
inp.addEventListener('input', openPanel);
inp.addEventListener('blur', () => setTimeout(closePanel, 150)); // 自由文本:输入保留,不还原"""
b = """ inp.addEventListener('focus', openPanel);
inp.addEventListener('input', () => { openPanel(); syncX(); });
inp.addEventListener('blur', () => setTimeout(() => { closePanel(); syncX(); }, 150)); // 自由文本:输入保留,不还原
clr.addEventListener('mousedown', e => { e.preventDefault(); inp.value = ''; syncX(); inp.focus(); });"""
assert a in src, "events"
src = src.replace(a, b, 1)
# 键盘 Enter/选择后同步 ×
a = """ 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;
}"""
b = """ 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(); syncX();
return;
}
else if (e.key === 'Escape') { closePanel(); syncX(); 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(); syncX(); }
});
inp._cfree = { panel, close: closePanel };
return inp;
}"""
assert a in src, "keydown"
src = src.replace(a, b, 1)
io.open(p, "w", encoding="utf-8", newline="\n").write(src)
print("comboFree x ok")