glass-easel 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/README.md +40 -0
- package/dist/glass_easel.all.d.ts +1 -0
- package/dist/glass_easel.all.js +2 -0
- package/dist/glass_easel.all.js.map +1 -0
- package/dist/glass_easel.domlike.global.d.ts +1 -0
- package/dist/glass_easel.domlike.global.js +2 -0
- package/dist/glass_easel.domlike.global.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types/src/backend/backend_protocol.d.ts +119 -0
- package/dist/types/src/backend/backend_protocol.d.ts.map +1 -0
- package/dist/types/src/backend/composed_backend_protocol.d.ts +90 -0
- package/dist/types/src/backend/composed_backend_protocol.d.ts.map +1 -0
- package/dist/types/src/backend/domlike_backend_protocol.d.ts +76 -0
- package/dist/types/src/backend/domlike_backend_protocol.d.ts.map +1 -0
- package/dist/types/src/backend/mode.d.ts +46 -0
- package/dist/types/src/backend/mode.d.ts.map +1 -0
- package/dist/types/src/backend/suggested_backend_protocol.d.ts +30 -0
- package/dist/types/src/backend/suggested_backend_protocol.d.ts.map +1 -0
- package/dist/types/src/behavior.d.ts +428 -0
- package/dist/types/src/behavior.d.ts.map +1 -0
- package/dist/types/src/class_list.d.ts +79 -0
- package/dist/types/src/class_list.d.ts.map +1 -0
- package/dist/types/src/component.d.ts +291 -0
- package/dist/types/src/component.d.ts.map +1 -0
- package/dist/types/src/component_params.d.ts +239 -0
- package/dist/types/src/component_params.d.ts.map +1 -0
- package/dist/types/src/component_space.d.ts +164 -0
- package/dist/types/src/component_space.d.ts.map +1 -0
- package/dist/types/src/data_path.d.ts +5 -0
- package/dist/types/src/data_path.d.ts.map +1 -0
- package/dist/types/src/data_proxy.d.ts +107 -0
- package/dist/types/src/data_proxy.d.ts.map +1 -0
- package/dist/types/src/data_utils.d.ts +3 -0
- package/dist/types/src/data_utils.d.ts.map +1 -0
- package/dist/types/src/element.d.ts +275 -0
- package/dist/types/src/element.d.ts.map +1 -0
- package/dist/types/src/element_iterator.d.ts +43 -0
- package/dist/types/src/element_iterator.d.ts.map +1 -0
- package/dist/types/src/event.d.ts +104 -0
- package/dist/types/src/event.d.ts.map +1 -0
- package/dist/types/src/external_shadow_tree.d.ts +20 -0
- package/dist/types/src/external_shadow_tree.d.ts.map +1 -0
- package/dist/types/src/func_arr.d.ts +39 -0
- package/dist/types/src/func_arr.d.ts.map +1 -0
- package/dist/types/src/global_options.d.ts +111 -0
- package/dist/types/src/global_options.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +43 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/mutation_observer.d.ts +79 -0
- package/dist/types/src/mutation_observer.d.ts.map +1 -0
- package/dist/types/src/native_node.d.ts +8 -0
- package/dist/types/src/native_node.d.ts.map +1 -0
- package/dist/types/src/node.d.ts +49 -0
- package/dist/types/src/node.d.ts.map +1 -0
- package/dist/types/src/relation.d.ts +47 -0
- package/dist/types/src/relation.d.ts.map +1 -0
- package/dist/types/src/render.d.ts +3 -0
- package/dist/types/src/render.d.ts.map +1 -0
- package/dist/types/src/selector.d.ts +32 -0
- package/dist/types/src/selector.d.ts.map +1 -0
- package/dist/types/src/shadow_root.d.ts +136 -0
- package/dist/types/src/shadow_root.d.ts.map +1 -0
- package/dist/types/src/template_engine.d.ts +18 -0
- package/dist/types/src/template_engine.d.ts.map +1 -0
- package/dist/types/src/text_node.d.ts +32 -0
- package/dist/types/src/text_node.d.ts.map +1 -0
- package/dist/types/src/tmpl/index.d.ts +18 -0
- package/dist/types/src/tmpl/index.d.ts.map +1 -0
- package/dist/types/src/tmpl/native_rendering.d.ts +45 -0
- package/dist/types/src/tmpl/native_rendering.d.ts.map +1 -0
- package/dist/types/src/tmpl/proc_gen_wrapper.d.ts +80 -0
- package/dist/types/src/tmpl/proc_gen_wrapper.d.ts.map +1 -0
- package/dist/types/src/tmpl/proc_gen_wrapper_dom.d.ts +50 -0
- package/dist/types/src/tmpl/proc_gen_wrapper_dom.d.ts.map +1 -0
- package/dist/types/src/tmpl/range_list_diff.d.ts +19 -0
- package/dist/types/src/tmpl/range_list_diff.d.ts.map +1 -0
- package/dist/types/src/trait_behaviors.d.ts +38 -0
- package/dist/types/src/trait_behaviors.d.ts.map +1 -0
- package/dist/types/src/virtual_node.d.ts +10 -0
- package/dist/types/src/virtual_node.d.ts.map +1 -0
- package/dist/types/tests/backend/domlike.test.d.ts +2 -0
- package/dist/types/tests/backend/domlike.test.d.ts.map +1 -0
- package/dist/types/tests/base/env.d.ts +29 -0
- package/dist/types/tests/base/env.d.ts.map +1 -0
- package/dist/types/tests/base/match.d.ts +9 -0
- package/dist/types/tests/base/match.d.ts.map +1 -0
- package/dist/types/tests/core/backend.test.d.ts +2 -0
- package/dist/types/tests/core/backend.test.d.ts.map +1 -0
- package/dist/types/tests/core/behavior.test.d.ts +2 -0
- package/dist/types/tests/core/behavior.test.d.ts.map +1 -0
- package/dist/types/tests/core/component_space.test.d.ts +2 -0
- package/dist/types/tests/core/component_space.test.d.ts.map +1 -0
- package/dist/types/tests/core/data_update.test.d.ts +2 -0
- package/dist/types/tests/core/data_update.test.d.ts.map +1 -0
- package/dist/types/tests/core/misc.test.d.ts +2 -0
- package/dist/types/tests/core/misc.test.d.ts.map +1 -0
- package/dist/types/tests/core/placeholder.test.d.ts +2 -0
- package/dist/types/tests/core/placeholder.test.d.ts.map +1 -0
- package/dist/types/tests/core/slot.test.d.ts +2 -0
- package/dist/types/tests/core/slot.test.d.ts.map +1 -0
- package/dist/types/tests/core/trait_behaviors.test.d.ts +2 -0
- package/dist/types/tests/core/trait_behaviors.test.d.ts.map +1 -0
- package/dist/types/tests/tmpl/binding_map.test.d.ts +2 -0
- package/dist/types/tests/tmpl/binding_map.test.d.ts.map +1 -0
- package/dist/types/tests/tmpl/event.test.d.ts +2 -0
- package/dist/types/tests/tmpl/event.test.d.ts.map +1 -0
- package/dist/types/tests/tmpl/expression.test.d.ts +2 -0
- package/dist/types/tests/tmpl/expression.test.d.ts.map +1 -0
- package/dist/types/tests/tmpl/lvalue.test.d.ts +2 -0
- package/dist/types/tests/tmpl/lvalue.test.d.ts.map +1 -0
- package/dist/types/tests/tmpl/native_rendering.test.d.ts +2 -0
- package/dist/types/tests/tmpl/native_rendering.test.d.ts.map +1 -0
- package/dist/types/tests/tmpl/structure.test.d.ts +2 -0
- package/dist/types/tests/tmpl/structure.test.d.ts.map +1 -0
- package/dist/types/tests/types/chaining.test.d.ts +2 -0
- package/dist/types/tests/types/chaining.test.d.ts.map +1 -0
- package/dist/types/tests/types/createElement.test.d.ts +2 -0
- package/dist/types/tests/types/createElement.test.d.ts.map +1 -0
- package/dist/types/tests/types/definition.test.d.ts +2 -0
- package/dist/types/tests/types/definition.test.d.ts.map +1 -0
- package/guide/zh_CN/advanced/binding_map_update.md +32 -0
- package/guide/zh_CN/advanced/build_args.md +28 -0
- package/guide/zh_CN/advanced/component_filter.md +70 -0
- package/guide/zh_CN/advanced/component_space.md +124 -0
- package/guide/zh_CN/advanced/custom_backend.md +53 -0
- package/guide/zh_CN/advanced/error_listener.md +32 -0
- package/guide/zh_CN/advanced/external_component.md +73 -0
- package/guide/zh_CN/advanced/template_engine.md +61 -0
- package/guide/zh_CN/appendix/backend_protocol.md +501 -0
- package/guide/zh_CN/appendix/list_diff_algorithm.md +406 -0
- package/guide/zh_CN/basic/beginning.md +94 -0
- package/guide/zh_CN/basic/component.md +156 -0
- package/guide/zh_CN/basic/event.md +169 -0
- package/guide/zh_CN/basic/lifetime.md +66 -0
- package/guide/zh_CN/basic/method.md +62 -0
- package/guide/zh_CN/basic/template.md +135 -0
- package/guide/zh_CN/data_management/advanced_update.md +170 -0
- package/guide/zh_CN/data_management/data_deep_copy.md +157 -0
- package/guide/zh_CN/data_management/data_observer.md +154 -0
- package/guide/zh_CN/data_management/property_early_init.md +31 -0
- package/guide/zh_CN/data_management/pure_data_pattern.md +21 -0
- package/guide/zh_CN/index.md +93 -0
- package/guide/zh_CN/interaction/behavior.md +52 -0
- package/guide/zh_CN/interaction/component_path.md +37 -0
- package/guide/zh_CN/interaction/generic.md +73 -0
- package/guide/zh_CN/interaction/placeholder.md +40 -0
- package/guide/zh_CN/interaction/relation.md +151 -0
- package/guide/zh_CN/interaction/slot.md +137 -0
- package/guide/zh_CN/interaction/template_import.md +94 -0
- package/guide/zh_CN/interaction/trait_behavior.md +117 -0
- package/guide/zh_CN/styling/external_class.md +46 -0
- package/guide/zh_CN/styling/style_isolation.md +54 -0
- package/guide/zh_CN/styling/virtual_host.md +52 -0
- package/guide/zh_CN/tree/element_iterator.md +54 -0
- package/guide/zh_CN/tree/mutation_observer.md +52 -0
- package/guide/zh_CN/tree/node_tree.md +142 -0
- package/guide/zh_CN/tree/node_tree_modification.md +78 -0
- package/guide/zh_CN/tree/selector.md +66 -0
- package/jest.config.js +6 -0
- package/jest.dts.config.js +9 -0
- package/jest.unit.config.js +14 -0
- package/package.json +28 -0
- package/src/backend/backend_protocol.ts +313 -0
- package/src/backend/composed_backend_protocol.ts +252 -0
- package/src/backend/domlike_backend_protocol.ts +370 -0
- package/src/backend/mode.ts +51 -0
- package/src/backend/suggested_backend_protocol.ts +83 -0
- package/src/behavior.ts +1655 -0
- package/src/bootstrap_dom_dev.js +22 -0
- package/src/class_list.ts +376 -0
- package/src/component.ts +1309 -0
- package/src/component_params.ts +461 -0
- package/src/component_space.ts +547 -0
- package/src/data_path.ts +225 -0
- package/src/data_proxy.ts +670 -0
- package/src/data_utils.ts +50 -0
- package/src/element.ts +1966 -0
- package/src/element_iterator.ts +158 -0
- package/src/event.ts +401 -0
- package/src/external_shadow_tree.ts +27 -0
- package/src/func_arr.ts +198 -0
- package/src/global_options.ts +242 -0
- package/src/index.ts +187 -0
- package/src/mutation_observer.ts +252 -0
- package/src/native_node.ts +74 -0
- package/src/node.ts +174 -0
- package/src/relation.ts +380 -0
- package/src/render.ts +25 -0
- package/src/selector.ts +218 -0
- package/src/shadow_root.ts +766 -0
- package/src/template_engine.ts +45 -0
- package/src/text_node.ts +149 -0
- package/src/tmpl/index.ts +199 -0
- package/src/tmpl/native_rendering.ts +175 -0
- package/src/tmpl/proc_gen_wrapper.ts +954 -0
- package/src/tmpl/proc_gen_wrapper_dom.ts +230 -0
- package/src/tmpl/range_list_diff.ts +443 -0
- package/src/trait_behaviors.ts +51 -0
- package/src/virtual_node.ts +51 -0
- package/tests/backend/domlike.test.ts +254 -0
- package/tests/base/env.ts +78 -0
- package/tests/base/match.ts +185 -0
- package/tests/core/backend.test.ts +144 -0
- package/tests/core/behavior.test.ts +546 -0
- package/tests/core/component_space.test.ts +212 -0
- package/tests/core/data_update.test.ts +461 -0
- package/tests/core/misc.test.ts +339 -0
- package/tests/core/placeholder.test.ts +180 -0
- package/tests/core/slot.test.ts +1495 -0
- package/tests/core/trait_behaviors.test.ts +153 -0
- package/tests/legacy/README.md +3 -0
- package/tests/legacy/behavior.test.js +293 -0
- package/tests/legacy/component.test.js +1247 -0
- package/tests/legacy/data_path.test.js +149 -0
- package/tests/legacy/data_proxy.test.js +759 -0
- package/tests/legacy/element_iterator.test.js +148 -0
- package/tests/legacy/event.test.js +849 -0
- package/tests/legacy/external.test.js +510 -0
- package/tests/legacy/extra_info.test.js +109 -0
- package/tests/legacy/generics.test.js +176 -0
- package/tests/legacy/mutation_observer.test.js +210 -0
- package/tests/legacy/relation.test.js +517 -0
- package/tests/legacy/selector.test.js +263 -0
- package/tests/legacy/slot.test.js +915 -0
- package/tests/legacy/virtual.test.js +394 -0
- package/tests/tmpl/binding_map.test.ts +208 -0
- package/tests/tmpl/event.test.ts +206 -0
- package/tests/tmpl/expression.test.ts +429 -0
- package/tests/tmpl/lvalue.test.ts +160 -0
- package/tests/tmpl/native_rendering.test.ts +155 -0
- package/tests/tmpl/structure.test.ts +998 -0
- package/tests/types/chaining.test.ts +614 -0
- package/tests/types/createElement.test.ts +82 -0
- package/tests/types/definition.test.ts +442 -0
- package/tsconfig.json +11 -0
- package/webpack.config.js +270 -0
package/src/func_arr.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Component,
|
|
3
|
+
GeneralComponent,
|
|
4
|
+
} from '.'
|
|
5
|
+
import {
|
|
6
|
+
globalOptions,
|
|
7
|
+
} from './global_options'
|
|
8
|
+
|
|
9
|
+
export type GeneralFuncType = (this: any, ...args: any[]) => any
|
|
10
|
+
|
|
11
|
+
type ErrorInfo = {
|
|
12
|
+
message: string
|
|
13
|
+
type: string
|
|
14
|
+
element: unknown
|
|
15
|
+
method: unknown
|
|
16
|
+
args: unknown[]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function dispatchError(err: unknown, info: ErrorInfo, avoidErrorHandler?: boolean) {
|
|
20
|
+
if (!avoidErrorHandler && errorFuncArr.call(err as any, [err, info]) === false) return
|
|
21
|
+
if (globalOptions.throwGlobalError) {
|
|
22
|
+
throw err
|
|
23
|
+
} else {
|
|
24
|
+
// eslint-disable-next-line no-console
|
|
25
|
+
console.error(err instanceof Error ? `${err.message}\n${err.stack || ''}` : String(err))
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class FuncArr<F extends GeneralFuncType> {
|
|
30
|
+
empty = true
|
|
31
|
+
private _$type = ''
|
|
32
|
+
private _$arr: F[] | null = null
|
|
33
|
+
_$avoidErrorHandler = false
|
|
34
|
+
|
|
35
|
+
add(func: F) {
|
|
36
|
+
if (!this._$arr) this._$arr = [func]
|
|
37
|
+
else this._$arr = this._$arr.concat(func)
|
|
38
|
+
this.empty = false
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
remove(func: F): F | null {
|
|
42
|
+
let ret: F | null = null
|
|
43
|
+
if (this._$arr) {
|
|
44
|
+
const newArr: F[] = []
|
|
45
|
+
const oldArr = this._$arr
|
|
46
|
+
for (let i = 0; i < oldArr.length; i += 1) {
|
|
47
|
+
if (oldArr[i] === func) {
|
|
48
|
+
newArr.push(...oldArr.slice(i + 1))
|
|
49
|
+
ret = func
|
|
50
|
+
break
|
|
51
|
+
}
|
|
52
|
+
newArr.push(oldArr[i]!)
|
|
53
|
+
}
|
|
54
|
+
this._$arr = newArr
|
|
55
|
+
this.empty = this._$arr.length === 0
|
|
56
|
+
}
|
|
57
|
+
return ret
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
call(
|
|
61
|
+
caller: ThisType<F>,
|
|
62
|
+
args: Parameters<F>,
|
|
63
|
+
relatedComponent?: GeneralComponent,
|
|
64
|
+
): boolean {
|
|
65
|
+
const arr = this._$arr
|
|
66
|
+
let ret = true
|
|
67
|
+
if (arr) {
|
|
68
|
+
for (let i = 0; i < arr.length; i += 1) {
|
|
69
|
+
const r = safeCallback(
|
|
70
|
+
this._$type,
|
|
71
|
+
arr[i]!,
|
|
72
|
+
caller,
|
|
73
|
+
args,
|
|
74
|
+
relatedComponent,
|
|
75
|
+
this._$avoidErrorHandler,
|
|
76
|
+
)
|
|
77
|
+
if (r === false) ret = false
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return ret
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
static safeCallback<F extends GeneralFuncType>(
|
|
84
|
+
this: void,
|
|
85
|
+
type: string,
|
|
86
|
+
method: F,
|
|
87
|
+
caller: ThisType<F>,
|
|
88
|
+
args: Parameters<F>,
|
|
89
|
+
relatedComponent?: GeneralComponent,
|
|
90
|
+
avoidErrorHandler = false,
|
|
91
|
+
): ReturnType<F> | undefined {
|
|
92
|
+
try {
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
94
|
+
return method.apply(caller, args)
|
|
95
|
+
} catch (e) {
|
|
96
|
+
let msg = `[Error] [Component] ${type || 'Error Listener'} Error @ `
|
|
97
|
+
if (caller instanceof Component) msg += caller.is
|
|
98
|
+
msg += `#${method.name || '(anonymous)'}`
|
|
99
|
+
if (relatedComponent) {
|
|
100
|
+
relatedComponent.triggerLifetime('error', [e])
|
|
101
|
+
}
|
|
102
|
+
dispatchError(e, {
|
|
103
|
+
message: msg,
|
|
104
|
+
type,
|
|
105
|
+
element: caller,
|
|
106
|
+
method,
|
|
107
|
+
args,
|
|
108
|
+
}, avoidErrorHandler)
|
|
109
|
+
return undefined
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static addGlobalErrorListener(this: void, func: (err: Error, info: ErrorInfo) => boolean) {
|
|
114
|
+
errorFuncArr.add(func)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static removeGlobalErrorListener(this: void, func: (err: Error, info: ErrorInfo) => boolean) {
|
|
118
|
+
errorFuncArr.remove(func)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static addGlobalWarningListener(this: void, func: (msg: string) => void) {
|
|
122
|
+
warningFuncArr.add(func)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static removeGlobalWarningListener(this: void, func: (msg: string) => void) {
|
|
126
|
+
warningFuncArr.remove(func)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function triggerWarning(msg: string) {
|
|
131
|
+
const message = `[Warning] [Component] ${msg}`
|
|
132
|
+
if (warningFuncArr.call(message, [message])) {
|
|
133
|
+
// eslint-disable-next-line no-console
|
|
134
|
+
console.warn(message)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const errorFuncArr = new FuncArr()
|
|
139
|
+
errorFuncArr._$avoidErrorHandler = true
|
|
140
|
+
const warningFuncArr = new FuncArr()
|
|
141
|
+
|
|
142
|
+
export class FuncArrWithMeta<F extends GeneralFuncType, T> {
|
|
143
|
+
empty = true
|
|
144
|
+
private _$type = ''
|
|
145
|
+
private _$arr: { f: F, data: T }[] | null = null
|
|
146
|
+
|
|
147
|
+
add(func: F, data: T) {
|
|
148
|
+
const item = { f: func, data }
|
|
149
|
+
if (!this._$arr) this._$arr = [item]
|
|
150
|
+
else this._$arr = this._$arr.concat(item)
|
|
151
|
+
this.empty = false
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
remove(func: F): T | null {
|
|
155
|
+
let ret: T | null = null
|
|
156
|
+
if (this._$arr) {
|
|
157
|
+
const newArr: { f: F, data: T }[] = []
|
|
158
|
+
const oldArr = this._$arr
|
|
159
|
+
for (let i = 0; i < oldArr.length; i += 1) {
|
|
160
|
+
const v = oldArr[i]!
|
|
161
|
+
if (v.f === func) {
|
|
162
|
+
newArr.push(...oldArr.slice(i + 1))
|
|
163
|
+
ret = v.data
|
|
164
|
+
break
|
|
165
|
+
}
|
|
166
|
+
newArr.push(v)
|
|
167
|
+
}
|
|
168
|
+
this._$arr = newArr
|
|
169
|
+
this.empty = this._$arr.length === 0
|
|
170
|
+
}
|
|
171
|
+
return ret
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
call(
|
|
175
|
+
caller: ThisType<F>,
|
|
176
|
+
args: Parameters<F>,
|
|
177
|
+
retainFn: (data: T) => boolean,
|
|
178
|
+
relatedComponent?: GeneralComponent,
|
|
179
|
+
): boolean {
|
|
180
|
+
const arr = this._$arr
|
|
181
|
+
let ret = true
|
|
182
|
+
if (arr) {
|
|
183
|
+
for (let i = 0; i < arr.length; i += 1) {
|
|
184
|
+
const { f, data } = arr[i]!
|
|
185
|
+
if (!retainFn(data)) continue
|
|
186
|
+
const r = FuncArr.safeCallback(this._$type, f, caller, args, relatedComponent)
|
|
187
|
+
if (r === false) ret = false
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return ret
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export const safeCallback = FuncArr.safeCallback
|
|
195
|
+
export const addGlobalErrorListener = FuncArr.addGlobalErrorListener
|
|
196
|
+
export const removeGlobalErrorListener = FuncArr.removeGlobalErrorListener
|
|
197
|
+
export const addGlobalWarningListener = FuncArr.addGlobalWarningListener
|
|
198
|
+
export const removeGlobalWarningListener = FuncArr.removeGlobalWarningListener
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import * as backend from './backend/backend_protocol'
|
|
2
|
+
import * as composedBackend from './backend/composed_backend_protocol'
|
|
3
|
+
import * as domlikeBackend from './backend/domlike_backend_protocol'
|
|
4
|
+
import {
|
|
5
|
+
StyleScopeId,
|
|
6
|
+
StyleScopeManager,
|
|
7
|
+
} from './class_list'
|
|
8
|
+
import {
|
|
9
|
+
Context as BackendContext,
|
|
10
|
+
} from './backend/backend_protocol'
|
|
11
|
+
import {
|
|
12
|
+
TemplateEngine,
|
|
13
|
+
} from './template_engine'
|
|
14
|
+
import {
|
|
15
|
+
ComponentSpace,
|
|
16
|
+
} from './component_space'
|
|
17
|
+
import {
|
|
18
|
+
GlassEaselTemplateEngine,
|
|
19
|
+
} from './tmpl'
|
|
20
|
+
import {
|
|
21
|
+
GeneralComponent,
|
|
22
|
+
GeneralBackendContext,
|
|
23
|
+
} from '.'
|
|
24
|
+
import {
|
|
25
|
+
BM,
|
|
26
|
+
} from './backend/mode'
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The deep copy strategy
|
|
30
|
+
*
|
|
31
|
+
* Higher level indicates more accuracy but probably less performance.
|
|
32
|
+
*/
|
|
33
|
+
export enum DeepCopyKind {
|
|
34
|
+
/**
|
|
35
|
+
* Avoid deep copy
|
|
36
|
+
*
|
|
37
|
+
* This avoids any copy, and recursive objects can be handled without any failing.
|
|
38
|
+
* However, changing non-copied data will sometimes break the logic.
|
|
39
|
+
*/
|
|
40
|
+
None = 'none',
|
|
41
|
+
/**
|
|
42
|
+
* Do a simple deep copy
|
|
43
|
+
*
|
|
44
|
+
* This simply clones each enumerable fields in an object to a new object.
|
|
45
|
+
* It causes stack overflow in recursive objects.
|
|
46
|
+
* Everything in prototypes is ignored.
|
|
47
|
+
*/
|
|
48
|
+
Simple = 'simple',
|
|
49
|
+
/**
|
|
50
|
+
* Do a deep copy with recursion detection
|
|
51
|
+
*
|
|
52
|
+
* This clones each enumerable fields in an object to a new object.
|
|
53
|
+
* It can handle recursive objects by recursive detection.
|
|
54
|
+
* Everything in prototypes is ignored.
|
|
55
|
+
*/
|
|
56
|
+
SimpleWithRecursion = 'simple-recursion',
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let defaultComponentSpace: ComponentSpace | null = null
|
|
60
|
+
|
|
61
|
+
export const getDefaultComponentSpace = (): ComponentSpace => {
|
|
62
|
+
if (defaultComponentSpace) {
|
|
63
|
+
return defaultComponentSpace
|
|
64
|
+
}
|
|
65
|
+
const cs = new ComponentSpace()
|
|
66
|
+
defaultComponentSpace = cs
|
|
67
|
+
return cs
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let defaultBackendContext: GeneralBackendContext | null = null
|
|
71
|
+
|
|
72
|
+
export const getDefaultBackendContext = (): GeneralBackendContext => {
|
|
73
|
+
if (defaultBackendContext) return defaultBackendContext
|
|
74
|
+
let c: GeneralBackendContext
|
|
75
|
+
if (BM.DOMLIKE) {
|
|
76
|
+
c = new domlikeBackend.CurrentWindowBackendContext()
|
|
77
|
+
} else if (BM.COMPOSED) {
|
|
78
|
+
c = new composedBackend.EmptyComposedBackendContext()
|
|
79
|
+
} else {
|
|
80
|
+
c = new backend.EmptyBackendContext()
|
|
81
|
+
}
|
|
82
|
+
defaultBackendContext = c
|
|
83
|
+
return c
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Options for a component
|
|
88
|
+
*/
|
|
89
|
+
export type ComponentOptions = {
|
|
90
|
+
/** Is external component or not */
|
|
91
|
+
externalComponent?: boolean
|
|
92
|
+
/** The template engine */
|
|
93
|
+
templateEngine?: TemplateEngine
|
|
94
|
+
/** The style scope */
|
|
95
|
+
styleScope?: StyleScopeId
|
|
96
|
+
/** An extra style scope assigned to the component */
|
|
97
|
+
extraStyleScope?: StyleScopeId | null
|
|
98
|
+
/** Enable multiple slots or not */
|
|
99
|
+
multipleSlots?: boolean
|
|
100
|
+
/** Enable dynamic slots or not */
|
|
101
|
+
dynamicSlots?: boolean
|
|
102
|
+
/** Write property values of components to backend with `setAttribute` */
|
|
103
|
+
reflectToAttributes?: boolean
|
|
104
|
+
/** Allow properties and methods to be able to visit directly in component instance */
|
|
105
|
+
writeFieldsToNode?: boolean
|
|
106
|
+
/** Write node ID to backend node */
|
|
107
|
+
writeIdToDOM?: boolean
|
|
108
|
+
/** Generate a prefix for ID written to backend node */
|
|
109
|
+
idPrefixGenerator?: ((this: GeneralComponent) => string) | null
|
|
110
|
+
/** Filter some fields out when applying to templates */
|
|
111
|
+
pureDataPattern?: RegExp | null
|
|
112
|
+
/** Decide how to deep copy component data when updates */
|
|
113
|
+
dataDeepCopy?: DeepCopyKind
|
|
114
|
+
/** Decide how to deep copy when a property changes */
|
|
115
|
+
propertyPassingDeepCopy?: DeepCopyKind
|
|
116
|
+
/** Enable listener change events or not */
|
|
117
|
+
listenerChangeLifetimes?: boolean
|
|
118
|
+
/** Component host node is virtual or not */
|
|
119
|
+
virtualHost?: boolean
|
|
120
|
+
/** Init component with property values or not */
|
|
121
|
+
propertyEarlyInit?: boolean
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type NormalizedComponentOptions = {
|
|
125
|
+
externalComponent: boolean
|
|
126
|
+
templateEngine: TemplateEngine
|
|
127
|
+
styleScope: StyleScopeId
|
|
128
|
+
extraStyleScope: StyleScopeId | null
|
|
129
|
+
multipleSlots: boolean
|
|
130
|
+
dynamicSlots: boolean
|
|
131
|
+
reflectToAttributes: boolean
|
|
132
|
+
writeFieldsToNode: boolean
|
|
133
|
+
writeIdToDOM: boolean
|
|
134
|
+
idPrefixGenerator: ((this: GeneralComponent) => string) | null
|
|
135
|
+
pureDataPattern: RegExp | null
|
|
136
|
+
dataDeepCopy: DeepCopyKind
|
|
137
|
+
propertyPassingDeepCopy: DeepCopyKind
|
|
138
|
+
listenerChangeLifetimes: boolean
|
|
139
|
+
virtualHost: boolean
|
|
140
|
+
propertyEarlyInit: boolean
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Options for global environment
|
|
145
|
+
*/
|
|
146
|
+
export type EnvironmentOptions = {
|
|
147
|
+
/** The default component space */
|
|
148
|
+
defaultComponentSpace: ComponentSpace | null
|
|
149
|
+
/** Throw errors when errors caught in event callbacks (useful in testing scripts) */
|
|
150
|
+
throwGlobalError: boolean
|
|
151
|
+
/** Write some extra attributes to DOM backend (for testing) */
|
|
152
|
+
writeExtraInfoToAttr: boolean
|
|
153
|
+
/** The default backend context */
|
|
154
|
+
backendContext: BackendContext | null
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The default options
|
|
159
|
+
*/
|
|
160
|
+
export const globalOptions: NormalizedComponentOptions & EnvironmentOptions = {
|
|
161
|
+
defaultComponentSpace: null,
|
|
162
|
+
externalComponent: false,
|
|
163
|
+
templateEngine: new GlassEaselTemplateEngine(),
|
|
164
|
+
styleScope: StyleScopeManager.globalScope(),
|
|
165
|
+
extraStyleScope: null,
|
|
166
|
+
multipleSlots: false,
|
|
167
|
+
dynamicSlots: false,
|
|
168
|
+
reflectToAttributes: false,
|
|
169
|
+
writeFieldsToNode: true,
|
|
170
|
+
writeIdToDOM: false,
|
|
171
|
+
idPrefixGenerator: null,
|
|
172
|
+
pureDataPattern: null,
|
|
173
|
+
dataDeepCopy: DeepCopyKind.Simple,
|
|
174
|
+
propertyPassingDeepCopy: DeepCopyKind.Simple,
|
|
175
|
+
listenerChangeLifetimes: false,
|
|
176
|
+
virtualHost: false,
|
|
177
|
+
propertyEarlyInit: false,
|
|
178
|
+
throwGlobalError: false,
|
|
179
|
+
writeExtraInfoToAttr: false,
|
|
180
|
+
backendContext: null,
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export const normalizeComponentOptions = (
|
|
184
|
+
componentOptions: ComponentOptions | undefined,
|
|
185
|
+
baseComponentOptions?: NormalizedComponentOptions,
|
|
186
|
+
): NormalizedComponentOptions => {
|
|
187
|
+
const b = baseComponentOptions === undefined ? globalOptions : baseComponentOptions
|
|
188
|
+
if (componentOptions === undefined && b !== globalOptions) {
|
|
189
|
+
return b
|
|
190
|
+
}
|
|
191
|
+
const p = componentOptions ?? {}
|
|
192
|
+
return {
|
|
193
|
+
externalComponent: p.externalComponent !== undefined
|
|
194
|
+
? p.externalComponent
|
|
195
|
+
: b.externalComponent,
|
|
196
|
+
templateEngine: p.templateEngine !== undefined
|
|
197
|
+
? p.templateEngine
|
|
198
|
+
: b.templateEngine,
|
|
199
|
+
styleScope: p.styleScope !== undefined
|
|
200
|
+
? p.styleScope
|
|
201
|
+
: b.styleScope,
|
|
202
|
+
extraStyleScope: p.extraStyleScope !== undefined
|
|
203
|
+
? p.extraStyleScope
|
|
204
|
+
: b.extraStyleScope,
|
|
205
|
+
multipleSlots: p.multipleSlots !== undefined
|
|
206
|
+
? p.multipleSlots
|
|
207
|
+
: b.multipleSlots,
|
|
208
|
+
dynamicSlots: p.dynamicSlots !== undefined
|
|
209
|
+
? p.dynamicSlots
|
|
210
|
+
: b.dynamicSlots,
|
|
211
|
+
reflectToAttributes: p.reflectToAttributes !== undefined
|
|
212
|
+
? p.reflectToAttributes
|
|
213
|
+
: b.reflectToAttributes,
|
|
214
|
+
writeFieldsToNode: p.writeFieldsToNode !== undefined
|
|
215
|
+
? p.writeFieldsToNode
|
|
216
|
+
: b.writeFieldsToNode,
|
|
217
|
+
writeIdToDOM: p.writeIdToDOM !== undefined
|
|
218
|
+
? p.writeIdToDOM
|
|
219
|
+
: b.writeIdToDOM,
|
|
220
|
+
idPrefixGenerator: p.idPrefixGenerator !== undefined
|
|
221
|
+
? p.idPrefixGenerator
|
|
222
|
+
: b.idPrefixGenerator,
|
|
223
|
+
pureDataPattern: p.pureDataPattern !== undefined
|
|
224
|
+
? p.pureDataPattern
|
|
225
|
+
: b.pureDataPattern,
|
|
226
|
+
dataDeepCopy: p.dataDeepCopy !== undefined
|
|
227
|
+
? p.dataDeepCopy
|
|
228
|
+
: b.dataDeepCopy,
|
|
229
|
+
propertyPassingDeepCopy: p.propertyPassingDeepCopy !== undefined
|
|
230
|
+
? p.propertyPassingDeepCopy
|
|
231
|
+
: b.propertyPassingDeepCopy,
|
|
232
|
+
listenerChangeLifetimes: p.listenerChangeLifetimes !== undefined
|
|
233
|
+
? p.listenerChangeLifetimes
|
|
234
|
+
: b.listenerChangeLifetimes,
|
|
235
|
+
virtualHost: p.virtualHost !== undefined
|
|
236
|
+
? p.virtualHost
|
|
237
|
+
: b.virtualHost,
|
|
238
|
+
propertyEarlyInit: p.propertyEarlyInit !== undefined
|
|
239
|
+
? p.propertyEarlyInit
|
|
240
|
+
: b.propertyEarlyInit,
|
|
241
|
+
}
|
|
242
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/* eslint-disable import/first */
|
|
2
|
+
/* eslint-disable arrow-body-style */
|
|
3
|
+
|
|
4
|
+
export {
|
|
5
|
+
TextNode,
|
|
6
|
+
} from './text_node'
|
|
7
|
+
export {
|
|
8
|
+
NativeNode,
|
|
9
|
+
} from './native_node'
|
|
10
|
+
export {
|
|
11
|
+
ShadowRoot,
|
|
12
|
+
} from './shadow_root'
|
|
13
|
+
export {
|
|
14
|
+
VirtualNode,
|
|
15
|
+
} from './virtual_node'
|
|
16
|
+
export {
|
|
17
|
+
globalOptions,
|
|
18
|
+
DeepCopyKind,
|
|
19
|
+
ComponentOptions,
|
|
20
|
+
EnvironmentOptions,
|
|
21
|
+
NormalizedComponentOptions,
|
|
22
|
+
getDefaultComponentSpace,
|
|
23
|
+
} from './global_options'
|
|
24
|
+
export {
|
|
25
|
+
Element,
|
|
26
|
+
StyleSegmentIndex,
|
|
27
|
+
} from './element'
|
|
28
|
+
export * as dataUtils from './data_utils'
|
|
29
|
+
export * as dataPath from './data_path'
|
|
30
|
+
export {
|
|
31
|
+
DataGroup,
|
|
32
|
+
GeneralDataGroup,
|
|
33
|
+
DataValue,
|
|
34
|
+
DataObserver,
|
|
35
|
+
DataChange,
|
|
36
|
+
PropertyChange,
|
|
37
|
+
DataUpdateCallback,
|
|
38
|
+
} from './data_proxy'
|
|
39
|
+
export {
|
|
40
|
+
Event,
|
|
41
|
+
ShadowedEvent,
|
|
42
|
+
EventBubbleStatus,
|
|
43
|
+
EventOptions,
|
|
44
|
+
} from './event'
|
|
45
|
+
export * as typeUtils from './component_params'
|
|
46
|
+
export {
|
|
47
|
+
Behavior,
|
|
48
|
+
GeneralBehavior,
|
|
49
|
+
BehaviorBuilder,
|
|
50
|
+
GeneralBehaviorBuilder,
|
|
51
|
+
BuilderContext,
|
|
52
|
+
} from './behavior'
|
|
53
|
+
export {
|
|
54
|
+
Component,
|
|
55
|
+
ComponentDefinition,
|
|
56
|
+
GeneralComponent,
|
|
57
|
+
GeneralComponentDefinition,
|
|
58
|
+
} from './component'
|
|
59
|
+
export {
|
|
60
|
+
MutationObserver as Observer,
|
|
61
|
+
MutationObserver,
|
|
62
|
+
} from './mutation_observer'
|
|
63
|
+
export * as mutationObserver from './mutation_observer'
|
|
64
|
+
export {
|
|
65
|
+
Node,
|
|
66
|
+
dumpElement,
|
|
67
|
+
dumpElementToString,
|
|
68
|
+
dumpSingleElementToString,
|
|
69
|
+
GeneralBackendElement,
|
|
70
|
+
GeneralBackendContext,
|
|
71
|
+
NodeCast,
|
|
72
|
+
} from './node'
|
|
73
|
+
export {
|
|
74
|
+
ElementIterator,
|
|
75
|
+
ElementIteratorType,
|
|
76
|
+
} from './element_iterator'
|
|
77
|
+
export {
|
|
78
|
+
FuncArr,
|
|
79
|
+
GeneralFuncType,
|
|
80
|
+
safeCallback,
|
|
81
|
+
addGlobalErrorListener,
|
|
82
|
+
removeGlobalErrorListener,
|
|
83
|
+
addGlobalWarningListener,
|
|
84
|
+
removeGlobalWarningListener,
|
|
85
|
+
} from './func_arr'
|
|
86
|
+
export {
|
|
87
|
+
ParsedSelector,
|
|
88
|
+
} from './selector'
|
|
89
|
+
export {
|
|
90
|
+
StyleScopeManager,
|
|
91
|
+
ClassList,
|
|
92
|
+
StyleScopeId,
|
|
93
|
+
} from './class_list'
|
|
94
|
+
export {
|
|
95
|
+
RelationType,
|
|
96
|
+
RelationListener,
|
|
97
|
+
RelationFailedListener,
|
|
98
|
+
} from './relation'
|
|
99
|
+
export {
|
|
100
|
+
TraitBehavior,
|
|
101
|
+
} from './trait_behaviors'
|
|
102
|
+
export {
|
|
103
|
+
triggerRender,
|
|
104
|
+
} from './render'
|
|
105
|
+
export {
|
|
106
|
+
ComponentSpace,
|
|
107
|
+
} from './component_space'
|
|
108
|
+
export {
|
|
109
|
+
ExternalShadowRoot,
|
|
110
|
+
} from './external_shadow_tree'
|
|
111
|
+
export * as template from './tmpl'
|
|
112
|
+
export * as backend from './backend/backend_protocol'
|
|
113
|
+
export * as composedBackend from './backend/composed_backend_protocol'
|
|
114
|
+
export * as domlikeBackend from './backend/domlike_backend_protocol'
|
|
115
|
+
export {
|
|
116
|
+
BackendMode,
|
|
117
|
+
} from './backend/mode'
|
|
118
|
+
export * as templateEngine from './template_engine'
|
|
119
|
+
export * as glassEaselTemplate from './tmpl'
|
|
120
|
+
|
|
121
|
+
import {
|
|
122
|
+
ComponentParams,
|
|
123
|
+
DataList,
|
|
124
|
+
PropertyList,
|
|
125
|
+
MethodList,
|
|
126
|
+
ComponentInstance,
|
|
127
|
+
GeneralComponentInstance,
|
|
128
|
+
Empty,
|
|
129
|
+
} from './component_params'
|
|
130
|
+
import {
|
|
131
|
+
Behavior,
|
|
132
|
+
} from './behavior'
|
|
133
|
+
import {
|
|
134
|
+
Component,
|
|
135
|
+
ComponentDefinition,
|
|
136
|
+
GeneralComponentDefinition,
|
|
137
|
+
} from './component'
|
|
138
|
+
import {
|
|
139
|
+
Event,
|
|
140
|
+
} from './event'
|
|
141
|
+
import {
|
|
142
|
+
getDefaultComponentSpace,
|
|
143
|
+
} from './global_options'
|
|
144
|
+
|
|
145
|
+
export const registerBehavior = <
|
|
146
|
+
TData extends DataList = Empty,
|
|
147
|
+
TProperty extends PropertyList = Empty,
|
|
148
|
+
TMethod extends MethodList = Empty,
|
|
149
|
+
>(
|
|
150
|
+
def: ComponentParams<TData, TProperty, TMethod> &
|
|
151
|
+
ThisType<ComponentInstance<TData, TProperty, TMethod>>,
|
|
152
|
+
): Behavior<TData, TProperty, TMethod, never> => {
|
|
153
|
+
return getDefaultComponentSpace().defineBehavior(def)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export const registerElement = <
|
|
157
|
+
TData extends DataList = Empty,
|
|
158
|
+
TProperty extends PropertyList = Empty,
|
|
159
|
+
TMethod extends MethodList = Empty,
|
|
160
|
+
>(
|
|
161
|
+
def: ComponentParams<TData, TProperty, TMethod> &
|
|
162
|
+
ThisType<ComponentInstance<TData, TProperty, TMethod>>,
|
|
163
|
+
): ComponentDefinition<TData, TProperty, TMethod> => {
|
|
164
|
+
return getDefaultComponentSpace().defineComponent(def)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function createElement(
|
|
168
|
+
tagName: string,
|
|
169
|
+
compDef?: GeneralComponentDefinition
|
|
170
|
+
): GeneralComponentInstance
|
|
171
|
+
export function createElement <
|
|
172
|
+
TData extends DataList,
|
|
173
|
+
TProperty extends PropertyList,
|
|
174
|
+
TMethod extends MethodList,
|
|
175
|
+
>(
|
|
176
|
+
tagName: string,
|
|
177
|
+
compDef: ComponentDefinition<TData, TProperty, TMethod>,
|
|
178
|
+
): ComponentInstance<TData, TProperty, TMethod>
|
|
179
|
+
export function createElement(
|
|
180
|
+
tagName: string,
|
|
181
|
+
compDef?: GeneralComponentDefinition,
|
|
182
|
+
): GeneralComponentInstance {
|
|
183
|
+
return Component.create(tagName, compDef || null)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export const triggerEvent = Event.triggerEvent
|
|
187
|
+
export const triggerExternalEvent = Event.triggerExternalEvent
|