" +
gridHtml(viewY, viewM, holidays, today, function (iso) {
const c = [];
if (iso === due || (showStart && iso === start)) c.push("is-selected");
if (showStart && start && due && iso > start && iso < due) c.push("is-inrange");
return c;
}) +
'
' +
'' +
(opts.clear === false ? "" : '') +
"
";
wire();
}
// 시작일 칸이 활성이면 시작일(그 뒤 마감일로), 아니면 마감일(저장하고 닫음). 시작일 > 마감일이 되지 않게 맞춘다.
function pick(iso) {
if (active === "start") {
start = iso;
if (due && due < iso) due = iso;
active = "due";
render(); emit(); focusActive();
return;
}
due = iso;
if (start && start > iso) start = iso;
emit();
close();
}
function wire() {
pop.querySelectorAll(".pj-dp-day").forEach(function (b) {
b.addEventListener("click", function () { pick(b.dataset.iso); });
});
pop.querySelectorAll(".pj-dp-navbtn").forEach(function (b) {
b.addEventListener("click", function () {
viewM += +b.dataset.nav;
while (viewM < 0) { viewM += 12; viewY--; }
while (viewM > 11) { viewM -= 12; viewY++; }
render();
});
});
const add = pop.querySelector(".pj-dp-addstart");
if (add) add.addEventListener("click", function () { showStart = true; active = "start"; render(); focusActive(); });
pop.querySelectorAll(".pj-dp-field").forEach(function (inp) {
inp.addEventListener("focus", function () {
active = inp.dataset.kind;
pop.querySelectorAll(".pj-dp-field").forEach(function (x) { x.classList.toggle("is-active", x === inp); });
});
function commit() {
const kind = inp.dataset.kind;
const cur = kind === "start" ? start : due;
const parsed = parseTyped(inp.value, today);
if (parsed === null || parsed === cur) { inp.value = fmtField(cur); return; }
if (!parsed) {
if (opts.clear === false) { inp.value = fmtField(cur); return; }
if (kind === "start") { start = ""; showStart = false; } else due = "";
render(); emit(); return;
}
active = kind;
const d = new Date(parsed + "T00:00:00");
viewY = d.getFullYear(); viewM = d.getMonth();
pick(parsed);
}
inp.addEventListener("keydown", function (e) {
if (e.isComposing) return;
if (e.key === "Enter") { e.preventDefault(); commit(); }
});
inp.addEventListener("change", commit);
});
pop.querySelector(".pj-dp-today").addEventListener("click", function () {
const d = new Date(); viewY = d.getFullYear(); viewM = d.getMonth();
pick(today);
});
const clr = pop.querySelector(".pj-dp-clear");
if (clr) clr.addEventListener("click", function () { start = ""; due = ""; emit(); close(); });
}
function focusActive() {
const f = pop.querySelector('.pj-dp-field[data-kind="' + active + '"]');
if (f) f.focus();
}
function place() {
const r = anchor.getBoundingClientRect();
const vw = document.documentElement.clientWidth, vh = document.documentElement.clientHeight;
const w = pop.offsetWidth, h = pop.offsetHeight;
const ax = r.left + r.width / 2;
const left = Math.min(Math.max(8, ax - w / 2), vw - w - 8);
const below = r.bottom + 10 + h <= vh - 8 || r.top - 10 - h < 8;
pop.classList.toggle("is-above", !below);
pop.style.left = left + "px";
pop.style.top = (below ? r.bottom + 10 : r.top - 10 - h) + "px";
pop.style.setProperty("--pj-dp-tail", Math.min(Math.max(ax - left, 16), w - 16) + "px");
}
function onDown(e) {
if (pop.contains(e.target) || anchor.contains(e.target)) return;
close();
}
function onKey(e) { if (e.key === "Escape") { e.stopPropagation(); close(); } }
render();
place();
setTimeout(function () {
if (popEl !== pop) return; // 그 사이 닫혔으면 걸지 않는다(남은 Esc 처리가 이후 Esc 를 삼키지 않게)
document.addEventListener("mousedown", onDown, true);
document.addEventListener("keydown", onKey, true);
}, 0);
window.addEventListener("resize", close);
cleanup = function () {
document.removeEventListener("mousedown", onDown, true);
document.removeEventListener("keydown", onKey, true);
window.removeEventListener("resize", close);
};
focusActive();
}
// 날짜 input 하나 → 날짜 버튼 하나(2026-10-27, 입사일 등). opts: { label, placeholder, format(iso), yearNav, clear(기본 true), holidays }
// input 은 숨긴 값 저장소 — value 쓰기를 가로채 버튼 글자도 바뀌고, 고르거나 지우면 input·change 이벤트를 낸다.
function enhance(input, opts) {
if (!input || input.dataset.erpDate) return null;
opts = opts || {};
const desc = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value");
const format = opts.format || fmt;
input.dataset.erpDate = "1";
input.style.display = "none";
const btn = document.createElement("button");
btn.type = "button";
btn.className = "pj-date-btn erp-date-btn" + (opts.className ? " " + opts.className : "");
input.after(btn);
Object.defineProperty(input, "value", {
configurable: true,
get: function () { return desc.get.call(this); },
set: function (v) { desc.set.call(this, v); sync(); },
});
function sync() {
const text = format(input.value);
btn.innerHTML = 'calendar_month';
btn.querySelector(".pj-date-btn-text").textContent = text || opts.placeholder || "날짜 선택";
btn.classList.toggle("is-empty", !text);
btn.title = (opts.label || "날짜") + (text ? " 변경" : " 지정");
}
btn.addEventListener("click", function (e) {
e.preventDefault(); e.stopPropagation();
open(btn, {
due: input.value, allowStart: false, clear: opts.clear !== false, yearNav: !!opts.yearNav, holidays: opts.holidays || {},
labels: { due: opts.label || "날짜" },
onChange: function (patch) {
const v = patch.due_date || "";
if ((input.value || "") === v) return;
input.value = v;
input.dispatchEvent(new Event("input", { bubbles: true }));
input.dispatchEvent(new Event("change", { bubbles: true }));
},
});
});
sync();
return { sync: sync, button: btn };
}
// 시작·끝 날짜 input 두 개 → 날짜 버튼 두 개(각 input 자리), 어느 쪽을 눌러도 같은 기간 팝업(누른 쪽 칸이 먼저).
// endInput 이 숨겨져 있으면(opts.single() 이 true — 예: 반차) 하루짜리 팝업으로 시작=끝을 같이 맞춘다.
// input 은 숨겨 값 저장소로 둔다 — value 쓰기를 가로채 버튼 글자도 따라 바뀌고, 고르면 input·change 이벤트를 낸다.
function enhanceRange(startInput, endInput, opts) {
opts = opts || {};
const labels = Object.assign({ start: "시작일", due: "종료일" }, opts.labels || {});
const desc = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value");
const pairs = [[startInput, "start"], [endInput, "due"]].filter(function (p) { return p[0] && !p[0].dataset.erpDate; });
const btns = {};
pairs.forEach(function (p) {
const inp = p[0], kind = p[1];
inp.dataset.erpDate = "1";
inp.style.display = "none";
const btn = document.createElement("button");
btn.type = "button";
btn.className = "pj-date-btn erp-date-btn";
btn.dataset.kind = kind;
inp.after(btn);
btns[kind] = btn;
Object.defineProperty(inp, "value", {
configurable: true,
get: function () { return desc.get.call(this); },
set: function (v) { desc.set.call(this, v); sync(); },
});
btn.addEventListener("click", function (e) {
e.preventDefault(); e.stopPropagation();
const single = !!(opts.single && opts.single());
open(btn, {
start: single ? "" : startInput.value, due: single ? startInput.value : endInput.value,
allowStart: !single, active: single ? "due" : kind, clear: false, holidays: opts.holidays || {},
labels: single ? { due: opts.singleLabel || "날짜" } : labels,
onChange: function (patch) {
if (single) { set(startInput, patch.due_date); set(endInput, patch.due_date); return; }
set(startInput, patch.start_date);
set(endInput, patch.due_date);
},
});
});
});
function set(inp, v) {
if (!v || (inp.value || "") === v) return; // 필수 칸 — 비우지 않는다
inp.value = v;
inp.dispatchEvent(new Event("input", { bubbles: true }));
inp.dispatchEvent(new Event("change", { bubbles: true }));
}
function sync() {
[["start", startInput], ["due", endInput]].forEach(function (p) {
const btn = btns[p[0]];
if (!btn) return;
const text = fmt(p[1].value);
btn.innerHTML = 'calendar_month';
btn.querySelector(".pj-date-btn-text").textContent = text || labels[p[0]] + " 선택";
btn.classList.toggle("is-empty", !text);
btn.title = text ? labels[p[0]] + " 변경" : labels[p[0]] + " 지정";
});
}
sync();
return { sync: sync, buttons: btns };
}
// 화면에 붙박이는 기간 달력(2026-11-02, 휴가 신청) — 팝업과 같은 달력 서식(.pj-dp-*).
// 첫 클릭 = 시작일(동그라미), 다음 클릭 = 종료일(둥근 막대), 그다음 클릭 = 새로 시작. 시작일보다 앞을 누르면 그 날이 새 시작일.
// opts: { start, end, holidays, single() — true 면 클릭마다 하루, onChange(start, end), locale }. 반환 { set(start, end) }
function mountRange(box, opts) {
const holidays = opts.holidays || {};
const today = isoToday();
let start = opts.start || "", end = opts.end || opts.start || "", picking = false;
const base = new Date((start || today) + "T00:00:00");
let viewY = base.getFullYear(), viewM = base.getMonth();
function bandClasses(iso, d) {
if (!start || iso < start || iso > end) return [];
if (start === end) return ["is-selected"];
const c = ["is-band"];
if (iso === start) c.push("is-selected", "is-band-start");
else if (iso === end) c.push("is-selected", "is-band-end");
if (d.getDay() === 0) c.push("is-row-first");
if (d.getDay() === 6) c.push("is-row-last");
return c;
}
function render() {
const loc = opts.locale || {};
box.innerHTML =
'