@symbo.ls/utils 3.1.2 → 3.14.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/LICENSE +25 -12
- package/README.md +106 -0
- package/array.js +162 -0
- package/cache.js +7 -0
- package/cdn.js +83 -0
- package/component.js +25 -0
- package/cookie.js +71 -0
- package/detectHeight.js +42 -0
- package/dist/cjs/array.js +168 -0
- package/dist/cjs/browser.js +0 -2
- package/dist/cjs/cache.js +26 -0
- package/dist/cjs/cdn.js +85 -0
- package/dist/cjs/component.js +45 -0
- package/dist/cjs/cookie.js +75 -0
- package/dist/cjs/date.js +1 -3
- package/dist/cjs/detectHeight.js +22 -13
- package/dist/cjs/element.js +136 -0
- package/dist/cjs/env.js +43 -0
- package/dist/cjs/events.js +39 -0
- package/dist/cjs/extends.js +434 -0
- package/dist/cjs/fibonacci.js +1 -3
- package/dist/cjs/files.js +1 -3
- package/dist/cjs/function.js +114 -0
- package/dist/cjs/globals.js +30 -0
- package/dist/cjs/if.js +48 -0
- package/dist/cjs/index.js +38 -371
- package/dist/cjs/key.js +38 -0
- package/dist/cjs/keys.js +201 -0
- package/dist/cjs/load.js +67 -12
- package/dist/cjs/log.js +34 -0
- package/dist/cjs/metadata.js +310 -0
- package/dist/cjs/methods.js +599 -0
- package/dist/cjs/node.js +35 -0
- package/dist/cjs/object.js +634 -0
- package/dist/cjs/projectKeys.js +35 -0
- package/dist/cjs/props.js +282 -0
- package/dist/cjs/scaling.js +5 -169
- package/dist/cjs/scope.js +44 -0
- package/dist/cjs/sharedLibraries.js +146 -0
- package/dist/cjs/state.js +173 -0
- package/dist/cjs/string-extra.js +72 -0
- package/dist/cjs/string.js +183 -0
- package/dist/cjs/style.js +1 -3
- package/dist/cjs/tags.js +220 -0
- package/dist/cjs/triggerEvent.js +112 -0
- package/dist/cjs/types.js +74 -0
- package/dist/cjs/update.js +42 -0
- package/dist/esm/array.js +148 -0
- package/dist/esm/browser.js +14 -0
- package/dist/esm/cache.js +6 -0
- package/dist/esm/cdn.js +65 -0
- package/dist/esm/component.js +25 -0
- package/dist/esm/cookie.js +55 -0
- package/dist/esm/date.js +11 -0
- package/dist/esm/detectHeight.js +42 -0
- package/dist/esm/element.js +116 -0
- package/dist/esm/env.js +23 -0
- package/dist/esm/events.js +19 -0
- package/dist/esm/extends.js +414 -0
- package/dist/esm/fibonacci.js +10 -0
- package/dist/esm/files.js +4 -0
- package/dist/esm/function.js +94 -0
- package/dist/esm/globals.js +10 -0
- package/dist/esm/if.js +28 -0
- package/dist/esm/index.js +37 -0
- package/dist/esm/key.js +18 -0
- package/dist/esm/keys.js +181 -0
- package/dist/esm/load.js +151 -0
- package/dist/esm/log.js +14 -0
- package/dist/esm/metadata.js +290 -0
- package/dist/esm/methods.js +569 -0
- package/dist/esm/node.js +15 -0
- package/dist/esm/object.js +621 -0
- package/dist/esm/projectKeys.js +16 -0
- package/dist/esm/props.js +262 -0
- package/dist/esm/scaling.js +15 -0
- package/dist/esm/scope.js +24 -0
- package/dist/esm/sharedLibraries.js +126 -0
- package/dist/esm/state.js +163 -0
- package/dist/esm/string-extra.js +52 -0
- package/dist/esm/string.js +163 -0
- package/dist/esm/style.js +20 -0
- package/dist/esm/tags.js +200 -0
- package/dist/esm/triggerEvent.js +92 -0
- package/dist/esm/types.js +54 -0
- package/dist/esm/update.js +22 -0
- package/dist/iife/index.js +5818 -0
- package/element.js +148 -0
- package/env.js +15 -0
- package/events.js +19 -0
- package/extends.js +519 -0
- package/function.js +145 -0
- package/globals.js +8 -0
- package/if.js +30 -0
- package/index.js +41 -0
- package/key.js +20 -0
- package/keys.js +176 -0
- package/load.js +236 -0
- package/log.js +12 -0
- package/metadata.js +392 -0
- package/methods.js +636 -0
- package/node.js +37 -0
- package/object.js +803 -0
- package/package.json +38 -22
- package/projectKeys.js +30 -0
- package/props.js +327 -0
- package/{src/scaling.js → scaling.js} +1 -1
- package/scope.js +32 -0
- package/sharedLibraries.js +184 -0
- package/state.js +203 -0
- package/{src/index.js → string-extra.js} +22 -25
- package/string.js +206 -0
- package/tags.js +176 -0
- package/triggerEvent.js +102 -0
- package/types.js +55 -0
- package/update.js +27 -0
- package/dist/cjs/package.json +0 -4
- package/src/detectHeight.js +0 -31
- package/src/load.js +0 -105
- /package/{src/browser.js → browser.js} +0 -0
- /package/{src/date.js → date.js} +0 -0
- /package/{src/fibonacci.js → fibonacci.js} +0 -0
- /package/{src/files.js → files.js} +0 -0
- /package/{src/style.js → style.js} +0 -0
package/function.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Debounces a function, ensuring that it is only executed after a specified timeout
|
|
5
|
+
* period has elapsed since the last invocation.
|
|
6
|
+
*
|
|
7
|
+
* @param {function} func - The function to be debounced.
|
|
8
|
+
* @param {number} [timeout=300] - The time (in milliseconds) to wait after the last call to
|
|
9
|
+
* `debounce` before executing the `func`.
|
|
10
|
+
* @returns {function} - A debounced version of the input function `func`.
|
|
11
|
+
* @example
|
|
12
|
+
* // Usage example:
|
|
13
|
+
* const debouncedFunction = debounce(this, myFunction, 500);
|
|
14
|
+
* window.addEventListener('resize', debouncedFunction);
|
|
15
|
+
*/
|
|
16
|
+
export function debounce (func, wait, immediate) {
|
|
17
|
+
let timeout
|
|
18
|
+
return function () {
|
|
19
|
+
const context = this
|
|
20
|
+
const args = arguments
|
|
21
|
+
const later = function () {
|
|
22
|
+
timeout = null
|
|
23
|
+
if (!immediate) func.apply(context, args)
|
|
24
|
+
}
|
|
25
|
+
const callNow = immediate && !timeout
|
|
26
|
+
clearTimeout(timeout)
|
|
27
|
+
timeout = setTimeout(later, wait)
|
|
28
|
+
if (callNow) func.apply(context, args)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Debounces a function, ensuring that it is only executed after a specified timeout
|
|
34
|
+
* period has elapsed since the last invocation.
|
|
35
|
+
*
|
|
36
|
+
* @param {Object} element - The context (this) to which the debounced function will be applied.
|
|
37
|
+
* @param {function} func - The function to be debounced.
|
|
38
|
+
* @param {number} [timeout=300] - The time (in milliseconds) to wait after the last call to
|
|
39
|
+
* `debounce` before executing the `func`.
|
|
40
|
+
* @returns {function} - A debounced version of the input function `func`.
|
|
41
|
+
* @example
|
|
42
|
+
* // Usage example:
|
|
43
|
+
* const debouncedFunction = debounce(this, myFunction, 500);
|
|
44
|
+
* window.addEventListener('resize', debouncedFunction);
|
|
45
|
+
*/
|
|
46
|
+
export const debounceOnContext = (element, func, timeout = 300) => {
|
|
47
|
+
let timer
|
|
48
|
+
return (...args) => {
|
|
49
|
+
clearTimeout(timer)
|
|
50
|
+
timer = setTimeout(() => {
|
|
51
|
+
func.apply(element, args)
|
|
52
|
+
}, timeout)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const memoize = fn => {
|
|
57
|
+
const cache = {}
|
|
58
|
+
return (...args) => {
|
|
59
|
+
const n = args[0]
|
|
60
|
+
if (n in cache) {
|
|
61
|
+
return cache[n]
|
|
62
|
+
} else {
|
|
63
|
+
const result = fn(n)
|
|
64
|
+
cache[n] = result
|
|
65
|
+
return result
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const RE_STRING_FUNCTION = /^((function\s*\([^)]*\)\s*\{[^}]*\})|(\([^)]*\)\s*=>))/
|
|
71
|
+
|
|
72
|
+
export const isStringFunction = inputString => {
|
|
73
|
+
return RE_STRING_FUNCTION.test(inputString)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Resolve an event handler through context plugins.
|
|
78
|
+
*
|
|
79
|
+
* If `handler` is already a function it is returned as-is.
|
|
80
|
+
* Otherwise, iterates `context.plugins` looking for a plugin
|
|
81
|
+
* with a `resolveHandler` method that can compile the handler
|
|
82
|
+
* (e.g. a funcql schema) into a callable function.
|
|
83
|
+
*
|
|
84
|
+
* @param {*} handler - event handler (function, object, array, string)
|
|
85
|
+
* @param {object} element - domql element
|
|
86
|
+
* @returns {function|*} resolved handler
|
|
87
|
+
*/
|
|
88
|
+
/**
|
|
89
|
+
* Check if context has any plugin that can resolve non-function handlers.
|
|
90
|
+
*/
|
|
91
|
+
export const hasHandlerPlugin = (ctx) => {
|
|
92
|
+
const plugins = ctx?.plugins
|
|
93
|
+
if (!plugins || !plugins.length) return false
|
|
94
|
+
for (const plugin of plugins) {
|
|
95
|
+
if (plugin.resolveHandler) return true
|
|
96
|
+
}
|
|
97
|
+
return false
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export const resolveHandler = (handler, element) => {
|
|
101
|
+
if (typeof handler === 'function') return handler
|
|
102
|
+
const plugins = element?.context?.plugins
|
|
103
|
+
if (!plugins) return handler
|
|
104
|
+
for (const plugin of plugins) {
|
|
105
|
+
if (plugin.resolveHandler) {
|
|
106
|
+
const resolved = plugin.resolveHandler(handler, element)
|
|
107
|
+
if (typeof resolved === 'function') return resolved
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return handler
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Run a named hook across all context plugins.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} hookName - lifecycle hook name
|
|
117
|
+
* @param {object} element - domql element
|
|
118
|
+
* @param {*[]} args - additional arguments forwarded to each plugin hook
|
|
119
|
+
*/
|
|
120
|
+
export const runPluginHook = (hookName, element, ...args) => {
|
|
121
|
+
const plugins = element?.context?.plugins
|
|
122
|
+
if (!plugins) return false
|
|
123
|
+
let handled = false
|
|
124
|
+
for (const plugin of plugins) {
|
|
125
|
+
if (typeof plugin[hookName] === 'function') {
|
|
126
|
+
plugin[hookName](element, ...args)
|
|
127
|
+
handled = true
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return handled
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function cloneFunction (fn, win = window) {
|
|
134
|
+
const temp = function () {
|
|
135
|
+
return fn.apply(win, arguments)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Copy properties from original function
|
|
139
|
+
for (const key in fn) {
|
|
140
|
+
if (Object.prototype.hasOwnProperty.call(fn, key)) {
|
|
141
|
+
temp[key] = fn[key]
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return temp
|
|
145
|
+
}
|
package/globals.js
ADDED
package/if.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { isFunction } from './types.js'
|
|
4
|
+
import { exec } from './object.js'
|
|
5
|
+
|
|
6
|
+
export const createIfConditionFlag = (element, parent) => {
|
|
7
|
+
const { __ref: ref } = element
|
|
8
|
+
const ifVal = element.if
|
|
9
|
+
|
|
10
|
+
if (isFunction(ifVal)) {
|
|
11
|
+
let result
|
|
12
|
+
try {
|
|
13
|
+
result = ifVal(element, element.state, element.context)
|
|
14
|
+
} catch (e) {
|
|
15
|
+
if (typeof console !== 'undefined') console.warn('[DOMQL] if condition error:', e)
|
|
16
|
+
delete ref.__if
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
if (!result) {
|
|
20
|
+
delete ref.__if
|
|
21
|
+
} else ref.__if = true
|
|
22
|
+
} else if (ifVal != null && typeof ifVal === 'object') {
|
|
23
|
+
// funcql schema — evaluate via exec() which delegates to plugins
|
|
24
|
+
const result = exec(ifVal, element)
|
|
25
|
+
if (!result) delete ref.__if
|
|
26
|
+
else ref.__if = true
|
|
27
|
+
} else {
|
|
28
|
+
ref.__if = true
|
|
29
|
+
}
|
|
30
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
export * from './key.js'
|
|
4
|
+
export * from './env.js'
|
|
5
|
+
export * from './types.js'
|
|
6
|
+
export * from './object.js'
|
|
7
|
+
export * from './function.js'
|
|
8
|
+
export * from './array.js'
|
|
9
|
+
export * from './node.js'
|
|
10
|
+
export * from './if.js'
|
|
11
|
+
export * from './log.js'
|
|
12
|
+
export * from './string.js'
|
|
13
|
+
export * from './globals.js'
|
|
14
|
+
export * from './cookie.js'
|
|
15
|
+
export * from './tags.js'
|
|
16
|
+
export * from './component.js'
|
|
17
|
+
export * from './props.js'
|
|
18
|
+
export * from './extends.js'
|
|
19
|
+
export * from './element.js'
|
|
20
|
+
export * from './state.js'
|
|
21
|
+
export * from './keys.js'
|
|
22
|
+
export * from './scope.js'
|
|
23
|
+
export * from './methods.js'
|
|
24
|
+
export * from './cache.js'
|
|
25
|
+
export * from './update.js'
|
|
26
|
+
export * from './triggerEvent.js'
|
|
27
|
+
export * from './projectKeys.js'
|
|
28
|
+
|
|
29
|
+
// ── Merged from the former @symbo.ls/smbls-utils package (4.x) ────────────
|
|
30
|
+
export * from './browser.js'
|
|
31
|
+
export * from './cdn.js'
|
|
32
|
+
export * from './date.js'
|
|
33
|
+
export * from './detectHeight.js'
|
|
34
|
+
export * from './fibonacci.js'
|
|
35
|
+
export * from './files.js'
|
|
36
|
+
export * from './load.js'
|
|
37
|
+
export * from './metadata.js'
|
|
38
|
+
export * from './scaling.js'
|
|
39
|
+
export * from './sharedLibraries.js'
|
|
40
|
+
export * from './string-extra.js'
|
|
41
|
+
export * from './style.js'
|
package/key.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { exec } from './object.js'
|
|
4
|
+
|
|
5
|
+
export const generateKey = (function () {
|
|
6
|
+
let index = 0
|
|
7
|
+
|
|
8
|
+
function newId () {
|
|
9
|
+
index++
|
|
10
|
+
return index
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return newId
|
|
14
|
+
})()
|
|
15
|
+
|
|
16
|
+
export const createSnapshotId = generateKey
|
|
17
|
+
|
|
18
|
+
export const createKey = (element, parent, key) => {
|
|
19
|
+
return (exec(key, element) || key || element.key || generateKey()).toString()
|
|
20
|
+
}
|
package/keys.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
export const DOMQ_PROPERTIES = new Set([
|
|
4
|
+
'attr',
|
|
5
|
+
'style',
|
|
6
|
+
'text',
|
|
7
|
+
'html',
|
|
8
|
+
'content',
|
|
9
|
+
'data',
|
|
10
|
+
'classlist',
|
|
11
|
+
'state',
|
|
12
|
+
'scope',
|
|
13
|
+
'root',
|
|
14
|
+
'deps',
|
|
15
|
+
'extend',
|
|
16
|
+
'extends',
|
|
17
|
+
'$router',
|
|
18
|
+
'routes',
|
|
19
|
+
'children',
|
|
20
|
+
'childExtend',
|
|
21
|
+
'childExtends',
|
|
22
|
+
'childExtendRecursive',
|
|
23
|
+
'childExtendsRecursive',
|
|
24
|
+
'props',
|
|
25
|
+
'if',
|
|
26
|
+
'define',
|
|
27
|
+
'__name',
|
|
28
|
+
'__ref',
|
|
29
|
+
'__hash',
|
|
30
|
+
'__text',
|
|
31
|
+
'key',
|
|
32
|
+
'tag',
|
|
33
|
+
'query',
|
|
34
|
+
'parent',
|
|
35
|
+
'node',
|
|
36
|
+
'variables',
|
|
37
|
+
'on',
|
|
38
|
+
'fetch',
|
|
39
|
+
'component',
|
|
40
|
+
'context',
|
|
41
|
+
'preventContentUpdate',
|
|
42
|
+
'preventContentRecreate'
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
export const PARSED_DOMQ_PROPERTIES = new Set([
|
|
46
|
+
'attr',
|
|
47
|
+
'style',
|
|
48
|
+
'text',
|
|
49
|
+
'html',
|
|
50
|
+
'content',
|
|
51
|
+
'data',
|
|
52
|
+
'class',
|
|
53
|
+
'state',
|
|
54
|
+
'scope',
|
|
55
|
+
'children',
|
|
56
|
+
'props',
|
|
57
|
+
'if',
|
|
58
|
+
'key',
|
|
59
|
+
'tag',
|
|
60
|
+
'query',
|
|
61
|
+
'on',
|
|
62
|
+
'context'
|
|
63
|
+
])
|
|
64
|
+
|
|
65
|
+
export const STATE_PROPERTIES = [
|
|
66
|
+
'ref',
|
|
67
|
+
'parent',
|
|
68
|
+
'__element',
|
|
69
|
+
'__depends',
|
|
70
|
+
'__ref',
|
|
71
|
+
'__children',
|
|
72
|
+
'root'
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
export const STATE_METHODS = new Set([
|
|
76
|
+
'update',
|
|
77
|
+
'parse',
|
|
78
|
+
'clean',
|
|
79
|
+
'create',
|
|
80
|
+
'destroy',
|
|
81
|
+
'add',
|
|
82
|
+
'toggle',
|
|
83
|
+
'remove',
|
|
84
|
+
'apply',
|
|
85
|
+
'set',
|
|
86
|
+
'reset',
|
|
87
|
+
'replace',
|
|
88
|
+
'quietReplace',
|
|
89
|
+
'quietUpdate',
|
|
90
|
+
'applyReplace',
|
|
91
|
+
'applyFunction',
|
|
92
|
+
'keys',
|
|
93
|
+
'values',
|
|
94
|
+
'ref',
|
|
95
|
+
'rootUpdate',
|
|
96
|
+
'parentUpdate',
|
|
97
|
+
'parent',
|
|
98
|
+
'__element',
|
|
99
|
+
'__depends',
|
|
100
|
+
'__ref',
|
|
101
|
+
'__children',
|
|
102
|
+
'root',
|
|
103
|
+
'setByPath',
|
|
104
|
+
'setPathCollection',
|
|
105
|
+
'removeByPath',
|
|
106
|
+
'removePathCollection',
|
|
107
|
+
'getByPath'
|
|
108
|
+
])
|
|
109
|
+
|
|
110
|
+
export const PROPS_METHODS = new Set(['update', '__element'])
|
|
111
|
+
|
|
112
|
+
export const METHODS = new Set([
|
|
113
|
+
'set',
|
|
114
|
+
'reset',
|
|
115
|
+
'update',
|
|
116
|
+
'remove',
|
|
117
|
+
'updateContent',
|
|
118
|
+
'removeContent',
|
|
119
|
+
'lookup',
|
|
120
|
+
'lookdown',
|
|
121
|
+
'lookdownAll',
|
|
122
|
+
'getRef',
|
|
123
|
+
'getPath',
|
|
124
|
+
'setNodeStyles',
|
|
125
|
+
'spotByPath',
|
|
126
|
+
'keys',
|
|
127
|
+
'parse',
|
|
128
|
+
'setProps',
|
|
129
|
+
'parseDeep',
|
|
130
|
+
'variables',
|
|
131
|
+
'if',
|
|
132
|
+
'log',
|
|
133
|
+
'verbose',
|
|
134
|
+
'warn',
|
|
135
|
+
'error',
|
|
136
|
+
'call',
|
|
137
|
+
'nextElement',
|
|
138
|
+
'previousElement',
|
|
139
|
+
'getRootState',
|
|
140
|
+
'getRoot',
|
|
141
|
+
'getRootData',
|
|
142
|
+
'getRootContext',
|
|
143
|
+
'getContext',
|
|
144
|
+
'getQuery',
|
|
145
|
+
'getDB',
|
|
146
|
+
'getChildren',
|
|
147
|
+
'preventContentUpdate',
|
|
148
|
+
'preventContentRecreate'
|
|
149
|
+
])
|
|
150
|
+
|
|
151
|
+
export const METHODS_EXL = new Set([
|
|
152
|
+
'node', 'context', 'extends', '__element', '__ref',
|
|
153
|
+
...METHODS,
|
|
154
|
+
...STATE_METHODS,
|
|
155
|
+
...PROPS_METHODS
|
|
156
|
+
])
|
|
157
|
+
|
|
158
|
+
export const DOMQL_EVENTS = new Set([
|
|
159
|
+
'init',
|
|
160
|
+
'beforeClassAssign',
|
|
161
|
+
'render',
|
|
162
|
+
'renderRouter',
|
|
163
|
+
'attachNode',
|
|
164
|
+
'stateInit',
|
|
165
|
+
'stateCreated',
|
|
166
|
+
'beforeStateUpdate',
|
|
167
|
+
'stateUpdate',
|
|
168
|
+
'beforeUpdate',
|
|
169
|
+
'done',
|
|
170
|
+
'create',
|
|
171
|
+
'complete',
|
|
172
|
+
'frame',
|
|
173
|
+
'update',
|
|
174
|
+
'fetchError',
|
|
175
|
+
'fetchComplete'
|
|
176
|
+
])
|
package/load.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
export const loadJavascriptFile = (
|
|
4
|
+
FILE_URL,
|
|
5
|
+
async = false,
|
|
6
|
+
doc = document,
|
|
7
|
+
type = 'text/javascript'
|
|
8
|
+
) => {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
try {
|
|
11
|
+
const scriptEle = doc.createElement('script')
|
|
12
|
+
scriptEle.type = type
|
|
13
|
+
scriptEle.async = async
|
|
14
|
+
scriptEle.src = FILE_URL
|
|
15
|
+
|
|
16
|
+
scriptEle.addEventListener('load', ev => {
|
|
17
|
+
resolve({
|
|
18
|
+
status: true
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
scriptEle.addEventListener('error', ev => {
|
|
23
|
+
reject(
|
|
24
|
+
new Error({
|
|
25
|
+
status: false,
|
|
26
|
+
message: `Failed to load the script ${FILE_URL}`
|
|
27
|
+
})
|
|
28
|
+
)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
doc.body.appendChild(scriptEle)
|
|
32
|
+
} catch (error) {
|
|
33
|
+
reject(error)
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const loadJavascriptFileSync = (
|
|
39
|
+
fileUrl,
|
|
40
|
+
doc = document,
|
|
41
|
+
type = 'text/javascript'
|
|
42
|
+
) => {
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const scriptEle = doc.createElement('script')
|
|
45
|
+
scriptEle.type = type
|
|
46
|
+
scriptEle.src = fileUrl
|
|
47
|
+
|
|
48
|
+
// Create a blocking overlay
|
|
49
|
+
const blocker = doc.createElement('div')
|
|
50
|
+
blocker.style.cssText =
|
|
51
|
+
'position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(255,255,255,0.8);z-index:9999;'
|
|
52
|
+
doc.body.appendChild(blocker)
|
|
53
|
+
|
|
54
|
+
scriptEle.onload = () => {
|
|
55
|
+
console.log(`Successfully loaded: ${fileUrl}`)
|
|
56
|
+
doc.body.removeChild(blocker)
|
|
57
|
+
resolve()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
scriptEle.onerror = () => {
|
|
61
|
+
doc.body.removeChild(blocker)
|
|
62
|
+
reject(new Error(`Failed to load: ${fileUrl}`))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
doc.body.appendChild(scriptEle)
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const loadJavascriptFileEmbedSync = (
|
|
70
|
+
FILE_URL,
|
|
71
|
+
doc = document,
|
|
72
|
+
fallback,
|
|
73
|
+
type = 'text/javascript'
|
|
74
|
+
) => {
|
|
75
|
+
const xhr = new window.XMLHttpRequest()
|
|
76
|
+
xhr.open('GET', FILE_URL, false) // false makes the request synchronous
|
|
77
|
+
xhr.send()
|
|
78
|
+
|
|
79
|
+
if (xhr.status === 200) {
|
|
80
|
+
const scriptEle = doc.createElement('script')
|
|
81
|
+
scriptEle.type = type
|
|
82
|
+
scriptEle.text = xhr.responseText
|
|
83
|
+
doc.body.appendChild(scriptEle)
|
|
84
|
+
if (typeof fallback === 'function') fallback()
|
|
85
|
+
} else {
|
|
86
|
+
throw new Error(`Failed to load the script ${FILE_URL}`)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const loadCssFile = (
|
|
91
|
+
FILE_URL,
|
|
92
|
+
async = false,
|
|
93
|
+
doc = document,
|
|
94
|
+
type = 'text/javascript'
|
|
95
|
+
) => {
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
try {
|
|
98
|
+
const linkElem = doc.createElement('link')
|
|
99
|
+
linkElem.rel = 'stylesheet'
|
|
100
|
+
linkElem.href = FILE_URL
|
|
101
|
+
|
|
102
|
+
linkElem.addEventListener('load', ev => {
|
|
103
|
+
resolve({
|
|
104
|
+
status: true
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
doc.head.appendChild(linkElem)
|
|
109
|
+
} catch (error) {
|
|
110
|
+
reject(error)
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const loadJavascript = (
|
|
116
|
+
body,
|
|
117
|
+
async = false,
|
|
118
|
+
doc = document,
|
|
119
|
+
type = 'text/javascript',
|
|
120
|
+
id = 'smbls-script'
|
|
121
|
+
) => {
|
|
122
|
+
try {
|
|
123
|
+
const scriptEle = doc.createElement('script')
|
|
124
|
+
scriptEle.type = type
|
|
125
|
+
scriptEle.async = async
|
|
126
|
+
scriptEle.id = id
|
|
127
|
+
scriptEle.innerHTML = body
|
|
128
|
+
|
|
129
|
+
doc.body.appendChild(scriptEle)
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.warn(error)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Dynamically loads a remote JavaScript file and returns a Promise
|
|
137
|
+
* that resolves when the script has completely loaded or rejects on error.
|
|
138
|
+
*
|
|
139
|
+
* @param {string} url - The URL of the remote JavaScript file to load
|
|
140
|
+
* @param {Object} [options] - Optional configuration
|
|
141
|
+
* @param {string} [options.id] - Optional ID to assign to the script element
|
|
142
|
+
* @param {boolean} [options.async=true] - Whether to load the script asynchronously
|
|
143
|
+
* @param {string} [options.type='text/javascript'] - The type attribute for the script
|
|
144
|
+
* @param {string} [options.integrity] - Optional integrity hash for SRI
|
|
145
|
+
* @param {string} [options.crossOrigin] - Optional crossorigin attribute
|
|
146
|
+
* @return {Promise} A promise that resolves when the script loads or rejects on error
|
|
147
|
+
*/
|
|
148
|
+
export function loadRemoteScript (url, options = {}) {
|
|
149
|
+
const { window = globalThis } = options
|
|
150
|
+
const { document = window.document } = options
|
|
151
|
+
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
// Check if script is already loaded
|
|
154
|
+
const existingScript = document.querySelector(`script[src="${url}"]`)
|
|
155
|
+
if (existingScript) {
|
|
156
|
+
return resolve(existingScript)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Create script element
|
|
160
|
+
const script = document.createElement('script')
|
|
161
|
+
script.src = url
|
|
162
|
+
script.async = options.async === true
|
|
163
|
+
script.type = options.type || 'text/javascript'
|
|
164
|
+
|
|
165
|
+
// Add optional attributes
|
|
166
|
+
if (options.id) script.id = options.id
|
|
167
|
+
if (options.integrity) script.integrity = options.integrity
|
|
168
|
+
if (options.crossOrigin) script.crossOrigin = options.crossOrigin
|
|
169
|
+
|
|
170
|
+
// Setup load and error handlers
|
|
171
|
+
script.onload = () => {
|
|
172
|
+
script.onerror = script.onload = null
|
|
173
|
+
resolve(script)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
script.onerror = () => {
|
|
177
|
+
script.onerror = script.onload = null
|
|
178
|
+
reject(new Error(`Failed to load script: ${url}`))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Append the script to the document head
|
|
182
|
+
document.head.appendChild(script)
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Dynamically loads a remote CSS file and returns a Promise
|
|
188
|
+
* that resolves when the stylesheet has completely loaded or rejects on error.
|
|
189
|
+
*
|
|
190
|
+
* @param {string} url - The URL of the remote CSS file to load
|
|
191
|
+
* @param {Object} [options] - Optional configuration
|
|
192
|
+
* @param {string} [options.id] - Optional ID to assign to the link element
|
|
193
|
+
* @param {string} [options.rel='stylesheet'] - The rel attribute for the link
|
|
194
|
+
* @param {string} [options.media='all'] - The media attribute
|
|
195
|
+
* @param {string} [options.integrity] - Optional integrity hash for SRI
|
|
196
|
+
* @param {string} [options.crossOrigin] - Optional crossorigin attribute
|
|
197
|
+
* @return {Promise} A promise that resolves when the stylesheet loads or rejects on error
|
|
198
|
+
*/
|
|
199
|
+
export async function loadRemoteCSS (url, options = {}) {
|
|
200
|
+
const { window = globalThis } = options
|
|
201
|
+
const { document = window.document } = options
|
|
202
|
+
|
|
203
|
+
return new Promise((resolve, reject) => {
|
|
204
|
+
// Check if stylesheet is already loaded
|
|
205
|
+
const existingLink = document.querySelector(`link[href="${url}"]`)
|
|
206
|
+
if (existingLink) {
|
|
207
|
+
return resolve(existingLink)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Create link element
|
|
211
|
+
const link = document.createElement('link')
|
|
212
|
+
link.href = url
|
|
213
|
+
link.rel = options.rel || 'stylesheet'
|
|
214
|
+
link.type = 'text/css'
|
|
215
|
+
link.media = options.media || 'all'
|
|
216
|
+
|
|
217
|
+
// Add optional attributes
|
|
218
|
+
if (options.id) link.id = options.id
|
|
219
|
+
if (options.integrity) link.integrity = options.integrity
|
|
220
|
+
if (options.crossOrigin) link.crossOrigin = options.crossOrigin
|
|
221
|
+
|
|
222
|
+
// Setup load and error handlers
|
|
223
|
+
link.onload = () => {
|
|
224
|
+
link.onerror = link.onload = null
|
|
225
|
+
resolve(link)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
link.onerror = () => {
|
|
229
|
+
link.onerror = link.onload = null
|
|
230
|
+
reject(new Error(`Failed to load stylesheet: ${url}`))
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Append the link to the document head
|
|
234
|
+
document.head.appendChild(link)
|
|
235
|
+
})
|
|
236
|
+
}
|
package/log.js
ADDED