@daracl/dialog 0.0.6 → 0.0.8

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,904 @@
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.8
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
+ this.removeDialogOverrayElement();
364
+ if (wasOpen) {
365
+ this.restoreFocus(hadFocus);
366
+ }
367
+ const wrapper = this.dialogWrapperElement;
368
+ if (wrapper.parentNode) {
369
+ wrapper.parentNode.removeChild(wrapper);
370
+ }
371
+ this.dialogWrapperElement = null;
372
+ this.options = null;
373
+ this.idx = -1;
374
+ });
375
+ if (typeof options === "string") {
376
+ options = /** @type {DialogOptions} */
377
+ { template: options, items: options };
378
+ }
379
+ this.options = Object.assign({}, defaultOptions, options);
380
+ this.idx = DIALOG_IDX += 1;
381
+ this.eventList = [];
382
+ this.timerSet = /* @__PURE__ */ new Set();
383
+ const dialogWrapperElement = document.createElement("div");
384
+ dialogWrapperElement.className = `daracl-dialog-wrapper hide dd-${DIALOG_IDX}`;
385
+ dialogWrapperElement.setAttribute("role", "dialog");
386
+ dialogWrapperElement.setAttribute("tabindex", "-1");
387
+ if (this.options.isModal === true) {
388
+ dialogWrapperElement.setAttribute("aria-modal", "true");
389
+ }
390
+ const style = dialogWrapperElement.style;
391
+ style.zIndex = String(this.options.zIndex + DIALOG_IDX);
392
+ style.width = toCssLength(this.options.width);
393
+ style.height = toCssLength(this.options.height);
394
+ style.minHeight = toCssLength(this.options.minHeight);
395
+ style.minWidth = toCssLength(this.options.minWidth);
396
+ dialogHiddenElement().appendChild(dialogWrapperElement);
397
+ this.dialogWrapperElement = dialogWrapperElement;
398
+ this.setTheme(this.options.theme);
399
+ this.dialogTemplate();
400
+ this.addEvent(dialogWrapperElement, "keydown", this.onKeydown);
401
+ if (this.options.draggable !== false) {
402
+ this.initDrag();
403
+ }
404
+ if (this.options.resizable === true) {
405
+ dialogWrapperElement.classList.add("resizable");
406
+ this.initResize();
407
+ }
408
+ if (this.options.autoOpen !== false) {
409
+ this.show();
410
+ }
411
+ }
412
+ /**
413
+ * @param {DialogOptions | string} options
414
+ * @returns {Dialog}
415
+ */
416
+ static create(options) {
417
+ return new _Dialog(options);
418
+ }
419
+ /**
420
+ * default options 셋팅
421
+ *
422
+ * @static
423
+ * @param {DialogOptions} options
424
+ */
425
+ static setOptions(options) {
426
+ defaultOptions = Object.assign({}, defaultOptions, options);
427
+ }
428
+ /**
429
+ * 문자열 HTML 을 sanitize 옵션에 따라 target 에 추가한다.
430
+ *
431
+ * @param {Element} target 추가할 요소
432
+ * @param {string | number | Node | null | undefined} value 내용
433
+ * @param {boolean} trusted template 처럼 기본적으로 신뢰된 HTML 로 취급하는 값인지 여부
434
+ */
435
+ appendHtml(target, value, trusted) {
436
+ if (value === void 0 || value === null) return;
437
+ if (typeof Node !== "undefined" && value instanceof Node) {
438
+ target.appendChild(value);
439
+ return;
440
+ }
441
+ const html = String(value);
442
+ const sanitize = this.options.sanitize;
443
+ if (typeof sanitize === "function") {
444
+ const result = sanitize(html);
445
+ if (typeof Node !== "undefined" && result instanceof Node) {
446
+ target.appendChild(result);
447
+ } else {
448
+ target.insertAdjacentHTML("beforeend", result == null ? "" : String(result));
449
+ }
450
+ return;
451
+ }
452
+ if (sanitize === false || trusted && sanitize !== true) {
453
+ target.insertAdjacentHTML("beforeend", html);
454
+ return;
455
+ }
456
+ target.appendChild(sanitizeHtml(html));
457
+ }
458
+ /**
459
+ * 포커스 가능한 요소 목록
460
+ * @returns {HTMLElement[]}
461
+ */
462
+ getFocusableElements() {
463
+ if (!this.dialogWrapperElement) return [];
464
+ return Array.prototype.slice.call(this.dialogWrapperElement.querySelectorAll(FOCUSABLE_SELECTOR)).filter((el) => !el.closest(".resizer"));
465
+ }
466
+ /**
467
+ * 지연 실행 타이머 등록 (destroy 에서 정리한다)
468
+ * @param {() => void} fn
469
+ * @param {number} delay
470
+ * @returns {ReturnType<typeof setTimeout>}
471
+ */
472
+ setTimer(fn, delay) {
473
+ const timer = setTimeout(() => {
474
+ this.timerSet.delete(timer);
475
+ fn();
476
+ }, delay);
477
+ this.timerSet.add(timer);
478
+ return timer;
479
+ }
480
+ /** @param {ReturnType<typeof setTimeout> | undefined} timer */
481
+ clearTimer(timer) {
482
+ if (timer === void 0) return;
483
+ clearTimeout(timer);
484
+ this.timerSet.delete(timer);
485
+ }
486
+ /** 사이즈 조절 초기화 */
487
+ initResize() {
488
+ const dialogElement = this.dialogWrapperElement;
489
+ const reslzeElement = this.dialogWrapperElement.querySelectorAll(".resizer");
490
+ let dialogWidth, dialogHeight;
491
+ let startX, startY;
492
+ let moveMode = "";
493
+ let offsetX = -1, offsetY = -1;
494
+ let isResizeTop, isResizeLeft;
495
+ this.addEvent(reslzeElement, "mousedown touchstart", (e) => {
496
+ if (this.isMaximise()) {
497
+ return false;
498
+ }
499
+ moveMode = /** @type {HTMLElement} */
500
+ e.currentTarget.getAttribute("data-mode") || "";
501
+ isResizeTop = moveMode.indexOf("t") > -1;
502
+ isResizeLeft = moveMode.indexOf("l") > -1;
503
+ offsetX = isResizeLeft ? dialogElement.getBoundingClientRect().left : -1;
504
+ offsetY = isResizeTop ? dialogElement.getBoundingClientRect().top : -1;
505
+ e.preventDefault();
506
+ const point = eventPoint(e);
507
+ startX = point.pageX;
508
+ startY = point.pageY;
509
+ dialogWidth = dialogElement.clientWidth;
510
+ dialogHeight = dialogElement.clientHeight;
511
+ if (!dialogElement.classList.contains("transform-disable")) {
512
+ const rect = dialogElement.getBoundingClientRect();
513
+ this.setPosition(point, point.clientX - rect.left, point.clientY - rect.top, dialogWidth, dialogHeight);
514
+ dialogElement.classList.add("transform-disable");
515
+ }
516
+ this.addEvent(document, "mousemove touchmove", onResizeMouseMove);
517
+ this.addEvent(document, "mouseup touchend touchcancel", onResizeMouseUp);
518
+ });
519
+ let startTime = -1;
520
+ let moveTimer;
521
+ const moveDelay = this.options.moveDelay;
522
+ const onResizeMouseMove = (e) => {
523
+ if (startTime == -1) {
524
+ startTime = (/* @__PURE__ */ new Date()).getTime();
525
+ }
526
+ if ((/* @__PURE__ */ new Date()).getTime() - moveDelay <= startTime) {
527
+ this.clearTimer(moveTimer);
528
+ }
529
+ const point = eventPoint(e);
530
+ moveTimer = this.setTimer(() => {
531
+ startTime = -1;
532
+ if (!this.options) return;
533
+ let moveX = point.pageX - startX, moveY = point.pageY - startY;
534
+ moveY = isResizeTop ? moveY * -1 : moveY;
535
+ moveX = isResizeLeft ? moveX * -1 : moveX;
536
+ let resizeHeight = dialogHeight + moveY, resizeWidth = dialogWidth + moveX;
537
+ if (this.options.minHeight >= resizeHeight) {
538
+ resizeHeight = this.options.minHeight;
539
+ moveY = (dialogHeight - resizeHeight) * -1;
540
+ }
541
+ if (this.options.minWidth >= resizeWidth) {
542
+ resizeWidth = this.options.minWidth;
543
+ moveX = (dialogWidth - resizeWidth) * -1;
544
+ }
545
+ let topVal = -1;
546
+ if (isResizeTop) {
547
+ topVal = offsetY - moveY;
548
+ if (topVal <= 0) {
549
+ topVal = 0;
550
+ resizeHeight = dialogHeight + offsetY;
551
+ }
552
+ dialogElement.style.top = topVal + "px";
553
+ }
554
+ let leftVal = -1;
555
+ if (isResizeLeft) {
556
+ leftVal = offsetX - moveX;
557
+ if (leftVal <= 0) {
558
+ leftVal = 0;
559
+ resizeWidth = dialogWidth + offsetX;
560
+ }
561
+ dialogElement.style.left = leftVal + "px";
562
+ }
563
+ if (moveMode != "b" && moveMode != "t") {
564
+ dialogElement.style.width = resizeWidth + "px";
565
+ }
566
+ if (moveMode != "r" && moveMode != "l") {
567
+ dialogElement.style.height = resizeHeight + "px";
568
+ }
569
+ }, moveDelay);
570
+ };
571
+ const onResizeMouseUp = () => {
572
+ this.clearTimer(moveTimer);
573
+ startTime = -1;
574
+ this.removeEvent(document, "mousemove touchmove", onResizeMouseMove);
575
+ this.removeEvent(document, "mouseup touchend touchcancel", onResizeMouseUp);
576
+ };
577
+ }
578
+ /**
579
+ * init drag
580
+ */
581
+ initDrag() {
582
+ const dialogElement = this.dialogWrapperElement;
583
+ const dragElement = this.dialogWrapperElement.querySelector(".dialog-header");
584
+ if (!dragElement) return;
585
+ let isDragging = false;
586
+ let offsetX, offsetY, dialogWidth, dialogHeight;
587
+ let isDrag = false;
588
+ this.addEvent(dragElement, "mousedown touchstart", (e) => {
589
+ const target = (
590
+ /** @type {HTMLElement} */
591
+ e.target
592
+ );
593
+ const tagName = target.tagName || "";
594
+ if (tagName.search(/(input|textarea|select|button)/gi) > -1) {
595
+ return true;
596
+ }
597
+ if (target.closest && target.closest(".btn-area")) {
598
+ return true;
599
+ }
600
+ if (this.isMaximise()) {
601
+ return false;
602
+ }
603
+ e.preventDefault();
604
+ const point = eventPoint(e);
605
+ const rect = dialogElement.getBoundingClientRect();
606
+ dialogWidth = dialogElement.clientWidth;
607
+ dialogHeight = dialogElement.clientHeight;
608
+ isDragging = true;
609
+ offsetX = point.clientX - rect.left;
610
+ offsetY = point.clientY - rect.top;
611
+ if (!isDrag) {
612
+ this.setPosition(point, offsetX, offsetY, dialogWidth, dialogHeight);
613
+ isDrag = true;
614
+ dialogElement.classList.add("transform-disable");
615
+ }
616
+ dialogElement.classList.add("move-on");
617
+ this.addEvent(document, "mousemove touchmove", onMouseMove);
618
+ this.addEvent(document, "mouseup touchend touchcancel", onMouseUp);
619
+ });
620
+ let startTime = -1;
621
+ let moveTimer;
622
+ const moveDelay = this.options.moveDelay;
623
+ const onMouseMove = (e) => {
624
+ if (isDragging) {
625
+ if (startTime == -1) {
626
+ startTime = (/* @__PURE__ */ new Date()).getTime();
627
+ }
628
+ if ((/* @__PURE__ */ new Date()).getTime() - moveDelay <= startTime) {
629
+ this.clearTimer(moveTimer);
630
+ }
631
+ const point = eventPoint(e);
632
+ moveTimer = this.setTimer(() => {
633
+ startTime = -1;
634
+ if (!this.dialogWrapperElement) return;
635
+ this.setPosition(point, offsetX, offsetY, dialogWidth, dialogHeight);
636
+ }, moveDelay);
637
+ }
638
+ };
639
+ const onMouseUp = () => {
640
+ isDragging = false;
641
+ startTime = -1;
642
+ this.clearTimer(moveTimer);
643
+ dialogElement.classList.remove("move-on");
644
+ this.removeEvent(document, "mousemove touchmove", onMouseMove);
645
+ this.removeEvent(document, "mouseup touchend touchcancel", onMouseUp);
646
+ };
647
+ }
648
+ /**
649
+ * dialog position
650
+ *
651
+ * @param {{clientX: number, clientY: number}} e event 또는 좌표
652
+ * @param {Number} offsetX offset x
653
+ * @param {Number} offsetY offset y
654
+ * @param {Number} dialogWidth dialog width
655
+ * @param {Number} dialogHeight dialog height
656
+ */
657
+ setPosition(e, offsetX, offsetY, dialogWidth, dialogHeight) {
658
+ let minX, minY;
659
+ if (this.options.dragOverflow === false) {
660
+ minX = Math.min(Math.max(e.clientX - offsetX, 0), window.innerWidth - dialogWidth);
661
+ minY = Math.min(Math.max(e.clientY - offsetY, 0), window.innerHeight - dialogHeight);
662
+ } else {
663
+ minX = Math.min(Math.max(e.clientX - offsetX, (dialogWidth - 80) * -1), window.innerWidth - 80);
664
+ minY = Math.min(Math.max(e.clientY - offsetY, 0), window.innerHeight - 30);
665
+ }
666
+ this.dialogWrapperElement.style.left = minX + "px";
667
+ this.dialogWrapperElement.style.top = minY + "px";
668
+ }
669
+ /**
670
+ * span 버튼에 role / tabindex / 키보드 동작을 붙인다.
671
+ * @param {HTMLElement} element
672
+ * @param {string} eventName 마우스 이벤트 이름 (기존 동작 유지: 닫기 · 최대화는 click, 하단 버튼은 mousedown)
673
+ * @param {(e: Event) => void} handler
674
+ */
675
+ bindButton(element, eventName, handler) {
676
+ element.setAttribute("role", "button");
677
+ element.setAttribute("tabindex", "0");
678
+ this.addEvent(element, eventName, handler);
679
+ this.addEvent(element, "keydown", (e) => {
680
+ if (isActivateKey(
681
+ /** @type {KeyboardEvent} */
682
+ e
683
+ )) {
684
+ e.preventDefault();
685
+ handler(e);
686
+ }
687
+ });
688
+ }
689
+ dialogTemplate() {
690
+ var _a, _b, _c, _d, _e, _f;
691
+ const options = this.options;
692
+ const enableHeader = options.enableHeader !== false;
693
+ const enableFooter = options.enableFooter !== false;
694
+ const dialog = document.createElement("div");
695
+ dialog.className = "daracl-dialog";
696
+ addClassNames(dialog, (_a = options.styleClass) != null ? _a : "");
697
+ if (enableHeader) {
698
+ const header = document.createElement("div");
699
+ header.className = "dialog-header";
700
+ this.appendHtml(header, (_b = options.title) != null ? _b : "", false);
701
+ const label = (header.textContent || "").trim();
702
+ if (label !== "") {
703
+ this.dialogWrapperElement.setAttribute("aria-label", label);
704
+ }
705
+ const btnArea = document.createElement("div");
706
+ btnArea.className = "btn-area";
707
+ if (options.enableMaxButton) {
708
+ const maxButton2 = document.createElement("span");
709
+ maxButton2.className = "dialog-maximise";
710
+ maxButton2.setAttribute("aria-label", "Maximize");
711
+ maxButton2.setAttribute("title", "Maximize");
712
+ btnArea.appendChild(maxButton2);
713
+ }
714
+ if (options.enableCloseButton) {
715
+ const closeButton2 = document.createElement("span");
716
+ closeButton2.className = "dialog-close";
717
+ closeButton2.setAttribute("aria-label", "Close");
718
+ closeButton2.setAttribute("title", "Close");
719
+ btnArea.appendChild(closeButton2);
720
+ }
721
+ header.appendChild(btnArea);
722
+ dialog.appendChild(header);
723
+ }
724
+ const body = document.createElement("div");
725
+ body.className = "dialog-body";
726
+ if (Object.prototype.hasOwnProperty.call(iconStyleMap, options.icon)) {
727
+ body.classList.add(iconStyleMap[options.icon]);
728
+ }
729
+ body.style.height = `calc(100%${enableHeader ? " - 40px" : ""}${enableFooter ? " - 45px" : ""})`;
730
+ const moveOverlay = document.createElement("div");
731
+ moveOverlay.className = "dialog-move-overay";
732
+ body.appendChild(moveOverlay);
733
+ const content = document.createElement("div");
734
+ content.className = "dialog-content";
735
+ body.appendChild(content);
736
+ if (options.url) {
737
+ const url = String(options.url);
738
+ if (isSafeUrl(url)) {
739
+ const frame = document.createElement("iframe");
740
+ frame.className = "dialog-frame";
741
+ frame.src = url;
742
+ if (typeof options.title === "string") frame.title = (this.dialogWrapperElement.getAttribute("aria-label") || "").slice(0, 200);
743
+ content.appendChild(frame);
744
+ } else {
745
+ console.warn("[daracl.dialog] unsafe url blocked : " + url.slice(0, 100));
746
+ }
747
+ } else if (typeof options.template === "function") {
748
+ this.appendHtml(content, options.template(), true);
749
+ } else {
750
+ this.appendHtml(content, options.template, true);
751
+ }
752
+ dialog.appendChild(body);
753
+ const buttons = (_c = options.buttons) != null ? _c : [];
754
+ if (enableFooter) {
755
+ const footer = document.createElement("div");
756
+ footer.className = "dialog-footer";
757
+ const btnArea = document.createElement("div");
758
+ btnArea.className = "btn-area";
759
+ for (let i = 0; i < buttons.length; i++) {
760
+ const button = (
761
+ /** @type {DialogButton} */
762
+ (_d = buttons[i]) != null ? _d : {}
763
+ );
764
+ const btnElement = document.createElement("span");
765
+ btnElement.className = `dialog-btn dialog-btn-${i}`;
766
+ addClassNames(btnElement, (_e = button.styleClass) != null ? _e : "success");
767
+ this.appendHtml(btnElement, (_f = button.label) != null ? _f : "", false);
768
+ this.bindButton(btnElement, "mousedown", (e) => {
769
+ if (typeof button.callback === "function") {
770
+ button.callback(e);
771
+ }
772
+ });
773
+ btnArea.appendChild(btnElement);
774
+ }
775
+ footer.appendChild(btnArea);
776
+ dialog.appendChild(footer);
777
+ }
778
+ if (options.resizable === true) {
779
+ [
780
+ ["top", "t"],
781
+ ["left", "l"],
782
+ ["right", "r"],
783
+ ["bottom", "b"],
784
+ ["top-left", "tl"],
785
+ ["top-right", "tr"],
786
+ ["bottom-left", "bl"],
787
+ ["bottom-right", "br"]
788
+ ].forEach(([className, mode]) => {
789
+ const resizer = document.createElement("div");
790
+ resizer.className = `resizer ${className}`;
791
+ resizer.setAttribute("data-mode", mode);
792
+ dialog.appendChild(resizer);
793
+ });
794
+ }
795
+ this.dialogWrapperElement.appendChild(dialog);
796
+ const maxButton = (
797
+ /** @type {HTMLElement | null} */
798
+ dialog.querySelector(".dialog-header .dialog-maximise")
799
+ );
800
+ if (maxButton) {
801
+ this.bindButton(maxButton, "click", () => {
802
+ this.maximise();
803
+ });
804
+ }
805
+ const closeButton = (
806
+ /** @type {HTMLElement | null} */
807
+ dialog.querySelector(".dialog-header .dialog-close")
808
+ );
809
+ if (closeButton) {
810
+ this.bindButton(closeButton, "click", () => {
811
+ this.hide();
812
+ });
813
+ }
814
+ }
815
+ /**
816
+ * dialog 별 테마 지정. 빈 값이면 해제하고 상위 요소의 data-daracl-theme 를 따른다.
817
+ *
818
+ * @param {string} [themeName] light, dark
819
+ * @returns {Dialog}
820
+ */
821
+ setTheme(themeName) {
822
+ if (!this.dialogWrapperElement) return this;
823
+ this.themeName = themeName ? String(themeName) : "";
824
+ [this.dialogWrapperElement, this.dialogOverrayElement].forEach((element) => {
825
+ if (!element) return;
826
+ if (this.themeName) {
827
+ element.setAttribute(THEME_ATTRIBUTE, this.themeName);
828
+ } else {
829
+ element.removeAttribute(THEME_ATTRIBUTE);
830
+ }
831
+ });
832
+ return this;
833
+ }
834
+ /**
835
+ * dialog 가 보이는지 여부
836
+ * @returns {boolean}
837
+ */
838
+ isOpen() {
839
+ return !!this.dialogWrapperElement && !this.dialogWrapperElement.classList.contains("hide");
840
+ }
841
+ /**
842
+ * 열기 전에 포커스가 있던 요소로 되돌린다.
843
+ * @param {boolean} hadFocus 포커스가 dialog 안에 있었는지 여부
844
+ */
845
+ restoreFocus(hadFocus) {
846
+ const returnElement = this.returnFocusElement;
847
+ this.returnFocusElement = null;
848
+ if (!this.options || this.options.autoFocus === false || !hadFocus) return;
849
+ if (returnElement && typeof returnElement.focus === "function" && document.body.contains(returnElement)) {
850
+ returnElement.focus();
851
+ }
852
+ }
853
+ removeDialogOverrayElement() {
854
+ const overlay = this.dialogOverrayElement;
855
+ this.dialogOverrayElement = void 0;
856
+ if (overlay && overlay.parentNode) {
857
+ overlay.parentNode.removeChild(overlay);
858
+ }
859
+ }
860
+ isMaximise() {
861
+ return !!this.dialogWrapperElement && this.dialogWrapperElement.classList.contains("maximise");
862
+ }
863
+ /**
864
+ * 이벤트 등록. 등록한 이벤트는 destroy 에서 모두 해제한다.
865
+ * @param {EventTarget | NodeList | EventTarget[]} elements
866
+ * @param {string} events 공백으로 구분한 이벤트 이름
867
+ * @param {EventListener} fnHandler
868
+ */
869
+ addEvent(elements, events, fnHandler) {
870
+ const targets = elements instanceof NodeList || Array.isArray(elements) ? Array.prototype.slice.call(elements) : [elements];
871
+ events.split(" ").forEach((e) => {
872
+ if (e === "") return;
873
+ targets.forEach((element) => {
874
+ if (!element) return;
875
+ if (this.eventList.some(([el, name, fn]) => el === element && name === e && fn === fnHandler)) return;
876
+ element.addEventListener(e, fnHandler, false);
877
+ this.eventList.push([element, e, fnHandler]);
878
+ });
879
+ });
880
+ }
881
+ /**
882
+ * @param {EventTarget | NodeList | EventTarget[]} elements
883
+ * @param {string} events 공백으로 구분한 이벤트 이름
884
+ * @param {EventListener} fnHandler
885
+ */
886
+ removeEvent(elements, events, fnHandler) {
887
+ const targets = elements instanceof NodeList || Array.isArray(elements) ? Array.prototype.slice.call(elements) : [elements];
888
+ events.split(" ").forEach((e) => {
889
+ targets.forEach((element) => {
890
+ if (!element) return;
891
+ element.removeEventListener(e, fnHandler, false);
892
+ this.eventList = this.eventList.filter(([el, name, fn]) => !(el === element && name === e && fn === fnHandler));
893
+ });
894
+ });
895
+ }
896
+ };
897
+ __publicField(_Dialog, "VERSION", "0.0.8");
898
+ let Dialog = _Dialog;
899
+ if (typeof window !== "undefined") {
900
+ window.Daracl = window.Daracl || {};
901
+ window.Daracl.dialog = Dialog;
902
+ }
903
+ }));
904
+ //# sourceMappingURL=daracl.dialog.umd.js.map