@dodlhuat/basix 1.2.0 → 1.2.2
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.
- package/README.md +266 -6
- package/css/accordion.scss +86 -87
- package/css/alert.scss +137 -137
- package/css/button.scss +48 -0
- package/css/calendar.scss +957 -0
- package/css/card.scss +65 -65
- package/css/chart.scss +270 -157
- package/css/chat-bubbles.scss +134 -68
- package/css/chips.scss +109 -19
- package/css/colors.scss +32 -32
- package/css/datepicker.scss +336 -336
- package/css/defaults.scss +90 -90
- package/css/docs.scss +529 -0
- package/css/editor.scss +36 -0
- package/css/file-uploader.scss +1 -1
- package/css/flyout-menu.scss +361 -361
- package/css/form.scss +0 -15
- package/css/gallery.scss +65 -6
- package/css/grid.scss +41 -40
- package/css/group-picker.scss +345 -0
- package/css/guitar-chords.css +250 -250
- package/css/icons.scss +330 -330
- package/css/parameters.scss +3 -3
- package/css/placeholder.scss +33 -33
- package/css/popover.scss +206 -0
- package/css/progress.scss +76 -32
- package/css/properties.scss +51 -36
- package/css/push-menu.scss +302 -174
- package/css/reset.scss +39 -39
- package/css/scrollbar.scss +62 -5
- package/css/sidebar-nav.scss +92 -0
- package/css/spinner.scss +65 -65
- package/css/stepper.scss +48 -12
- package/css/style.css +3155 -254
- package/css/style.css.map +1 -1
- package/css/style.min.css +1 -1
- package/css/style.scss +51 -45
- package/css/table.scss +199 -199
- package/css/tabs.scss +154 -123
- package/css/timeline.scss +83 -38
- package/css/timepicker.scss +100 -5
- package/css/toast.scss +81 -81
- package/css/virtual-dropdown.scss +35 -29
- package/js/calendar.js +532 -0
- package/js/calendar.ts +706 -0
- package/js/chart.js +573 -257
- package/js/chart.ts +692 -0
- package/js/code-viewer.js +10 -10
- package/js/code-viewer.ts +188 -188
- package/js/datepicker.ts +627 -627
- package/js/docs-nav.js +204 -0
- package/js/dropdown.ts +179 -179
- package/js/editor.js +50 -6
- package/js/editor.ts +483 -444
- package/js/file-uploader.js +1 -0
- package/js/file-uploader.ts +1 -0
- package/js/flyout-menu.js +14 -14
- package/js/flyout-menu.ts +249 -249
- package/js/form-builder.js +106 -106
- package/js/gallery.js +14 -8
- package/js/gallery.ts +245 -236
- package/js/group-picker.js +342 -0
- package/js/group-picker.ts +447 -0
- package/js/guitar-chords.js +268 -268
- package/js/lazy-loader.js +121 -121
- package/js/modal.ts +166 -166
- package/js/popover.js +163 -0
- package/js/popover.ts +219 -0
- package/js/position.js +108 -0
- package/js/position.ts +111 -0
- package/js/push-menu.js +113 -0
- package/js/push-menu.ts +284 -145
- package/js/request.js +50 -50
- package/js/scroll.ts +47 -47
- package/js/scrollbar.js +13 -0
- package/js/scrollbar.ts +324 -307
- package/js/select.ts +216 -216
- package/js/sidebar-nav.js +41 -0
- package/js/sidebar-nav.ts +66 -0
- package/js/table.ts +452 -452
- package/js/tabs.ts +279 -279
- package/js/theme.js +17 -6
- package/js/theme.ts +234 -224
- package/js/toast.ts +137 -137
- package/js/tooltip.js +6 -60
- package/js/tooltip.ts +184 -251
- package/js/tsconfig.json +18 -18
- package/js/utils.ts +83 -83
- package/js/virtual-dropdown.js +25 -25
- package/js/virtual-dropdown.ts +365 -365
- package/package.json +37 -39
- package/js/index.js +0 -816
- package/js/index.ts +0 -987
package/js/lazy-loader.js
CHANGED
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LazyLoader - A class to lazy load content into elements when they scroll into view.
|
|
3
|
-
*
|
|
4
|
-
* how to use:
|
|
5
|
-
* const lazyLoader = new LazyLoader({
|
|
6
|
-
* selector: '.lazy-section',
|
|
7
|
-
* rootMargin: '0px 0px 100px 0px', // Load 100px before element is visible
|
|
8
|
-
* threshold: 0.1,
|
|
9
|
-
* simulatedDelay: 2000, // 2 second delay to see the spinner
|
|
10
|
-
* onLoad: (el) => console.log('Loaded content for', el),
|
|
11
|
-
* onError: (err, el) => console.error('Failed to load', el)
|
|
12
|
-
* });
|
|
13
|
-
*
|
|
14
|
-
* in html:
|
|
15
|
-
* <div class="lazy-section" data-src="content/reviews.html"></div>
|
|
16
|
-
*/
|
|
17
|
-
class LazyLoader {
|
|
18
|
-
/**
|
|
19
|
-
* @param {Object} options - Configuration options
|
|
20
|
-
* @param {string} options.selector - CSS selector for elements to observe (default: '.lazy-load')
|
|
21
|
-
* @param {string} options.attribute - Attribute containing the URL to load (default: 'data-src')
|
|
22
|
-
* @param {string} options.rootMargin - IntersectionObserver rootMargin (default: '0px 0px 200px 0px')
|
|
23
|
-
* @param {number} options.threshold - IntersectionObserver threshold (default: 0.1)
|
|
24
|
-
* @param {Function} options.onError - Callback function when loading fails
|
|
25
|
-
* @param {Function} options.onLoad - Callback function when loading succeeds
|
|
26
|
-
*/
|
|
27
|
-
constructor(options = {}) {
|
|
28
|
-
this.selector = options.selector || '.lazy-load';
|
|
29
|
-
this.attribute = options.attribute || 'data-src';
|
|
30
|
-
this.rootMargin = options.rootMargin || '0px 0px 200px 0px';
|
|
31
|
-
this.threshold = options.threshold || 0.1;
|
|
32
|
-
this.onError = options.onError || null;
|
|
33
|
-
this.onLoad = options.onLoad || null;
|
|
34
|
-
this.simulatedDelay = options.simulatedDelay || 0; // Delay in ms for testing
|
|
35
|
-
this.init();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
init() {
|
|
39
|
-
this.elements = document.querySelectorAll(this.selector);
|
|
40
|
-
|
|
41
|
-
if ('IntersectionObserver' in window) {
|
|
42
|
-
this.setupIntersectionObserver();
|
|
43
|
-
} else {
|
|
44
|
-
this.setupScrollListener();
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
setupIntersectionObserver() {
|
|
49
|
-
const observerOptions = {
|
|
50
|
-
root: null,
|
|
51
|
-
rootMargin: this.rootMargin,
|
|
52
|
-
threshold: this.threshold
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const observer = new IntersectionObserver((entries, observer) => {
|
|
56
|
-
entries.forEach(entry => {
|
|
57
|
-
if (entry.isIntersecting) {
|
|
58
|
-
this.loadContent(entry.target);
|
|
59
|
-
observer.unobserve(entry.target);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
}, observerOptions);
|
|
63
|
-
|
|
64
|
-
this.elements.forEach(el => observer.observe(el));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
setupScrollListener() {
|
|
68
|
-
// Fallback for older browsers
|
|
69
|
-
const checkVisible = () => {
|
|
70
|
-
this.elements.forEach(el => {
|
|
71
|
-
if (el.getAttribute(this.attribute)) {
|
|
72
|
-
const rect = el.getBoundingClientRect();
|
|
73
|
-
const windowHeight = window.innerHeight;
|
|
74
|
-
// Check if element is close to viewport (using approx 200px buffer like rootMargin)
|
|
75
|
-
if (rect.top <= windowHeight + 200) {
|
|
76
|
-
this.loadContent(el);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
window.addEventListener('scroll', checkVisible);
|
|
83
|
-
window.addEventListener('resize', checkVisible);
|
|
84
|
-
// Initial check
|
|
85
|
-
checkVisible();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async loadContent(element) {
|
|
89
|
-
const url = element.getAttribute(this.attribute);
|
|
90
|
-
if (!url) return;
|
|
91
|
-
|
|
92
|
-
// Remove attribute so we don't try to load again if using scroll fallback
|
|
93
|
-
element.removeAttribute(this.attribute);
|
|
94
|
-
element.classList.add('loading');
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
// Simulate network delay if configured
|
|
98
|
-
if (this.simulatedDelay > 0) {
|
|
99
|
-
await new Promise(resolve => setTimeout(resolve, this.simulatedDelay));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const response = await fetch(url);
|
|
103
|
-
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
|
104
|
-
const html = await response.text();
|
|
105
|
-
|
|
106
|
-
element.innerHTML = html;
|
|
107
|
-
element.classList.remove('loading');
|
|
108
|
-
element.classList.add('loaded'); // Animation hook
|
|
109
|
-
|
|
110
|
-
if (this.onLoad) this.onLoad(element);
|
|
111
|
-
|
|
112
|
-
} catch (error) {
|
|
113
|
-
console.error('LazyLoad Error:', error);
|
|
114
|
-
element.classList.remove('loading');
|
|
115
|
-
element.classList.add('error');
|
|
116
|
-
element.innerHTML = '<div class="error-msg">Failed to load content.</div>';
|
|
117
|
-
|
|
118
|
-
if (this.onError) this.onError(error, element);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* LazyLoader - A class to lazy load content into elements when they scroll into view.
|
|
3
|
+
*
|
|
4
|
+
* how to use:
|
|
5
|
+
* const lazyLoader = new LazyLoader({
|
|
6
|
+
* selector: '.lazy-section',
|
|
7
|
+
* rootMargin: '0px 0px 100px 0px', // Load 100px before element is visible
|
|
8
|
+
* threshold: 0.1,
|
|
9
|
+
* simulatedDelay: 2000, // 2 second delay to see the spinner
|
|
10
|
+
* onLoad: (el) => console.log('Loaded content for', el),
|
|
11
|
+
* onError: (err, el) => console.error('Failed to load', el)
|
|
12
|
+
* });
|
|
13
|
+
*
|
|
14
|
+
* in html:
|
|
15
|
+
* <div class="lazy-section" data-src="content/reviews.html"></div>
|
|
16
|
+
*/
|
|
17
|
+
class LazyLoader {
|
|
18
|
+
/**
|
|
19
|
+
* @param {Object} options - Configuration options
|
|
20
|
+
* @param {string} options.selector - CSS selector for elements to observe (default: '.lazy-load')
|
|
21
|
+
* @param {string} options.attribute - Attribute containing the URL to load (default: 'data-src')
|
|
22
|
+
* @param {string} options.rootMargin - IntersectionObserver rootMargin (default: '0px 0px 200px 0px')
|
|
23
|
+
* @param {number} options.threshold - IntersectionObserver threshold (default: 0.1)
|
|
24
|
+
* @param {Function} options.onError - Callback function when loading fails
|
|
25
|
+
* @param {Function} options.onLoad - Callback function when loading succeeds
|
|
26
|
+
*/
|
|
27
|
+
constructor(options = {}) {
|
|
28
|
+
this.selector = options.selector || '.lazy-load';
|
|
29
|
+
this.attribute = options.attribute || 'data-src';
|
|
30
|
+
this.rootMargin = options.rootMargin || '0px 0px 200px 0px';
|
|
31
|
+
this.threshold = options.threshold || 0.1;
|
|
32
|
+
this.onError = options.onError || null;
|
|
33
|
+
this.onLoad = options.onLoad || null;
|
|
34
|
+
this.simulatedDelay = options.simulatedDelay || 0; // Delay in ms for testing
|
|
35
|
+
this.init();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
init() {
|
|
39
|
+
this.elements = document.querySelectorAll(this.selector);
|
|
40
|
+
|
|
41
|
+
if ('IntersectionObserver' in window) {
|
|
42
|
+
this.setupIntersectionObserver();
|
|
43
|
+
} else {
|
|
44
|
+
this.setupScrollListener();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
setupIntersectionObserver() {
|
|
49
|
+
const observerOptions = {
|
|
50
|
+
root: null,
|
|
51
|
+
rootMargin: this.rootMargin,
|
|
52
|
+
threshold: this.threshold
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const observer = new IntersectionObserver((entries, observer) => {
|
|
56
|
+
entries.forEach(entry => {
|
|
57
|
+
if (entry.isIntersecting) {
|
|
58
|
+
this.loadContent(entry.target);
|
|
59
|
+
observer.unobserve(entry.target);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}, observerOptions);
|
|
63
|
+
|
|
64
|
+
this.elements.forEach(el => observer.observe(el));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setupScrollListener() {
|
|
68
|
+
// Fallback for older browsers
|
|
69
|
+
const checkVisible = () => {
|
|
70
|
+
this.elements.forEach(el => {
|
|
71
|
+
if (el.getAttribute(this.attribute)) {
|
|
72
|
+
const rect = el.getBoundingClientRect();
|
|
73
|
+
const windowHeight = window.innerHeight;
|
|
74
|
+
// Check if element is close to viewport (using approx 200px buffer like rootMargin)
|
|
75
|
+
if (rect.top <= windowHeight + 200) {
|
|
76
|
+
this.loadContent(el);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
window.addEventListener('scroll', checkVisible);
|
|
83
|
+
window.addEventListener('resize', checkVisible);
|
|
84
|
+
// Initial check
|
|
85
|
+
checkVisible();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async loadContent(element) {
|
|
89
|
+
const url = element.getAttribute(this.attribute);
|
|
90
|
+
if (!url) return;
|
|
91
|
+
|
|
92
|
+
// Remove attribute so we don't try to load again if using scroll fallback
|
|
93
|
+
element.removeAttribute(this.attribute);
|
|
94
|
+
element.classList.add('loading');
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
// Simulate network delay if configured
|
|
98
|
+
if (this.simulatedDelay > 0) {
|
|
99
|
+
await new Promise(resolve => setTimeout(resolve, this.simulatedDelay));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const response = await fetch(url);
|
|
103
|
+
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
|
104
|
+
const html = await response.text();
|
|
105
|
+
|
|
106
|
+
element.innerHTML = html;
|
|
107
|
+
element.classList.remove('loading');
|
|
108
|
+
element.classList.add('loaded'); // Animation hook
|
|
109
|
+
|
|
110
|
+
if (this.onLoad) this.onLoad(element);
|
|
111
|
+
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.error('LazyLoad Error:', error);
|
|
114
|
+
element.classList.remove('loading');
|
|
115
|
+
element.classList.add('error');
|
|
116
|
+
element.innerHTML = '<div class="error-msg">Failed to load content.</div>';
|
|
117
|
+
|
|
118
|
+
if (this.onError) this.onError(error, element);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
package/js/modal.ts
CHANGED
|
@@ -1,167 +1,167 @@
|
|
|
1
|
-
const CLOSE_ICON = '<div class="icon icon-close close"></div>';
|
|
2
|
-
|
|
3
|
-
type ModalType = 'default' | 'success' | 'error' | 'warning' | 'info';
|
|
4
|
-
|
|
5
|
-
interface ModalOptions {
|
|
6
|
-
content: string;
|
|
7
|
-
header?: string;
|
|
8
|
-
footer?: string;
|
|
9
|
-
closeable?: boolean;
|
|
10
|
-
type?: ModalType;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
class Modal {
|
|
14
|
-
private content: string;
|
|
15
|
-
private readonly header?: string;
|
|
16
|
-
private readonly footer?: string;
|
|
17
|
-
private readonly closeable: boolean;
|
|
18
|
-
private readonly type: ModalType;
|
|
19
|
-
private template: string;
|
|
20
|
-
private modalWrapper: HTMLElement | null = null;
|
|
21
|
-
|
|
22
|
-
constructor(options: ModalOptions);
|
|
23
|
-
constructor(content: string, header?: string, footer?: string, closeable?: boolean, type?: ModalType);
|
|
24
|
-
constructor(
|
|
25
|
-
contentOrOptions: string | ModalOptions,
|
|
26
|
-
header?: string,
|
|
27
|
-
footer?: string,
|
|
28
|
-
closeable: boolean = true,
|
|
29
|
-
type: ModalType = 'default'
|
|
30
|
-
) {
|
|
31
|
-
if (typeof contentOrOptions === 'object') {
|
|
32
|
-
this.content = contentOrOptions.content;
|
|
33
|
-
this.header = contentOrOptions.header;
|
|
34
|
-
this.footer = contentOrOptions.footer;
|
|
35
|
-
this.closeable = contentOrOptions.closeable ?? true;
|
|
36
|
-
this.type = contentOrOptions.type ?? 'default';
|
|
37
|
-
} else {
|
|
38
|
-
this.content = contentOrOptions;
|
|
39
|
-
this.header = header;
|
|
40
|
-
this.footer = footer;
|
|
41
|
-
this.closeable = closeable;
|
|
42
|
-
this.type = type;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
this.template = this.buildTemplate();
|
|
46
|
-
|
|
47
|
-
this.hide = this.hide.bind(this);
|
|
48
|
-
this.handleEscape = this.handleEscape.bind(this);
|
|
49
|
-
this.handleBackgroundClick = this.handleBackgroundClick.bind(this);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public show(): void {
|
|
53
|
-
this.hide();
|
|
54
|
-
|
|
55
|
-
const wrapper = document.createElement('div');
|
|
56
|
-
wrapper.className = 'modal-wrapper';
|
|
57
|
-
wrapper.innerHTML = this.template;
|
|
58
|
-
document.body.append(wrapper);
|
|
59
|
-
|
|
60
|
-
this.modalWrapper = wrapper;
|
|
61
|
-
|
|
62
|
-
if (this.closeable) {
|
|
63
|
-
const closeBtn = wrapper.querySelector('.close');
|
|
64
|
-
closeBtn?.addEventListener('click', this.hide);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const background = wrapper.querySelector('.modal-background');
|
|
68
|
-
if (this.closeable && background) {
|
|
69
|
-
background.addEventListener('click', this.handleBackgroundClick);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (this.closeable) {
|
|
73
|
-
document.addEventListener('keydown', this.handleEscape);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
document.body.style.overflow = 'hidden';
|
|
77
|
-
|
|
78
|
-
requestAnimationFrame(() => {
|
|
79
|
-
wrapper.classList.add('is-visible');
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
public hide(): void {
|
|
84
|
-
const wrapper = document.querySelector('.modal-wrapper');
|
|
85
|
-
if (!wrapper) return;
|
|
86
|
-
|
|
87
|
-
// Remove event listeners
|
|
88
|
-
const closeBtn = wrapper.querySelector('.close');
|
|
89
|
-
closeBtn?.removeEventListener('click', this.hide);
|
|
90
|
-
|
|
91
|
-
const background = wrapper.querySelector('.modal-background');
|
|
92
|
-
background?.removeEventListener('click', this.handleBackgroundClick);
|
|
93
|
-
|
|
94
|
-
document.removeEventListener('keydown', this.handleEscape);
|
|
95
|
-
document.body.style.overflow = '';
|
|
96
|
-
|
|
97
|
-
wrapper.classList.remove('is-visible');
|
|
98
|
-
|
|
99
|
-
setTimeout(() => {
|
|
100
|
-
wrapper.remove();
|
|
101
|
-
this.modalWrapper = null;
|
|
102
|
-
}, 300);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
private handleEscape(e: KeyboardEvent): void {
|
|
106
|
-
if (e.key === 'Escape') {
|
|
107
|
-
this.hide();
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private handleBackgroundClick(e: Event): void {
|
|
112
|
-
if ((e.target as HTMLElement)?.classList.contains('modal-background')) {
|
|
113
|
-
this.hide();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private buildTemplate(): string {
|
|
118
|
-
const parts: string[] = ['<div class="modal">'];
|
|
119
|
-
|
|
120
|
-
if (this.closeable) {
|
|
121
|
-
parts.push(CLOSE_ICON);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (this.header !== undefined) {
|
|
125
|
-
const headerClass = `header ${this.type}-bg`;
|
|
126
|
-
parts.push(`<div class="${headerClass}">${this.header}</div>`);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
parts.push(this.content);
|
|
130
|
-
|
|
131
|
-
if (this.footer !== undefined) {
|
|
132
|
-
parts.push(`<div class="footer">${this.footer}</div>`);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
parts.push('</div>');
|
|
136
|
-
parts.push('<div class="modal-background"></div>');
|
|
137
|
-
|
|
138
|
-
return parts.join('');
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
public updateContent(content: string): void {
|
|
142
|
-
this.content = content;
|
|
143
|
-
this.template = this.buildTemplate();
|
|
144
|
-
|
|
145
|
-
if (this.modalWrapper) {
|
|
146
|
-
const modalElement = this.modalWrapper.querySelector('.modal');
|
|
147
|
-
if (modalElement) {
|
|
148
|
-
const tempWrapper = document.createElement('div');
|
|
149
|
-
tempWrapper.innerHTML = this.template;
|
|
150
|
-
const newModal = tempWrapper.querySelector('.modal');
|
|
151
|
-
if (newModal) {
|
|
152
|
-
modalElement.innerHTML = newModal.innerHTML;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
public isVisible(): boolean {
|
|
159
|
-
return this.modalWrapper !== null && document.body.contains(this.modalWrapper);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
public destroy(): void {
|
|
163
|
-
this.hide();
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
1
|
+
const CLOSE_ICON = '<div class="icon icon-close close"></div>';
|
|
2
|
+
|
|
3
|
+
type ModalType = 'default' | 'success' | 'error' | 'warning' | 'info';
|
|
4
|
+
|
|
5
|
+
interface ModalOptions {
|
|
6
|
+
content: string;
|
|
7
|
+
header?: string;
|
|
8
|
+
footer?: string;
|
|
9
|
+
closeable?: boolean;
|
|
10
|
+
type?: ModalType;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
class Modal {
|
|
14
|
+
private content: string;
|
|
15
|
+
private readonly header?: string;
|
|
16
|
+
private readonly footer?: string;
|
|
17
|
+
private readonly closeable: boolean;
|
|
18
|
+
private readonly type: ModalType;
|
|
19
|
+
private template: string;
|
|
20
|
+
private modalWrapper: HTMLElement | null = null;
|
|
21
|
+
|
|
22
|
+
constructor(options: ModalOptions);
|
|
23
|
+
constructor(content: string, header?: string, footer?: string, closeable?: boolean, type?: ModalType);
|
|
24
|
+
constructor(
|
|
25
|
+
contentOrOptions: string | ModalOptions,
|
|
26
|
+
header?: string,
|
|
27
|
+
footer?: string,
|
|
28
|
+
closeable: boolean = true,
|
|
29
|
+
type: ModalType = 'default'
|
|
30
|
+
) {
|
|
31
|
+
if (typeof contentOrOptions === 'object') {
|
|
32
|
+
this.content = contentOrOptions.content;
|
|
33
|
+
this.header = contentOrOptions.header;
|
|
34
|
+
this.footer = contentOrOptions.footer;
|
|
35
|
+
this.closeable = contentOrOptions.closeable ?? true;
|
|
36
|
+
this.type = contentOrOptions.type ?? 'default';
|
|
37
|
+
} else {
|
|
38
|
+
this.content = contentOrOptions;
|
|
39
|
+
this.header = header;
|
|
40
|
+
this.footer = footer;
|
|
41
|
+
this.closeable = closeable;
|
|
42
|
+
this.type = type;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.template = this.buildTemplate();
|
|
46
|
+
|
|
47
|
+
this.hide = this.hide.bind(this);
|
|
48
|
+
this.handleEscape = this.handleEscape.bind(this);
|
|
49
|
+
this.handleBackgroundClick = this.handleBackgroundClick.bind(this);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public show(): void {
|
|
53
|
+
this.hide();
|
|
54
|
+
|
|
55
|
+
const wrapper = document.createElement('div');
|
|
56
|
+
wrapper.className = 'modal-wrapper';
|
|
57
|
+
wrapper.innerHTML = this.template;
|
|
58
|
+
document.body.append(wrapper);
|
|
59
|
+
|
|
60
|
+
this.modalWrapper = wrapper;
|
|
61
|
+
|
|
62
|
+
if (this.closeable) {
|
|
63
|
+
const closeBtn = wrapper.querySelector('.close');
|
|
64
|
+
closeBtn?.addEventListener('click', this.hide);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const background = wrapper.querySelector('.modal-background');
|
|
68
|
+
if (this.closeable && background) {
|
|
69
|
+
background.addEventListener('click', this.handleBackgroundClick);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (this.closeable) {
|
|
73
|
+
document.addEventListener('keydown', this.handleEscape);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
document.body.style.overflow = 'hidden';
|
|
77
|
+
|
|
78
|
+
requestAnimationFrame(() => {
|
|
79
|
+
wrapper.classList.add('is-visible');
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public hide(): void {
|
|
84
|
+
const wrapper = document.querySelector('.modal-wrapper');
|
|
85
|
+
if (!wrapper) return;
|
|
86
|
+
|
|
87
|
+
// Remove event listeners
|
|
88
|
+
const closeBtn = wrapper.querySelector('.close');
|
|
89
|
+
closeBtn?.removeEventListener('click', this.hide);
|
|
90
|
+
|
|
91
|
+
const background = wrapper.querySelector('.modal-background');
|
|
92
|
+
background?.removeEventListener('click', this.handleBackgroundClick);
|
|
93
|
+
|
|
94
|
+
document.removeEventListener('keydown', this.handleEscape);
|
|
95
|
+
document.body.style.overflow = '';
|
|
96
|
+
|
|
97
|
+
wrapper.classList.remove('is-visible');
|
|
98
|
+
|
|
99
|
+
setTimeout(() => {
|
|
100
|
+
wrapper.remove();
|
|
101
|
+
this.modalWrapper = null;
|
|
102
|
+
}, 300);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private handleEscape(e: KeyboardEvent): void {
|
|
106
|
+
if (e.key === 'Escape') {
|
|
107
|
+
this.hide();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private handleBackgroundClick(e: Event): void {
|
|
112
|
+
if ((e.target as HTMLElement)?.classList.contains('modal-background')) {
|
|
113
|
+
this.hide();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private buildTemplate(): string {
|
|
118
|
+
const parts: string[] = ['<div class="modal">'];
|
|
119
|
+
|
|
120
|
+
if (this.closeable) {
|
|
121
|
+
parts.push(CLOSE_ICON);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (this.header !== undefined) {
|
|
125
|
+
const headerClass = `header ${this.type}-bg`;
|
|
126
|
+
parts.push(`<div class="${headerClass}">${this.header}</div>`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
parts.push(this.content);
|
|
130
|
+
|
|
131
|
+
if (this.footer !== undefined) {
|
|
132
|
+
parts.push(`<div class="footer">${this.footer}</div>`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
parts.push('</div>');
|
|
136
|
+
parts.push('<div class="modal-background"></div>');
|
|
137
|
+
|
|
138
|
+
return parts.join('');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public updateContent(content: string): void {
|
|
142
|
+
this.content = content;
|
|
143
|
+
this.template = this.buildTemplate();
|
|
144
|
+
|
|
145
|
+
if (this.modalWrapper) {
|
|
146
|
+
const modalElement = this.modalWrapper.querySelector('.modal');
|
|
147
|
+
if (modalElement) {
|
|
148
|
+
const tempWrapper = document.createElement('div');
|
|
149
|
+
tempWrapper.innerHTML = this.template;
|
|
150
|
+
const newModal = tempWrapper.querySelector('.modal');
|
|
151
|
+
if (newModal) {
|
|
152
|
+
modalElement.innerHTML = newModal.innerHTML;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public isVisible(): boolean {
|
|
159
|
+
return this.modalWrapper !== null && document.body.contains(this.modalWrapper);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public destroy(): void {
|
|
163
|
+
this.hide();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
167
|
export { Modal, type ModalOptions, type ModalType };
|