@windwalker-io/unicorn-next 0.1.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/.editorconfig +18 -0
- package/.gulp.json +7 -0
- package/bin/release.mjs +47 -0
- package/dist/chunks/_arrayPush.js +168 -0
- package/dist/chunks/_arrayPush.js.map +1 -0
- package/dist/chunks/_baseRest.js +73 -0
- package/dist/chunks/_baseRest.js.map +1 -0
- package/dist/chunks/_commonjsHelpers.js +7 -0
- package/dist/chunks/_commonjsHelpers.js.map +1 -0
- package/dist/chunks/_getPrototype.js +130 -0
- package/dist/chunks/_getPrototype.js.map +1 -0
- package/dist/chunks/button-radio.js +147 -0
- package/dist/chunks/button-radio.js.map +1 -0
- package/dist/chunks/checkboxes-multi-select.js +44 -0
- package/dist/chunks/checkboxes-multi-select.js.map +1 -0
- package/dist/chunks/cloneDeep.js +287 -0
- package/dist/chunks/cloneDeep.js.map +1 -0
- package/dist/chunks/cropper.min.js +5 -0
- package/dist/chunks/cropper.min.js.map +1 -0
- package/dist/chunks/field-cascade-select.js +256 -0
- package/dist/chunks/field-cascade-select.js.map +1 -0
- package/dist/chunks/field-file-drag.js +218 -0
- package/dist/chunks/field-file-drag.js.map +1 -0
- package/dist/chunks/field-flatpickr.js +893 -0
- package/dist/chunks/field-flatpickr.js.map +1 -0
- package/dist/chunks/field-modal-select.js +403 -0
- package/dist/chunks/field-modal-select.js.map +1 -0
- package/dist/chunks/field-modal-tree.js +790 -0
- package/dist/chunks/field-modal-tree.js.map +1 -0
- package/dist/chunks/field-multi-uploader.js +256 -0
- package/dist/chunks/field-multi-uploader.js.map +1 -0
- package/dist/chunks/field-repeatable.js +132 -0
- package/dist/chunks/field-repeatable.js.map +1 -0
- package/dist/chunks/field-single-image-drag.js +338 -0
- package/dist/chunks/field-single-image-drag.js.map +1 -0
- package/dist/chunks/form.js +154 -0
- package/dist/chunks/form.js.map +1 -0
- package/dist/chunks/grid.js +345 -0
- package/dist/chunks/grid.js.map +1 -0
- package/dist/chunks/http-client.js +229 -0
- package/dist/chunks/http-client.js.map +1 -0
- package/dist/chunks/iframe-modal.js +124 -0
- package/dist/chunks/iframe-modal.js.map +1 -0
- package/dist/chunks/index.js +309 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/chunks/isArguments.js +146 -0
- package/dist/chunks/isArguments.js.map +1 -0
- package/dist/chunks/keep-tab.js +101 -0
- package/dist/chunks/keep-tab.js.map +1 -0
- package/dist/chunks/legacy.js +210 -0
- package/dist/chunks/legacy.js.map +1 -0
- package/dist/chunks/list-dependent.js +231 -0
- package/dist/chunks/list-dependent.js.map +1 -0
- package/dist/chunks/s3-multipart-uploader.js +172 -0
- package/dist/chunks/s3-multipart-uploader.js.map +1 -0
- package/dist/chunks/s3-uploader.js +136 -0
- package/dist/chunks/s3-uploader.js.map +1 -0
- package/dist/chunks/show-on.js +237 -0
- package/dist/chunks/show-on.js.map +1 -0
- package/dist/chunks/tinymce.js +196 -0
- package/dist/chunks/tinymce.js.map +1 -0
- package/dist/chunks/ui-bootstrap5.js +71 -0
- package/dist/chunks/ui-bootstrap5.js.map +1 -0
- package/dist/chunks/unicorn.js +2202 -0
- package/dist/chunks/unicorn.js.map +1 -0
- package/dist/chunks/validation.js +854 -0
- package/dist/chunks/validation.js.map +1 -0
- package/dist/editor.css +1 -0
- package/dist/index.d.ts +1427 -0
- package/dist/multi-level-menu.css +1 -0
- package/dist/switcher.css +1 -0
- package/dist/unicorn-next.css +12 -0
- package/dist/unicorn.js +125 -0
- package/dist/unicorn.js.map +1 -0
- package/fusionfile.mjs +155 -0
- package/images/ajax-loader.gif +0 -0
- package/images/placeholder/avatar.png +0 -0
- package/images/placeholder/image-16x10.png +0 -0
- package/images/placeholder/image-16x9.png +0 -0
- package/images/placeholder/image-1x1.png +0 -0
- package/images/placeholder/image-4x3.png +0 -0
- package/package.json +102 -0
- package/scss/bootstrap/multi-level-menu.scss +121 -0
- package/scss/editor.scss +116 -0
- package/scss/field/file-drag.scss +102 -0
- package/scss/field/single-image-drag.scss +88 -0
- package/scss/field/vue-drag-uploader.scss +160 -0
- package/scss/switcher.scss +156 -0
- package/src/app.ts +128 -0
- package/src/bootstrap/button-radio.ts +208 -0
- package/src/bootstrap/keep-tab.ts +155 -0
- package/src/composable/index.ts +21 -0
- package/src/composable/useCheckboxesMultiSelect.ts +22 -0
- package/src/composable/useFieldCascadeSelect.ts +9 -0
- package/src/composable/useFieldFileDrag.ts +9 -0
- package/src/composable/useFieldFlatpickr.ts +3 -0
- package/src/composable/useFieldModalSelect.ts +6 -0
- package/src/composable/useFieldModalTree.ts +3 -0
- package/src/composable/useFieldMultiUploader.ts +3 -0
- package/src/composable/useFieldRepeatable.ts +9 -0
- package/src/composable/useFieldSingleImageDrag.ts +5 -0
- package/src/composable/useForm.ts +43 -0
- package/src/composable/useGrid.ts +57 -0
- package/src/composable/useHttp.ts +8 -0
- package/src/composable/useIframeModal.ts +9 -0
- package/src/composable/useListDependent.ts +26 -0
- package/src/composable/useQueue.ts +13 -0
- package/src/composable/useS3Uploader.ts +32 -0
- package/src/composable/useShowOn.ts +9 -0
- package/src/composable/useStack.ts +13 -0
- package/src/composable/useTinymce.ts +29 -0
- package/src/composable/useTomSelect.ts +72 -0
- package/src/composable/useUIBootstrap5.ts +48 -0
- package/src/composable/useUniDirective.ts +32 -0
- package/src/composable/useValidation.ts +39 -0
- package/src/data.ts +36 -0
- package/src/events.ts +73 -0
- package/src/legacy/legacy.ts +186 -0
- package/src/legacy/loader.ts +125 -0
- package/src/module/checkboxes-multi-select.ts +54 -0
- package/src/module/field-cascade-select.ts +292 -0
- package/src/module/field-file-drag.ts +292 -0
- package/src/module/field-flatpickr.ts +127 -0
- package/src/module/field-modal-select.ts +174 -0
- package/src/module/field-modal-tree.ts +27 -0
- package/src/module/field-multi-uploader.ts +361 -0
- package/src/module/field-repeatable.ts +202 -0
- package/src/module/field-single-image-drag.ts +468 -0
- package/src/module/form.ts +223 -0
- package/src/module/grid.ts +465 -0
- package/src/module/http-client.ts +243 -0
- package/src/module/iframe-modal.ts +167 -0
- package/src/module/list-dependent.ts +321 -0
- package/src/module/s3-multipart-uploader.ts +300 -0
- package/src/module/s3-uploader.ts +234 -0
- package/src/module/show-on.ts +173 -0
- package/src/module/tinymce.ts +263 -0
- package/src/module/ui-bootstrap5.ts +107 -0
- package/src/module/validation.ts +1019 -0
- package/src/plugin/index.ts +1 -0
- package/src/plugin/php-adapter.ts +65 -0
- package/src/polyfill/form-request-submit.ts +31 -0
- package/src/polyfill/index.ts +9 -0
- package/src/service/animate.ts +58 -0
- package/src/service/crypto.ts +27 -0
- package/src/service/dom-watcher.ts +62 -0
- package/src/service/dom.ts +265 -0
- package/src/service/helper.ts +48 -0
- package/src/service/index.ts +10 -0
- package/src/service/lang.ts +122 -0
- package/src/service/loader.ts +152 -0
- package/src/service/router.ts +118 -0
- package/src/service/ui.ts +497 -0
- package/src/service/uri.ts +106 -0
- package/src/types/base.ts +9 -0
- package/src/types/index.ts +4 -0
- package/src/types/modal-tree.ts +12 -0
- package/src/types/plugin.ts +6 -0
- package/src/types/shims.d.ts +18 -0
- package/src/types/ui.ts +6 -0
- package/src/unicorn.ts +63 -0
- package/src/utilities/arr.ts +25 -0
- package/src/utilities/base.ts +9 -0
- package/src/utilities/data.ts +48 -0
- package/src/utilities/index.ts +5 -0
- package/src/utilities/tree.ts +20 -0
- package/src/vue/components/ModalTree/ModalTreeApp.vue +175 -0
- package/src/vue/components/ModalTree/TreeItem.vue +262 -0
- package/src/vue/components/ModalTree/TreeModal.vue +225 -0
- package/tests/test.js +4 -0
- package/tsconfig.js.json +25 -0
- package/tsconfig.json +17 -0
- package/vite.assets.config.ts +61 -0
- package/vite.config.test.ts +36 -0
- package/vite.config.ts +112 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './php-adapter';
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { UnicornApp } from '../app';
|
|
2
|
+
import {
|
|
3
|
+
useFieldCascadeSelect,
|
|
4
|
+
useFieldFileDrag,
|
|
5
|
+
useFieldFlatpickr,
|
|
6
|
+
useFieldModalSelect, useFieldModalTree,
|
|
7
|
+
useFieldRepeatable,
|
|
8
|
+
useFieldSingleImageDrag,
|
|
9
|
+
useIframeModal, useS3Uploader,
|
|
10
|
+
useShowOn, useTomSelect,
|
|
11
|
+
} from '../composable';
|
|
12
|
+
import { useFieldMultiUploader } from '../composable/useFieldMultiUploader';
|
|
13
|
+
import { useTinymce } from '../composable/useTinymce';
|
|
14
|
+
import { useUnicorn } from '../unicorn';
|
|
15
|
+
|
|
16
|
+
declare module '../app' {
|
|
17
|
+
export interface UnicornApp {
|
|
18
|
+
/** @deprecated Only for code generator use. */
|
|
19
|
+
$ui: typeof methods;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
declare module '@windwalker-io/unicorn-next' {
|
|
25
|
+
export interface UnicornApp {
|
|
26
|
+
/** @deprecated Only for code generator use. */
|
|
27
|
+
$ui: typeof methods;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function useUnicornPhpAdapter(app?: UnicornApp) {
|
|
32
|
+
app ??= useUnicorn();
|
|
33
|
+
|
|
34
|
+
app.use(UnicornPhpAdapter);
|
|
35
|
+
|
|
36
|
+
return app.$ui;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const methods = {
|
|
40
|
+
repeatable: useFieldRepeatable,
|
|
41
|
+
flatpickr: useFieldFlatpickr,
|
|
42
|
+
fileDrag: useFieldFileDrag,
|
|
43
|
+
modalField: useFieldModalSelect,
|
|
44
|
+
cascadeSelect: useFieldCascadeSelect,
|
|
45
|
+
sid: useFieldSingleImageDrag,
|
|
46
|
+
tinymce: {
|
|
47
|
+
init: useTinymce
|
|
48
|
+
},
|
|
49
|
+
s3Uploader: useS3Uploader,
|
|
50
|
+
iframeModal: useIframeModal,
|
|
51
|
+
initShowOn: useShowOn,
|
|
52
|
+
modalTree: useFieldModalTree,
|
|
53
|
+
multiUploader: useFieldMultiUploader,
|
|
54
|
+
tomSelect: useTomSelect,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export class UnicornPhpAdapter {
|
|
58
|
+
static install(app: UnicornApp) {
|
|
59
|
+
if (app.$ui) {
|
|
60
|
+
app.$ui = { ...app.$ui, ...methods };
|
|
61
|
+
} else {
|
|
62
|
+
app.$ui = methods;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// @see https://github.com/javan/form-request-submit-polyfill
|
|
3
|
+
export function formRequestSubmit(prototype) {
|
|
4
|
+
if (typeof prototype.requestSubmit == 'function') {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
prototype.requestSubmit = function (submitter) {
|
|
9
|
+
if (submitter) {
|
|
10
|
+
validateSubmitter(submitter, this);
|
|
11
|
+
submitter.click();
|
|
12
|
+
} else {
|
|
13
|
+
submitter = document.createElement('input');
|
|
14
|
+
submitter.type = 'submit';
|
|
15
|
+
submitter.hidden = true;
|
|
16
|
+
this.appendChild(submitter);
|
|
17
|
+
submitter.click();
|
|
18
|
+
this.removeChild(submitter);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function validateSubmitter(submitter, form) {
|
|
23
|
+
submitter instanceof HTMLElement || raise(TypeError, 'parameter 1 is not of type \'HTMLElement\'');
|
|
24
|
+
submitter.type == 'submit' || raise(TypeError, 'The specified element is not a submit button');
|
|
25
|
+
submitter.form == form || raise(DOMException, 'The specified element is not owned by this form element', 'NotFoundError');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function raise(errorConstructor, message, name) {
|
|
29
|
+
throw new errorConstructor('Failed to execute \'requestSubmit\' on \'HTMLFormElement\': ' + message + '.', name);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { selectOne } from './dom';
|
|
2
|
+
|
|
3
|
+
export function animateTo(
|
|
4
|
+
element: HTMLElement,
|
|
5
|
+
styles: Partial<Record<keyof CSSStyleDeclaration, any>>,
|
|
6
|
+
options: number | KeyframeAnimationOptions = {}
|
|
7
|
+
): Animation {
|
|
8
|
+
element = selectOne(element);
|
|
9
|
+
|
|
10
|
+
const currentStyles = window.getComputedStyle(element);
|
|
11
|
+
const transitions: Record<string, any[]> = {};
|
|
12
|
+
|
|
13
|
+
for (const name in styles) {
|
|
14
|
+
const value = styles[name];
|
|
15
|
+
|
|
16
|
+
transitions[name] = Array.isArray(value)
|
|
17
|
+
? value
|
|
18
|
+
: [
|
|
19
|
+
currentStyles.getPropertyValue(name),
|
|
20
|
+
value
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (typeof options === 'number') {
|
|
25
|
+
options = { duration: options };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
options = Object.assign(
|
|
29
|
+
{
|
|
30
|
+
duration: 400,
|
|
31
|
+
easing: 'linear',
|
|
32
|
+
fill: 'both'
|
|
33
|
+
},
|
|
34
|
+
options
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const animation = element.animate(
|
|
38
|
+
transitions,
|
|
39
|
+
options
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
animation.addEventListener('finish', () => {
|
|
43
|
+
for (const name in styles) {
|
|
44
|
+
const value = styles[name];
|
|
45
|
+
|
|
46
|
+
element.style.setProperty(
|
|
47
|
+
name,
|
|
48
|
+
Array.isArray(value)
|
|
49
|
+
? value[value.length - 1]
|
|
50
|
+
: value
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
animation.cancel();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return animation;
|
|
58
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { uid, tid, randomBytes, randomBytesString } from '@lyrasoft/ts-toolkit/generic';
|
|
2
|
+
|
|
3
|
+
export function base64UrlEncode(string: string): string {
|
|
4
|
+
return btoa(String(string))
|
|
5
|
+
.replace(/\+/, '-')
|
|
6
|
+
.replace(new RegExp('\\/'), '_')
|
|
7
|
+
.replace(/=+$/, '');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Base64 URL decode
|
|
12
|
+
*/
|
|
13
|
+
export function base64UrlDecode(string: string): string {
|
|
14
|
+
return atob(
|
|
15
|
+
String(string)
|
|
16
|
+
.replace(/-/, '+')
|
|
17
|
+
.replace(/_/, '/')
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { uid, tid, randomBytes, randomBytesString };
|
|
22
|
+
|
|
23
|
+
let globalSerial = 1;
|
|
24
|
+
|
|
25
|
+
export function serial(): number {
|
|
26
|
+
return globalSerial++;
|
|
27
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export function watchAttributes<T extends HTMLElement>(el: T, callback?: AttributeMutationCallback<T>) {
|
|
2
|
+
return new AttributeMutationObserver<T>(el, callback);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export type AttributeMutationCallback<T extends HTMLElement = HTMLElement> = (el: T, name: string, value: any, oldValue: any) => void;
|
|
6
|
+
export type AttributeWatcher<T extends HTMLElement = HTMLElement> = (el: T, value: any, oldValue: any) => void;
|
|
7
|
+
|
|
8
|
+
export class AttributeMutationObserver<T extends HTMLElement> {
|
|
9
|
+
observer: MutationObserver;
|
|
10
|
+
watches: Record<string, (AttributeWatcher<T>)[]> = {};
|
|
11
|
+
|
|
12
|
+
constructor(protected element: T, public callback?: (el: T, name: string, value: any, oldValue: any) => void) {
|
|
13
|
+
this.element = element;
|
|
14
|
+
|
|
15
|
+
this.observer = new MutationObserver((mutations) => {
|
|
16
|
+
for (const mutation of mutations) {
|
|
17
|
+
if (mutation.type === 'attributes') {
|
|
18
|
+
const attrName = mutation.attributeName!;
|
|
19
|
+
const target = mutation.target as T;
|
|
20
|
+
const value = target.getAttribute(attrName);
|
|
21
|
+
|
|
22
|
+
this.callback?.(
|
|
23
|
+
target,
|
|
24
|
+
attrName,
|
|
25
|
+
value,
|
|
26
|
+
mutation.oldValue
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
if (this.watches[attrName]) {
|
|
30
|
+
for (const watch of this.watches[attrName]) {
|
|
31
|
+
watch(target, value, mutation.oldValue);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
this.observe();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
watch(name: string, callback: AttributeWatcher<T>): () => void {
|
|
42
|
+
this.watches[name] ??= [];
|
|
43
|
+
this.watches[name].push(callback);
|
|
44
|
+
|
|
45
|
+
return () => {
|
|
46
|
+
this.watches[name] = this.watches[name].filter(fn => fn !== callback);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
observe() {
|
|
51
|
+
this.observer.observe(this.element, {
|
|
52
|
+
attributes: true,
|
|
53
|
+
attributeOldValue: true,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
disconnect() {
|
|
58
|
+
this.observer.disconnect();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { defData } from '../utilities';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @see https://stackoverflow.com/a/9899701
|
|
5
|
+
*/
|
|
6
|
+
export function domready(callback?: ((value: any) => any)): Promise<void> {
|
|
7
|
+
let promise = new Promise<void>((resolve) => {
|
|
8
|
+
// see if DOM is already available
|
|
9
|
+
if (document.readyState === 'complete' || document.readyState === 'interactive') {
|
|
10
|
+
// call on next available tick
|
|
11
|
+
setTimeout(resolve, 0);
|
|
12
|
+
} else {
|
|
13
|
+
document.addEventListener('DOMContentLoaded', () => resolve());
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
if (callback) {
|
|
18
|
+
promise = promise.then(callback);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return promise;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function selectOne<K extends keyof HTMLElementTagNameMap>(ele: K): HTMLElementTagNameMap[K] | null;
|
|
25
|
+
export function selectOne<E extends Element = Element>(ele: string): E | null;
|
|
26
|
+
export function selectOne<E extends Element = Element>(ele: E): E;
|
|
27
|
+
export function selectOne<E extends Element = Element>(ele: string | E): E | null;
|
|
28
|
+
// selectOne(ele: string): Element;
|
|
29
|
+
export function selectOne<E extends Element = Element>(ele: E | string): E | null {
|
|
30
|
+
let r: E | null;
|
|
31
|
+
|
|
32
|
+
if (typeof ele === 'string') {
|
|
33
|
+
r = document.querySelector<E>(ele);
|
|
34
|
+
} else {
|
|
35
|
+
r = ele;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!r) {
|
|
39
|
+
return r;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return r;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function selectAll<E extends Element = Element>(ele: string, callback?: ((ele: E) => any)): E[];
|
|
46
|
+
export function selectAll<E extends Element = Element>(ele: NodeListOf<E> | E[], callback?: ((ele: E) => any)): E[];
|
|
47
|
+
export function selectAll<E extends Element = Element>(
|
|
48
|
+
ele: string | NodeListOf<E> | E[],
|
|
49
|
+
callback?: ((ele: E) => any)
|
|
50
|
+
): E[];
|
|
51
|
+
export function selectAll<E extends keyof HTMLElementTagNameMap>(
|
|
52
|
+
ele: E,
|
|
53
|
+
callback?: ((ele: HTMLElementTagNameMap[E]) => any)
|
|
54
|
+
): HTMLElementTagNameMap[E][];
|
|
55
|
+
export function selectAll(
|
|
56
|
+
ele: NodeListOf<Element> | Element[] | string,
|
|
57
|
+
callback: ((el: Element) => any) | undefined = undefined
|
|
58
|
+
): Element[] {
|
|
59
|
+
if (typeof ele === 'string') {
|
|
60
|
+
ele = document.querySelectorAll(ele);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const resultSet: Element[] = [].slice.call(ele);
|
|
64
|
+
|
|
65
|
+
if (callback) {
|
|
66
|
+
return resultSet.map((el) => callback(el) || el);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return resultSet;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function getBoundedInstance<T = any, E = Element>(selector: E, name: string, callback?: ((el: E) => any)): T;
|
|
73
|
+
export function getBoundedInstance<T = any, E extends Element = Element>(
|
|
74
|
+
selector: string | E,
|
|
75
|
+
name: string,
|
|
76
|
+
callback?: ((el: E) => any)
|
|
77
|
+
): T | null;
|
|
78
|
+
export function getBoundedInstance<T = any, E extends Element = Element>(selector: string | E,
|
|
79
|
+
name: string,
|
|
80
|
+
callback: ((el: E) => any) = () => null): T | null {
|
|
81
|
+
const element = typeof selector === 'string' ? document.querySelector<E>(selector) : selector;
|
|
82
|
+
|
|
83
|
+
if (!element) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return defData(element, name, callback);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function getBoundedInstanceList<T = any, E extends Element = Element>(
|
|
91
|
+
selector: string | NodeListOf<E>,
|
|
92
|
+
name: string,
|
|
93
|
+
callback: ((el: E) => any) = () => null
|
|
94
|
+
): (T | null)[] {
|
|
95
|
+
const items = typeof selector === 'string' ? document.querySelectorAll<E>(selector) : selector;
|
|
96
|
+
|
|
97
|
+
return Array.from(items).map((ele: E) => getBoundedInstance(ele, name, callback));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function module<T = any, E extends Element = Element>(
|
|
101
|
+
ele: string,
|
|
102
|
+
name: string,
|
|
103
|
+
callback?: ((el: E) => any)
|
|
104
|
+
): (T | null)[];
|
|
105
|
+
export function module<T = any, E extends Element = Element>(
|
|
106
|
+
ele: NodeListOf<Element>,
|
|
107
|
+
name: string,
|
|
108
|
+
callback?: ((el: E) => any)): (T | null)[];
|
|
109
|
+
export function module<T = any, E extends Element = Element>(
|
|
110
|
+
ele: Element,
|
|
111
|
+
name: string,
|
|
112
|
+
callback?: ((el: E) => any)
|
|
113
|
+
): T | null;
|
|
114
|
+
export function module<T = any, E extends Element = Element>(
|
|
115
|
+
ele: string | Element | NodeListOf<Element>,
|
|
116
|
+
name: string,
|
|
117
|
+
callback?: ((el: E) => any)
|
|
118
|
+
): (T | null)[] | T | null;
|
|
119
|
+
export function module<T = any, E extends Element = Element>(
|
|
120
|
+
ele: string | E | NodeListOf<E>,
|
|
121
|
+
name: string,
|
|
122
|
+
callback: ((el: E) => any) = () => null
|
|
123
|
+
): (T | null)[] | T | null {
|
|
124
|
+
if (typeof ele === 'string') {
|
|
125
|
+
return getBoundedInstanceList<T, E>(ele, name, callback);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (ele instanceof HTMLElement) {
|
|
129
|
+
return getBoundedInstance<T, E>(ele, name, callback);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return getBoundedInstanceList<T, E>(ele as NodeListOf<E>, name, callback);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function h<T extends keyof HTMLElementTagNameMap>(
|
|
136
|
+
element: T,
|
|
137
|
+
attrs?: Record<string, any>,
|
|
138
|
+
content?: any
|
|
139
|
+
): HTMLElementTagNameMap[T]
|
|
140
|
+
export function h(element: string, attrs: Record<string, any> = {}, content: any = undefined): HTMLElement {
|
|
141
|
+
const ele = document.createElement(element);
|
|
142
|
+
|
|
143
|
+
for (let i in attrs) {
|
|
144
|
+
const v = attrs[i];
|
|
145
|
+
|
|
146
|
+
ele.setAttribute(i, v);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (content !== null) {
|
|
150
|
+
ele.innerHTML = content;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return ele;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function html<T extends Element = HTMLElement>(html: string): T {
|
|
157
|
+
const div = document.createElement('div');
|
|
158
|
+
div.innerHTML = html;
|
|
159
|
+
return div.children[0] as T;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Pure JS version of jQuery delegate()
|
|
164
|
+
*
|
|
165
|
+
* @see https://gist.github.com/iagobruno/4db2ed62dc40fa841bb9a5c7de92f5f8
|
|
166
|
+
*/
|
|
167
|
+
export function delegate(
|
|
168
|
+
wrapper: Element | string,
|
|
169
|
+
selector: string,
|
|
170
|
+
eventName: string,
|
|
171
|
+
callback: (e: Event) => void
|
|
172
|
+
): () => void {
|
|
173
|
+
if (typeof selector === 'undefined' || selector === '') {
|
|
174
|
+
throw new Error('The provided selector is empty.');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (typeof callback === 'undefined' || typeof callback !== 'function') {
|
|
178
|
+
throw new Error('Please specify an callback.');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const delegationSelectorsMap: Record<string, Function[]> = {};
|
|
182
|
+
|
|
183
|
+
const wrapperElement = selectOne(wrapper);
|
|
184
|
+
|
|
185
|
+
wrapperElement?.addEventListener(eventName, function (event) {
|
|
186
|
+
let element: HTMLElement | null = event.target as HTMLElement;
|
|
187
|
+
let forceBreak = false;
|
|
188
|
+
|
|
189
|
+
while (element && element !== wrapperElement) {
|
|
190
|
+
for (const selector in delegationSelectorsMap) {
|
|
191
|
+
if (element.matches(selector)) {
|
|
192
|
+
event.stopPropagation = function () {
|
|
193
|
+
forceBreak = true;
|
|
194
|
+
};
|
|
195
|
+
Object.defineProperty(
|
|
196
|
+
event,
|
|
197
|
+
'currentTarget',
|
|
198
|
+
{
|
|
199
|
+
get() {
|
|
200
|
+
return element;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const callbackList = delegationSelectorsMap[selector];
|
|
206
|
+
|
|
207
|
+
callbackList.forEach(function (callback) {
|
|
208
|
+
callback(event);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (forceBreak) {
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
element = element.parentElement;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
if (!delegationSelectorsMap[selector]) {
|
|
222
|
+
// Add new selector to the list
|
|
223
|
+
delegationSelectorsMap[selector] = [callback];
|
|
224
|
+
} else {
|
|
225
|
+
delegationSelectorsMap[selector].push(callback);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return function unsubscribe() {
|
|
229
|
+
if (!delegationSelectorsMap[selector]) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (delegationSelectorsMap[selector].length >= 2) {
|
|
234
|
+
delegationSelectorsMap[selector] = delegationSelectorsMap[selector].filter(cb => cb !== callback);
|
|
235
|
+
} else {
|
|
236
|
+
delete delegationSelectorsMap[selector];
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function injectCssToDocument(doc: Document, ...css: (string | CSSStyleSheet)[]): CSSStyleSheet[];
|
|
242
|
+
export function injectCssToDocument(...css: (string | CSSStyleSheet)[]): CSSStyleSheet[];
|
|
243
|
+
export function injectCssToDocument(
|
|
244
|
+
doc: Document | string | CSSStyleSheet,
|
|
245
|
+
...css: (string | CSSStyleSheet)[]
|
|
246
|
+
): CSSStyleSheet[] {
|
|
247
|
+
if (!(doc instanceof Document)) {
|
|
248
|
+
css.push(doc);
|
|
249
|
+
doc = document;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const styles = css.map((css) => {
|
|
253
|
+
if (typeof css === 'string') {
|
|
254
|
+
const style = new CSSStyleSheet();
|
|
255
|
+
style.replaceSync(css);
|
|
256
|
+
return style;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return css;
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
doc.adoptedStyleSheets = [...doc.adoptedStyleSheets, ...styles];
|
|
263
|
+
|
|
264
|
+
return styles;
|
|
265
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { data } from '../data';
|
|
2
|
+
import { sleep } from '@lyrasoft/ts-toolkit/generic';
|
|
3
|
+
|
|
4
|
+
export { sleep };
|
|
5
|
+
|
|
6
|
+
export function forceArray<T>(item: T | T[]): T[] {
|
|
7
|
+
if (Array.isArray(item)) {
|
|
8
|
+
return item;
|
|
9
|
+
} else {
|
|
10
|
+
return [item];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function debounce<T extends Function = Function>(handler: T, wait = 1): T {
|
|
15
|
+
let timer: ReturnType<typeof setTimeout> | number, result: any;
|
|
16
|
+
return function (this: any, ...args: any[]) {
|
|
17
|
+
clearTimeout(timer);
|
|
18
|
+
timer = setTimeout(() => result = handler.call(this, ...args), wait);
|
|
19
|
+
return result;
|
|
20
|
+
} as any as T;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function throttle<T extends Function = Function>(handler: T, wait: number = 1): T {
|
|
24
|
+
let timer: ReturnType<typeof setTimeout> | number | undefined, result: any;
|
|
25
|
+
return function (this: any, ...args: any[]) {
|
|
26
|
+
if (!timer) {
|
|
27
|
+
return result = handler.call(this, ...args);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
clearTimeout(timer);
|
|
31
|
+
timer = setTimeout(() => timer = undefined, wait);
|
|
32
|
+
return result;
|
|
33
|
+
} as any as T;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isDebug() {
|
|
37
|
+
return Boolean(data('windwalker.debug'));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function nextTick(callback?: () => any): Promise<any> {
|
|
41
|
+
return Promise.resolve().then(callback ?? (() => null));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function wait<T extends readonly unknown[]>(
|
|
45
|
+
...promisee: { [K in keyof T]: PromiseLike<T[K]> | T[K] }
|
|
46
|
+
): Promise<Awaited<T>> {
|
|
47
|
+
return Promise.all(promisee) as Promise<Awaited<T>>;
|
|
48
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './animate';
|
|
2
|
+
export * from './crypto';
|
|
3
|
+
export * from './dom';
|
|
4
|
+
export * from './dom-watcher';
|
|
5
|
+
export * from './helper';
|
|
6
|
+
export * from './lang';
|
|
7
|
+
export * from './loader';
|
|
8
|
+
export * from './ui';
|
|
9
|
+
export * from './uri';
|
|
10
|
+
export * from './router';
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { data } from '../data';
|
|
2
|
+
import { isDebug } from './/helper';
|
|
3
|
+
import { Dictionary } from '../types';
|
|
4
|
+
import { vsprintf } from 'sprintf-js';
|
|
5
|
+
|
|
6
|
+
let lang: UnicornLang;
|
|
7
|
+
|
|
8
|
+
export function useLang() {
|
|
9
|
+
return lang ??= new UnicornLang();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function trans(id: string, ...args: any[]) {
|
|
13
|
+
return useLang().trans(id, ...args);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function __(id: string, ...args: any[]) {
|
|
17
|
+
return trans(id, ...args);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default class UnicornLang {
|
|
21
|
+
/**
|
|
22
|
+
* Translate a string.
|
|
23
|
+
*/
|
|
24
|
+
trans(id: string, ...args: any[]): string {
|
|
25
|
+
const key = this.normalize(id);
|
|
26
|
+
|
|
27
|
+
let translated = this.get(key) || '';
|
|
28
|
+
|
|
29
|
+
translated = this.replace(translated, args);
|
|
30
|
+
|
|
31
|
+
return translated !== '' ? translated : this.wrapDebug(id, false);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
protected replace(str: string, args: any[]): string {
|
|
35
|
+
let replacements: Dictionary<any> = {};
|
|
36
|
+
let values: any[] = [];
|
|
37
|
+
|
|
38
|
+
for (const arg of args) {
|
|
39
|
+
if (typeof arg === 'object') {
|
|
40
|
+
replacements = { ...replacements, ...arg };
|
|
41
|
+
} else {
|
|
42
|
+
values.push(arg);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (values.length) {
|
|
47
|
+
str = vsprintf(str, values);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (Object.values(replacements).length) {
|
|
51
|
+
for (const key in replacements) {
|
|
52
|
+
let value = replacements[key];
|
|
53
|
+
|
|
54
|
+
if (typeof value === 'function') {
|
|
55
|
+
value = value();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
str = str.replace(new RegExp(':' + key, 'g'), String(value));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return str;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Find text.
|
|
67
|
+
*/
|
|
68
|
+
get(id: string): string | null {
|
|
69
|
+
const strings = this.getStrings();
|
|
70
|
+
|
|
71
|
+
if (strings[id]) {
|
|
72
|
+
return strings[id];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Has language key.
|
|
80
|
+
*/
|
|
81
|
+
has(key: string): boolean {
|
|
82
|
+
const strings = this.getStrings();
|
|
83
|
+
|
|
84
|
+
return strings[key] !== undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Add language key.
|
|
89
|
+
*/
|
|
90
|
+
add(key: string, value: string): this {
|
|
91
|
+
const strings = this.getStrings();
|
|
92
|
+
|
|
93
|
+
strings[this.normalize(key)] = value;
|
|
94
|
+
|
|
95
|
+
data('unicorn.languages', strings);
|
|
96
|
+
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Replace all symbols to dot(.).
|
|
102
|
+
*/
|
|
103
|
+
protected normalize(text: string): string {
|
|
104
|
+
return text.replace(/[^A-Z0-9]+/ig, '.');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
protected wrapDebug(text: string, success: boolean): string {
|
|
108
|
+
if (isDebug()) {
|
|
109
|
+
if (success) {
|
|
110
|
+
return '**' + text + '**';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return '??' + text + '??';
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return text;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
getStrings(): Record<string, string> {
|
|
120
|
+
return data('unicorn.languages') || {};
|
|
121
|
+
}
|
|
122
|
+
}
|