adt-js-components 1.11.2 → 2.0.0
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/package.json +1 -1
- package/src/ComponentLoader.js +53 -11
- package/src/DateInput/index.js +18 -2
- package/src/Notifications/index.js +12 -5
- package/src/Select2/_init.js +17 -0
- package/src/Select2/index.js +2 -2
- package/src/SubmitForm/index.js +1 -1
package/package.json
CHANGED
package/src/ComponentLoader.js
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
const loadedComponents = [];
|
|
2
2
|
let componentsConfig = {};
|
|
3
3
|
|
|
4
|
+
// Component module resolution (Vite only).
|
|
5
|
+
//
|
|
6
|
+
// Rollup/Vite cannot statically analyse concatenated dynamic imports, so callers
|
|
7
|
+
// register `import.meta.glob()` maps that this loader resolves from. Maps are
|
|
8
|
+
// MERGED per scope, so multiple callers can contribute — e.g. FancyAdmin registers
|
|
9
|
+
// its own components ('fancyadmin'), while the consuming app registers its own
|
|
10
|
+
// ('app') and the built-ins it uses ('builtin'):
|
|
11
|
+
// AdtJsComponents.registerModules({
|
|
12
|
+
// app: import.meta.glob('.../app/UI/**/index.js'),
|
|
13
|
+
// builtin: import.meta.glob('.../node_modules/adt-js-components/src/{Select2,...}/index.js'),
|
|
14
|
+
// });
|
|
15
|
+
const registeredModules = { fancyadmin: {}, app: {}, builtin: {} };
|
|
16
|
+
|
|
17
|
+
const registerModules = (maps) => {
|
|
18
|
+
for (const scope of Object.keys(maps)) {
|
|
19
|
+
registeredModules[scope] = Object.assign(registeredModules[scope] || {}, maps[scope]);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// Find the loader in a glob map whose key resolves to the requested sub-path.
|
|
24
|
+
// Match on a leading-slash boundary so e.g. "Map" never matches "Sitemap".
|
|
25
|
+
const resolveFromMap = (map, subPath) => {
|
|
26
|
+
if (!map) return null;
|
|
27
|
+
const needle = subPath + '/index.js';
|
|
28
|
+
const key = Object.keys(map).find((k) => k === needle || k.endsWith('/' + needle));
|
|
29
|
+
return key ? map[key] : null;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Returns a Promise<module> for the given component path, resolved from the
|
|
33
|
+
// registered import.meta.glob() maps.
|
|
34
|
+
const importComponent = (path) => {
|
|
35
|
+
let loader;
|
|
36
|
+
if (path.startsWith('~')) {
|
|
37
|
+
loader = resolveFromMap(registeredModules.fancyadmin, path.slice(1));
|
|
38
|
+
} else if (path.includes('/')) {
|
|
39
|
+
loader = resolveFromMap(registeredModules.app, path);
|
|
40
|
+
} else {
|
|
41
|
+
loader = resolveFromMap(registeredModules.builtin, path);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!loader) {
|
|
45
|
+
return Promise.reject(new Error(`adt-js-components: component "${path}" not found in registered modules.`));
|
|
46
|
+
}
|
|
47
|
+
return loader();
|
|
48
|
+
};
|
|
49
|
+
|
|
4
50
|
const init = (selector, path) => {
|
|
5
51
|
let bodyDataset = document.querySelector(`body`).dataset.adtJsComponents;
|
|
6
52
|
|
|
@@ -11,19 +57,14 @@ const init = (selector, path) => {
|
|
|
11
57
|
}
|
|
12
58
|
|
|
13
59
|
const runPath = (path, selector) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
component.default.run(componentsConfig[selector] || {});
|
|
17
|
-
});
|
|
18
|
-
} else if (path.includes('/')) {
|
|
19
|
-
import('JsComponents/' + path + '/index.js').then(component => {
|
|
60
|
+
importComponent(path)
|
|
61
|
+
.then(component => {
|
|
20
62
|
component.default.run(componentsConfig[selector] || {});
|
|
63
|
+
})
|
|
64
|
+
.catch(err => {
|
|
65
|
+
// Don't let a single missing/optional component break page init.
|
|
66
|
+
console.warn(`adt-js-components: failed to load component "${path}":`, err);
|
|
21
67
|
});
|
|
22
|
-
} else {
|
|
23
|
-
import('adt-js-components/src/' + path + '/index.js').then(component => {
|
|
24
|
-
component.default.run(componentsConfig[selector] || {});
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
68
|
};
|
|
28
69
|
|
|
29
70
|
const existingTarget = document.querySelector(`[data-adt-${selector}]`);
|
|
@@ -135,6 +176,7 @@ export const loadScssModule = (styles) => {
|
|
|
135
176
|
|
|
136
177
|
export default {
|
|
137
178
|
init,
|
|
179
|
+
registerModules,
|
|
138
180
|
initCurrencyInput,
|
|
139
181
|
initDateInput,
|
|
140
182
|
initGLightbox,
|
package/src/DateInput/index.js
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
import flatpickr from "flatpickr";
|
|
2
2
|
import 'flatpickr/dist/flatpickr.min.css';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// timepicker is a jQuery plugin attaching to the global $ (provided by the bundler).
|
|
5
|
+
// Plain side-effect import works in both webpack and Vite; the "script-loader!" prefix
|
|
6
|
+
// was webpack-only and unresolvable in Vite/Rollup.
|
|
7
|
+
import 'timepicker';
|
|
5
8
|
import 'timepicker/jquery.timepicker.min.css';
|
|
6
9
|
|
|
7
10
|
const locale = document.querySelector('html').getAttribute('lang');
|
|
8
11
|
|
|
12
|
+
// flatpickr locale modules, resolved lazily (Vite). Replaces the webpack-only
|
|
13
|
+
// dynamic require('flatpickr/dist/l10n/' + locale + '.js').
|
|
14
|
+
const flatpickrLocales = import.meta.glob('../../../flatpickr/dist/l10n/*.js');
|
|
15
|
+
|
|
16
|
+
async function loadFlatpickrLocale(loc) {
|
|
17
|
+
const key = Object.keys(flatpickrLocales).find((k) => k.endsWith(`/l10n/${loc}.js`));
|
|
18
|
+
if (!key) return null;
|
|
19
|
+
const mod = await flatpickrLocales[key]();
|
|
20
|
+
return mod.default[loc];
|
|
21
|
+
}
|
|
22
|
+
|
|
9
23
|
function initTime(input, options) {
|
|
10
24
|
$(input).timepicker({
|
|
11
25
|
scrollDefault: 'now',
|
|
@@ -24,11 +38,13 @@ async function initDate(input, options) {
|
|
|
24
38
|
options.locale = locale;
|
|
25
39
|
}
|
|
26
40
|
|
|
41
|
+
const localeData = options.locale ? await loadFlatpickrLocale(options.locale) : null;
|
|
42
|
+
|
|
27
43
|
flatpickr(input, {
|
|
28
44
|
dateFormat: options.format, // default datetime-local
|
|
29
45
|
enableTime: options.type === 'datetime' || options.type === 'datetime-local',
|
|
30
46
|
time_24hr: true,
|
|
31
|
-
locale:
|
|
47
|
+
locale: localeData,
|
|
32
48
|
defaultDate: options.value ? new Date(options.value) : null,
|
|
33
49
|
minDate: options.minDate ? new Date(options.minDate) : null,
|
|
34
50
|
maxDate: options.maxDate ? new Date(options.maxDate) : null,
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { getToken, deleteToken } from "firebase/messaging";
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const $disableBtn = $('[data-adt-notifications-disable]');
|
|
3
|
+
const ENABLE_SELECTOR = '[data-adt-notifications-enable]';
|
|
4
|
+
const DISABLE_SELECTOR = '[data-adt-notifications-disable]';
|
|
6
5
|
|
|
6
|
+
const run = (config) => {
|
|
7
7
|
const updateButtons = () => {
|
|
8
|
+
const $enableBtn = $(ENABLE_SELECTOR);
|
|
9
|
+
const $disableBtn = $(DISABLE_SELECTOR);
|
|
10
|
+
|
|
8
11
|
if (Notification.permission !== 'granted') {
|
|
9
12
|
$enableBtn.show();
|
|
10
13
|
$disableBtn.hide();
|
|
@@ -27,7 +30,7 @@ const run = (config) => {
|
|
|
27
30
|
};
|
|
28
31
|
|
|
29
32
|
// Enable notifications
|
|
30
|
-
$
|
|
33
|
+
$(document).on('click', ENABLE_SELECTOR, function () {
|
|
31
34
|
if (window.messaging) {
|
|
32
35
|
Notification.requestPermission().then(function (permission) {
|
|
33
36
|
if (permission !== 'granted') {
|
|
@@ -58,7 +61,7 @@ const run = (config) => {
|
|
|
58
61
|
});
|
|
59
62
|
|
|
60
63
|
// Disable notifications
|
|
61
|
-
$
|
|
64
|
+
$(document).on('click', DISABLE_SELECTOR, function () {
|
|
62
65
|
if (window.messaging) {
|
|
63
66
|
deleteToken(window.messaging)
|
|
64
67
|
.then(function () {
|
|
@@ -100,6 +103,10 @@ const run = (config) => {
|
|
|
100
103
|
} else {
|
|
101
104
|
updateButtons();
|
|
102
105
|
}
|
|
106
|
+
|
|
107
|
+
$(document).on('ajaxComplete', function () {
|
|
108
|
+
updateButtons();
|
|
109
|
+
});
|
|
103
110
|
}
|
|
104
111
|
|
|
105
112
|
export default { run };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// The select2 "full" UMD build resolves to its CommonJS branch under Vite/Rollup,
|
|
2
|
+
// which exports an uninvoked factory — a side-effect import alone does not attach
|
|
3
|
+
// $.fn.select2 (nor $.fn.select2.amd). Invoke the factory against the global jQuery.
|
|
4
|
+
// Namespace import avoids the static "default is not exported" error for the UMD file.
|
|
5
|
+
import $ from 'jquery';
|
|
6
|
+
import * as select2Full from 'select2/dist/js/select2.full';
|
|
7
|
+
|
|
8
|
+
const initSelect2 = select2Full.default || select2Full;
|
|
9
|
+
if (typeof initSelect2 === 'function') {
|
|
10
|
+
initSelect2(window, $);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// The i18n locale needs $.fn.select2.amd to already exist. A static import can't
|
|
14
|
+
// guarantee that — bundlers order modules by dependency, not source order, so a
|
|
15
|
+
// dependency-free locale file would evaluate before this one. A dynamic import runs
|
|
16
|
+
// here, after select2 is registered.
|
|
17
|
+
import('select2/dist/js/i18n/cs');
|
package/src/Select2/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
// Initializes $.fn.select2 against the global jQuery, then loads its i18n locale.
|
|
2
|
+
import './_init';
|
|
2
3
|
import 'select2/dist/css/select2.min.css';
|
|
3
4
|
import 'select2-bootstrap-5-theme/dist/select2-bootstrap-5-theme.css'
|
|
4
|
-
import 'select2/dist/js/i18n/cs'
|
|
5
5
|
|
|
6
6
|
function run(options) {
|
|
7
7
|
const noSelect2Class = '.select-default';
|
package/src/SubmitForm/index.js
CHANGED