juxscript 1.0.132 → 1.1.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 +1 -32
- package/bin/cli.js +4 -2
- package/index.d.ts +200 -0
- package/index.js +96 -22
- package/juxconfig.example.js +58 -63
- package/lib/components/alert.ts +200 -0
- package/lib/components/app.ts +247 -0
- package/lib/components/badge.ts +101 -0
- package/lib/components/base/BaseComponent.ts +421 -0
- package/lib/components/base/FormInput.ts +227 -0
- package/lib/components/button.ts +178 -0
- package/lib/components/card.ts +173 -0
- package/lib/components/chart.ts +231 -0
- package/lib/components/checkbox.ts +242 -0
- package/lib/components/code.ts +123 -0
- package/lib/components/container.ts +140 -0
- package/lib/components/data.ts +135 -0
- package/lib/components/datepicker.ts +234 -0
- package/lib/components/dialog.ts +172 -0
- package/lib/components/divider.ts +100 -0
- package/lib/components/dropdown.ts +186 -0
- package/lib/components/element.ts +267 -0
- package/lib/components/fileupload.ts +309 -0
- package/lib/components/grid.ts +291 -0
- package/lib/components/guard.ts +92 -0
- package/lib/components/heading.ts +96 -0
- package/lib/components/helpers.ts +41 -0
- package/lib/components/hero.ts +224 -0
- package/lib/components/icon.ts +178 -0
- package/lib/components/icons.ts +464 -0
- package/lib/components/include.ts +410 -0
- package/lib/components/input.ts +457 -0
- package/lib/components/list.ts +419 -0
- package/lib/components/loading.ts +100 -0
- package/lib/components/menu.ts +275 -0
- package/lib/components/modal.ts +284 -0
- package/lib/components/nav.ts +257 -0
- package/lib/components/paragraph.ts +97 -0
- package/lib/components/progress.ts +159 -0
- package/lib/components/radio.ts +278 -0
- package/lib/components/req.ts +303 -0
- package/lib/components/script.ts +41 -0
- package/lib/components/select.ts +252 -0
- package/lib/components/sidebar.ts +275 -0
- package/lib/components/style.ts +41 -0
- package/lib/components/switch.ts +246 -0
- package/lib/components/table.ts +1249 -0
- package/lib/components/tabs.ts +250 -0
- package/lib/components/theme-toggle.ts +293 -0
- package/lib/components/tooltip.ts +144 -0
- package/lib/components/view.ts +190 -0
- package/lib/components/write.ts +272 -0
- package/lib/globals.d.ts +19 -5
- package/lib/layouts/default.css +260 -0
- package/lib/layouts/figma.css +334 -0
- package/lib/reactivity/state.ts +78 -0
- package/lib/utils/{fetch.js → fetch.ts} +206 -81
- package/machinery/ast.js +347 -0
- package/machinery/build.js +466 -0
- package/machinery/compiler3.js +6 -66
- package/machinery/config.js +6 -93
- package/machinery/doc-generator.js +136 -0
- package/machinery/imports.js +155 -0
- package/machinery/server.js +166 -0
- package/machinery/ts-shim.js +46 -0
- package/machinery/watcher.js +162 -50
- package/package.json +9 -30
- package/create/index.jux +0 -77
- package/create/layout.jux +0 -18
- package/create/style.css +0 -57
- package/create/themes/assets/jux.svg +0 -34
- package/create/themes/base.css +0 -197
- package/create/themes/base2.css +0 -54
- package/create/themes/layouts/base.jux +0 -16
- package/create/themes/layouts/base_marketing.jux +0 -0
- package/create/themes/layouts/base_saas.jux +0 -0
- package/lib/componentsv2/base/BaseEngine.d.ts +0 -112
- package/lib/componentsv2/base/BaseEngine.js +0 -279
- package/lib/componentsv2/base/BaseSkin.d.ts +0 -74
- package/lib/componentsv2/base/BaseSkin.js +0 -130
- package/lib/componentsv2/base/Neighborhood.d.ts +0 -22
- package/lib/componentsv2/base/Neighborhood.js +0 -56
- package/lib/componentsv2/base/OptionsContract.d.ts +0 -20
- package/lib/componentsv2/base/OptionsContract.js +0 -107
- package/lib/componentsv2/base/State.d.ts +0 -18
- package/lib/componentsv2/base/State.js +0 -68
- package/lib/componentsv2/element/Element.d.ts +0 -30
- package/lib/componentsv2/element/Element.js +0 -50
- package/lib/componentsv2/element/ElementEngine.d.ts +0 -59
- package/lib/componentsv2/element/ElementEngine.js +0 -118
- package/lib/componentsv2/element/ElementSkin.d.ts +0 -10
- package/lib/componentsv2/element/ElementSkin.js +0 -56
- package/lib/componentsv2/element/structure.css +0 -261
- package/lib/componentsv2/grid/Grid.d.ts +0 -13
- package/lib/componentsv2/grid/Grid.js +0 -27
- package/lib/componentsv2/grid/GridEngine.d.ts +0 -77
- package/lib/componentsv2/grid/GridEngine.js +0 -153
- package/lib/componentsv2/grid/GridSkin.d.ts +0 -11
- package/lib/componentsv2/grid/GridSkin.js +0 -84
- package/lib/componentsv2/grid/structure.css +0 -27
- package/lib/componentsv2/input/Input.d.ts +0 -6
- package/lib/componentsv2/input/Input.js +0 -21
- package/lib/componentsv2/input/InputEngine.d.ts +0 -70
- package/lib/componentsv2/input/InputEngine.js +0 -143
- package/lib/componentsv2/input/InputSkin.d.ts +0 -11
- package/lib/componentsv2/input/InputSkin.js +0 -89
- package/lib/componentsv2/input/structure.css +0 -47
- package/lib/componentsv2/list/List.d.ts +0 -49
- package/lib/componentsv2/list/List.js +0 -105
- package/lib/componentsv2/list/ListEngine.d.ts +0 -121
- package/lib/componentsv2/list/ListEngine.js +0 -322
- package/lib/componentsv2/list/ListSkin.d.ts +0 -20
- package/lib/componentsv2/list/ListSkin.js +0 -345
- package/lib/componentsv2/list/structure.css +0 -359
- package/lib/componentsv2/plugins/ClientSQLitePlugin.d.ts +0 -21
- package/lib/componentsv2/plugins/ClientSQLitePlugin.js +0 -130
- package/lib/componentsv2/plugins/IndexedDBPlugin.d.ts +0 -18
- package/lib/componentsv2/plugins/IndexedDBPlugin.js +0 -75
- package/lib/componentsv2/plugins/LocalStoragePlugin.d.ts +0 -20
- package/lib/componentsv2/plugins/LocalStoragePlugin.js +0 -65
- package/lib/componentsv2/plugins/ServerSQLitePlugin.d.ts +0 -25
- package/lib/componentsv2/plugins/ServerSQLitePlugin.js +0 -70
- package/lib/componentsv2/stubs/ComponentComposition.ts.stub +0 -32
- package/lib/componentsv2/stubs/ComponentEngine.ts.stub +0 -36
- package/lib/componentsv2/stubs/ComponentSkin.ts.stub +0 -35
- package/lib/componentsv2/stubs/ComponentStructure.css.d.ts.stub +0 -2
- package/lib/componentsv2/stubs/ComponentStructure.css.stub +0 -13
- package/lib/utils/fetch.d.ts +0 -176
- package/machinery/serve.js +0 -255
- package/types/css.d.ts +0 -10
- /package/{create/themes/layouts/base_blog.jux → machinery/bundleAssets.js} +0 -0
- /package/{create/themes/layouts/base_docs.jux → machinery/bundleJux.js} +0 -0
- /package/{create/themes/layouts/base_login.jux → machinery/bundleVendors.js} +0 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { BaseComponent } from './base/BaseComponent.js';
|
|
2
|
+
import { renderIcon } from './icons.js';
|
|
3
|
+
import { req } from './req.js';
|
|
4
|
+
|
|
5
|
+
// Event definitions
|
|
6
|
+
const TRIGGER_EVENTS = [] as const;
|
|
7
|
+
const CALLBACK_EVENTS = ['itemClick'] as const; // ✅ Fire when menu item is clicked
|
|
8
|
+
|
|
9
|
+
export interface MenuItem {
|
|
10
|
+
label: string;
|
|
11
|
+
href?: string;
|
|
12
|
+
click?: () => void;
|
|
13
|
+
icon?: string;
|
|
14
|
+
items?: MenuItem[];
|
|
15
|
+
active?: boolean;
|
|
16
|
+
itemClass?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface MenuOptions {
|
|
20
|
+
items?: MenuItem[];
|
|
21
|
+
orientation?: 'vertical' | 'horizontal';
|
|
22
|
+
style?: string;
|
|
23
|
+
class?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type MenuState = {
|
|
27
|
+
items: MenuItem[];
|
|
28
|
+
orientation: string;
|
|
29
|
+
style: string;
|
|
30
|
+
class: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export class Menu extends BaseComponent<MenuState> {
|
|
34
|
+
constructor(id: string, options: MenuOptions = {}) {
|
|
35
|
+
super(id, {
|
|
36
|
+
items: options.items ?? [],
|
|
37
|
+
orientation: options.orientation ?? 'vertical',
|
|
38
|
+
style: options.style ?? '',
|
|
39
|
+
class: options.class ?? ''
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
this._setActiveStates();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected getTriggerEvents(): readonly string[] {
|
|
46
|
+
return TRIGGER_EVENTS;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected getCallbackEvents(): readonly string[] {
|
|
50
|
+
return CALLBACK_EVENTS;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private _setActiveStates(): void {
|
|
54
|
+
this.state.items = this.state.items.map(item => ({
|
|
55
|
+
...item,
|
|
56
|
+
active: item.href ? req.isActiveNavItem(item.href) : false,
|
|
57
|
+
items: item.items?.map(subItem => ({
|
|
58
|
+
...subItem,
|
|
59
|
+
active: subItem.href ? req.isActiveNavItem(subItem.href) : false
|
|
60
|
+
}))
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* ═════════════════════════════════════════════════════════════════
|
|
65
|
+
* FLUENT API
|
|
66
|
+
* ═════════════════════════════════════════════════════════════════ */
|
|
67
|
+
|
|
68
|
+
// ✅ Inherited from BaseComponent
|
|
69
|
+
|
|
70
|
+
items(value: MenuItem[]): this {
|
|
71
|
+
this.state.items = value;
|
|
72
|
+
this._setActiveStates();
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
addItem(item: MenuItem): this {
|
|
77
|
+
this.state.items = [...this.state.items, item];
|
|
78
|
+
this._setActiveStates();
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
itemClass(className: string): this {
|
|
83
|
+
this.state.items = this.state.items.map(item => ({
|
|
84
|
+
...item,
|
|
85
|
+
itemClass: className,
|
|
86
|
+
items: item.items?.map(subItem => ({
|
|
87
|
+
...subItem,
|
|
88
|
+
itemClass: className
|
|
89
|
+
}))
|
|
90
|
+
}));
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
orientation(value: 'vertical' | 'horizontal'): this {
|
|
95
|
+
this.state.orientation = value;
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* ═════════════════════════════════════════════════════════════════
|
|
100
|
+
* HELPERS
|
|
101
|
+
* ═════════════════════════════════════════════════════════════════ */
|
|
102
|
+
|
|
103
|
+
private _renderMenuItem(item: MenuItem): HTMLElement {
|
|
104
|
+
const menuItem = document.createElement('div');
|
|
105
|
+
menuItem.className = 'jux-menu-item';
|
|
106
|
+
|
|
107
|
+
if (item.itemClass) {
|
|
108
|
+
menuItem.className += ` ${item.itemClass}`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (item.active) {
|
|
112
|
+
menuItem.classList.add('jux-menu-item-active');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (item.href) {
|
|
116
|
+
const link = document.createElement('a');
|
|
117
|
+
link.className = 'jux-menu-link';
|
|
118
|
+
link.href = item.href;
|
|
119
|
+
|
|
120
|
+
if (item.active) {
|
|
121
|
+
link.classList.add('jux-menu-link-active');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (item.icon) {
|
|
125
|
+
const icon = document.createElement('span');
|
|
126
|
+
icon.className = 'jux-menu-icon';
|
|
127
|
+
icon.appendChild(renderIcon(item.icon));
|
|
128
|
+
link.appendChild(icon);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const label = document.createElement('span');
|
|
132
|
+
label.className = 'jux-menu-label';
|
|
133
|
+
label.textContent = item.label;
|
|
134
|
+
link.appendChild(label);
|
|
135
|
+
|
|
136
|
+
// ✅ Fire itemClick callback when link is clicked
|
|
137
|
+
link.addEventListener('click', (e) => {
|
|
138
|
+
this._triggerCallback('itemClick', { item, event: e });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
menuItem.appendChild(link);
|
|
142
|
+
} else {
|
|
143
|
+
const button = document.createElement('button');
|
|
144
|
+
button.className = 'jux-menu-button';
|
|
145
|
+
|
|
146
|
+
if (item.icon) {
|
|
147
|
+
const icon = document.createElement('span');
|
|
148
|
+
icon.className = 'jux-menu-icon';
|
|
149
|
+
icon.appendChild(renderIcon(item.icon));
|
|
150
|
+
button.appendChild(icon);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const label = document.createElement('span');
|
|
154
|
+
label.className = 'jux-menu-label';
|
|
155
|
+
label.textContent = item.label;
|
|
156
|
+
button.appendChild(label);
|
|
157
|
+
|
|
158
|
+
menuItem.appendChild(button);
|
|
159
|
+
|
|
160
|
+
// ✅ Fire both the item's click handler AND the callback event
|
|
161
|
+
button.addEventListener('click', (e) => {
|
|
162
|
+
if (item.click) {
|
|
163
|
+
item.click();
|
|
164
|
+
}
|
|
165
|
+
this._triggerCallback('itemClick', { item, event: e });
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (item.items && item.items.length > 0) {
|
|
170
|
+
// Mark item as having submenu
|
|
171
|
+
menuItem.classList.add('jux-menu-item-has-submenu');
|
|
172
|
+
|
|
173
|
+
const submenu = document.createElement('div');
|
|
174
|
+
submenu.className = 'jux-menu-submenu';
|
|
175
|
+
|
|
176
|
+
item.items.forEach(subItem => {
|
|
177
|
+
submenu.appendChild(this._renderMenuItem(subItem));
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
menuItem.appendChild(submenu);
|
|
181
|
+
|
|
182
|
+
// Add click handler to toggle submenu expansion
|
|
183
|
+
const clickTarget = menuItem.querySelector('a, button');
|
|
184
|
+
if (clickTarget) {
|
|
185
|
+
clickTarget.addEventListener('click', (e) => {
|
|
186
|
+
// Prevent default link behavior if it has submenus
|
|
187
|
+
if (!item.href) {
|
|
188
|
+
e.preventDefault();
|
|
189
|
+
}
|
|
190
|
+
menuItem.classList.toggle('jux-menu-item-expanded');
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return menuItem;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/* ═════════════════════════════════════════════════════════════════
|
|
199
|
+
* RENDER
|
|
200
|
+
* ═════════════════════════════════════════════════════════════════ */
|
|
201
|
+
|
|
202
|
+
render(targetId?: string): this {
|
|
203
|
+
const container = this._setupContainer(targetId);
|
|
204
|
+
|
|
205
|
+
const { items, orientation, style, class: className } = this.state;
|
|
206
|
+
|
|
207
|
+
const menu = document.createElement('nav');
|
|
208
|
+
menu.className = `jux-menu jux-menu-${orientation}`;
|
|
209
|
+
menu.id = this._id;
|
|
210
|
+
|
|
211
|
+
if (className) {
|
|
212
|
+
menu.className += ` ${className}`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (style) {
|
|
216
|
+
menu.setAttribute('style', style);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
items.forEach(item => {
|
|
220
|
+
menu.appendChild(this._renderMenuItem(item));
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
this._wireStandardEvents(menu);
|
|
224
|
+
|
|
225
|
+
// Wire sync bindings
|
|
226
|
+
this._syncBindings.forEach(({ property, stateObj, toState, toComponent }) => {
|
|
227
|
+
if (property === 'items') {
|
|
228
|
+
const transform = toComponent || ((v: any) => v);
|
|
229
|
+
|
|
230
|
+
stateObj.subscribe((val: any) => {
|
|
231
|
+
const transformed = transform(val);
|
|
232
|
+
this.state.items = transformed;
|
|
233
|
+
this._setActiveStates();
|
|
234
|
+
|
|
235
|
+
const existingItems = menu.querySelectorAll('.jux-menu-item');
|
|
236
|
+
existingItems.forEach(item => item.remove());
|
|
237
|
+
|
|
238
|
+
this.state.items.forEach(item => {
|
|
239
|
+
menu.appendChild(this._renderMenuItem(item));
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
requestAnimationFrame(() => {
|
|
243
|
+
if ((window as any).lucide) {
|
|
244
|
+
(window as any).lucide.createIcons();
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
else if (property === 'orientation') {
|
|
250
|
+
const transform = toComponent || ((v: any) => String(v));
|
|
251
|
+
|
|
252
|
+
stateObj.subscribe((val: any) => {
|
|
253
|
+
const transformed = transform(val);
|
|
254
|
+
menu.classList.remove(`jux-menu-${this.state.orientation}`);
|
|
255
|
+
this.state.orientation = transformed;
|
|
256
|
+
menu.classList.add(`jux-menu-${transformed}`);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
container.appendChild(menu);
|
|
262
|
+
|
|
263
|
+
requestAnimationFrame(() => {
|
|
264
|
+
if ((window as any).lucide) {
|
|
265
|
+
(window as any).lucide.createIcons();
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
return this;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function menu(id: string, options: MenuOptions = {}): Menu {
|
|
274
|
+
return new Menu(id, options);
|
|
275
|
+
}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { BaseComponent } from './base/BaseComponent.js';
|
|
2
|
+
|
|
3
|
+
// Event definitions
|
|
4
|
+
const TRIGGER_EVENTS = [] as const;
|
|
5
|
+
const CALLBACK_EVENTS = ['open', 'close'] as const;
|
|
6
|
+
|
|
7
|
+
export interface ModalOptions {
|
|
8
|
+
title?: string;
|
|
9
|
+
content?: string;
|
|
10
|
+
showCloseButton?: boolean;
|
|
11
|
+
closeOnBackdropClick?: boolean;
|
|
12
|
+
size?: 'small' | 'medium' | 'large';
|
|
13
|
+
style?: string;
|
|
14
|
+
class?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type ModalState = {
|
|
18
|
+
title: string;
|
|
19
|
+
content: string;
|
|
20
|
+
showCloseButton: boolean;
|
|
21
|
+
closeOnBackdropClick: boolean;
|
|
22
|
+
size: string;
|
|
23
|
+
open: boolean;
|
|
24
|
+
style: string;
|
|
25
|
+
class: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export class Modal extends BaseComponent<ModalState> {
|
|
29
|
+
private _overlay: HTMLElement | null = null;
|
|
30
|
+
|
|
31
|
+
constructor(id: string, options: ModalOptions = {}) {
|
|
32
|
+
super(id, {
|
|
33
|
+
title: options.title ?? '',
|
|
34
|
+
content: options.content ?? '',
|
|
35
|
+
showCloseButton: options.showCloseButton ?? true,
|
|
36
|
+
closeOnBackdropClick: options.closeOnBackdropClick ?? true,
|
|
37
|
+
size: options.size ?? 'medium',
|
|
38
|
+
open: false,
|
|
39
|
+
style: options.style ?? '',
|
|
40
|
+
class: options.class ?? ''
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected getTriggerEvents(): readonly string[] {
|
|
45
|
+
return TRIGGER_EVENTS;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected getCallbackEvents(): readonly string[] {
|
|
49
|
+
return CALLBACK_EVENTS;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* ═════════════════════════════════════════════════════════════════
|
|
53
|
+
* FLUENT API
|
|
54
|
+
* ═════════════════════════════════════════════════════════════════ */
|
|
55
|
+
|
|
56
|
+
// ✅ Inherited from BaseComponent:
|
|
57
|
+
// - style(), class()
|
|
58
|
+
// - bind(), sync(), renderTo()
|
|
59
|
+
// - addClass(), removeClass(), toggleClass()
|
|
60
|
+
// - visible(), show(), hide()
|
|
61
|
+
// - attr(), attrs(), removeAttr()
|
|
62
|
+
|
|
63
|
+
title(value: string): this {
|
|
64
|
+
this.state.title = value;
|
|
65
|
+
if (this._overlay) {
|
|
66
|
+
const modal = this._overlay.querySelector('.jux-modal');
|
|
67
|
+
const header = modal?.querySelector('.jux-modal-header');
|
|
68
|
+
if (header) {
|
|
69
|
+
header.textContent = value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
content(value: string): this {
|
|
76
|
+
this.state.content = value;
|
|
77
|
+
if (this._overlay) {
|
|
78
|
+
const body = this._overlay.querySelector('.jux-modal-body');
|
|
79
|
+
if (body) {
|
|
80
|
+
body.innerHTML = value;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
showCloseButton(value: boolean): this {
|
|
87
|
+
this.state.showCloseButton = value;
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
closeOnBackdropClick(value: boolean): this {
|
|
92
|
+
this.state.closeOnBackdropClick = value;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
size(value: 'small' | 'medium' | 'large'): this {
|
|
97
|
+
this.state.size = value;
|
|
98
|
+
if (this._overlay) {
|
|
99
|
+
const modal = this._overlay.querySelector('.jux-modal');
|
|
100
|
+
if (modal) {
|
|
101
|
+
modal.className = modal.className.replace(/jux-modal-(small|medium|large)/, `jux-modal-${value}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
open(): this {
|
|
108
|
+
this.state.open = true;
|
|
109
|
+
if (this._overlay) {
|
|
110
|
+
this._overlay.style.display = 'flex';
|
|
111
|
+
}
|
|
112
|
+
// 🎯 Fire the open callback event
|
|
113
|
+
this._triggerCallback('open');
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
close(): this {
|
|
118
|
+
this.state.open = false;
|
|
119
|
+
if (this._overlay) {
|
|
120
|
+
this._overlay.style.display = 'none';
|
|
121
|
+
}
|
|
122
|
+
// 🎯 Fire the close callback event
|
|
123
|
+
this._triggerCallback('close');
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Get the modal body element ID for rendering child components
|
|
129
|
+
* @returns The ID of the modal body element (e.g., 'mymodal-body')
|
|
130
|
+
*
|
|
131
|
+
* Usage:
|
|
132
|
+
* const modal = jux.modal('files-modal', { title: 'Files' }).render('body').open();
|
|
133
|
+
* jux.table('files-table', {...}).render(modal.bodyId());
|
|
134
|
+
*/
|
|
135
|
+
bodyId(): string {
|
|
136
|
+
return `${this._id}-body`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Get the modal body element (only available after render)
|
|
141
|
+
* @returns The modal body element or null if not rendered
|
|
142
|
+
*/
|
|
143
|
+
getBodyElement(): HTMLElement | null {
|
|
144
|
+
return document.getElementById(this.bodyId());
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/* ═════════════════════════════════════════════════════════════════
|
|
148
|
+
* RENDER
|
|
149
|
+
* ═════════════════════════════════════════════════════════════════ */
|
|
150
|
+
|
|
151
|
+
render(targetId?: string): this {
|
|
152
|
+
const container = this._setupContainer(targetId);
|
|
153
|
+
|
|
154
|
+
const { open, title, content, size, closeOnBackdropClick: closeOnBackdrop, showCloseButton: showClose, style, class: className } = this.state;
|
|
155
|
+
const hasOpenSync = this._syncBindings.some(b => b.property === 'open');
|
|
156
|
+
|
|
157
|
+
const overlay = document.createElement('div');
|
|
158
|
+
overlay.className = 'jux-modal-overlay';
|
|
159
|
+
overlay.id = this._id;
|
|
160
|
+
overlay.style.display = open ? 'flex' : 'none';
|
|
161
|
+
if (className) overlay.className += ` ${className}`;
|
|
162
|
+
if (style) overlay.setAttribute('style', style);
|
|
163
|
+
this._overlay = overlay;
|
|
164
|
+
|
|
165
|
+
const modal = document.createElement('div');
|
|
166
|
+
modal.className = `jux-modal jux-modal-${size}`;
|
|
167
|
+
|
|
168
|
+
if (showClose) {
|
|
169
|
+
const closeButton = document.createElement('button');
|
|
170
|
+
closeButton.className = 'jux-modal-close';
|
|
171
|
+
closeButton.innerHTML = '×';
|
|
172
|
+
modal.appendChild(closeButton);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (title) {
|
|
176
|
+
const header = document.createElement('div');
|
|
177
|
+
header.className = 'jux-modal-header';
|
|
178
|
+
header.textContent = title;
|
|
179
|
+
modal.appendChild(header);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const body = document.createElement('div');
|
|
183
|
+
body.className = 'jux-modal-body';
|
|
184
|
+
body.id = this.bodyId(); // Set predictable ID for child component rendering
|
|
185
|
+
body.innerHTML = content;
|
|
186
|
+
modal.appendChild(body);
|
|
187
|
+
|
|
188
|
+
overlay.appendChild(modal);
|
|
189
|
+
|
|
190
|
+
if (!hasOpenSync) {
|
|
191
|
+
if (showClose) {
|
|
192
|
+
const closeButton = modal.querySelector('.jux-modal-close');
|
|
193
|
+
closeButton?.addEventListener('click', () => {
|
|
194
|
+
this.state.open = false;
|
|
195
|
+
overlay.style.display = 'none';
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (closeOnBackdrop) {
|
|
200
|
+
overlay.addEventListener('click', (e) => {
|
|
201
|
+
if (e.target === overlay) {
|
|
202
|
+
this.state.open = false;
|
|
203
|
+
overlay.style.display = 'none';
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
this._wireStandardEvents(overlay);
|
|
210
|
+
|
|
211
|
+
this._syncBindings.forEach(({ property, stateObj, toState, toComponent }) => {
|
|
212
|
+
if (property === 'open') {
|
|
213
|
+
const transformToState = toState || ((v: any) => Boolean(v));
|
|
214
|
+
const transformToComponent = toComponent || ((v: any) => Boolean(v));
|
|
215
|
+
|
|
216
|
+
let isUpdating = false;
|
|
217
|
+
|
|
218
|
+
stateObj.subscribe((val: any) => {
|
|
219
|
+
if (isUpdating) return;
|
|
220
|
+
const transformed = transformToComponent(val);
|
|
221
|
+
this.state.open = transformed;
|
|
222
|
+
overlay.style.display = transformed ? 'flex' : 'none';
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
if (showClose) {
|
|
226
|
+
const closeButton = modal.querySelector('.jux-modal-close');
|
|
227
|
+
closeButton?.addEventListener('click', () => {
|
|
228
|
+
if (isUpdating) return;
|
|
229
|
+
isUpdating = true;
|
|
230
|
+
|
|
231
|
+
this.state.open = false;
|
|
232
|
+
overlay.style.display = 'none';
|
|
233
|
+
stateObj.set(transformToState(false));
|
|
234
|
+
|
|
235
|
+
setTimeout(() => { isUpdating = false; }, 0);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (closeOnBackdrop) {
|
|
240
|
+
overlay.addEventListener('click', (e) => {
|
|
241
|
+
if (e.target === overlay) {
|
|
242
|
+
if (isUpdating) return;
|
|
243
|
+
isUpdating = true;
|
|
244
|
+
|
|
245
|
+
this.state.open = false;
|
|
246
|
+
overlay.style.display = 'none';
|
|
247
|
+
stateObj.set(transformToState(false));
|
|
248
|
+
|
|
249
|
+
setTimeout(() => { isUpdating = false; }, 0);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
else if (property === 'content') {
|
|
255
|
+
const transform = toComponent || ((v: any) => String(v));
|
|
256
|
+
|
|
257
|
+
stateObj.subscribe((val: any) => {
|
|
258
|
+
const transformed = transform(val);
|
|
259
|
+
body.innerHTML = transformed;
|
|
260
|
+
this.state.content = transformed;
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
else if (property === 'title') {
|
|
264
|
+
const transform = toComponent || ((v: any) => String(v));
|
|
265
|
+
|
|
266
|
+
stateObj.subscribe((val: any) => {
|
|
267
|
+
const transformed = transform(val);
|
|
268
|
+
const header = modal.querySelector('.jux-modal-header');
|
|
269
|
+
if (header) {
|
|
270
|
+
header.textContent = transformed;
|
|
271
|
+
}
|
|
272
|
+
this.state.title = transformed;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
container.appendChild(overlay);
|
|
278
|
+
return this;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function modal(id: string, options: ModalOptions = {}): Modal {
|
|
283
|
+
return new Modal(id, options);
|
|
284
|
+
}
|