@daracl/dialog 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,936 @@
1
+ (function(factory) {
2
+ typeof define === "function" && define.amd ? define(factory) : factory();
3
+ })((function() {
4
+ "use strict";var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ /*!
8
+ * @daracl/dialog v0.0.9
9
+ * Copyright 2024-2026 darainfo and other contributors;
10
+ * Licensed MIT
11
+ */
12
+
13
+ function isSafeUrl(url) {
14
+ const value = url.trim().replace(/[\u0000-\u001F\u007F\s]+/g, "");
15
+ if (!value) return false;
16
+ const match = /^([a-z][a-z0-9+.-]*):/i.exec(value);
17
+ if (!match) return true;
18
+ if (match[1].toLowerCase() === "data") return /^data:image\/(?:png|gif|jpe?g|webp|avif|bmp)(?:;|,)/i.test(value);
19
+ return ["http", "https", "mailto", "tel"].includes(match[1].toLowerCase());
20
+ }
21
+ const DROP_TAGS = /* @__PURE__ */ new Set([
22
+ "script",
23
+ "style",
24
+ "iframe",
25
+ "frame",
26
+ "frameset",
27
+ "object",
28
+ "embed",
29
+ "applet",
30
+ "link",
31
+ "meta",
32
+ "base",
33
+ "form",
34
+ "input",
35
+ "button",
36
+ "textarea",
37
+ "select",
38
+ "option",
39
+ "svg",
40
+ "math",
41
+ "template",
42
+ "noscript",
43
+ "title"
44
+ ]);
45
+ const ALLOWED_TAGS = /* @__PURE__ */ new Set([
46
+ "a",
47
+ "abbr",
48
+ "b",
49
+ "blockquote",
50
+ "br",
51
+ "caption",
52
+ "code",
53
+ "col",
54
+ "colgroup",
55
+ "dd",
56
+ "del",
57
+ "div",
58
+ "dl",
59
+ "dt",
60
+ "em",
61
+ "figcaption",
62
+ "figure",
63
+ "h1",
64
+ "h2",
65
+ "h3",
66
+ "h4",
67
+ "h5",
68
+ "h6",
69
+ "hr",
70
+ "i",
71
+ "img",
72
+ "ins",
73
+ "li",
74
+ "mark",
75
+ "ol",
76
+ "p",
77
+ "pre",
78
+ "s",
79
+ "small",
80
+ "span",
81
+ "strong",
82
+ "sub",
83
+ "sup",
84
+ "table",
85
+ "tbody",
86
+ "td",
87
+ "tfoot",
88
+ "th",
89
+ "thead",
90
+ "tr",
91
+ "u",
92
+ "ul"
93
+ ]);
94
+ const ALLOWED_ATTRS = /* @__PURE__ */ new Set([
95
+ "class",
96
+ "style",
97
+ "title",
98
+ "alt",
99
+ "href",
100
+ "src",
101
+ "target",
102
+ "width",
103
+ "height",
104
+ "colspan",
105
+ "rowspan",
106
+ "align",
107
+ "valign",
108
+ "dir",
109
+ "lang"
110
+ ]);
111
+ const URL_ATTRS = /* @__PURE__ */ new Set(["href", "src"]);
112
+ const UNSAFE_STYLE = /url\s*\(|expression\s*\(|javascript:|@import|behavior\s*:|-moz-binding|position\s*:\s*(?:fixed|absolute|sticky)|\\|\/\*/i;
113
+ function sanitizeElement(el) {
114
+ Array.from(el.attributes).forEach((attr) => {
115
+ const name = attr.name.toLowerCase();
116
+ const value = attr.value;
117
+ const keep = ALLOWED_ATTRS.has(name) && (!URL_ATTRS.has(name) || isSafeUrl(value)) && (name !== "style" || !UNSAFE_STYLE.test(value)) && (name !== "target" || value === "_blank" || value === "_self");
118
+ if (!keep) el.removeAttribute(attr.name);
119
+ });
120
+ if (el.tagName === "A" && el.getAttribute("target") === "_blank") {
121
+ el.setAttribute("rel", "noopener noreferrer");
122
+ }
123
+ }
124
+ function sanitizeChildren(parent) {
125
+ Array.from(parent.childNodes).forEach((node) => {
126
+ var _a;
127
+ if (node.nodeType === Node.TEXT_NODE) return;
128
+ if (node.nodeType !== Node.ELEMENT_NODE) {
129
+ (_a = node.parentNode) == null ? void 0 : _a.removeChild(node);
130
+ return;
131
+ }
132
+ const el = node;
133
+ const tag = el.tagName.toLowerCase();
134
+ if (DROP_TAGS.has(tag)) {
135
+ el.remove();
136
+ return;
137
+ }
138
+ sanitizeChildren(el);
139
+ if (ALLOWED_TAGS.has(tag)) {
140
+ sanitizeElement(el);
141
+ } else {
142
+ el.replaceWith(...Array.from(el.childNodes));
143
+ }
144
+ });
145
+ }
146
+ function sanitizeHtml(html) {
147
+ const template = document.createElement("template");
148
+ template.innerHTML = html;
149
+ const fragment = template.content;
150
+ sanitizeChildren(fragment);
151
+ return document.importNode(fragment, true);
152
+ }
153
+ let DIALOG_IDX = 0;
154
+ let STACK_SEQ = 0;
155
+ const HIDDEN_ELEMENT_ID = "daraclDialogHidden";
156
+ const THEME_ATTRIBUTE = "data-daracl-theme";
157
+ const iconStyleMap = {
158
+ primary: "primary",
159
+ secondary: "secondary",
160
+ info: "info",
161
+ success: "success",
162
+ warning: "warning",
163
+ danger: "danger"
164
+ };
165
+ const FOCUSABLE_SELECTOR = [
166
+ "a[href]",
167
+ "area[href]",
168
+ "button:not([disabled])",
169
+ "input:not([disabled]):not([type=hidden])",
170
+ "select:not([disabled])",
171
+ "textarea:not([disabled])",
172
+ "iframe",
173
+ "[contenteditable=true]",
174
+ "[tabindex]:not([tabindex='-1'])"
175
+ ].join(",");
176
+ let defaultOptions = {
177
+ width: 400,
178
+ height: 300,
179
+ styleClass: "",
180
+ isModal: false,
181
+ icon: "",
182
+ // info,
183
+ // 자동 열기 여부
184
+ autoOpen: true,
185
+ // drag 가능여부
186
+ draggable: true,
187
+ // 창 리사이즈 여부
188
+ resizable: false,
189
+ // 헤더 영역 보일지 여부
190
+ enableHeader: true,
191
+ // 하단 버튼 영역 보일지 여부
192
+ enableFooter: true,
193
+ // 최대화 버튼
194
+ enableMaxButton: true,
195
+ // 닫기 버튼 활성화 여부
196
+ enableCloseButton: true,
197
+ // ESC 로 닫기
198
+ closeOnEscape: true,
199
+ // 포커스 이동 / 복원
200
+ autoFocus: true,
201
+ // 최소 높이
202
+ minHeight: 130,
203
+ // 최소 넓이
204
+ minWidth: 230,
205
+ // css z-index
206
+ zIndex: 1e4,
207
+ dragOverflow: true,
208
+ buttons: [],
209
+ moveDelay: 15,
210
+ afterOpen: false
211
+ // open 후 호출 메소드
212
+ };
213
+ function dialogHiddenElement() {
214
+ let hiddenElement = document.getElementById(HIDDEN_ELEMENT_ID);
215
+ if (hiddenElement == null) {
216
+ hiddenElement = document.createElement("div");
217
+ hiddenElement.id = HIDDEN_ELEMENT_ID;
218
+ hiddenElement.className = "daracl-dialog-hidden";
219
+ document.body.appendChild(hiddenElement);
220
+ }
221
+ return hiddenElement;
222
+ }
223
+ function toCssLength(value) {
224
+ if (value === void 0 || value === null || value === "") return "";
225
+ if (typeof value === "number" || /^\d+(\.\d+)?$/.test(String(value))) return value + "px";
226
+ return String(value);
227
+ }
228
+ function addClassNames(element, className) {
229
+ if (typeof className !== "string") return;
230
+ className.split(/\s+/).filter((name) => name !== "").forEach((name) => element.classList.add(name));
231
+ }
232
+ function isActivateKey(e) {
233
+ return e.key === "Enter" || e.key === " " || e.key === "Spacebar";
234
+ }
235
+ function eventPoint(e) {
236
+ const touch = e.touches && e.touches[0] ? e.touches[0] : e.changedTouches && e.changedTouches[0] ? e.changedTouches[0] : e;
237
+ return { clientX: touch.clientX, clientY: touch.clientY, pageX: touch.pageX, pageY: touch.pageY };
238
+ }
239
+ const _Dialog = class _Dialog {
240
+ /**
241
+ * @param {DialogOptions | string} options 옵션. 문자열이면 template 으로 쓴다.
242
+ */
243
+ constructor(options) {
244
+ /**
245
+ * ESC 닫기, 모달일 때 Tab focus trap
246
+ * @param {KeyboardEvent} e
247
+ */
248
+ __publicField(this, "onKeydown", (e) => {
249
+ if (!this.options) return;
250
+ if ((e.key === "Escape" || e.key === "Esc") && this.options.closeOnEscape !== false && this.options.enableCloseButton !== false) {
251
+ e.stopPropagation();
252
+ this.hide();
253
+ return;
254
+ }
255
+ if (e.key === "Tab" && this.options.isModal === true) {
256
+ const focusable = this.getFocusableElements();
257
+ if (focusable.length === 0) {
258
+ e.preventDefault();
259
+ this.dialogWrapperElement.focus();
260
+ return;
261
+ }
262
+ const first = focusable[0];
263
+ const last = focusable[focusable.length - 1];
264
+ const active = document.activeElement;
265
+ if (e.shiftKey && (active === first || active === this.dialogWrapperElement)) {
266
+ e.preventDefault();
267
+ last.focus();
268
+ } else if (!e.shiftKey && active === last) {
269
+ e.preventDefault();
270
+ first.focus();
271
+ }
272
+ }
273
+ });
274
+ /**
275
+ * show dialog message
276
+ * @returns {Dialog}
277
+ */
278
+ __publicField(this, "show", () => {
279
+ if (!this.dialogWrapperElement || !this.dialogWrapperElement.classList.contains("hide")) {
280
+ return this;
281
+ }
282
+ STACK_SEQ = Math.max(STACK_SEQ, DIALOG_IDX) + 2;
283
+ const zIndex = this.options.zIndex + STACK_SEQ;
284
+ this.returnFocusElement = typeof document !== "undefined" ? (
285
+ /** @type {HTMLElement | null} */
286
+ document.activeElement
287
+ ) : null;
288
+ this.dialogWrapperElement.style.zIndex = String(zIndex);
289
+ this.dialogWrapperElement.classList.remove("hide");
290
+ if (this.options.isModal === true) {
291
+ this.removeDialogOverrayElement();
292
+ const dialogOverrayElement = document.createElement("div");
293
+ dialogOverrayElement.className = "daracl-dialog-overlay";
294
+ dialogOverrayElement.style.zIndex = String(zIndex - 1);
295
+ if (this.themeName) {
296
+ dialogOverrayElement.setAttribute(THEME_ATTRIBUTE, this.themeName);
297
+ }
298
+ const hiddenElement = dialogHiddenElement();
299
+ const firstChild = hiddenElement.firstChild;
300
+ if (firstChild) {
301
+ hiddenElement.insertBefore(dialogOverrayElement, firstChild);
302
+ } else {
303
+ hiddenElement.appendChild(dialogOverrayElement);
304
+ }
305
+ this.dialogOverrayElement = dialogOverrayElement;
306
+ }
307
+ if (this.options.autoFocus !== false) {
308
+ const focusable = this.getFocusableElements();
309
+ const contentFirst = focusable.find((el) => el.closest(".dialog-content")) || focusable.find((el) => el.closest(".dialog-footer"));
310
+ const target = contentFirst || this.dialogWrapperElement;
311
+ try {
312
+ target.focus({ preventScroll: true });
313
+ } catch (e) {
314
+ target.focus();
315
+ }
316
+ }
317
+ if (typeof this.options.afterOpen === "function") {
318
+ this.options.afterOpen.call(this, this.dialogWrapperElement);
319
+ }
320
+ return this;
321
+ });
322
+ /**
323
+ * dialog hide
324
+ * @returns {Dialog}
325
+ */
326
+ __publicField(this, "hide", () => {
327
+ if (!this.dialogWrapperElement) return this;
328
+ const wasOpen = this.isOpen();
329
+ const hadFocus = this.dialogWrapperElement.contains(document.activeElement);
330
+ this.dialogWrapperElement.classList.add("hide");
331
+ if (this.options.isModal === true) {
332
+ this.removeDialogOverrayElement();
333
+ }
334
+ if (wasOpen) {
335
+ this.restoreFocus(hadFocus);
336
+ }
337
+ return this;
338
+ });
339
+ /**
340
+ * maximise hide
341
+ *
342
+ */
343
+ __publicField(this, "maximise", () => {
344
+ if (!this.dialogWrapperElement) return;
345
+ const classList = this.dialogWrapperElement.classList;
346
+ if (classList.contains("maximise")) {
347
+ classList.remove("maximise");
348
+ } else {
349
+ classList.add("maximise");
350
+ }
351
+ });
352
+ /**
353
+ * dialog destroy. 이벤트 · 타이머 · overlay · DOM 을 모두 정리한다. 여러 번 호출해도 된다.
354
+ */
355
+ __publicField(this, "destroy", () => {
356
+ if (!this.dialogWrapperElement) return;
357
+ const wasOpen = this.isOpen();
358
+ const hadFocus = this.dialogWrapperElement.contains(document.activeElement);
359
+ this.eventList.forEach(([element, name, fn]) => element.removeEventListener(name, fn, false));
360
+ this.eventList = [];
361
+ this.timerSet.forEach((timer) => clearTimeout(timer));
362
+ this.timerSet.clear();
363
+ if (typeof cancelAnimationFrame === "function") {
364
+ this.frameSet.forEach((frame) => cancelAnimationFrame(frame));
365
+ }
366
+ this.frameSet.clear();
367
+ this.removeDialogOverrayElement();
368
+ if (wasOpen) {
369
+ this.restoreFocus(hadFocus);
370
+ }
371
+ const wrapper = this.dialogWrapperElement;
372
+ if (wrapper.parentNode) {
373
+ wrapper.parentNode.removeChild(wrapper);
374
+ }
375
+ this.dialogWrapperElement = null;
376
+ this.options = null;
377
+ this.idx = -1;
378
+ });
379
+ if (typeof options === "string") {
380
+ options = /** @type {DialogOptions} */
381
+ { template: options, items: options };
382
+ }
383
+ this.options = Object.assign({}, defaultOptions, options);
384
+ this.idx = DIALOG_IDX += 1;
385
+ this.eventList = [];
386
+ this.timerSet = /* @__PURE__ */ new Set();
387
+ this.frameSet = /* @__PURE__ */ new Set();
388
+ const dialogWrapperElement = document.createElement("div");
389
+ dialogWrapperElement.className = `daracl-dialog-wrapper hide dd-${DIALOG_IDX}`;
390
+ dialogWrapperElement.setAttribute("role", "dialog");
391
+ dialogWrapperElement.setAttribute("tabindex", "-1");
392
+ if (this.options.isModal === true) {
393
+ dialogWrapperElement.setAttribute("aria-modal", "true");
394
+ }
395
+ const style = dialogWrapperElement.style;
396
+ style.zIndex = String(this.options.zIndex + DIALOG_IDX);
397
+ style.width = toCssLength(this.options.width);
398
+ style.height = toCssLength(this.options.height);
399
+ style.minHeight = toCssLength(this.options.minHeight);
400
+ style.minWidth = toCssLength(this.options.minWidth);
401
+ dialogHiddenElement().appendChild(dialogWrapperElement);
402
+ this.dialogWrapperElement = dialogWrapperElement;
403
+ this.setTheme(this.options.theme);
404
+ this.dialogTemplate();
405
+ this.addEvent(dialogWrapperElement, "keydown", this.onKeydown);
406
+ if (this.options.draggable !== false) {
407
+ dialogWrapperElement.classList.add("draggable");
408
+ this.initDrag();
409
+ }
410
+ if (this.options.resizable === true) {
411
+ dialogWrapperElement.classList.add("resizable");
412
+ this.initResize();
413
+ }
414
+ if (this.options.autoOpen !== false) {
415
+ this.show();
416
+ }
417
+ }
418
+ /**
419
+ * @param {DialogOptions | string} options
420
+ * @returns {Dialog}
421
+ */
422
+ static create(options) {
423
+ return new _Dialog(options);
424
+ }
425
+ /**
426
+ * default options 셋팅
427
+ *
428
+ * @static
429
+ * @param {DialogOptions} options
430
+ */
431
+ static setOptions(options) {
432
+ defaultOptions = Object.assign({}, defaultOptions, options);
433
+ }
434
+ /**
435
+ * 문자열 HTML 을 sanitize 옵션에 따라 target 에 추가한다.
436
+ *
437
+ * @param {Element} target 추가할 요소
438
+ * @param {string | number | Node | null | undefined} value 내용
439
+ * @param {boolean} trusted template 처럼 기본적으로 신뢰된 HTML 로 취급하는 값인지 여부
440
+ */
441
+ appendHtml(target, value, trusted) {
442
+ if (value === void 0 || value === null) return;
443
+ if (typeof Node !== "undefined" && value instanceof Node) {
444
+ target.appendChild(value);
445
+ return;
446
+ }
447
+ const html = String(value);
448
+ const sanitize = this.options.sanitize;
449
+ if (typeof sanitize === "function") {
450
+ const result = sanitize(html);
451
+ if (typeof Node !== "undefined" && result instanceof Node) {
452
+ target.appendChild(result);
453
+ } else {
454
+ target.insertAdjacentHTML("beforeend", result == null ? "" : String(result));
455
+ }
456
+ return;
457
+ }
458
+ if (sanitize === false || trusted && sanitize !== true) {
459
+ target.insertAdjacentHTML("beforeend", html);
460
+ return;
461
+ }
462
+ target.appendChild(sanitizeHtml(html));
463
+ }
464
+ /**
465
+ * 포커스 가능한 요소 목록
466
+ * @returns {HTMLElement[]}
467
+ */
468
+ getFocusableElements() {
469
+ if (!this.dialogWrapperElement) return [];
470
+ return Array.prototype.slice.call(this.dialogWrapperElement.querySelectorAll(FOCUSABLE_SELECTOR)).filter((el) => !el.closest(".resizer"));
471
+ }
472
+ /**
473
+ * 지연 실행 타이머 등록 (destroy 에서 정리한다)
474
+ * @param {() => void} fn
475
+ * @param {number} delay
476
+ * @returns {ReturnType<typeof setTimeout>}
477
+ */
478
+ setTimer(fn, delay) {
479
+ const timer = setTimeout(() => {
480
+ this.timerSet.delete(timer);
481
+ fn();
482
+ }, delay);
483
+ this.timerSet.add(timer);
484
+ return timer;
485
+ }
486
+ /** @param {ReturnType<typeof setTimeout> | undefined} timer */
487
+ clearTimer(timer) {
488
+ if (timer === void 0) return;
489
+ clearTimeout(timer);
490
+ this.timerSet.delete(timer);
491
+ }
492
+ /**
493
+ * 마우스 / 터치 이동을 화면 갱신 주기(requestAnimationFrame)마다 한 번만 반영한다.
494
+ * 한 프레임 안의 여러 move 이벤트는 마지막 좌표만 쓰고, flush 는 남은 좌표를 바로 반영한다 (놓을 때 마지막 이동 유지).
495
+ * requestAnimationFrame 이 없는 환경에서는 moveDelay(ms) 타이머로 대신한다.
496
+ * @param {(point: {clientX: number, clientY: number, pageX: number, pageY: number}) => void} apply
497
+ */
498
+ createFrameUpdater(apply) {
499
+ let pending = null;
500
+ let frame = 0;
501
+ let timer;
502
+ const cancel = () => {
503
+ if (frame) {
504
+ cancelAnimationFrame(frame);
505
+ this.frameSet.delete(frame);
506
+ frame = 0;
507
+ }
508
+ this.clearTimer(timer);
509
+ timer = void 0;
510
+ };
511
+ const run = () => {
512
+ cancel();
513
+ const point = pending;
514
+ pending = null;
515
+ if (point && this.dialogWrapperElement) {
516
+ apply(point);
517
+ }
518
+ };
519
+ return {
520
+ /** @param {{clientX: number, clientY: number, pageX: number, pageY: number}} point */
521
+ request: (point) => {
522
+ pending = point;
523
+ if (frame || timer !== void 0) return;
524
+ if (typeof requestAnimationFrame === "function") {
525
+ const id = requestAnimationFrame(() => {
526
+ this.frameSet.delete(id);
527
+ frame = 0;
528
+ run();
529
+ });
530
+ frame = id;
531
+ this.frameSet.add(id);
532
+ } else {
533
+ timer = this.setTimer(() => {
534
+ timer = void 0;
535
+ run();
536
+ }, Math.max(this.options.moveDelay || 0, 0));
537
+ }
538
+ },
539
+ flush: run
540
+ };
541
+ }
542
+ /** 사이즈 조절 초기화 */
543
+ initResize() {
544
+ const dialogElement = this.dialogWrapperElement;
545
+ const reslzeElement = this.dialogWrapperElement.querySelectorAll(".resizer");
546
+ let dialogWidth, dialogHeight;
547
+ let startX, startY;
548
+ let moveMode = "";
549
+ let offsetX = -1, offsetY = -1;
550
+ let isResizeTop, isResizeLeft;
551
+ this.addEvent(reslzeElement, "mousedown touchstart", (e) => {
552
+ if (this.isMaximise()) {
553
+ return false;
554
+ }
555
+ moveMode = /** @type {HTMLElement} */
556
+ e.currentTarget.getAttribute("data-mode") || "";
557
+ isResizeTop = moveMode.indexOf("t") > -1;
558
+ isResizeLeft = moveMode.indexOf("l") > -1;
559
+ offsetX = isResizeLeft ? dialogElement.getBoundingClientRect().left : -1;
560
+ offsetY = isResizeTop ? dialogElement.getBoundingClientRect().top : -1;
561
+ e.preventDefault();
562
+ const point = eventPoint(e);
563
+ startX = point.pageX;
564
+ startY = point.pageY;
565
+ dialogWidth = dialogElement.clientWidth;
566
+ dialogHeight = dialogElement.clientHeight;
567
+ if (!dialogElement.classList.contains("transform-disable")) {
568
+ const rect = dialogElement.getBoundingClientRect();
569
+ this.setPosition(point, point.clientX - rect.left, point.clientY - rect.top, dialogWidth, dialogHeight);
570
+ dialogElement.classList.add("transform-disable");
571
+ }
572
+ this.addEvent(document, "mousemove touchmove", onResizeMouseMove);
573
+ this.addEvent(document, "mouseup touchend touchcancel", onResizeMouseUp);
574
+ });
575
+ const resizeFrame = this.createFrameUpdater((point) => {
576
+ let moveX = point.pageX - startX, moveY = point.pageY - startY;
577
+ moveY = isResizeTop ? moveY * -1 : moveY;
578
+ moveX = isResizeLeft ? moveX * -1 : moveX;
579
+ let resizeHeight = dialogHeight + moveY, resizeWidth = dialogWidth + moveX;
580
+ if (this.options.minHeight >= resizeHeight) {
581
+ resizeHeight = this.options.minHeight;
582
+ moveY = (dialogHeight - resizeHeight) * -1;
583
+ }
584
+ if (this.options.minWidth >= resizeWidth) {
585
+ resizeWidth = this.options.minWidth;
586
+ moveX = (dialogWidth - resizeWidth) * -1;
587
+ }
588
+ let topVal = -1;
589
+ if (isResizeTop) {
590
+ topVal = offsetY - moveY;
591
+ if (topVal <= 0) {
592
+ topVal = 0;
593
+ resizeHeight = dialogHeight + offsetY;
594
+ }
595
+ dialogElement.style.top = topVal + "px";
596
+ }
597
+ let leftVal = -1;
598
+ if (isResizeLeft) {
599
+ leftVal = offsetX - moveX;
600
+ if (leftVal <= 0) {
601
+ leftVal = 0;
602
+ resizeWidth = dialogWidth + offsetX;
603
+ }
604
+ dialogElement.style.left = leftVal + "px";
605
+ }
606
+ if (moveMode != "b" && moveMode != "t") {
607
+ dialogElement.style.width = resizeWidth + "px";
608
+ }
609
+ if (moveMode != "r" && moveMode != "l") {
610
+ dialogElement.style.height = resizeHeight + "px";
611
+ }
612
+ });
613
+ const onResizeMouseMove = (e) => {
614
+ resizeFrame.request(eventPoint(e));
615
+ };
616
+ const onResizeMouseUp = () => {
617
+ resizeFrame.flush();
618
+ this.removeEvent(document, "mousemove touchmove", onResizeMouseMove);
619
+ this.removeEvent(document, "mouseup touchend touchcancel", onResizeMouseUp);
620
+ };
621
+ }
622
+ /**
623
+ * init drag
624
+ */
625
+ initDrag() {
626
+ const dialogElement = this.dialogWrapperElement;
627
+ const dragElement = this.dialogWrapperElement.querySelector(".dialog-header");
628
+ if (!dragElement) return;
629
+ let isDragging = false;
630
+ let offsetX, offsetY, dialogWidth, dialogHeight;
631
+ let isDrag = false;
632
+ this.addEvent(dragElement, "mousedown touchstart", (e) => {
633
+ const target = (
634
+ /** @type {HTMLElement} */
635
+ e.target
636
+ );
637
+ const tagName = target.tagName || "";
638
+ if (tagName.search(/(input|textarea|select|button)/gi) > -1) {
639
+ return true;
640
+ }
641
+ if (target.closest && target.closest(".btn-area")) {
642
+ return true;
643
+ }
644
+ if (this.isMaximise()) {
645
+ return false;
646
+ }
647
+ e.preventDefault();
648
+ const point = eventPoint(e);
649
+ const rect = dialogElement.getBoundingClientRect();
650
+ dialogWidth = dialogElement.clientWidth;
651
+ dialogHeight = dialogElement.clientHeight;
652
+ isDragging = true;
653
+ offsetX = point.clientX - rect.left;
654
+ offsetY = point.clientY - rect.top;
655
+ if (!isDrag) {
656
+ this.setPosition(point, offsetX, offsetY, dialogWidth, dialogHeight);
657
+ isDrag = true;
658
+ dialogElement.classList.add("transform-disable");
659
+ }
660
+ dialogElement.classList.add("move-on");
661
+ this.addEvent(document, "mousemove touchmove", onMouseMove);
662
+ this.addEvent(document, "mouseup touchend touchcancel", onMouseUp);
663
+ });
664
+ const dragFrame = this.createFrameUpdater((point) => {
665
+ this.setPosition(point, offsetX, offsetY, dialogWidth, dialogHeight);
666
+ });
667
+ const onMouseMove = (e) => {
668
+ if (isDragging) {
669
+ dragFrame.request(eventPoint(e));
670
+ }
671
+ };
672
+ const onMouseUp = () => {
673
+ dragFrame.flush();
674
+ isDragging = false;
675
+ dialogElement.classList.remove("move-on");
676
+ this.removeEvent(document, "mousemove touchmove", onMouseMove);
677
+ this.removeEvent(document, "mouseup touchend touchcancel", onMouseUp);
678
+ };
679
+ }
680
+ /**
681
+ * dialog position
682
+ *
683
+ * @param {{clientX: number, clientY: number}} e event 또는 좌표
684
+ * @param {Number} offsetX offset x
685
+ * @param {Number} offsetY offset y
686
+ * @param {Number} dialogWidth dialog width
687
+ * @param {Number} dialogHeight dialog height
688
+ */
689
+ setPosition(e, offsetX, offsetY, dialogWidth, dialogHeight) {
690
+ let minX, minY;
691
+ if (this.options.dragOverflow === false) {
692
+ minX = Math.min(Math.max(e.clientX - offsetX, 0), window.innerWidth - dialogWidth);
693
+ minY = Math.min(Math.max(e.clientY - offsetY, 0), window.innerHeight - dialogHeight);
694
+ } else {
695
+ minX = Math.min(Math.max(e.clientX - offsetX, (dialogWidth - 80) * -1), window.innerWidth - 80);
696
+ minY = Math.min(Math.max(e.clientY - offsetY, 0), window.innerHeight - 30);
697
+ }
698
+ this.dialogWrapperElement.style.left = minX + "px";
699
+ this.dialogWrapperElement.style.top = minY + "px";
700
+ }
701
+ /**
702
+ * span 버튼에 role / tabindex / 키보드 동작을 붙인다.
703
+ * @param {HTMLElement} element
704
+ * @param {string} eventName 마우스 이벤트 이름 (기존 동작 유지: 닫기 · 최대화는 click, 하단 버튼은 mousedown)
705
+ * @param {(e: Event) => void} handler
706
+ */
707
+ bindButton(element, eventName, handler) {
708
+ element.setAttribute("role", "button");
709
+ element.setAttribute("tabindex", "0");
710
+ this.addEvent(element, eventName, handler);
711
+ this.addEvent(element, "keydown", (e) => {
712
+ if (isActivateKey(
713
+ /** @type {KeyboardEvent} */
714
+ e
715
+ )) {
716
+ e.preventDefault();
717
+ handler(e);
718
+ }
719
+ });
720
+ }
721
+ dialogTemplate() {
722
+ var _a, _b, _c, _d, _e, _f;
723
+ const options = this.options;
724
+ const enableHeader = options.enableHeader !== false;
725
+ const enableFooter = options.enableFooter !== false;
726
+ const dialog = document.createElement("div");
727
+ dialog.className = "daracl-dialog";
728
+ addClassNames(dialog, (_a = options.styleClass) != null ? _a : "");
729
+ if (enableHeader) {
730
+ const header = document.createElement("div");
731
+ header.className = "dialog-header";
732
+ this.appendHtml(header, (_b = options.title) != null ? _b : "", false);
733
+ const label = (header.textContent || "").trim();
734
+ if (label !== "") {
735
+ this.dialogWrapperElement.setAttribute("aria-label", label);
736
+ }
737
+ const btnArea = document.createElement("div");
738
+ btnArea.className = "btn-area";
739
+ if (options.enableMaxButton) {
740
+ const maxButton2 = document.createElement("span");
741
+ maxButton2.className = "dialog-maximise";
742
+ maxButton2.setAttribute("aria-label", "Maximize");
743
+ maxButton2.setAttribute("title", "Maximize");
744
+ btnArea.appendChild(maxButton2);
745
+ }
746
+ if (options.enableCloseButton) {
747
+ const closeButton2 = document.createElement("span");
748
+ closeButton2.className = "dialog-close";
749
+ closeButton2.setAttribute("aria-label", "Close");
750
+ closeButton2.setAttribute("title", "Close");
751
+ btnArea.appendChild(closeButton2);
752
+ }
753
+ header.appendChild(btnArea);
754
+ dialog.appendChild(header);
755
+ }
756
+ const body = document.createElement("div");
757
+ body.className = "dialog-body";
758
+ if (Object.prototype.hasOwnProperty.call(iconStyleMap, options.icon)) {
759
+ body.classList.add(iconStyleMap[options.icon]);
760
+ }
761
+ body.style.height = `calc(100%${enableHeader ? " - 40px" : ""}${enableFooter ? " - 45px" : ""})`;
762
+ const moveOverlay = document.createElement("div");
763
+ moveOverlay.className = "dialog-move-overay";
764
+ body.appendChild(moveOverlay);
765
+ const content = document.createElement("div");
766
+ content.className = "dialog-content";
767
+ body.appendChild(content);
768
+ if (options.url) {
769
+ const url = String(options.url);
770
+ if (isSafeUrl(url)) {
771
+ const frame = document.createElement("iframe");
772
+ frame.className = "dialog-frame";
773
+ frame.src = url;
774
+ if (typeof options.title === "string") frame.title = (this.dialogWrapperElement.getAttribute("aria-label") || "").slice(0, 200);
775
+ content.appendChild(frame);
776
+ } else {
777
+ console.warn("[daracl.dialog] unsafe url blocked : " + url.slice(0, 100));
778
+ }
779
+ } else if (typeof options.template === "function") {
780
+ this.appendHtml(content, options.template(), true);
781
+ } else {
782
+ this.appendHtml(content, options.template, true);
783
+ }
784
+ dialog.appendChild(body);
785
+ const buttons = (_c = options.buttons) != null ? _c : [];
786
+ if (enableFooter) {
787
+ const footer = document.createElement("div");
788
+ footer.className = "dialog-footer";
789
+ const btnArea = document.createElement("div");
790
+ btnArea.className = "btn-area";
791
+ for (let i = 0; i < buttons.length; i++) {
792
+ const button = (
793
+ /** @type {DialogButton} */
794
+ (_d = buttons[i]) != null ? _d : {}
795
+ );
796
+ const btnElement = document.createElement("span");
797
+ btnElement.className = `dialog-btn dialog-btn-${i}`;
798
+ addClassNames(btnElement, (_e = button.styleClass) != null ? _e : "success");
799
+ this.appendHtml(btnElement, (_f = button.label) != null ? _f : "", false);
800
+ this.bindButton(btnElement, "mousedown", (e) => {
801
+ if (typeof button.callback === "function") {
802
+ button.callback(e);
803
+ }
804
+ });
805
+ btnArea.appendChild(btnElement);
806
+ }
807
+ footer.appendChild(btnArea);
808
+ dialog.appendChild(footer);
809
+ }
810
+ if (options.resizable === true) {
811
+ [
812
+ ["top", "t"],
813
+ ["left", "l"],
814
+ ["right", "r"],
815
+ ["bottom", "b"],
816
+ ["top-left", "tl"],
817
+ ["top-right", "tr"],
818
+ ["bottom-left", "bl"],
819
+ ["bottom-right", "br"]
820
+ ].forEach(([className, mode]) => {
821
+ const resizer = document.createElement("div");
822
+ resizer.className = `resizer ${className}`;
823
+ resizer.setAttribute("data-mode", mode);
824
+ dialog.appendChild(resizer);
825
+ });
826
+ }
827
+ this.dialogWrapperElement.appendChild(dialog);
828
+ const maxButton = (
829
+ /** @type {HTMLElement | null} */
830
+ dialog.querySelector(".dialog-header .dialog-maximise")
831
+ );
832
+ if (maxButton) {
833
+ this.bindButton(maxButton, "click", () => {
834
+ this.maximise();
835
+ });
836
+ }
837
+ const closeButton = (
838
+ /** @type {HTMLElement | null} */
839
+ dialog.querySelector(".dialog-header .dialog-close")
840
+ );
841
+ if (closeButton) {
842
+ this.bindButton(closeButton, "click", () => {
843
+ this.hide();
844
+ });
845
+ }
846
+ }
847
+ /**
848
+ * dialog 별 테마 지정. 빈 값이면 해제하고 상위 요소의 data-daracl-theme 를 따른다.
849
+ *
850
+ * @param {string} [themeName] light, dark
851
+ * @returns {Dialog}
852
+ */
853
+ setTheme(themeName) {
854
+ if (!this.dialogWrapperElement) return this;
855
+ this.themeName = themeName ? String(themeName) : "";
856
+ [this.dialogWrapperElement, this.dialogOverrayElement].forEach((element) => {
857
+ if (!element) return;
858
+ if (this.themeName) {
859
+ element.setAttribute(THEME_ATTRIBUTE, this.themeName);
860
+ } else {
861
+ element.removeAttribute(THEME_ATTRIBUTE);
862
+ }
863
+ });
864
+ return this;
865
+ }
866
+ /**
867
+ * dialog 가 보이는지 여부
868
+ * @returns {boolean}
869
+ */
870
+ isOpen() {
871
+ return !!this.dialogWrapperElement && !this.dialogWrapperElement.classList.contains("hide");
872
+ }
873
+ /**
874
+ * 열기 전에 포커스가 있던 요소로 되돌린다.
875
+ * @param {boolean} hadFocus 포커스가 dialog 안에 있었는지 여부
876
+ */
877
+ restoreFocus(hadFocus) {
878
+ const returnElement = this.returnFocusElement;
879
+ this.returnFocusElement = null;
880
+ if (!this.options || this.options.autoFocus === false || !hadFocus) return;
881
+ if (returnElement && typeof returnElement.focus === "function" && document.body.contains(returnElement)) {
882
+ returnElement.focus();
883
+ }
884
+ }
885
+ removeDialogOverrayElement() {
886
+ const overlay = this.dialogOverrayElement;
887
+ this.dialogOverrayElement = void 0;
888
+ if (overlay && overlay.parentNode) {
889
+ overlay.parentNode.removeChild(overlay);
890
+ }
891
+ }
892
+ isMaximise() {
893
+ return !!this.dialogWrapperElement && this.dialogWrapperElement.classList.contains("maximise");
894
+ }
895
+ /**
896
+ * 이벤트 등록. 등록한 이벤트는 destroy 에서 모두 해제한다.
897
+ * @param {EventTarget | NodeList | EventTarget[]} elements
898
+ * @param {string} events 공백으로 구분한 이벤트 이름
899
+ * @param {EventListener} fnHandler
900
+ */
901
+ addEvent(elements, events, fnHandler) {
902
+ const targets = elements instanceof NodeList || Array.isArray(elements) ? Array.prototype.slice.call(elements) : [elements];
903
+ events.split(" ").forEach((e) => {
904
+ if (e === "") return;
905
+ targets.forEach((element) => {
906
+ if (!element) return;
907
+ if (this.eventList.some(([el, name, fn]) => el === element && name === e && fn === fnHandler)) return;
908
+ element.addEventListener(e, fnHandler, false);
909
+ this.eventList.push([element, e, fnHandler]);
910
+ });
911
+ });
912
+ }
913
+ /**
914
+ * @param {EventTarget | NodeList | EventTarget[]} elements
915
+ * @param {string} events 공백으로 구분한 이벤트 이름
916
+ * @param {EventListener} fnHandler
917
+ */
918
+ removeEvent(elements, events, fnHandler) {
919
+ const targets = elements instanceof NodeList || Array.isArray(elements) ? Array.prototype.slice.call(elements) : [elements];
920
+ events.split(" ").forEach((e) => {
921
+ targets.forEach((element) => {
922
+ if (!element) return;
923
+ element.removeEventListener(e, fnHandler, false);
924
+ this.eventList = this.eventList.filter(([el, name, fn]) => !(el === element && name === e && fn === fnHandler));
925
+ });
926
+ });
927
+ }
928
+ };
929
+ __publicField(_Dialog, "VERSION", "0.0.9");
930
+ let Dialog = _Dialog;
931
+ if (typeof window !== "undefined") {
932
+ window.Daracl = window.Daracl || {};
933
+ window.Daracl.dialog = Dialog;
934
+ }
935
+ }));
936
+ //# sourceMappingURL=daracl.dialog.umd.js.map