重新按英文命名,修改成相对路径
This commit is contained in:
160
CostReview/app/ui/period.js
Normal file
160
CostReview/app/ui/period.js
Normal file
@@ -0,0 +1,160 @@
|
||||
/* ============================================================
|
||||
期次区间选择器(工作台 / 后台共用)
|
||||
—— 一个输入框,点一下弹出「一个」面板,面板里一次选完「起」「止」两个月,不再弹两次。
|
||||
|
||||
布局(2026-09-29 你定的两排式):
|
||||
第 1 排 起始 [年下拉][月下拉]
|
||||
第 2 排 结束 [年下拉][月下拉]
|
||||
第 3 排 清空 止=起
|
||||
第 4 排 确定(主操作,占整行)
|
||||
|
||||
用法:
|
||||
mkPeriod(el, onChange) 把 el 变成期次区间输入框
|
||||
PVAL(el) 取值 {f,t,fs,ts} 月序号 + "2026-01" 字符串
|
||||
setPeriod(el, from, to) 赋值("2026-01" / "2026-12"),传空串清空
|
||||
============================================================ */
|
||||
(function () {
|
||||
const pad = n => String(n).padStart(2, '0');
|
||||
const m2s = m => (m == null || m === '' ? '' : Math.floor(m / 12) + '-' + pad(m % 12 || 12));
|
||||
const s2m = s => { const m = /^(\d{4})-(\d{1,2})$/.exec((s || '').trim()); return m ? (+m[1]) * 12 + (+m[2]) : null; };
|
||||
|
||||
let box = null, curEl = null, st = { fy: 0, fm: 0, ty: 0, tm: 0 };
|
||||
const Y0 = 2015, Y1 = 2035;
|
||||
|
||||
function ensureBox() {
|
||||
if (box) return box;
|
||||
box = document.createElement('div');
|
||||
box.className = 'pcal';
|
||||
box.innerHTML =
|
||||
'<div class="pcal-hd">期次区间</div>' +
|
||||
'<div class="pcal-row"><b>起始</b><select class="py" data-k="f"></select><select class="pmo" data-k="f"></select></div>' +
|
||||
'<div class="pcal-row"><b>结束</b><select class="py" data-k="t"></select><select class="pmo" data-k="t"></select></div>' +
|
||||
'<div class="pcal-prev"></div>' +
|
||||
'<div class="pcal-ft">' +
|
||||
'<div class="pcal-ft1"><button type="button" class="pq" data-a="clear">清空</button>' +
|
||||
'<button type="button" class="pq" data-a="same">止=起</button></div>' +
|
||||
'<div class="pcal-ft2"><button type="button" class="pq pri" data-a="ok">确定</button></div>' +
|
||||
'</div>';
|
||||
document.body.appendChild(box);
|
||||
|
||||
// 年 / 月 下拉(起始、结束各一组)
|
||||
box.querySelectorAll('select.py').forEach(sel => {
|
||||
for (let y = Y0; y <= Y1; y++) {
|
||||
const o = document.createElement('option');
|
||||
o.value = y; o.textContent = y + ' 年';
|
||||
sel.appendChild(o);
|
||||
}
|
||||
sel.onchange = () => {
|
||||
const k = sel.dataset.k;
|
||||
if (k === 'f') st.fy = +sel.value; else st.ty = +sel.value;
|
||||
fixOrder(); paint();
|
||||
};
|
||||
});
|
||||
box.querySelectorAll('select.pmo').forEach(sel => {
|
||||
for (let m = 1; m <= 12; m++) {
|
||||
const o = document.createElement('option');
|
||||
o.value = m; o.textContent = m + ' 月';
|
||||
sel.appendChild(o);
|
||||
}
|
||||
sel.onchange = () => {
|
||||
const k = sel.dataset.k;
|
||||
if (k === 'f') st.fm = +sel.value; else st.tm = +sel.value;
|
||||
fixOrder(); paint();
|
||||
};
|
||||
});
|
||||
box.querySelector('[data-a="clear"]').onclick = () => { setPeriod(curEl, '', ''); close(); };
|
||||
box.querySelector('[data-a="same"]').onclick = () => { st.ty = st.fy; st.tm = st.fm; paint(); };
|
||||
box.querySelector('[data-a="ok"]').onclick = () => { apply(); close(); };
|
||||
document.addEventListener('mousedown', e => {
|
||||
if (!box.contains(e.target) && e.target !== curEl) close();
|
||||
});
|
||||
return box;
|
||||
}
|
||||
|
||||
// 止早于起 → 自动把止拉到起(跟以前一样,不改规则)
|
||||
function fixOrder() {
|
||||
if (st.ty * 12 + st.tm < st.fy * 12 + st.fm) { st.ty = st.fy; st.tm = st.fm; }
|
||||
}
|
||||
|
||||
function paint() {
|
||||
const F = st.fy * 12 + st.fm, T = st.ty * 12 + st.tm;
|
||||
box.querySelector('select.py[data-k="f"]').value = st.fy;
|
||||
box.querySelector('select.pmo[data-k="f"]').value = st.fm;
|
||||
box.querySelector('select.py[data-k="t"]').value = st.ty;
|
||||
box.querySelector('select.pmo[data-k="t"]').value = st.tm;
|
||||
box.querySelector('.pcal-prev').textContent = m2s(F) + ' ~ ' + m2s(T);
|
||||
}
|
||||
|
||||
function apply() {
|
||||
if (!curEl) return;
|
||||
setPeriod(curEl, st.fy + '-' + pad(st.fm), st.ty + '-' + pad(st.tm));
|
||||
}
|
||||
|
||||
function open(el) {
|
||||
curEl = el;
|
||||
ensureBox();
|
||||
const v = PVAL(el), now = new Date();
|
||||
const fy = v.fs ? +v.fs.slice(0, 4) : now.getFullYear();
|
||||
const fm = v.fs ? +v.fs.slice(5, 7) : now.getMonth() + 1;
|
||||
const ty = v.ts ? +v.ts.slice(0, 4) : fy;
|
||||
const tm = v.ts ? +v.ts.slice(5, 7) : fm;
|
||||
st = { fy, fm, ty, tm };
|
||||
fixOrder(); paint();
|
||||
const r = el.getBoundingClientRect();
|
||||
box.style.display = 'block';
|
||||
const W = 252, H = 210;
|
||||
let left = r.left, top = r.bottom + 4;
|
||||
if (left + W > window.innerWidth) left = Math.max(8, window.innerWidth - W - 10);
|
||||
if (top + H > window.innerHeight) top = Math.max(8, r.top - H - 4);
|
||||
box.style.left = left + 'px';
|
||||
box.style.top = top + 'px';
|
||||
}
|
||||
|
||||
function close() { if (box) box.style.display = 'none'; }
|
||||
|
||||
window.mkPeriod = function (el, onChange) {
|
||||
if (!el) return;
|
||||
el.classList.add('perinp');
|
||||
el.readOnly = true;
|
||||
el.onclick = () => open(el);
|
||||
if (onChange) el._onChange = onChange;
|
||||
};
|
||||
|
||||
window.setPeriod = function (el, from, to) {
|
||||
if (!el) return;
|
||||
el.dataset.pf = from || '';
|
||||
el.dataset.pt = to || '';
|
||||
el.value = (from && to) ? (from === to ? from : from + ' ~ ' + to) : (from || '');
|
||||
if (el._onChange) el._onChange();
|
||||
};
|
||||
|
||||
window.PVAL = function (el) {
|
||||
const fs = (el && el.dataset.pf) || '', ts = (el && el.dataset.pt) || '';
|
||||
return { fs, ts, f: s2m(fs), t: s2m(ts) };
|
||||
};
|
||||
|
||||
// 样式(两个页面共用,不写进各自的 <style>)
|
||||
const css = document.createElement('style');
|
||||
css.textContent = `
|
||||
.perinp{cursor:pointer;background:#fff url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='7'><path d='M0 0l5 6 5-6z' fill='%23788'/></svg>") no-repeat right 7px center}
|
||||
.pcal{display:none;position:fixed;z-index:9999;width:252px;background:#fff;border:1px solid #c9d4e3;
|
||||
border-radius:9px;box-shadow:0 8px 26px rgba(20,40,80,.20);padding:9px 10px;font-size:12.5px}
|
||||
.pcal-hd{font-weight:600;color:#1f3a5f;padding:0 2px 7px;border-bottom:1px solid #e6ecf4;margin-bottom:7px}
|
||||
.pcal-row{display:flex;align-items:center;gap:6px;margin-bottom:6px}
|
||||
.pcal-row b{width:30px;color:#456;font-weight:500}
|
||||
.pcal-row select{padding:3px 4px;border:1px solid #b8c4d4;border-radius:5px;font:inherit;font-size:12.5px}
|
||||
.pcal-row select.py{flex:1.1}
|
||||
.pcal-row select.pmo{flex:1}
|
||||
.pcal-prev{margin:2px 0 7px;padding:4px 6px;background:#f4f8fd;border:1px solid #e3ebf5;border-radius:5px;
|
||||
text-align:center;color:#1f5fae;font-weight:600;font-variant-numeric:tabular-nums}
|
||||
.pcal-ft{display:flex;flex-direction:column;gap:6px;margin-top:2px;padding-top:7px;border-top:1px solid #e6ecf4}
|
||||
.pcal-ft1{display:flex;gap:6px}
|
||||
.pcal-ft1 .pq{flex:1}
|
||||
.pcal-ft2{display:flex}
|
||||
.pcal-ft2 .pq{flex:1}
|
||||
.pq{padding:4px 10px;border:1px solid #b8c4d4;border-radius:6px;background:#fff;cursor:pointer;font:inherit;font-size:12.5px;color:#334}
|
||||
.pq:hover{background:#f0f5fb}
|
||||
.pq.pri{background:#2f6fd0;border-color:#2f6fd0;color:#fff;font-weight:600}
|
||||
.pq.pri:hover{background:#2660bb}`;
|
||||
document.head.appendChild(css);
|
||||
})();
|
||||
Reference in New Issue
Block a user