@zeyos/zx 2.1.0 → 2.2.1
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/CHANGELOG.md +51 -0
- package/README.md +15 -0
- package/dist/types/compat/base.d.ts +156 -0
- package/dist/types/compat/globals.d.ts +20 -0
- package/dist/types/compat/index.d.ts +128 -0
- package/dist/types/compat/map/basic.d.ts +175 -0
- package/dist/types/compat/map/data.d.ts +102 -0
- package/dist/types/compat/map/date-time.d.ts +80 -0
- package/dist/types/compat/map/factory.d.ts +9 -0
- package/dist/types/compat/map/form.d.ts +98 -0
- package/dist/types/compat/map/helpers.d.ts +12 -0
- package/dist/types/compat/map/http.d.ts +36 -0
- package/dist/types/compat/map/layout.d.ts +106 -0
- package/dist/types/compat/map/options.d.ts +80 -0
- package/dist/types/compat/map/select.d.ts +95 -0
- package/dist/types/compat/map/stubs.d.ts +32 -0
- package/dist/types/compat/map/utilities.d.ts +37 -0
- package/dist/types/compat-entry.d.ts +5 -0
- package/dist/types/components/badge/badge.d.ts +46 -0
- package/dist/types/components/breadcrumb/breadcrumb.d.ts +200 -0
- package/dist/types/components/button/button.d.ts +42 -0
- package/dist/types/components/check-button/check-button.d.ts +98 -0
- package/dist/types/components/checklist/checklist.d.ts +143 -0
- package/dist/types/components/checklist/field-adapter.d.ts +5 -0
- package/dist/types/components/context-menu/context-menu.d.ts +130 -0
- package/dist/types/components/copy/copy-button.d.ts +48 -0
- package/dist/types/components/copy/copy-input.d.ts +91 -0
- package/dist/types/components/data-filter/data-filter.d.ts +250 -0
- package/dist/types/components/data-filter/filter-core.d.ts +52 -0
- package/dist/types/components/date-picker/date-picker.d.ts +137 -0
- package/dist/types/components/date-picker/date-range-picker.d.ts +350 -0
- package/dist/types/components/date-picker/field-adapter.d.ts +6 -0
- package/dist/types/components/date-picker/month-picker.d.ts +75 -0
- package/dist/types/components/date-picker/range-field-adapter.d.ts +7 -0
- package/dist/types/components/date-picker/time-picker.d.ts +127 -0
- package/dist/types/components/datebox/date-range-box.d.ts +218 -0
- package/dist/types/components/datebox/datebox.d.ts +160 -0
- package/dist/types/components/datebox/field-adapter.d.ts +5 -0
- package/dist/types/components/dialog/dialog.d.ts +284 -0
- package/dist/types/components/dropdown/dropdown.d.ts +118 -0
- package/dist/types/components/empty-state/empty-state.d.ts +38 -0
- package/dist/types/components/field/field.d.ts +231 -0
- package/dist/types/components/field-upload/field-adapter.d.ts +5 -0
- package/dist/types/components/field-upload/field-upload.d.ts +213 -0
- package/dist/types/components/fieldset/fieldset.d.ts +125 -0
- package/dist/types/components/finder/finder.d.ts +252 -0
- package/dist/types/components/form/form.d.ts +194 -0
- package/dist/types/components/groupbox/groupbox.d.ts +89 -0
- package/dist/types/components/layout/layout.d.ts +132 -0
- package/dist/types/components/loading/inline-loading.d.ts +86 -0
- package/dist/types/components/loading/progress-bar.d.ts +168 -0
- package/dist/types/components/loading/spinner.d.ts +34 -0
- package/dist/types/components/master-panel/master-panel.d.ts +112 -0
- package/dist/types/components/menu-button/menu-button.d.ts +142 -0
- package/dist/types/components/message/message.d.ts +212 -0
- package/dist/types/components/modal/modal.d.ts +105 -0
- package/dist/types/components/multi-value-editor/field-adapter.d.ts +5 -0
- package/dist/types/components/multi-value-editor/multi-value-editor.d.ts +66 -0
- package/dist/types/components/navigation-bar/navigation-bar.d.ts +229 -0
- package/dist/types/components/number-field/field-adapter.d.ts +5 -0
- package/dist/types/components/number-field/number-field.d.ts +292 -0
- package/dist/types/components/pagination/pagination.d.ts +267 -0
- package/dist/types/components/panel/panel.d.ts +194 -0
- package/dist/types/components/permission/permission.d.ts +93 -0
- package/dist/types/components/rating/field-adapter.d.ts +5 -0
- package/dist/types/components/rating/rating.d.ts +213 -0
- package/dist/types/components/search/search.d.ts +98 -0
- package/dist/types/components/select/field-adapter.d.ts +5 -0
- package/dist/types/components/select/filter.d.ts +11 -0
- package/dist/types/components/select/select.d.ts +235 -0
- package/dist/types/components/skeleton/skeleton.d.ts +97 -0
- package/dist/types/components/slider/field-adapter.d.ts +5 -0
- package/dist/types/components/slider/slider.d.ts +216 -0
- package/dist/types/components/split-view/split-view.d.ts +335 -0
- package/dist/types/components/stepper/stepper.d.ts +314 -0
- package/dist/types/components/stepper/steps.d.ts +131 -0
- package/dist/types/components/tabbox/tabbox.d.ts +230 -0
- package/dist/types/components/table/sort.d.ts +49 -0
- package/dist/types/components/table/table.d.ts +872 -0
- package/dist/types/components/tag-picker/field-adapter.d.ts +5 -0
- package/dist/types/components/tag-picker/tag-picker.d.ts +375 -0
- package/dist/types/components/timebox/field-adapter.d.ts +5 -0
- package/dist/types/components/timebox/timebox.d.ts +121 -0
- package/dist/types/components/toggle/field-adapter.d.ts +5 -0
- package/dist/types/components/toggle/toggle.d.ts +94 -0
- package/dist/types/components/toolbar/toolbar.d.ts +230 -0
- package/dist/types/components/tooltip/tooltip.d.ts +183 -0
- package/dist/types/components/tree/hierarchy.d.ts +113 -0
- package/dist/types/components/tree/tree.d.ts +383 -0
- package/dist/types/components/truncate/truncate.d.ts +61 -0
- package/dist/types/components/value-list/field-adapter.d.ts +5 -0
- package/dist/types/components/value-list/value-list.d.ts +143 -0
- package/dist/types/core/breakpoint.d.ts +68 -0
- package/dist/types/core/component.d.ts +115 -0
- package/dist/types/core/date.d.ts +50 -0
- package/dist/types/core/dom.d.ts +86 -0
- package/dist/types/core/export.d.ts +70 -0
- package/dist/types/core/fontawesome.d.ts +107 -0
- package/dist/types/core/format.d.ts +107 -0
- package/dist/types/core/http.d.ts +122 -0
- package/dist/types/core/i18n.d.ts +35 -0
- package/dist/types/core/icons.d.ts +137 -0
- package/dist/types/core/keyboard.d.ts +72 -0
- package/dist/types/core/position.d.ts +45 -0
- package/dist/types/core/storage.d.ts +46 -0
- package/dist/types/core/util.d.ts +117 -0
- package/dist/types/elements/define.d.ts +73 -0
- package/dist/types/elements/reflect.d.ts +27 -0
- package/dist/types/index.d.ts +79 -0
- package/dist/types/zeyos/connect.d.ts +65 -0
- package/dist/types/zeyos/form.d.ts +140 -0
- package/dist/types/zeyos/icons.d.ts +104 -0
- package/dist/types/zeyos/index.d.ts +12 -0
- package/dist/types/zeyos/modules.d.ts +110 -0
- package/dist/types/zeyos/query.d.ts +157 -0
- package/dist/types/zeyos/schema.d.ts +79 -0
- package/dist/types/zeyos/select.d.ts +167 -0
- package/dist/types/zeyos/table.d.ts +205 -0
- package/dist/zx-compat.esm.js +375 -39
- package/dist/zx-compat.esm.min.js +1 -1
- package/dist/zx-compat.global.js +372 -73
- package/dist/zx-compat.global.min.js +3 -3
- package/dist/zx-zeyos.esm.js +355 -49
- package/dist/zx-zeyos.esm.min.js +1 -1
- package/dist/zx.css +68 -6
- package/dist/zx.esm.js +359 -73
- package/dist/zx.esm.min.js +3 -3
- package/dist/zx.global.js +359 -73
- package/dist/zx.global.min.js +3 -3
- package/dist/zx.min.css +1 -1
- package/docs/api.json +48 -2
- package/docs/llms.md +31 -0
- package/package.json +37 -9
- package/src/compat/base.js +291 -0
- package/src/compat/globals.js +165 -0
- package/src/compat/index.js +84 -0
- package/src/compat/map/basic.js +464 -0
- package/src/compat/map/data.js +232 -0
- package/src/compat/map/date-time.js +177 -0
- package/src/compat/map/factory.js +45 -0
- package/src/compat/map/form.js +330 -0
- package/src/compat/map/helpers.js +46 -0
- package/src/compat/map/http.js +117 -0
- package/src/compat/map/layout.js +256 -0
- package/src/compat/map/options.js +267 -0
- package/src/compat/map/select.js +198 -0
- package/src/compat/map/stubs.js +26 -0
- package/src/compat/map/utilities.js +113 -0
- package/src/compat-entry.js +11 -0
- package/src/components/badge/badge.css +147 -0
- package/src/components/badge/badge.js +92 -0
- package/src/components/breadcrumb/breadcrumb.css +105 -0
- package/src/components/breadcrumb/breadcrumb.js +331 -0
- package/src/components/button/button.css +104 -0
- package/src/components/button/button.js +76 -0
- package/src/components/check-button/check-button.css +66 -0
- package/src/components/check-button/check-button.js +167 -0
- package/src/components/checklist/checklist.css +65 -0
- package/src/components/checklist/checklist.js +282 -0
- package/src/components/checklist/field-adapter.js +46 -0
- package/src/components/context-menu/context-menu.css +54 -0
- package/src/components/context-menu/context-menu.js +334 -0
- package/src/components/copy/copy-button.css +27 -0
- package/src/components/copy/copy-button.js +78 -0
- package/src/components/copy/copy-input.css +59 -0
- package/src/components/copy/copy-input.js +123 -0
- package/src/components/data-filter/data-filter.css +53 -0
- package/src/components/data-filter/data-filter.js +400 -0
- package/src/components/data-filter/filter-core.js +128 -0
- package/src/components/date-picker/date-picker.css +181 -0
- package/src/components/date-picker/date-picker.js +500 -0
- package/src/components/date-picker/date-range-picker.css +264 -0
- package/src/components/date-picker/date-range-picker.js +990 -0
- package/src/components/date-picker/field-adapter.js +36 -0
- package/src/components/date-picker/month-picker.js +223 -0
- package/src/components/date-picker/range-field-adapter.js +31 -0
- package/src/components/date-picker/time-picker.css +191 -0
- package/src/components/date-picker/time-picker.js +406 -0
- package/src/components/datebox/date-range-box.css +128 -0
- package/src/components/datebox/date-range-box.js +492 -0
- package/src/components/datebox/datebox.css +108 -0
- package/src/components/datebox/datebox.js +358 -0
- package/src/components/datebox/field-adapter.js +34 -0
- package/src/components/dialog/dialog.css +74 -0
- package/src/components/dialog/dialog.js +450 -0
- package/src/components/dropdown/dropdown.css +24 -0
- package/src/components/dropdown/dropdown.js +288 -0
- package/src/components/empty-state/empty-state.css +80 -0
- package/src/components/empty-state/empty-state.js +92 -0
- package/src/components/field/field.css +127 -0
- package/src/components/field/field.js +605 -0
- package/src/components/field-upload/field-adapter.js +21 -0
- package/src/components/field-upload/field-upload.css +108 -0
- package/src/components/field-upload/field-upload.js +475 -0
- package/src/components/fieldset/fieldset.css +37 -0
- package/src/components/fieldset/fieldset.js +307 -0
- package/src/components/finder/finder.css +98 -0
- package/src/components/finder/finder.js +509 -0
- package/src/components/form/form.css +32 -0
- package/src/components/form/form.js +302 -0
- package/src/components/groupbox/groupbox.css +51 -0
- package/src/components/groupbox/groupbox.js +199 -0
- package/src/components/layout/layout.css +117 -0
- package/src/components/layout/layout.js +136 -0
- package/src/components/loading/inline-loading.css +34 -0
- package/src/components/loading/inline-loading.js +140 -0
- package/src/components/loading/progress-bar.css +115 -0
- package/src/components/loading/progress-bar.js +259 -0
- package/src/components/loading/spinner.css +58 -0
- package/src/components/loading/spinner.js +57 -0
- package/src/components/master-panel/master-panel.css +51 -0
- package/src/components/master-panel/master-panel.js +203 -0
- package/src/components/menu-button/menu-button.css +49 -0
- package/src/components/menu-button/menu-button.js +294 -0
- package/src/components/message/message.css +122 -0
- package/src/components/message/message.js +499 -0
- package/src/components/modal/modal.css +47 -0
- package/src/components/modal/modal.js +203 -0
- package/src/components/multi-value-editor/field-adapter.js +44 -0
- package/src/components/multi-value-editor/multi-value-editor.css +56 -0
- package/src/components/multi-value-editor/multi-value-editor.js +170 -0
- package/src/components/navigation-bar/navigation-bar.css +130 -0
- package/src/components/navigation-bar/navigation-bar.js +450 -0
- package/src/components/number-field/field-adapter.js +40 -0
- package/src/components/number-field/number-field.css +77 -0
- package/src/components/number-field/number-field.js +545 -0
- package/src/components/pagination/pagination.css +95 -0
- package/src/components/pagination/pagination.js +481 -0
- package/src/components/panel/panel.css +123 -0
- package/src/components/panel/panel.js +336 -0
- package/src/components/permission/permission.css +47 -0
- package/src/components/permission/permission.js +198 -0
- package/src/components/rating/field-adapter.js +34 -0
- package/src/components/rating/rating.css +97 -0
- package/src/components/rating/rating.js +395 -0
- package/src/components/search/search.css +70 -0
- package/src/components/search/search.js +193 -0
- package/src/components/select/field-adapter.js +24 -0
- package/src/components/select/filter.js +39 -0
- package/src/components/select/select.css +180 -0
- package/src/components/select/select.js +663 -0
- package/src/components/skeleton/skeleton.css +76 -0
- package/src/components/skeleton/skeleton.js +139 -0
- package/src/components/slider/field-adapter.js +36 -0
- package/src/components/slider/slider.css +181 -0
- package/src/components/slider/slider.js +399 -0
- package/src/components/split-view/split-view.css +139 -0
- package/src/components/split-view/split-view.js +806 -0
- package/src/components/stepper/stepper.css +234 -0
- package/src/components/stepper/stepper.js +459 -0
- package/src/components/stepper/steps.js +131 -0
- package/src/components/tabbox/tabbox.css +281 -0
- package/src/components/tabbox/tabbox.js +484 -0
- package/src/components/table/sort.js +85 -0
- package/src/components/table/table.css +364 -0
- package/src/components/table/table.js +1711 -0
- package/src/components/tag-picker/field-adapter.js +35 -0
- package/src/components/tag-picker/tag-picker.css +169 -0
- package/src/components/tag-picker/tag-picker.js +837 -0
- package/src/components/timebox/field-adapter.js +24 -0
- package/src/components/timebox/timebox.css +51 -0
- package/src/components/timebox/timebox.js +276 -0
- package/src/components/toggle/field-adapter.js +24 -0
- package/src/components/toggle/toggle.css +66 -0
- package/src/components/toggle/toggle.js +150 -0
- package/src/components/toolbar/toolbar.css +81 -0
- package/src/components/toolbar/toolbar.js +511 -0
- package/src/components/tooltip/tooltip.css +36 -0
- package/src/components/tooltip/tooltip.js +420 -0
- package/src/components/tree/hierarchy.js +151 -0
- package/src/components/tree/tree.css +99 -0
- package/src/components/tree/tree.js +784 -0
- package/src/components/truncate/truncate.css +20 -0
- package/src/components/truncate/truncate.js +92 -0
- package/src/components/value-list/field-adapter.js +24 -0
- package/src/components/value-list/value-list.css +108 -0
- package/src/components/value-list/value-list.js +336 -0
- package/src/core/breakpoint.js +150 -0
- package/src/core/component.js +292 -0
- package/src/core/date.js +216 -0
- package/src/core/dom.js +260 -0
- package/src/core/export.js +204 -0
- package/src/core/fontawesome.js +177 -0
- package/src/core/format.js +283 -0
- package/src/core/http.js +246 -0
- package/src/core/i18n.js +83 -0
- package/src/core/icons.js +283 -0
- package/src/core/keyboard.js +240 -0
- package/src/core/position.js +177 -0
- package/src/core/storage.js +175 -0
- package/src/core/util.js +336 -0
- package/src/elements/define.js +715 -0
- package/src/elements/reflect.js +79 -0
- package/src/index.js +131 -0
- package/src/zeyos/connect.js +142 -0
- package/src/zeyos/form.js +254 -0
- package/src/zeyos/icons.js +92 -0
- package/src/zeyos/index.js +21 -0
- package/src/zeyos/modules.js +269 -0
- package/src/zeyos/query.js +199 -0
- package/src/zeyos/schema.js +474 -0
- package/src/zeyos/select.js +175 -0
- package/src/zeyos/table.js +272 -0
- package/styles/base.css +124 -0
- package/styles/icons.css +34 -0
- package/styles/tokens/dark.css +84 -0
- package/styles/tokens/density.css +9 -0
- package/styles/tokens/global.css +80 -0
- package/styles/tokens/modules.css +109 -0
- package/styles/tokens/semantic.css +49 -0
- package/styles/zx.css +61 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Field as ZxField,
|
|
3
|
+
Fieldset as ZxFieldset,
|
|
4
|
+
FieldUpload as ZxFieldUpload,
|
|
5
|
+
Form as ZxForm,
|
|
6
|
+
MultiValueEditor as ZxMultiValueEditor,
|
|
7
|
+
ValueList as ZxValueList
|
|
8
|
+
} from '../../index.js';
|
|
9
|
+
import { GxWrapper } from '../base.js';
|
|
10
|
+
import { legacyContent, targetElement, textLabel } from './helpers.js';
|
|
11
|
+
import {
|
|
12
|
+
translateChecklistOptions, translateDateboxFormat, translateFieldOptions, translateSelectOptions
|
|
13
|
+
} from './options.js';
|
|
14
|
+
|
|
15
|
+
/** Legacy bootstrap form. */
|
|
16
|
+
export class Form extends GxWrapper {
|
|
17
|
+
static legacyName = 'gx.bootstrap.Form';
|
|
18
|
+
|
|
19
|
+
/** @param {Element|string|null} display @param {Record<string, any>} [options={}] */
|
|
20
|
+
constructor(display, options = {}) {
|
|
21
|
+
super(options, { fields: {}, horizontal: ['col-md-3', 'col-md-9'], columns: false });
|
|
22
|
+
const component = new ZxForm(display, { fieldsets: [], actions: normalizeActions(options.actions) });
|
|
23
|
+
this._fieldsets = [];
|
|
24
|
+
this._attach(component, {
|
|
25
|
+
events: {
|
|
26
|
+
submit: { type: 'submit', args: (detail) => [detail.values, this] },
|
|
27
|
+
change: { type: 'change', args: (detail) => [detail.id, detail.value, this] },
|
|
28
|
+
invalid: { type: 'invalid', args: (detail) => [detail.errors, this] }
|
|
29
|
+
},
|
|
30
|
+
ui: { body: () => component._body, actions: () => component._actions }
|
|
31
|
+
});
|
|
32
|
+
if (options.fields && typeof options.fields === 'object') {
|
|
33
|
+
this.addFieldset({ fields: options.fields, title: options.title, horizontal: options.horizontal, columns: options.columns });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** @param {Fieldset|Record<string, any>} fieldset @returns {Fieldset} */
|
|
38
|
+
addFieldset(fieldset) {
|
|
39
|
+
const wrapper = fieldset instanceof Fieldset ? fieldset : new Fieldset(null, {
|
|
40
|
+
horizontal: this.options.horizontal, columns: this.options.columns, ...fieldset
|
|
41
|
+
});
|
|
42
|
+
this._fieldsets.push(wrapper);
|
|
43
|
+
this._zx.addFieldset(wrapper._zx);
|
|
44
|
+
return wrapper;
|
|
45
|
+
}
|
|
46
|
+
/** @param {unknown} content @returns {this} */
|
|
47
|
+
addActions(content) {
|
|
48
|
+
const actions = normalizeActions(content);
|
|
49
|
+
if (actions.length) this._zx.setActions(actions);
|
|
50
|
+
else {
|
|
51
|
+
const values = Array.isArray(content) ? content : [content];
|
|
52
|
+
for (const value of values) {
|
|
53
|
+
const node = legacyContent(value);
|
|
54
|
+
if (node?.nodeType === 1) this._zx._actions.prepend(node);
|
|
55
|
+
}
|
|
56
|
+
this._zx._actions.hidden = false;
|
|
57
|
+
}
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/** @param {string} id @returns {unknown} */ getValue(id) { return this.getField(id)?.getValue() ?? null; }
|
|
61
|
+
/** @returns {Record<string, unknown>} */ getValues() { return Object.assign({}, ...this._fieldsets.map((fieldset) => fieldset.getValues())); }
|
|
62
|
+
/** @param {string} id @returns {Field|null} */ getField(id) { for (const fieldset of this._fieldsets) { const field = fieldset.getField(id); if (field) return field; } return null; }
|
|
63
|
+
/** @returns {Fieldset[]} */ getFieldsets() { return this._fieldsets.slice(); }
|
|
64
|
+
/** @param {string} id @param {unknown} value @returns {this} */ setValue(id, value) { this.getField(id)?.setValue(value); return this; }
|
|
65
|
+
/** @param {Record<string, unknown>} values @returns {this} */ setValues(values) { for (const fieldset of this._fieldsets) fieldset.setValues(values); return this; }
|
|
66
|
+
/** @param {Record<string, unknown>} [highlights={}] @param {string} [type='error'] @returns {number} */ setHighlights(highlights = {}, type = 'error') { return this._fieldsets.reduce((count, fieldset) => count + fieldset.setHighlights(highlights, type), 0); }
|
|
67
|
+
/** @param {Record<string, unknown>} [highlights={}] @param {string} [type='error'] @returns {number} */ setHintHighlights(highlights = {}, type = 'error') { return this.setHighlights(highlights, type); }
|
|
68
|
+
/** @returns {this} */ reset() { for (const fieldset of this._fieldsets) fieldset.reset().setHighlights(); return this; }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Legacy bootstrap fieldset. */
|
|
72
|
+
export class Fieldset extends GxWrapper {
|
|
73
|
+
static legacyName = 'gx.bootstrap.Fieldset';
|
|
74
|
+
|
|
75
|
+
/** @param {Element|string|null} display @param {Record<string, any>} [options={}] */
|
|
76
|
+
constructor(display, options = {}) {
|
|
77
|
+
super(options, { fields: {}, title: null, columns: 1 });
|
|
78
|
+
const columns = Number(options.columns) || 1;
|
|
79
|
+
const component = new ZxFieldset(display, { title: options.title ?? '', columns: Math.max(1, Math.min(3, columns)), fields: {} });
|
|
80
|
+
this._fields = {};
|
|
81
|
+
this._attach(component, { ui: { columns: () => component._grid } });
|
|
82
|
+
if (options.fields && typeof options.fields === 'object') this.addFields(options.fields);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** @param {Record<string, any>} fields @returns {this} */ addFields(fields) { for (const [id, source] of Object.entries(fields ?? {})) this.addFieldItem(id, source); return this; }
|
|
86
|
+
/** @param {string} id @param {unknown} field @returns {Field|unknown} */
|
|
87
|
+
addFieldItem(id, field) {
|
|
88
|
+
if (this.hasField(id)) throw new Error(`Field "${id}" already exists.`);
|
|
89
|
+
let wrapper = field;
|
|
90
|
+
if (!(field instanceof Field)) {
|
|
91
|
+
if (field?.type != null) wrapper = new Field({ ...field, id });
|
|
92
|
+
else if (field?.field != null) wrapper = new Field({ ...field, id, type: field.type ?? 'custom' });
|
|
93
|
+
else if (field?.nodeType || field?._zx) wrapper = new Field({ id, label: id, type: 'custom', field });
|
|
94
|
+
else wrapper = new Field({ ...(field ?? {}), id });
|
|
95
|
+
}
|
|
96
|
+
this._fields[id] = wrapper;
|
|
97
|
+
this._zx.addField(id, wrapper instanceof Field ? wrapper._zx : wrapper?._zx ?? wrapper);
|
|
98
|
+
return wrapper;
|
|
99
|
+
}
|
|
100
|
+
/** @param {string} id @param {string|Function|Element} type @param {Record<string, any>} [options={}] @returns {Field} */
|
|
101
|
+
addField(id, type, options = {}) { return /** @type {Field} */ (this.addFieldItem(id, new Field({ ...options, id, type }))); }
|
|
102
|
+
/** @returns {this} */ clear() { this._zx.clear(); this._fields = {}; return this; }
|
|
103
|
+
/** @param {string} id @returns {boolean} */ hasField(id) { return Object.hasOwn(this._fields, id); }
|
|
104
|
+
/** @param {string} id @returns {Field|null} */ getField(id) { return this._fields[id] ?? null; }
|
|
105
|
+
/** @returns {Record<string, Field>} */ getFields() { return { ...this._fields }; }
|
|
106
|
+
/** @param {string} id @returns {unknown} */ getValue(id) { return this._fields[id]?.getValue?.() ?? null; }
|
|
107
|
+
/** @returns {Record<string, unknown>} */ getValues() { const values = {}; for (const [id, field] of Object.entries(this._fields)) if (!id.startsWith('__')) values[id] = field.getValue?.(); return values; }
|
|
108
|
+
/** @param {string} id @param {unknown} value @returns {this} */ setValue(id, value) { this._fields[id]?.setValue?.(value); return this; }
|
|
109
|
+
/** @param {Record<string, unknown>} values @returns {this} */ setValues(values) { for (const [id, value] of Object.entries(values ?? {})) this.setValue(id, value); return this; }
|
|
110
|
+
/** @returns {this} */ reset() { for (const field of Object.values(this._fields)) field.reset?.(); return this; }
|
|
111
|
+
/** @param {string} id @returns {this} */ focus(id) { this._fields[id]?.focus?.(); return this; }
|
|
112
|
+
/** @param {Record<string, unknown>} [highlights={}] @param {string} [type='error'] @returns {number} */
|
|
113
|
+
setHighlights(highlights = {}, type = 'error') {
|
|
114
|
+
let count = 0;
|
|
115
|
+
for (const [id, field] of Object.entries(this._fields)) {
|
|
116
|
+
const highlight = highlights?.[id];
|
|
117
|
+
if (highlight && typeof highlight === 'object') { field.setHighlight(highlight.label, highlight.type ?? type); count += 1; }
|
|
118
|
+
else if (highlight) { field.setHighlight(highlight, type); count += 1; }
|
|
119
|
+
else field.setHighlight();
|
|
120
|
+
}
|
|
121
|
+
return count;
|
|
122
|
+
}
|
|
123
|
+
/** @param {Record<string, unknown>} [highlights={}] @param {string} [type='error'] @returns {number} */ setHintHighlights(highlights = {}, type = 'error') { return this.setHighlights(highlights, type); }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Legacy bootstrap single field. */
|
|
127
|
+
export class Field extends GxWrapper {
|
|
128
|
+
static legacyName = 'gx.bootstrap.Field';
|
|
129
|
+
|
|
130
|
+
/** @param {Record<string, any>} [options={}] */
|
|
131
|
+
constructor(options = {}) {
|
|
132
|
+
super(options, { label: '', type: 'text', description: '', default: null });
|
|
133
|
+
const translated = translateLegacyField(options);
|
|
134
|
+
const component = new ZxField(null, translated);
|
|
135
|
+
this._type = translated.type;
|
|
136
|
+
this._attach(component, {
|
|
137
|
+
events: { setValue: { type: 'change', args: (detail) => [detail.value] }, change: { type: 'change', args: (detail) => [detail.value] } },
|
|
138
|
+
ui: {
|
|
139
|
+
label: () => component.el.querySelector('.zx-field__label'),
|
|
140
|
+
field: () => component.getInput(),
|
|
141
|
+
controlwrapper: () => component.el.querySelector('.zx-field__control'),
|
|
142
|
+
highlight: () => component.el.querySelector('.zx-field__highlight'),
|
|
143
|
+
description: () => component.el.querySelector('.zx-field__description'),
|
|
144
|
+
hint: () => component.el.querySelector('.zx-field__control')
|
|
145
|
+
},
|
|
146
|
+
setters: { value: 'setValue', default: 'setValue', disabled: 'setDisabled' }
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/** @param {string} label @returns {this} */ setLabel(label) { const element = this.display('label'); if (element) element.textContent = textLabel(label); return this; }
|
|
150
|
+
/** @param {string} help @returns {this} */ setHelp(help) { const element = this.display('description'); if (element) element.textContent = String(help ?? ''); return this; }
|
|
151
|
+
/** @returns {unknown} */ getValue() { return this._zx.getValue(); }
|
|
152
|
+
/** @param {unknown} value @returns {this} */ setValue(value) { this._zx.setValue(value); return this; }
|
|
153
|
+
/** @param {boolean} disabled @returns {this} */ setDisabled(disabled) { this._zx.setDisabled(disabled); return this; }
|
|
154
|
+
/** @param {unknown} label @param {string} [type='warning'] @returns {this} */
|
|
155
|
+
setHighlight(label, type = 'warning') { if (!label) this._zx.clearHighlight(); else this._zx.setHighlight(String(label), highlightKind(type)); return this; }
|
|
156
|
+
/** @param {unknown} label @param {string} [type='warning'] @returns {this} */ setHintHighlight(label, type = 'warning') { return this.setHighlight(label, type); }
|
|
157
|
+
/** @returns {this} */ reset() { this._zx.reset(); return this; }
|
|
158
|
+
/** @returns {this} */ focus() { this._zx.focus(); return this; }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Legacy bootstrap chip list. */
|
|
162
|
+
export class ValueList extends GxWrapper {
|
|
163
|
+
static legacyName = 'gx.bootstrap.ValueList';
|
|
164
|
+
|
|
165
|
+
/** @param {Element|string|null} display @param {Record<string, any>} [options={}] */
|
|
166
|
+
constructor(display, options = {}) {
|
|
167
|
+
super(options, { deletable: true });
|
|
168
|
+
const component = new ZxValueList(display, { values: options.values ?? options.default ?? [], deletable: options.deletable !== false });
|
|
169
|
+
this._attach(component, { events: { change: { type: 'change', args: (detail) => [detail.values] }, click: { type: 'add', args: (detail) => [detail.value] } }, ui: { textbox: () => component._input, list: () => component._list } });
|
|
170
|
+
}
|
|
171
|
+
/** @param {unknown} value @returns {boolean} */ addValue(value) { return this._zx.addValue(value); }
|
|
172
|
+
/** @returns {unknown[]} */ getValues() { return this._zx.getValues(); }
|
|
173
|
+
/** @param {unknown[]} values @returns {this} */ setValues(values) { this._zx.setValues(values ?? []); return this; }
|
|
174
|
+
/** @returns {this} */ enable() { this._zx.enable(); return this; }
|
|
175
|
+
/** @returns {this} */ disable() { this._zx.disable(); return this; }
|
|
176
|
+
/** @returns {this} */ focus() { this._zx.focus(); return this; }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Legacy JSON-string multi-value editor. */
|
|
180
|
+
export class MultiValueEditor extends GxWrapper {
|
|
181
|
+
static legacyName = 'gx.bootstrap.MultiValueEditor';
|
|
182
|
+
|
|
183
|
+
/** @param {Element|string|null} display @param {Record<string, any>} [options={}] */
|
|
184
|
+
constructor(display, options = {}) {
|
|
185
|
+
super(options, { options: false, default: false });
|
|
186
|
+
const values = parseValues(options.default || options.values || options.options);
|
|
187
|
+
const component = new ZxMultiValueEditor(display, { values, options: null });
|
|
188
|
+
this._attach(component, { events: { change: { type: 'change', args: (detail) => [JSON.stringify(detail.values)] } }, ui: { optionsbox: () => component._rows, btnAdd: () => component._add } });
|
|
189
|
+
}
|
|
190
|
+
/** @returns {string} */ getValue() { return JSON.stringify(this._zx.getValues()); }
|
|
191
|
+
/** @param {string|unknown[]} value @returns {this} */ setValue(value) { this._zx.setValues(parseValues(value)); return this; }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Legacy bootstrap upload field. */
|
|
195
|
+
export class FieldUpload extends GxWrapper {
|
|
196
|
+
static legacyName = 'gx.bootstrap.FieldUpload';
|
|
197
|
+
|
|
198
|
+
/** @param {Record<string, any>} [options={}] */
|
|
199
|
+
constructor(options = {}) {
|
|
200
|
+
super(options, { inputname: 'upload', uploadurl: './index.php', params: {}, imageurl: '' });
|
|
201
|
+
const component = new ZxFieldUpload(null, {
|
|
202
|
+
url: options.uploadurl ?? options.url ?? './index.php', paramName: options.inputname ?? options.paramName ?? 'upload',
|
|
203
|
+
params: options.params ?? {}, accept: options.accept ?? null, multiple: Boolean(options.multiple),
|
|
204
|
+
autoUpload: options.autoUpload !== false, http: options.http ?? null
|
|
205
|
+
});
|
|
206
|
+
this._value = null;
|
|
207
|
+
this._attach(component, {
|
|
208
|
+
events: {
|
|
209
|
+
success: { type: 'success', args: (detail) => { this._handleResponse(detail.response); return [detail.response]; } },
|
|
210
|
+
failure: { type: 'error', args: (detail) => { options.showError?.(this, detail.error); return [detail.error]; } },
|
|
211
|
+
progress: { type: 'progress', args: (detail) => [detail.percent] }
|
|
212
|
+
},
|
|
213
|
+
ui: { select: () => component._input, placeholder: () => component._preview, progress: () => component._progress }
|
|
214
|
+
});
|
|
215
|
+
if (options.default) this.setValue(options.default);
|
|
216
|
+
if (options.disabled) this.setDisabled(true);
|
|
217
|
+
}
|
|
218
|
+
/** @param {unknown} source @returns {this} */ setValue(source) { this._value = source; this.setImage(source); return this; }
|
|
219
|
+
/** @returns {null} */ getValue() { return null; }
|
|
220
|
+
/** @param {unknown} source @returns {this} */
|
|
221
|
+
setImage(source) { this._zx._preview.replaceChildren(); if (source) { const image = document.createElement('img'); image.src = String(this.options.imageurl ?? '') + String(source); image.alt = ''; this._zx._preview.append(image); } return this; }
|
|
222
|
+
/** @param {boolean} disabled @returns {this} */ setDisabled(disabled) { this._zx.setDisabled(disabled); return this; }
|
|
223
|
+
/** @returns {this} */ reset() { this._zx.clear(); return this; }
|
|
224
|
+
/** @param {unknown} response @returns {void} */
|
|
225
|
+
_handleResponse(response) {
|
|
226
|
+
if (typeof this.options.parseResponse === 'function') this.options.parseResponse(this, response, (value) => { if (value != null) this.setValue(value); });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** @param {Record<string, any>} options @returns {Record<string, any>} */
|
|
231
|
+
function translateLegacyField(options) {
|
|
232
|
+
const translated = translateFieldOptions(options);
|
|
233
|
+
const nested = options.options && typeof options.options === 'object' && !Array.isArray(options.options) ? options.options : {};
|
|
234
|
+
if (options.field != null || (typeof options.type !== 'string' && options.type != null)) {
|
|
235
|
+
const source = options.field ?? options.type;
|
|
236
|
+
const instance = typeof source === 'function' ? new source(null, Object.keys(nested).length ? nested : options) : source;
|
|
237
|
+
translated.type = 'custom';
|
|
238
|
+
translated.adapter = () => externalAdapter(instance);
|
|
239
|
+
return translated;
|
|
240
|
+
}
|
|
241
|
+
if (translated.type === 'zxselect') {
|
|
242
|
+
translated.props = translateSelectOptions({ ...nested, ...options });
|
|
243
|
+
translated.props.items = Array.isArray(options.data) ? options.data : (nested.data ?? []);
|
|
244
|
+
translated.props.valueKey = options.elementIndex ?? nested.elementIndex ?? 'ID';
|
|
245
|
+
translated.props.labelKey = options.elementLabel ?? nested.elementLabel ?? 'name';
|
|
246
|
+
} else if (translated.type === 'checklist') {
|
|
247
|
+
translated.props = translateChecklistOptions(Object.keys(nested).length ? nested : options);
|
|
248
|
+
translated.props.items = nested.data ?? options.data ?? [];
|
|
249
|
+
} else if (translated.type === 'date' || translated.type === 'datetime') {
|
|
250
|
+
const source = Object.keys(nested).length ? nested : options;
|
|
251
|
+
translated.props = { format: translateDateboxFormat(source.format ?? '%d.%m.%Y'), time: translated.type === 'datetime' };
|
|
252
|
+
translated.value = normalizeFormDate(translated.value ?? source.date);
|
|
253
|
+
} else if (translated.type === 'month') {
|
|
254
|
+
translated.props = { ...nested };
|
|
255
|
+
translated.value = normalizeFormDate(translated.value ?? nested.date ?? options.date);
|
|
256
|
+
} else if (translated.type === 'time') {
|
|
257
|
+
const source = Object.keys(nested).length ? nested : options;
|
|
258
|
+
translated.props = { unit: source.unit ?? 'minutes', seconds: source.seconds !== false, signed: Boolean(source.prefix) };
|
|
259
|
+
translated.value = translated.value ?? source.time ?? 0;
|
|
260
|
+
} else if (translated.type === 'html') {
|
|
261
|
+
translated.value = legacyContent(options.content ?? translated.value);
|
|
262
|
+
}
|
|
263
|
+
return translated;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** @param {unknown} source @returns {import('../../components/field/field.js').FieldAdapter} */
|
|
267
|
+
function externalAdapter(source) {
|
|
268
|
+
const element = source?._zx?.toElement?.() ?? source?.toElement?.() ?? source;
|
|
269
|
+
if (!element?.nodeType) throw new TypeError('Custom legacy field must provide a DOM element');
|
|
270
|
+
return {
|
|
271
|
+
el: element,
|
|
272
|
+
get: () => source?.getValue?.() ?? source?.getValues?.() ?? source?.get?.() ?? ('value' in element ? element.value : null),
|
|
273
|
+
set: (value) => {
|
|
274
|
+
if (typeof source?.setValue === 'function') source.setValue(value);
|
|
275
|
+
else if (typeof source?.setValues === 'function') source.setValues(value);
|
|
276
|
+
else if (typeof source?.set === 'function') source.set(value);
|
|
277
|
+
else if ('value' in element) element.value = value == null ? '' : String(value);
|
|
278
|
+
},
|
|
279
|
+
focus: () => source?.focus?.() ?? element.focus?.(),
|
|
280
|
+
setDisabled: (disabled) => {
|
|
281
|
+
if (typeof source?.setDisabled === 'function') source.setDisabled(disabled);
|
|
282
|
+
else if (disabled) source?.disable?.(); else source?.enable?.();
|
|
283
|
+
if ('disabled' in element) element.disabled = disabled;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** @param {unknown} actions @returns {Array<Record<string, any>>} */
|
|
289
|
+
function normalizeActions(actions) {
|
|
290
|
+
const list = Array.isArray(actions) ? actions : (actions && typeof actions === 'object' && !actions.nodeType ? Object.values(actions) : []);
|
|
291
|
+
return list.filter((action) => action && typeof action === 'object' && !action.nodeType && !action.tag).map((action, index) => ({
|
|
292
|
+
id: action.id ?? String(index), label: action.label ?? action.text ?? '', icon: action.icon ?? null,
|
|
293
|
+
kind: actionKind(action.kind ?? action.type ?? (action.primary ? 'primary' : 'default')),
|
|
294
|
+
type: action.submit ? 'submit' : (action.type === 'reset' ? 'reset' : 'button'),
|
|
295
|
+
onClick: action.onClick ?? action.onclick ?? action.click
|
|
296
|
+
}));
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** @param {unknown} type @returns {'danger'|'warning'|'success'|'default'} */
|
|
300
|
+
function highlightKind(type) {
|
|
301
|
+
const value = String(type ?? '').toLowerCase();
|
|
302
|
+
if (value === 'error' || value === 'danger') return 'danger';
|
|
303
|
+
if (value === 'success' || value === 'primary') return 'success';
|
|
304
|
+
if (value === 'warning') return 'warning';
|
|
305
|
+
return 'default';
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** @param {unknown} kind @returns {'default'|'primary'|'danger'|'ghost'} */
|
|
309
|
+
function actionKind(kind) {
|
|
310
|
+
const value = String(kind ?? '').toLowerCase();
|
|
311
|
+
if (value === 'error' || value === 'danger') return 'danger';
|
|
312
|
+
if (value === 'primary' || value === 'success') return 'primary';
|
|
313
|
+
if (value === 'ghost' || value === 'link') return 'ghost';
|
|
314
|
+
return 'default';
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** @param {unknown} value @returns {unknown[]} */
|
|
318
|
+
function parseValues(value) {
|
|
319
|
+
if (Array.isArray(value)) return value;
|
|
320
|
+
if (!value) return [];
|
|
321
|
+
try { const parsed = JSON.parse(String(value)); return Array.isArray(parsed) ? parsed : []; } catch { return []; }
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** @param {unknown} value @returns {unknown} */
|
|
325
|
+
function normalizeFormDate(value) {
|
|
326
|
+
if (value instanceof Date || value == null) return value;
|
|
327
|
+
if (typeof value === 'number') return new Date(value * (Math.abs(value) < 1e12 ? 1000 : 1));
|
|
328
|
+
const date = new Date(String(value));
|
|
329
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
330
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { parse } from '../globals.js';
|
|
2
|
+
|
|
3
|
+
/** @param {Element|string|null|undefined} target @returns {Element|null} */
|
|
4
|
+
export function targetElement(target) {
|
|
5
|
+
if (target == null) return null;
|
|
6
|
+
if (typeof target === 'string') return document.querySelector(target);
|
|
7
|
+
return target?.nodeType === 1 ? target : null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** @param {unknown} value @returns {unknown} */
|
|
11
|
+
export function legacyContent(value) {
|
|
12
|
+
if (value == null || typeof value === 'string' || typeof value === 'number') return value;
|
|
13
|
+
if (value?.nodeType || typeof value?.toElement === 'function' || typeof value === 'function') return value;
|
|
14
|
+
return parse(value) || String(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @param {unknown} value @returns {'info'|'success'|'warning'|'error'} */
|
|
18
|
+
export function messageKind(value) {
|
|
19
|
+
const name = String(value ?? '').toLowerCase().replace(/^s_msg_32_/, '').replace(/^icon-/, '');
|
|
20
|
+
if (name.includes('error') || name.includes('danger') || name.includes('remove')) return 'error';
|
|
21
|
+
if (name.includes('success') || name.includes('ok') || name.includes('check')) return 'success';
|
|
22
|
+
if (name.includes('warn')) return 'warning';
|
|
23
|
+
return 'info';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @param {Function} fn @param {string} legacyName @param {string} replacement @returns {void} */
|
|
27
|
+
export function warnFunctionOnce(fn, legacyName, replacement) {
|
|
28
|
+
if (Object.hasOwn(fn, '_compatWarned')) return;
|
|
29
|
+
Object.defineProperty(fn, '_compatWarned', { value: true });
|
|
30
|
+
console.warn(`${legacyName} is running on the Zx compat layer — migrate to zx.${replacement}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @param {Function} owner @param {string} method @param {string} replacement @returns {void} */
|
|
34
|
+
export function warnMethodOnce(owner, method, replacement) {
|
|
35
|
+
const key = `_compatMethod_${method}`;
|
|
36
|
+
if (Object.hasOwn(owner, key)) return;
|
|
37
|
+
Object.defineProperty(owner, key, { value: true });
|
|
38
|
+
console.warn(`${owner.legacyName}.${method} has no direct Zx equivalent; use ${replacement}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @param {unknown} value @returns {string} */
|
|
42
|
+
export function textLabel(value) {
|
|
43
|
+
if (value == null) return '';
|
|
44
|
+
if (typeof value === 'string' || typeof value === 'number') return String(value);
|
|
45
|
+
return String(value.label ?? value.title ?? value.text ?? value.name ?? value.value ?? '');
|
|
46
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { Http, zeyosService } from '../../index.js';
|
|
2
|
+
import { GxWrapper } from '../base.js';
|
|
3
|
+
|
|
4
|
+
/** Legacy generic HTTP client. */
|
|
5
|
+
export class Client extends GxWrapper {
|
|
6
|
+
static legacyName = 'gx.zeyos.Client';
|
|
7
|
+
|
|
8
|
+
/** @param {Record<string, any>} [options={}] */
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
super(options, { url: './remotecall.php' });
|
|
11
|
+
this._http = new Http({ base: String(options.url ?? './remotecall.php'), headers: options.headers ?? {}, timeout: options.timeout ?? 30000 });
|
|
12
|
+
}
|
|
13
|
+
/** @param {string} path @param {unknown} data @param {Function|Record<string, Function>} [callback] @param {string} [resulttype] @returns {Promise<unknown>} */
|
|
14
|
+
post(path, data, callback, resulttype) { return this.request(data, 'POST', callback, resulttype, path); }
|
|
15
|
+
/** @param {string} path @param {unknown} data @param {Function|Record<string, Function>} [callback] @param {string} [resulttype] @returns {Promise<unknown>} */
|
|
16
|
+
get(path, data, callback, resulttype) { return this.request(data, 'GET', callback, resulttype, path); }
|
|
17
|
+
/** @param {unknown} data @param {string} [method='GET'] @param {Function|Record<string, Function>} [callback] @param {string} [resulttype] @param {string} [path=''] @returns {Promise<unknown>} */
|
|
18
|
+
async request(data, method = 'GET', callback, resulttype, path = '') {
|
|
19
|
+
registerCallbackEvents(this, callback);
|
|
20
|
+
this.fireEvent('request');
|
|
21
|
+
try {
|
|
22
|
+
const normalizedMethod = String(method).toUpperCase();
|
|
23
|
+
const result = await this._http.request(String(path ?? ''), normalizedMethod === 'GET'
|
|
24
|
+
? { method: normalizedMethod, query: data }
|
|
25
|
+
: { method: normalizedMethod, data });
|
|
26
|
+
if (resulttype && legacyType(result) !== resulttype) throw new TypeError(`Invalid server response: expected ${resulttype}`);
|
|
27
|
+
if (typeof callback === 'function') callback.call(this, result);
|
|
28
|
+
this.fireEvent('success', [result]);
|
|
29
|
+
return result;
|
|
30
|
+
} catch (error) {
|
|
31
|
+
this.fireEvent('failure', [error]);
|
|
32
|
+
throw error;
|
|
33
|
+
} finally {
|
|
34
|
+
this.fireEvent('complete');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Legacy ZeyOS REST request client. */
|
|
40
|
+
export class Request extends GxWrapper {
|
|
41
|
+
static legacyName = 'gx.zeyos.Request';
|
|
42
|
+
|
|
43
|
+
/** @param {Record<string, any>} [options={}] */
|
|
44
|
+
constructor(options = {}) {
|
|
45
|
+
super(options, { service: false, accesskey: false });
|
|
46
|
+
this._files = null;
|
|
47
|
+
this.setService(options.service ?? '', options.accesskey ?? '');
|
|
48
|
+
if (typeof options.showError === 'function') this.showError = options.showError;
|
|
49
|
+
}
|
|
50
|
+
/** @param {string} service @param {string} [accesskey=''] @returns {this} */
|
|
51
|
+
setService(service, accesskey = '') {
|
|
52
|
+
this.options.service = service;
|
|
53
|
+
this.options.accesskey = accesskey;
|
|
54
|
+
this.baseUrl = `${this.options.apiBase ?? '../remotecall/'}${service}${accesskey ? `:${accesskey}` : ''}/`;
|
|
55
|
+
this._http = zeyosService(String(service), String(accesskey), {
|
|
56
|
+
apiBase: this.options.apiBase ?? '../remotecall/', headers: this.options.headers ?? {}, timeout: this.options.timeout ?? 30000
|
|
57
|
+
});
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/** @param {string} path @param {unknown} data @param {Function} [callback] @param {string} [method='GET'] @returns {Promise<unknown>} */
|
|
61
|
+
async send(path, data, callback, method = 'GET') {
|
|
62
|
+
this.fireEvent('request');
|
|
63
|
+
try {
|
|
64
|
+
const verb = String(method ?? 'GET').toUpperCase();
|
|
65
|
+
const request = { method: verb };
|
|
66
|
+
if (verb === 'GET') request.query = data;
|
|
67
|
+
else request.data = data;
|
|
68
|
+
if (this._files) request.files = this._files;
|
|
69
|
+
const result = await this._http.request(String(path ?? ''), request);
|
|
70
|
+
this._files = null;
|
|
71
|
+
this.fireEvent('success', [result]);
|
|
72
|
+
callback?.call(this, result, '200 OK');
|
|
73
|
+
return result;
|
|
74
|
+
} catch (caught) {
|
|
75
|
+
this._files = null;
|
|
76
|
+
const error = caught instanceof Error ? caught : new Error(String(caught));
|
|
77
|
+
this.fireEvent('failure', [error]);
|
|
78
|
+
this.fireEvent('error', [error.message, error.response ?? null, error.response?.headers ?? null]);
|
|
79
|
+
this.fireEvent('exception', [error, null]);
|
|
80
|
+
this.showError?.(error.message);
|
|
81
|
+
throw error;
|
|
82
|
+
} finally {
|
|
83
|
+
this.fireEvent('complete');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** @param {string} path @param {unknown} data @param {Function} [callback] @returns {Promise<unknown>} */ post(path, data, callback) { return this.send(path, data, callback, 'POST'); }
|
|
87
|
+
/** @param {string} path @param {unknown} data @param {Function} [callback] @returns {Promise<unknown>} */ get(path, data, callback) { return this.send(path, data, callback, 'GET'); }
|
|
88
|
+
/** @param {string} path @param {unknown} data @param {Function} [callback] @returns {Promise<unknown>} */ put(path, data, callback) { return this.send(path, data, callback, 'PUT'); }
|
|
89
|
+
/** @param {string} path @param {unknown} data @param {Function} [callback] @returns {Promise<unknown>} */ delete(path, data, callback) { return this.send(path, data, callback, 'DELETE'); }
|
|
90
|
+
/** @param {string} path @param {unknown} data @param {File[]|FileList|Element[]} files @param {Function} [callback] @returns {Promise<unknown>} */
|
|
91
|
+
upload(path, data, files, callback) { this._files = normalizeFiles(files); return this.send(path, data, callback, 'POST'); }
|
|
92
|
+
/** @param {string} path @returns {Window|null} */ openLink(path) { return window.open(this.baseUrl + String(path ?? ''), '_blank'); }
|
|
93
|
+
/** @param {string} error @returns {void} */ showError(error) { console.error(error); }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** @param {GxWrapper} wrapper @param {Function|Record<string, Function>|undefined} callbacks @returns {void} */
|
|
97
|
+
function registerCallbackEvents(wrapper, callbacks) {
|
|
98
|
+
if (!callbacks || typeof callbacks !== 'object') return;
|
|
99
|
+
for (const [type, callback] of Object.entries(callbacks)) wrapper.addEvent(type, callback);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @param {unknown} value @returns {string} */
|
|
103
|
+
function legacyType(value) {
|
|
104
|
+
if (Array.isArray(value)) return 'array';
|
|
105
|
+
if (value === null) return 'null';
|
|
106
|
+
return typeof value === 'object' ? 'object' : typeof value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @param {File[]|FileList|Element[]|unknown} files @returns {File[]} */
|
|
110
|
+
function normalizeFiles(files) {
|
|
111
|
+
const values = files == null ? [] : Array.from(files);
|
|
112
|
+
return values.flatMap((value) => {
|
|
113
|
+
if (typeof File !== 'undefined' && value instanceof File) return [value];
|
|
114
|
+
if (value?.files) return Array.from(value.files);
|
|
115
|
+
return [];
|
|
116
|
+
});
|
|
117
|
+
}
|