// 긴급 공지 관리(/notice/, 수퍼관리자) — 만들기·고치기·취소·미리 보기·확인 현황. // 내용 편집기·날짜 팝업·받는 사람 고르기는 프로젝트 화면 부품 그대로(window.PJLib — project.js, PJ_LIB_ONLY): // · 내용 = 작업 설명란과 같은 편집기(굵게·기울임·밑줄·취소선·글자 색/크기·목록·체크리스트·인용·링크·서식 지우기·이미지 // 붙여넣기/버튼·크기 조절), 이미지는 /notice/api/images 로 올린다. // · 띄울 날짜 = 아사나식 날짜 팝업(공휴일·오늘·지우기), 시각은 옆 칸. // · 받는 사람 = 프로젝트 멤버 고르기와 같은 아이콘 + 이름 + 체크. (function () { "use strict"; const root = document.getElementById("ntc-root"); if (!root || !window.PJLib) return; const L = window.PJLib; const $ = function (id) { return document.getElementById(id); }; const people = window.NTC_PEOPLE || []; const byEmail = {}; people.forEach(function (p) { byEmail[String(p.email).toLowerCase()] = p; }); const nameOf = function (e) { const p = byEmail[String(e).toLowerCase()]; return p ? p.name : String(e).split("@")[0]; }; const esc = function (s) { return String(s == null ? "" : s).replace(/[&<>"']/g, function (c) { return { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]; }); }; const shortWhen = function (iso) { const m = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/.exec(iso || ""); return m ? m[1] + "-" + m[2] + "-" + m[3] + " " + m[4] + ":" + m[5] : ""; }; async function api(method, url, body, isForm) { const opts = { method: method, credentials: "same-origin", headers: {} }; if (body !== undefined) { if (isForm) opts.body = body; else { opts.headers["Content-Type"] = "application/json"; opts.body = JSON.stringify(body); } } const res = await fetch(url, opts); let data = null; try { data = await res.json(); } catch (_) {} if (!res.ok) { const d = data && data.detail; throw new Error(typeof d === "string" ? d : "HTTP " + res.status); } return data || {}; } const title = $("ntc-title"), dateEl = $("ntc-date"), timeEl = $("ntc-time"), idEl = $("ntc-id"); const peopleBox = $("ntc-people"), msg = $("ntc-msg"), save = $("ntc-save"); const say = function (text, ok) { msg.textContent = text || ""; msg.hidden = !text; msg.classList.toggle("is-ok", !!ok); }; // ── 내용 — 작업 설명란 편집기 ── const editor = L.createDescEditor({ box: $("ntc-body"), fmtbar: $("ntc-fmtbar"), imgInput: $("ntc-img-input"), hint: $("ntc-hint"), mentions: false, uploadImage: async function (file) { const fd = new FormData(); fd.append("file", file, file.name || "image"); return (await api("POST", "/notice/api/images", fd, true)).image.url; }, }); // ── 날짜 — 프로젝트 날짜 팝업 ── L.enhanceDateInput(dateEl, { placeholder: "날짜(비우면 바로)" }); // ── 받는 사람 — 프로젝트 멤버 고르기 모양 ── peopleBox.innerHTML = people.map(L.memberPickHtml).join("") || '등록된 사람이 없습니다.'; function toAll() { return root.querySelector('input[name="ntc-to"]:checked').value === "all"; } root.querySelectorAll('input[name="ntc-to"]').forEach(function (r) { r.addEventListener("change", function () { peopleBox.hidden = toAll(); }); }); function picked() { return Array.from(peopleBox.querySelectorAll("input[type=checkbox]:checked")).map(function (c) { return c.value; }); } function setPicked(emails) { const want = (emails || []).map(function (e) { return String(e).toLowerCase(); }); peopleBox.querySelectorAll("input[type=checkbox]").forEach(function (c) { c.checked = want.indexOf(c.value.toLowerCase()) >= 0; }); } // ── 저장(만들기·고치기) ── function showAtValue() { // 날짜를 비우면 바로(서버가 지금으로), 날짜만 있으면 그날 00:00, 시각까지 있으면 그 시각 — 모두 한국시간 return dateEl.value ? dateEl.value + "T" + (timeEl.value || "00:00") : ""; } function payload() { return { title: title.value.trim(), body_html: editor.getHtml(), show_at: showAtValue(), to_all: toAll(), targets: picked(), reshow: $("ntc-reshow").checked, }; } function resetForm() { idEl.value = ""; title.value = ""; editor.setHtml(""); dateEl.value = ""; timeEl.value = ""; root.querySelector('input[name="ntc-to"][value="all"]').checked = true; peopleBox.hidden = true; setPicked([]); $("ntc-reshow").checked = false; $("ntc-reshow-row").hidden = true; $("ntc-form-title").textContent = "새 긴급 공지"; save.textContent = "공지 만들기"; } $("ntc-reset").addEventListener("click", function () { resetForm(); say(""); }); let saving = false; save.addEventListener("click", async function () { if (saving) return; saving = true; save.disabled = true; try { await editor.whenIdle(); // 이미지 올리는 중이면 끝난 뒤 저장(빠진 채 저장되지 않게) const p = payload(); if (!p.title) { say("제목을 입력하세요."); title.focus(); return; } if (!p.to_all && !p.targets.length) { say("받는 사람을 한 명 이상 고르거나 '전체'를 고르세요."); return; } const id = idEl.value; if (id) await api("PUT", "/notice/api/notices/" + id, p); else await api("POST", "/notice/api/notices", p); resetForm(); say(id ? "공지를 고쳤습니다." : "공지를 만들었습니다. 띄울 시각이 되면 받는 사람 화면 가운데에 나타납니다.", true); await loadList(); } catch (e) { say("저장하지 못했습니다: " + e.message); } finally { saving = false; save.disabled = false; } }); // ── 미리 보기 — 받는 사람 화면과 같은 모양 ── $("ntc-preview").addEventListener("click", function () { preview(title.value.trim() || "(제목 없음)", editor.getHtml()); }); function preview(t, html) { const box = document.createElement("div"); box.className = "erp-notice-backdrop"; box.innerHTML = '