juxscript 1.1.2 → 1.1.4
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/machinery/build3.js +7 -91
- package/machinery/compiler3.js +3 -209
- package/machinery/config.js +93 -6
- package/machinery/serve.js +255 -0
- package/machinery/watcher.js +49 -161
- package/package.json +19 -5
- package/lib/components/alert.ts +0 -200
- package/lib/components/app.ts +0 -247
- package/lib/components/badge.ts +0 -101
- package/lib/components/base/BaseComponent.ts +0 -421
- package/lib/components/base/FormInput.ts +0 -227
- package/lib/components/button.ts +0 -178
- package/lib/components/card.ts +0 -173
- package/lib/components/chart.ts +0 -231
- package/lib/components/checkbox.ts +0 -242
- package/lib/components/code.ts +0 -123
- package/lib/components/container.ts +0 -140
- package/lib/components/data.ts +0 -135
- package/lib/components/datepicker.ts +0 -234
- package/lib/components/dialog.ts +0 -172
- package/lib/components/divider.ts +0 -100
- package/lib/components/dropdown.ts +0 -186
- package/lib/components/element.ts +0 -267
- package/lib/components/fileupload.ts +0 -309
- package/lib/components/grid.ts +0 -291
- package/lib/components/guard.ts +0 -92
- package/lib/components/heading.ts +0 -96
- package/lib/components/helpers.ts +0 -41
- package/lib/components/hero.ts +0 -224
- package/lib/components/icon.ts +0 -178
- package/lib/components/icons.ts +0 -464
- package/lib/components/include.ts +0 -410
- package/lib/components/input.ts +0 -457
- package/lib/components/list.ts +0 -419
- package/lib/components/loading.ts +0 -100
- package/lib/components/menu.ts +0 -275
- package/lib/components/modal.ts +0 -284
- package/lib/components/nav.ts +0 -257
- package/lib/components/paragraph.ts +0 -97
- package/lib/components/progress.ts +0 -159
- package/lib/components/radio.ts +0 -278
- package/lib/components/req.ts +0 -303
- package/lib/components/script.ts +0 -41
- package/lib/components/select.ts +0 -252
- package/lib/components/sidebar.ts +0 -275
- package/lib/components/style.ts +0 -41
- package/lib/components/switch.ts +0 -246
- package/lib/components/table.ts +0 -1249
- package/lib/components/tabs.ts +0 -250
- package/lib/components/theme-toggle.ts +0 -293
- package/lib/components/tooltip.ts +0 -144
- package/lib/components/view.ts +0 -190
- package/lib/components/write.ts +0 -272
- package/lib/layouts/default.css +0 -260
- package/lib/layouts/figma.css +0 -334
- package/lib/reactivity/state.ts +0 -78
- package/lib/utils/fetch.ts +0 -553
- package/machinery/ast.js +0 -347
- package/machinery/build.js +0 -466
- package/machinery/bundleAssets.js +0 -0
- package/machinery/bundleJux.js +0 -0
- package/machinery/bundleVendors.js +0 -0
- package/machinery/doc-generator.js +0 -136
- package/machinery/imports.js +0 -155
- package/machinery/server.js +0 -166
- package/machinery/ts-shim.js +0 -46
- package/machinery/validators/file-validator.js +0 -123
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
import { BaseComponent } from './base/BaseComponent.js';
|
|
2
|
-
|
|
3
|
-
// Event definitions
|
|
4
|
-
const TRIGGER_EVENTS = [] as const; // Element has no trigger events
|
|
5
|
-
const CALLBACK_EVENTS = [] as const; // Element has no callback events
|
|
6
|
-
|
|
7
|
-
export interface ElementOptions {
|
|
8
|
-
tagType?: string;
|
|
9
|
-
className?: string;
|
|
10
|
-
text?: string;
|
|
11
|
-
innerHTML?: string;
|
|
12
|
-
attributes?: Record<string, string>;
|
|
13
|
-
styles?: Record<string, string>;
|
|
14
|
-
style?: string;
|
|
15
|
-
class?: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
type ElementState = {
|
|
19
|
-
tagType: string;
|
|
20
|
-
className: string;
|
|
21
|
-
text: string;
|
|
22
|
-
innerHTML: string;
|
|
23
|
-
attributes: Record<string, string>;
|
|
24
|
-
styles: Record<string, string>;
|
|
25
|
-
style: string;
|
|
26
|
-
class: string;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export class Element extends BaseComponent<ElementState> {
|
|
30
|
-
private _element: HTMLElement | null = null;
|
|
31
|
-
|
|
32
|
-
constructor(id: string, options: ElementOptions = {}) {
|
|
33
|
-
super(id, {
|
|
34
|
-
tagType: options.tagType ?? 'div',
|
|
35
|
-
className: options.className ?? '',
|
|
36
|
-
text: options.text ?? '',
|
|
37
|
-
innerHTML: options.innerHTML ?? '',
|
|
38
|
-
attributes: options.attributes ?? {},
|
|
39
|
-
styles: options.styles ?? {},
|
|
40
|
-
style: options.style ?? '',
|
|
41
|
-
class: options.class ?? ''
|
|
42
|
-
});
|
|
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
|
-
/* ═════════════════════════════════════════════════════════════════
|
|
54
|
-
* FLUENT API
|
|
55
|
-
* ═════════════════════════════════════════════════════════════════ */
|
|
56
|
-
|
|
57
|
-
// ✅ Inherited from BaseComponent:
|
|
58
|
-
// - style(), class()
|
|
59
|
-
// - addClass(), removeClass(), toggleClass()
|
|
60
|
-
// - visible(), show(), hide(), toggleVisibility()
|
|
61
|
-
// - attr(), attrs(), removeAttr()
|
|
62
|
-
// - disabled(), enable(), disable()
|
|
63
|
-
// - loading()
|
|
64
|
-
// - focus(), blur()
|
|
65
|
-
// - remove()
|
|
66
|
-
// - bind(), sync(), renderTo()
|
|
67
|
-
|
|
68
|
-
tagType(value: string): Element {
|
|
69
|
-
this.state.tagType = value;
|
|
70
|
-
return this;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
className(value: string): Element {
|
|
74
|
-
this.state.className = value;
|
|
75
|
-
return this;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
text(value: string): Element {
|
|
79
|
-
this.state.text = value;
|
|
80
|
-
this.state.innerHTML = '';
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
innerHTML(value: string): Element {
|
|
85
|
-
this.state.innerHTML = value;
|
|
86
|
-
this.state.text = '';
|
|
87
|
-
return this;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
write(content: string, asHTML: boolean = false, newLine: boolean = false): Element {
|
|
91
|
-
if (!this._element) {
|
|
92
|
-
console.warn(`Element.write: Element "${this._id}" not yet rendered`);
|
|
93
|
-
return this;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const contentWithBreak = newLine ? (asHTML ? content + '<br>' : content + '\n') : content;
|
|
97
|
-
|
|
98
|
-
if (asHTML) {
|
|
99
|
-
this._element.insertAdjacentHTML('beforeend', contentWithBreak);
|
|
100
|
-
} else {
|
|
101
|
-
const textNode = document.createTextNode(contentWithBreak);
|
|
102
|
-
this._element.appendChild(textNode);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return this;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
prepend(content: string, asHTML: boolean = false, newLine: boolean = false): Element {
|
|
109
|
-
if (!this._element) {
|
|
110
|
-
console.warn(`Element.prepend: Element "${this._id}" not yet rendered`);
|
|
111
|
-
return this;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const contentWithBreak = newLine ? (asHTML ? content + '<br>' : content + '\n') : content;
|
|
115
|
-
|
|
116
|
-
if (asHTML) {
|
|
117
|
-
this._element.insertAdjacentHTML('afterbegin', contentWithBreak);
|
|
118
|
-
} else {
|
|
119
|
-
const textNode = document.createTextNode(contentWithBreak);
|
|
120
|
-
this._element.insertBefore(textNode, this._element.firstChild);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return this;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
clear(): Element {
|
|
127
|
-
if (!this._element) {
|
|
128
|
-
console.warn(`Element.clear: Element "${this._id}" not yet rendered`);
|
|
129
|
-
return this;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
this._element.innerHTML = '';
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Append child component or HTML element
|
|
138
|
-
*/
|
|
139
|
-
append(child: any): Element {
|
|
140
|
-
if (!this._element) {
|
|
141
|
-
console.warn(`Element.append: Element "${this._id}" not yet rendered`);
|
|
142
|
-
return this;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (child instanceof HTMLElement) {
|
|
146
|
-
this._element.appendChild(child);
|
|
147
|
-
} else if (child?._element) {
|
|
148
|
-
// Another Jux component
|
|
149
|
-
this._element.appendChild(child._element);
|
|
150
|
-
} else if (typeof child === 'string') {
|
|
151
|
-
const div = document.createElement('div');
|
|
152
|
-
div.innerHTML = child;
|
|
153
|
-
this._element.appendChild(div.firstChild || div);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
render(targetId?: string): this {
|
|
160
|
-
const container = this._setupContainer(targetId);
|
|
161
|
-
|
|
162
|
-
const { tagType, className, text, innerHTML, attributes, styles, style, class: classValue } = this.state;
|
|
163
|
-
|
|
164
|
-
// Build element
|
|
165
|
-
const element = document.createElement(tagType);
|
|
166
|
-
element.id = this._id;
|
|
167
|
-
|
|
168
|
-
if (className) element.className = className;
|
|
169
|
-
if (classValue) element.className = `${element.className} ${classValue}`.trim();
|
|
170
|
-
|
|
171
|
-
if (text) {
|
|
172
|
-
element.textContent = text;
|
|
173
|
-
} else if (innerHTML) {
|
|
174
|
-
element.innerHTML = innerHTML;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
Object.entries(attributes).forEach(([key, value]) => {
|
|
178
|
-
element.setAttribute(key, value);
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
const styleString = Object.entries(styles)
|
|
182
|
-
.map(([key, value]) => `${key}: ${value}`)
|
|
183
|
-
.join('; ');
|
|
184
|
-
|
|
185
|
-
if (styleString || style) {
|
|
186
|
-
element.setAttribute('style', `${styleString}; ${style}`.trim());
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Wire events using inherited method
|
|
190
|
-
this._wireStandardEvents(element);
|
|
191
|
-
|
|
192
|
-
// Wire sync bindings
|
|
193
|
-
this._syncBindings.forEach(({ property, stateObj, toState, toComponent }) => {
|
|
194
|
-
const transform = toComponent || ((v: any) => v);
|
|
195
|
-
|
|
196
|
-
stateObj.subscribe((val: any) => {
|
|
197
|
-
const transformed = transform(val);
|
|
198
|
-
|
|
199
|
-
if (property === 'text') {
|
|
200
|
-
element.textContent = String(transformed);
|
|
201
|
-
this.state.text = String(transformed);
|
|
202
|
-
} else if (property === 'innerHTML') {
|
|
203
|
-
element.innerHTML = String(transformed);
|
|
204
|
-
this.state.innerHTML = String(transformed);
|
|
205
|
-
} else if (property === 'class') {
|
|
206
|
-
element.className = String(transformed);
|
|
207
|
-
this.state.class = String(transformed);
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
container.appendChild(element);
|
|
213
|
-
this._element = element;
|
|
214
|
-
|
|
215
|
-
return this;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export function element(id: string, options: ElementOptions = {}): Element {
|
|
220
|
-
return new Element(id, options);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// Semantic HTML element factories with default classes
|
|
224
|
-
export function header(id: string, options: Omit<ElementOptions, 'tagType'> = {}): Element {
|
|
225
|
-
const defaultClass = 'jux-header';
|
|
226
|
-
const mergedClass = options.class ? `${defaultClass} ${options.class}` : defaultClass;
|
|
227
|
-
return new Element(id, { ...options, tagType: 'header', class: mergedClass });
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export function footer(id: string, options: Omit<ElementOptions, 'tagType'> = {}): Element {
|
|
231
|
-
const defaultClass = 'jux-footer';
|
|
232
|
-
const mergedClass = options.class ? `${defaultClass} ${options.class}` : defaultClass;
|
|
233
|
-
return new Element(id, { ...options, tagType: 'footer', class: mergedClass });
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
export function main(id: string, options: Omit<ElementOptions, 'tagType'> = {}): Element {
|
|
237
|
-
const defaultClass = 'jux-main';
|
|
238
|
-
const mergedClass = options.class ? `${defaultClass} ${options.class}` : defaultClass;
|
|
239
|
-
return new Element(id, { ...options, tagType: 'main', class: mergedClass });
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
export function aside(id: string, options: Omit<ElementOptions, 'tagType'> = {}): Element {
|
|
243
|
-
const defaultClass = 'jux-aside';
|
|
244
|
-
const mergedClass = options.class ? `${defaultClass} ${options.class}` : defaultClass;
|
|
245
|
-
return new Element(id, { ...options, tagType: 'aside', class: mergedClass });
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
export function section(id: string, options: Omit<ElementOptions, 'tagType'> = {}): Element {
|
|
249
|
-
const defaultClass = 'jux-section';
|
|
250
|
-
const mergedClass = options.class ? `${defaultClass} ${options.class}` : defaultClass;
|
|
251
|
-
return new Element(id, { ...options, tagType: 'section', class: mergedClass });
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
export function article(id: string, options: Omit<ElementOptions, 'tagType'> = {}): Element {
|
|
255
|
-
const defaultClass = 'jux-article';
|
|
256
|
-
const mergedClass = options.class ? `${defaultClass} ${options.class}` : defaultClass;
|
|
257
|
-
return new Element(id, { ...options, tagType: 'article', class: mergedClass });
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
export function div(id: string, options: Omit<ElementOptions, 'tagType'> = {}): Element {
|
|
261
|
-
return new Element(id, { ...options, tagType: 'div' });
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export function span(id: string, options: Omit<ElementOptions, 'tagType'> = {}): Element {
|
|
265
|
-
return new Element(id, { ...options, tagType: 'span' });
|
|
266
|
-
}
|
|
267
|
-
|
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
import { FormInput, FormInputState } from './base/FormInput.js';
|
|
2
|
-
import { renderIcon } from './icons.js';
|
|
3
|
-
|
|
4
|
-
// Event definitions
|
|
5
|
-
const TRIGGER_EVENTS = [] as const;
|
|
6
|
-
const CALLBACK_EVENTS = ['change', 'filesSelected', 'clear'] as const;
|
|
7
|
-
|
|
8
|
-
export interface FileUploadOptions {
|
|
9
|
-
label?: string;
|
|
10
|
-
accept?: string;
|
|
11
|
-
multiple?: boolean;
|
|
12
|
-
disabled?: boolean;
|
|
13
|
-
name?: string;
|
|
14
|
-
icon?: string;
|
|
15
|
-
required?: boolean;
|
|
16
|
-
style?: string;
|
|
17
|
-
class?: string;
|
|
18
|
-
onValidate?: (files: File[]) => boolean | string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface FileUploadState extends FormInputState {
|
|
22
|
-
files: File[];
|
|
23
|
-
accept: string;
|
|
24
|
-
multiple: boolean;
|
|
25
|
-
icon: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class FileUpload extends FormInput<FileUploadState> {
|
|
29
|
-
private _fileListElement: HTMLElement | null = null;
|
|
30
|
-
|
|
31
|
-
constructor(id: string, options: FileUploadOptions = {}) {
|
|
32
|
-
super(id, {
|
|
33
|
-
files: [],
|
|
34
|
-
accept: options.accept ?? '',
|
|
35
|
-
multiple: options.multiple ?? false,
|
|
36
|
-
icon: options.icon ?? 'upload',
|
|
37
|
-
label: options.label ?? '',
|
|
38
|
-
required: options.required ?? false,
|
|
39
|
-
disabled: options.disabled ?? false,
|
|
40
|
-
name: options.name ?? id,
|
|
41
|
-
style: options.style ?? '',
|
|
42
|
-
class: options.class ?? '',
|
|
43
|
-
errorMessage: undefined
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
if (options.onValidate) {
|
|
47
|
-
this._onValidate = options.onValidate;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
protected getTriggerEvents(): readonly string[] {
|
|
52
|
-
return TRIGGER_EVENTS;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
protected getCallbackEvents(): readonly string[] {
|
|
56
|
-
return CALLBACK_EVENTS;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/* ═════════════════════════════════════════════════════════════════
|
|
60
|
-
* FLUENT API
|
|
61
|
-
* ═════════════════════════════════════════════════════════════════ */
|
|
62
|
-
|
|
63
|
-
// ✅ Inherited from FormInput/BaseComponent:
|
|
64
|
-
// - label(), required(), name(), onValidate()
|
|
65
|
-
// - validate(), isValid()
|
|
66
|
-
// - style(), class()
|
|
67
|
-
// - bind(), sync(), renderTo()
|
|
68
|
-
// - disabled(), enable(), disable()
|
|
69
|
-
|
|
70
|
-
accept(value: string): this {
|
|
71
|
-
this.state.accept = value;
|
|
72
|
-
return this;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
multiple(value: boolean): this {
|
|
76
|
-
this.state.multiple = value;
|
|
77
|
-
return this;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
icon(value: string): this {
|
|
81
|
-
this.state.icon = value;
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
clear(): this {
|
|
86
|
-
this.state.files = [];
|
|
87
|
-
if (this._inputElement) {
|
|
88
|
-
(this._inputElement as HTMLInputElement).value = '';
|
|
89
|
-
}
|
|
90
|
-
if (this._fileListElement) {
|
|
91
|
-
this._updateFileList([]);
|
|
92
|
-
}
|
|
93
|
-
// 🎯 Fire the clear callback event
|
|
94
|
-
this._triggerCallback('clear');
|
|
95
|
-
return this;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/* ═════════════════════════════════════════════════════════════════
|
|
99
|
-
* FORM INPUT IMPLEMENTATION
|
|
100
|
-
* ═════════════════════════════════════════════════════════════════ */
|
|
101
|
-
|
|
102
|
-
getValue(): File[] {
|
|
103
|
-
return this.state.files;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
setValue(files: File[]): this {
|
|
107
|
-
this.state.files = files;
|
|
108
|
-
if (this._fileListElement) {
|
|
109
|
-
this._updateFileList(files);
|
|
110
|
-
}
|
|
111
|
-
return this;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
getFiles(): File[] {
|
|
115
|
-
return this.getValue();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
protected _validateValue(files: File[]): boolean | string {
|
|
119
|
-
const { required } = this.state;
|
|
120
|
-
|
|
121
|
-
if (required && files.length === 0) {
|
|
122
|
-
return 'Please select at least one file';
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (this._onValidate) {
|
|
126
|
-
const result = this._onValidate(files);
|
|
127
|
-
if (result !== true) {
|
|
128
|
-
return result || 'Invalid files';
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return true;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
protected _buildInputElement(): HTMLElement {
|
|
136
|
-
const { accept, multiple, required, disabled, name } = this.state;
|
|
137
|
-
|
|
138
|
-
const input = document.createElement('input');
|
|
139
|
-
input.type = 'file';
|
|
140
|
-
input.className = 'jux-fileupload-input';
|
|
141
|
-
input.id = `${this._id}-input`;
|
|
142
|
-
input.name = name;
|
|
143
|
-
input.required = required;
|
|
144
|
-
input.disabled = disabled;
|
|
145
|
-
|
|
146
|
-
if (accept) input.accept = accept;
|
|
147
|
-
if (multiple) input.multiple = multiple;
|
|
148
|
-
|
|
149
|
-
return input;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/* ═════════════════════════════════════════════════════════════════
|
|
153
|
-
* RENDER
|
|
154
|
-
* ═════════════════════════════════════════════════════════════════ */
|
|
155
|
-
|
|
156
|
-
render(targetId?: string): this {
|
|
157
|
-
const container = this._setupContainer(targetId);
|
|
158
|
-
|
|
159
|
-
const { icon, style, class: className } = this.state;
|
|
160
|
-
|
|
161
|
-
// Build wrapper
|
|
162
|
-
const wrapper = document.createElement('div');
|
|
163
|
-
wrapper.className = 'jux-input jux-fileupload';
|
|
164
|
-
wrapper.id = this._id;
|
|
165
|
-
if (className) wrapper.className += ` ${className}`;
|
|
166
|
-
if (style) wrapper.setAttribute('style', style);
|
|
167
|
-
|
|
168
|
-
// Label
|
|
169
|
-
if (this.state.label) {
|
|
170
|
-
wrapper.appendChild(this._renderLabel());
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Hidden file input
|
|
174
|
-
const inputEl = this._buildInputElement() as HTMLInputElement;
|
|
175
|
-
this._inputElement = inputEl;
|
|
176
|
-
wrapper.appendChild(inputEl);
|
|
177
|
-
|
|
178
|
-
// Button container
|
|
179
|
-
const buttonContainer = document.createElement('div');
|
|
180
|
-
buttonContainer.className = 'jux-fileupload-button-container';
|
|
181
|
-
|
|
182
|
-
if (icon) {
|
|
183
|
-
const iconEl = document.createElement('span');
|
|
184
|
-
iconEl.className = 'jux-fileupload-icon';
|
|
185
|
-
iconEl.appendChild(renderIcon(icon));
|
|
186
|
-
buttonContainer.appendChild(iconEl);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const button = document.createElement('button');
|
|
190
|
-
button.type = 'button';
|
|
191
|
-
button.className = 'jux-fileupload-button';
|
|
192
|
-
button.textContent = 'Choose File(s)';
|
|
193
|
-
button.disabled = this.state.disabled;
|
|
194
|
-
|
|
195
|
-
buttonContainer.appendChild(button);
|
|
196
|
-
wrapper.appendChild(buttonContainer);
|
|
197
|
-
|
|
198
|
-
// File list
|
|
199
|
-
const fileList = document.createElement('div');
|
|
200
|
-
fileList.className = 'jux-fileupload-list';
|
|
201
|
-
this._fileListElement = fileList;
|
|
202
|
-
wrapper.appendChild(fileList);
|
|
203
|
-
|
|
204
|
-
// Error element
|
|
205
|
-
wrapper.appendChild(this._renderError());
|
|
206
|
-
|
|
207
|
-
// Button click triggers file input
|
|
208
|
-
button.addEventListener('click', () => inputEl.click());
|
|
209
|
-
|
|
210
|
-
// Wire events
|
|
211
|
-
this._wireStandardEvents(wrapper);
|
|
212
|
-
|
|
213
|
-
// Wire file-specific sync
|
|
214
|
-
const filesSync = this._syncBindings.find(b => b.property === 'files' || b.property === 'value');
|
|
215
|
-
|
|
216
|
-
if (filesSync) {
|
|
217
|
-
const { stateObj, toState, toComponent } = filesSync;
|
|
218
|
-
|
|
219
|
-
const transformToState = toState || ((v: File[]) => v);
|
|
220
|
-
const transformToComponent = toComponent || ((v: any) => v);
|
|
221
|
-
|
|
222
|
-
let isUpdating = false;
|
|
223
|
-
|
|
224
|
-
// State → Component
|
|
225
|
-
stateObj.subscribe((val: any) => {
|
|
226
|
-
if (isUpdating) return;
|
|
227
|
-
const transformed = transformToComponent(val);
|
|
228
|
-
this.setValue(transformed);
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
// Component → State
|
|
232
|
-
inputEl.addEventListener('change', () => {
|
|
233
|
-
if (isUpdating) return;
|
|
234
|
-
isUpdating = true;
|
|
235
|
-
|
|
236
|
-
const files = Array.from(inputEl.files || []);
|
|
237
|
-
this.state.files = files;
|
|
238
|
-
this._updateFileList(files);
|
|
239
|
-
this._clearError();
|
|
240
|
-
|
|
241
|
-
const transformed = transformToState(files);
|
|
242
|
-
stateObj.set(transformed);
|
|
243
|
-
|
|
244
|
-
// 🎯 Fire the callback events
|
|
245
|
-
this._triggerCallback('change', files);
|
|
246
|
-
this._triggerCallback('filesSelected', files);
|
|
247
|
-
|
|
248
|
-
setTimeout(() => { isUpdating = false; }, 0);
|
|
249
|
-
});
|
|
250
|
-
} else {
|
|
251
|
-
// Default behavior without sync
|
|
252
|
-
inputEl.addEventListener('change', () => {
|
|
253
|
-
const files = Array.from(inputEl.files || []);
|
|
254
|
-
this.state.files = files;
|
|
255
|
-
this._updateFileList(files);
|
|
256
|
-
this._clearError();
|
|
257
|
-
|
|
258
|
-
// 🎯 Fire the callback events
|
|
259
|
-
this._triggerCallback('change', files);
|
|
260
|
-
this._triggerCallback('filesSelected', files);
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Always add blur validation
|
|
265
|
-
inputEl.addEventListener('blur', () => {
|
|
266
|
-
this.validate();
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
// Sync label changes
|
|
270
|
-
const labelSync = this._syncBindings.find(b => b.property === 'label');
|
|
271
|
-
if (labelSync) {
|
|
272
|
-
const transform = labelSync.toComponent || ((v: any) => String(v));
|
|
273
|
-
labelSync.stateObj.subscribe((val: any) => {
|
|
274
|
-
this.label(transform(val));
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
container.appendChild(wrapper);
|
|
279
|
-
return this;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
private _updateFileList(files: File[]): void {
|
|
283
|
-
if (!this._fileListElement) return;
|
|
284
|
-
|
|
285
|
-
this._fileListElement.innerHTML = '';
|
|
286
|
-
|
|
287
|
-
if (files.length === 0) {
|
|
288
|
-
this._fileListElement.textContent = 'No files selected';
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
files.forEach(file => {
|
|
293
|
-
const fileItem = document.createElement('div');
|
|
294
|
-
fileItem.className = 'jux-fileupload-item';
|
|
295
|
-
fileItem.textContent = `${file.name} (${this._formatFileSize(file.size)})`;
|
|
296
|
-
this._fileListElement!.appendChild(fileItem);
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
private _formatFileSize(bytes: number): string {
|
|
301
|
-
if (bytes < 1024) return `${bytes} B`;
|
|
302
|
-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
303
|
-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
export function fileupload(id: string, options: FileUploadOptions = {}): FileUpload {
|
|
308
|
-
return new FileUpload(id, options);
|
|
309
|
-
}
|