hzengine-core 0.1.2-dev

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.
Files changed (103) hide show
  1. package/dist/async/index.js +162 -0
  2. package/dist/async/zeppos_timer.js +58 -0
  3. package/dist/audio/index.js +260 -0
  4. package/dist/config/index.js +57 -0
  5. package/dist/debug/index.js +8 -0
  6. package/dist/index.js +103 -0
  7. package/dist/platform/index.js +1 -0
  8. package/dist/plugins/basic_command/$.js +8 -0
  9. package/dist/plugins/basic_command/audio.js +40 -0
  10. package/dist/plugins/basic_command/basic.js +124 -0
  11. package/dist/plugins/basic_command/character.js +112 -0
  12. package/dist/plugins/basic_command/conditional.js +260 -0
  13. package/dist/plugins/basic_command/config.js +22 -0
  14. package/dist/plugins/basic_command/decorator.js +24 -0
  15. package/dist/plugins/basic_command/eval.js +67 -0
  16. package/dist/plugins/basic_command/img.js +249 -0
  17. package/dist/plugins/basic_command/index.js +22 -0
  18. package/dist/plugins/basic_command/menu.js +140 -0
  19. package/dist/plugins/global_gesture/index.js +25 -0
  20. package/dist/plugins/transform/animation.js +440 -0
  21. package/dist/plugins/transform/commands.js +38 -0
  22. package/dist/plugins/transform/example_profiles.js +32 -0
  23. package/dist/plugins/transform/hz_anime.js +211 -0
  24. package/dist/plugins/transform/index.js +93 -0
  25. package/dist/script/index.js +537 -0
  26. package/dist/script/readscript.js +15 -0
  27. package/dist/script/strtools.js +157 -0
  28. package/dist/storage/decorator.js +260 -0
  29. package/dist/storage/fs.js +96 -0
  30. package/dist/storage/index.js +442 -0
  31. package/dist/system/index.js +144 -0
  32. package/dist/ui/index.js +535 -0
  33. package/dist/utils/path.js +289 -0
  34. package/license.txt +202 -0
  35. package/package.json +26 -0
  36. package/src/async/index.ts +124 -0
  37. package/src/async/zeppos_timer.js +65 -0
  38. package/src/audio/index.ts +224 -0
  39. package/src/config/index.ts +80 -0
  40. package/src/debug/index.ts +11 -0
  41. package/src/index.ts +122 -0
  42. package/src/platform/index.ts +158 -0
  43. package/src/plugins/basic_command/$.ts +11 -0
  44. package/src/plugins/basic_command/audio.ts +53 -0
  45. package/src/plugins/basic_command/basic.ts +145 -0
  46. package/src/plugins/basic_command/character.ts +144 -0
  47. package/src/plugins/basic_command/conditional.ts +349 -0
  48. package/src/plugins/basic_command/config.ts +29 -0
  49. package/src/plugins/basic_command/decorator.ts +29 -0
  50. package/src/plugins/basic_command/eval.ts +88 -0
  51. package/src/plugins/basic_command/img.ts +317 -0
  52. package/src/plugins/basic_command/index.ts +24 -0
  53. package/src/plugins/basic_command/menu.ts +178 -0
  54. package/src/plugins/global_gesture/index.ts +29 -0
  55. package/src/plugins/transform/animation.ts +542 -0
  56. package/src/plugins/transform/commands.ts +53 -0
  57. package/src/plugins/transform/example_profiles.ts +36 -0
  58. package/src/plugins/transform/hz_anime.ts +214 -0
  59. package/src/plugins/transform/index.ts +141 -0
  60. package/src/plugins/transform/readme.md +1 -0
  61. package/src/script/index.ts +623 -0
  62. package/src/script/readscript.ts +17 -0
  63. package/src/script/strtools.ts +159 -0
  64. package/src/storage/decorator.ts +473 -0
  65. package/src/storage/fs.ts +104 -0
  66. package/src/storage/index.ts +541 -0
  67. package/src/system/index.ts +95 -0
  68. package/src/ui/index.ts +699 -0
  69. package/src/utils/path.js +338 -0
  70. package/tsconfig.json +111 -0
  71. package/types/async/index.d.ts +24 -0
  72. package/types/async/zeppos_timer.d.ts +14 -0
  73. package/types/audio/index.d.ts +64 -0
  74. package/types/config/index.d.ts +9 -0
  75. package/types/debug/index.d.ts +6 -0
  76. package/types/index.d.ts +41 -0
  77. package/types/platform/index.d.ts +134 -0
  78. package/types/plugins/basic_command/$.d.ts +2 -0
  79. package/types/plugins/basic_command/audio.d.ts +2 -0
  80. package/types/plugins/basic_command/basic.d.ts +3 -0
  81. package/types/plugins/basic_command/character.d.ts +2 -0
  82. package/types/plugins/basic_command/conditional.d.ts +2 -0
  83. package/types/plugins/basic_command/config.d.ts +2 -0
  84. package/types/plugins/basic_command/decorator.d.ts +2 -0
  85. package/types/plugins/basic_command/eval.d.ts +2 -0
  86. package/types/plugins/basic_command/img.d.ts +2 -0
  87. package/types/plugins/basic_command/index.d.ts +2 -0
  88. package/types/plugins/basic_command/menu.d.ts +2 -0
  89. package/types/plugins/global_gesture/index.d.ts +2 -0
  90. package/types/plugins/transform/animation.d.ts +131 -0
  91. package/types/plugins/transform/commands.d.ts +7 -0
  92. package/types/plugins/transform/example_profiles.d.ts +2 -0
  93. package/types/plugins/transform/hz_anime.d.ts +51 -0
  94. package/types/plugins/transform/index.d.ts +13 -0
  95. package/types/script/index.d.ts +123 -0
  96. package/types/script/readscript.d.ts +2 -0
  97. package/types/script/strtools.d.ts +31 -0
  98. package/types/storage/decorator.d.ts +41 -0
  99. package/types/storage/fs.d.ts +1 -0
  100. package/types/storage/index.d.ts +86 -0
  101. package/types/system/index.d.ts +35 -0
  102. package/types/ui/index.d.ts +167 -0
  103. package/types/utils/path.d.ts +84 -0
@@ -0,0 +1,535 @@
1
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
2
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
3
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
4
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
5
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
6
+ var _, done = false;
7
+ for (var i = decorators.length - 1; i >= 0; i--) {
8
+ var context = {};
9
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
10
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
11
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
12
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
13
+ if (kind === "accessor") {
14
+ if (result === void 0) continue;
15
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
16
+ if (_ = accept(result.get)) descriptor.get = _;
17
+ if (_ = accept(result.set)) descriptor.set = _;
18
+ if (_ = accept(result.init)) initializers.unshift(_);
19
+ }
20
+ else if (_ = accept(result)) {
21
+ if (kind === "field") initializers.unshift(_);
22
+ else descriptor[key] = _;
23
+ }
24
+ }
25
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
26
+ done = true;
27
+ };
28
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
29
+ var useValue = arguments.length > 2;
30
+ for (var i = 0; i < initializers.length; i++) {
31
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
32
+ }
33
+ return useValue ? value : void 0;
34
+ };
35
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
36
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
37
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
38
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
39
+ };
40
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
41
+ if (kind === "m") throw new TypeError("Private method is not writable");
42
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
43
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
44
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
45
+ };
46
+ import { Save, CustomSave } from "../storage/decorator.js";
47
+ // / <reference path="node_modules/@zeppos/device-types/dist/index.d.ts" />
48
+ // import * as hmUI from "@zos/ui";
49
+ // import {} from "@zos/ui";
50
+ // import { getDeviceInfo, SCREEN_SHAPE_SQUARE } from "@zos/device";
51
+ // const { width, height, screenShape } = getDeviceInfo();
52
+ let UI = (() => {
53
+ var _a, _UI__layerList_accessor_storage, _UI__nextViewId_accessor_storage, _UI__viewMap_accessor_storage, _UI__routerMap_accessor_storage;
54
+ let __layerList_decorators;
55
+ let __layerList_initializers = [];
56
+ let __layerList_extraInitializers = [];
57
+ let __nextViewId_decorators;
58
+ let __nextViewId_initializers = [];
59
+ let __nextViewId_extraInitializers = [];
60
+ let __viewMap_decorators;
61
+ let __viewMap_initializers = [];
62
+ let __viewMap_extraInitializers = [];
63
+ let __routerMap_decorators;
64
+ let __routerMap_initializers = [];
65
+ let __routerMap_extraInitializers = [];
66
+ return _a = class UI {
67
+ constructor(_core) {
68
+ this._core = _core;
69
+ _UI__layerList_accessor_storage.set(this, __runInitializers(this, __layerList_initializers, new Map()));
70
+ // View Class
71
+ this._viewClassMap = (__runInitializers(this, __layerList_extraInitializers), new Map());
72
+ _UI__nextViewId_accessor_storage.set(this, __runInitializers(this, __nextViewId_initializers, 50));
73
+ _UI__viewMap_accessor_storage.set(this, (__runInitializers(this, __nextViewId_extraInitializers), __runInitializers(this, __viewMap_initializers, new Map())));
74
+ _UI__routerMap_accessor_storage.set(this, (__runInitializers(this, __viewMap_extraInitializers), __runInitializers(this, __routerMap_initializers, new Map())));
75
+ __runInitializers(this, __routerMap_extraInitializers);
76
+ this._core = _core;
77
+ this._initUI();
78
+ }
79
+ _initUI() {
80
+ this.addLayer("bg", 1);
81
+ this.addLayer("fg", 2);
82
+ this.addLayer("ct", 3);
83
+ this.addLayer("overlay", 4);
84
+ this.addRouter("page", "overlay", false);
85
+ }
86
+ _cleanUI() {
87
+ for (let [key, value] of this.layerList) {
88
+ value.destroy();
89
+ }
90
+ this.layerList.clear();
91
+ this._routerMap.clear();
92
+ }
93
+ resetUI() {
94
+ this._cleanUI();
95
+ this._initUI();
96
+ }
97
+ // Layer
98
+ get _layerList() { return __classPrivateFieldGet(this, _UI__layerList_accessor_storage, "f"); }
99
+ set _layerList(value) { __classPrivateFieldSet(this, _UI__layerList_accessor_storage, value, "f"); }
100
+ get layerList() {
101
+ return this._layerList;
102
+ }
103
+ addLayer(name, z_index) {
104
+ this._core.emit("beforeAddLayer", name, z_index);
105
+ if (this._layerList.has(name))
106
+ throw `Layer ${name} already exist`;
107
+ let newLayer = new _a.Layer(this._core, name, z_index);
108
+ this._layerList.set(name, newLayer);
109
+ this._core.emit("afterAddLayer", newLayer);
110
+ }
111
+ getLayer(name) {
112
+ return this.layerList.get(name);
113
+ }
114
+ // _activeViewList: [name: string, layer: string, instance: UI.View<unknown>][] =
115
+ // [];
116
+ registerView(name, cls) {
117
+ this._viewClassMap.set(name, cls);
118
+ }
119
+ // View
120
+ get _nextViewId() { return __classPrivateFieldGet(this, _UI__nextViewId_accessor_storage, "f"); }
121
+ set _nextViewId(value) { __classPrivateFieldSet(this, _UI__nextViewId_accessor_storage, value, "f"); }
122
+ get _viewMap() { return __classPrivateFieldGet(this, _UI__viewMap_accessor_storage, "f"); }
123
+ set _viewMap(value) { __classPrivateFieldSet(this, _UI__viewMap_accessor_storage, value, "f"); }
124
+ getView(id) {
125
+ var _b;
126
+ return (_b = this._viewMap.get(id)) !== null && _b !== void 0 ? _b : null;
127
+ }
128
+ createView(name, layer, prop, isSave) {
129
+ let id = this._nextViewId++;
130
+ let viewInstance = this._produceViewWithId(name, layer, prop, id);
131
+ this._core.debug.log(`creating view ${viewInstance.name}`);
132
+ viewInstance.isSave = isSave;
133
+ this._viewMap.set(id, viewInstance);
134
+ return viewInstance;
135
+ }
136
+ updateView(viewInstance, new_prop) {
137
+ viewInstance.commit(new_prop);
138
+ }
139
+ destroyView(viewInstance) {
140
+ if (viewInstance.id != null)
141
+ this._viewMap.delete(viewInstance.id);
142
+ viewInstance.destroy();
143
+ }
144
+ /**由調用者提供id,創建一個View,不會處理isSave,也不會更新viewMap */
145
+ _produceViewWithId(name, layer, prop, id) {
146
+ if (!this._viewClassMap.get(name)) {
147
+ throw "要创建的View不存在";
148
+ }
149
+ let _ViewFactory = this._viewClassMap.get(name);
150
+ let viewInstance = new _ViewFactory(layer, this._core);
151
+ viewInstance.id = id;
152
+ viewInstance.name = name;
153
+ viewInstance.create(prop);
154
+ this._core.debug.log(`producing view ${viewInstance.name}`);
155
+ return viewInstance;
156
+ }
157
+ get _routerMap() { return __classPrivateFieldGet(this, _UI__routerMap_accessor_storage, "f"); }
158
+ set _routerMap(value) { __classPrivateFieldSet(this, _UI__routerMap_accessor_storage, value, "f"); }
159
+ getRouter(tag) {
160
+ return this._routerMap.get(tag);
161
+ }
162
+ addRouter(tag, layer, isSave = true) {
163
+ if (this._routerMap.has(tag))
164
+ throw `Route with tag [${tag}] already exist!`;
165
+ let router = new _a.Router(this, tag, layer, isSave);
166
+ this._routerMap.set(tag, router);
167
+ return router;
168
+ }
169
+ getScreenSize() {
170
+ let [width, height] = this._core.platform.getScreenSize();
171
+ return { width, height };
172
+ }
173
+ /**
174
+ * 根据 BasicUniversalProp 计算屏幕上的位置
175
+ * @param prop 包含 BasicUniversalProp 的 prop
176
+ * @param size (可选)图像的尺寸,若不指定,返回的anchor坐标和origin坐标一样
177
+ * @returns
178
+ */
179
+ calcPosition(prop, size) {
180
+ var _b, _c, _d, _e, _f, _g, _h, _j;
181
+ let { width, height } = this.getScreenSize();
182
+ // 1. 确定 anchor
183
+ // 2. 通过 align 确定初始位置
184
+ // 3. offset
185
+ // 返回左上角的位置
186
+ let anchor_coord = {
187
+ x: (width * (((_b = prop.xalign) !== null && _b !== void 0 ? _b : 0) + 1)) / 2 + // 根据 align 求出 anchor 位置
188
+ ((_c = prop.xoffset) !== null && _c !== void 0 ? _c : 0), // offset
189
+ y: (height * (((_d = prop.yalign) !== null && _d !== void 0 ? _d : 0) + 1)) / 2 + // 根据 align 求出 anchor 位置
190
+ ((_e = prop.yoffset) !== null && _e !== void 0 ? _e : 0), // offset
191
+ };
192
+ let origin_coord = {
193
+ x: anchor_coord.x - ((((_f = prop.xanchor) !== null && _f !== void 0 ? _f : 0) + 1) / 2) * ((_g = size === null || size === void 0 ? void 0 : size.width) !== null && _g !== void 0 ? _g : 0),
194
+ y: anchor_coord.y - ((((_h = prop.yanchor) !== null && _h !== void 0 ? _h : 0) + 1) / 2) * ((_j = size === null || size === void 0 ? void 0 : size.height) !== null && _j !== void 0 ? _j : 0),
195
+ };
196
+ return {
197
+ anchor: anchor_coord,
198
+ origin: origin_coord,
199
+ };
200
+ }
201
+ },
202
+ _UI__layerList_accessor_storage = new WeakMap(),
203
+ _UI__nextViewId_accessor_storage = new WeakMap(),
204
+ _UI__viewMap_accessor_storage = new WeakMap(),
205
+ _UI__routerMap_accessor_storage = new WeakMap(),
206
+ (() => {
207
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
208
+ __layerList_decorators = [CustomSave("ui.layerList", function serializer(layerList) {
209
+ let obj = {};
210
+ for (let [key, value] of this.layerList) {
211
+ obj[key] = [value.name, value.z_index];
212
+ }
213
+ return obj;
214
+ }, function deserializer(obj) {
215
+ // destroy old layer
216
+ for (let [key, value] of this.layerList) {
217
+ value.destroy();
218
+ }
219
+ this.layerList.clear();
220
+ let newLayerList = new Map();
221
+ // create new layer
222
+ for (let key in obj) {
223
+ let newLayer = new UI.Layer(this._core, obj[key][0], obj[key][1]);
224
+ newLayerList.set(key, newLayer);
225
+ this._core.emit("afterAddLayer", newLayer);
226
+ }
227
+ return newLayerList;
228
+ })];
229
+ __nextViewId_decorators = [Save("ui.nextViewId")];
230
+ __viewMap_decorators = [CustomSave("ui.viewMap", function serializer(viewMap) {
231
+ let obj = {};
232
+ for (let [id, view] of viewMap) {
233
+ // 注意viewMap中的id是number,而obj中的id會自動轉成string
234
+ if (view.isSave)
235
+ obj[id] = view.serialize();
236
+ }
237
+ return obj;
238
+ }, function deserializer(obj) {
239
+ let newViewMap = new Map();
240
+ for (let key in obj) {
241
+ let item = obj[key];
242
+ let view = this._produceViewWithId(item.name, item.layer, item.prop, Number(key));
243
+ view.isSave = true;
244
+ newViewMap.set(Number(key), view);
245
+ }
246
+ return newViewMap;
247
+ })];
248
+ __routerMap_decorators = [CustomSave("ui.routerMap", function serializer(routerMap) {
249
+ let obj = {};
250
+ for (let [key, value] of routerMap) {
251
+ if (!value.isSave)
252
+ continue;
253
+ obj[key] = value.serialize();
254
+ }
255
+ return obj;
256
+ }, function deserializer(obj) {
257
+ let newRouterMap = new Map();
258
+ // reshow not save router
259
+ for (let [name, router] of this._routerMap) {
260
+ if (!router.isSave) {
261
+ if (router.length > 0) {
262
+ // TODO 因爲讀檔的時候會重置整個ui系統,所以要重新創建activeViewInstance 這裏感覺有點問題
263
+ router.activeViewInstance = this._core.ui.createView(router.viewStack[0][0], router.layer, router.viewStack[0][1], router.isSave);
264
+ }
265
+ newRouterMap.set(name, router);
266
+ }
267
+ }
268
+ // reshow save router
269
+ for (let key in obj) {
270
+ newRouterMap.set(key, UI.Router.deserialize(this, obj[key]));
271
+ }
272
+ return newRouterMap;
273
+ })];
274
+ __esDecorate(_a, null, __layerList_decorators, { kind: "accessor", name: "_layerList", static: false, private: false, access: { has: obj => "_layerList" in obj, get: obj => obj._layerList, set: (obj, value) => { obj._layerList = value; } }, metadata: _metadata }, __layerList_initializers, __layerList_extraInitializers);
275
+ __esDecorate(_a, null, __nextViewId_decorators, { kind: "accessor", name: "_nextViewId", static: false, private: false, access: { has: obj => "_nextViewId" in obj, get: obj => obj._nextViewId, set: (obj, value) => { obj._nextViewId = value; } }, metadata: _metadata }, __nextViewId_initializers, __nextViewId_extraInitializers);
276
+ __esDecorate(_a, null, __viewMap_decorators, { kind: "accessor", name: "_viewMap", static: false, private: false, access: { has: obj => "_viewMap" in obj, get: obj => obj._viewMap, set: (obj, value) => { obj._viewMap = value; } }, metadata: _metadata }, __viewMap_initializers, __viewMap_extraInitializers);
277
+ __esDecorate(_a, null, __routerMap_decorators, { kind: "accessor", name: "_routerMap", static: false, private: false, access: { has: obj => "_routerMap" in obj, get: obj => obj._routerMap, set: (obj, value) => { obj._routerMap = value; } }, metadata: _metadata }, __routerMap_initializers, __routerMap_extraInitializers);
278
+ if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
279
+ })(),
280
+ _a;
281
+ })();
282
+ export { UI };
283
+ (function (UI) {
284
+ class View {
285
+ get prop() {
286
+ return this._prop;
287
+ }
288
+ set prop(prop) {
289
+ this._prop = prop;
290
+ }
291
+ constructor(layer, core) {
292
+ this.layer = layer;
293
+ this.core = core;
294
+ this.id = null;
295
+ this.name = null;
296
+ this.isSave = true;
297
+ this._prop = null;
298
+ }
299
+ create(prop) {
300
+ this.prop = prop;
301
+ this.onCreate(prop);
302
+ }
303
+ commit(prop) {
304
+ this.prop = prop;
305
+ this.onCommit(prop);
306
+ }
307
+ destroy() {
308
+ this.onDestroy();
309
+ this.prop = null;
310
+ this.id = null;
311
+ }
312
+ serialize() {
313
+ if (this.name == null)
314
+ throw new Error("View name is null when serialize");
315
+ return {
316
+ name: this.name,
317
+ layer: this.layer,
318
+ prop: this.prop,
319
+ };
320
+ }
321
+ }
322
+ UI.View = View;
323
+ class MessageView extends View {
324
+ }
325
+ UI.MessageView = MessageView;
326
+ class MenuView extends View {
327
+ }
328
+ UI.MenuView = MenuView;
329
+ class FgImgView extends View {
330
+ }
331
+ UI.FgImgView = FgImgView;
332
+ class BgImgView extends View {
333
+ }
334
+ UI.BgImgView = BgImgView;
335
+ class Layer {
336
+ constructor(_core, name, z_index) {
337
+ this._core = _core;
338
+ this.name = name;
339
+ this.z_index = z_index;
340
+ // this.widgetFactory = hmUI.createWidget(
341
+ // (hmUI.widget as any).VIEW_CONTAINER,
342
+ // {
343
+ // scroll_enable: 0,
344
+ // z_index,
345
+ // }
346
+ // ) as unknown as Layer.WidgetFactory;
347
+ this.widgetFactory = _core.platform.createUILayer({
348
+ z_index,
349
+ });
350
+ }
351
+ destroy() {
352
+ // hmUI.deleteWidget(this.widgetFactory as any);
353
+ this._core.platform.deleteUILayer(this.widgetFactory);
354
+ }
355
+ }
356
+ UI.Layer = Layer;
357
+ class Router {
358
+ constructor(_ui, tag, layer, isSave = true) {
359
+ this._ui = _ui;
360
+ this.tag = tag;
361
+ this.layer = layer;
362
+ this.isSave = isSave;
363
+ this.defaultRouteStrategy = {
364
+ destroy: (viewInstance, ui) => {
365
+ this._ui._core.debug.log(`destroy view ${viewInstance.name}`);
366
+ ui.destroyView(viewInstance);
367
+ },
368
+ create: (viewName, layer, prop, ui, isSave) => {
369
+ this._ui._core.debug.log(`create view ${viewName}`);
370
+ return ui.createView(viewName, layer, prop, isSave);
371
+ },
372
+ update: (viewInstance, prop, ui) => {
373
+ this._ui._core.debug.log(`update view ${viewInstance.name}`);
374
+ ui.updateView(viewInstance, prop);
375
+ },
376
+ };
377
+ this.viewStack = [];
378
+ this.activeViewInstance = null;
379
+ }
380
+ serialize() {
381
+ var _a, _b;
382
+ return {
383
+ tag: this.tag,
384
+ layer: this.layer,
385
+ isSave: this.isSave,
386
+ viewStack: this.viewStack,
387
+ activeViewId: (_b = (_a = this.activeViewInstance) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : null,
388
+ };
389
+ }
390
+ static deserialize(ui, data) {
391
+ let router = new Router(ui, data.tag, data.layer, data.isSave);
392
+ router.viewStack = data.viewStack;
393
+ if (data.activeViewId != null) {
394
+ let viewInstance = ui.getView(data.activeViewId);
395
+ if (!viewInstance)
396
+ throw `View [${data.activeViewId}] not found when deserialize`;
397
+ router.activeViewInstance = viewInstance;
398
+ }
399
+ return router;
400
+ }
401
+ get length() {
402
+ return this.viewStack.length;
403
+ }
404
+ push(view_name, prop, strategy) {
405
+ if (this.activeViewInstance) {
406
+ // this._ui.destroyView(this.activeViewInstance);
407
+ // (strategy?.destroy ?? this.defaultRouteStrategy.destroy!)(
408
+ // this.activeViewInstance,
409
+ // this._ui
410
+ // );
411
+ if (strategy === null || strategy === void 0 ? void 0 : strategy.destroy) {
412
+ strategy.destroy(this.activeViewInstance, this._ui);
413
+ }
414
+ else {
415
+ this.defaultRouteStrategy.destroy(this.activeViewInstance, this._ui);
416
+ }
417
+ this.activeViewInstance = null;
418
+ }
419
+ let layerInstance = this._ui.getLayer(this.layer);
420
+ if (!layerInstance)
421
+ throw `Layer [${this.layer}] not found`;
422
+ // this.activeViewInstance = (
423
+ // strategy?.create ?? this.defaultRouteStrategy.create!
424
+ // )(view_name, this.layer, prop, this._ui, this.isSave);
425
+ if (strategy === null || strategy === void 0 ? void 0 : strategy.create) {
426
+ this.activeViewInstance = strategy.create(view_name, this.layer, prop, this._ui, this.isSave);
427
+ }
428
+ else {
429
+ this.activeViewInstance = this.defaultRouteStrategy.create(view_name, this.layer, prop, this._ui, this.isSave);
430
+ }
431
+ this.viewStack.push([view_name, prop]);
432
+ }
433
+ pop(back_prop, strategy) {
434
+ if (this.activeViewInstance) {
435
+ // this._ui.destroyView(this.activeViewInstance);
436
+ // (strategy?.destroy ?? this.defaultRouteStrategy.destroy!)(
437
+ // this.activeViewInstance,
438
+ // this._ui
439
+ // );
440
+ if (strategy === null || strategy === void 0 ? void 0 : strategy.destroy) {
441
+ strategy.destroy(this.activeViewInstance, this._ui);
442
+ }
443
+ else {
444
+ this.defaultRouteStrategy.destroy(this.activeViewInstance, this._ui);
445
+ }
446
+ this.activeViewInstance = null;
447
+ }
448
+ this.viewStack.pop();
449
+ if (this.viewStack.length) {
450
+ let backViewInfo = this.viewStack[this.viewStack.length - 1];
451
+ let layerInstance = this._ui.getLayer(this.layer);
452
+ if (!layerInstance)
453
+ throw `Layer [${this.layer}] not found`;
454
+ // this.activeViewInstance = (
455
+ // strategy?.create ?? this.defaultRouteStrategy.create!
456
+ // )(
457
+ // backViewInfo[0],
458
+ // this.layer,
459
+ // back_prop ?? backViewInfo[1],
460
+ // this._ui,
461
+ // this.isSave
462
+ // );
463
+ if (strategy === null || strategy === void 0 ? void 0 : strategy.create) {
464
+ this.activeViewInstance = strategy.create(backViewInfo[0], this.layer, back_prop !== null && back_prop !== void 0 ? back_prop : backViewInfo[1], this._ui, this.isSave);
465
+ }
466
+ else {
467
+ this.activeViewInstance = this.defaultRouteStrategy.create(backViewInfo[0], this.layer, back_prop !== null && back_prop !== void 0 ? back_prop : backViewInfo[1], this._ui, this.isSave);
468
+ }
469
+ }
470
+ }
471
+ replace(view_name, prop, strategy) {
472
+ if (this.activeViewInstance) {
473
+ // this._ui.destroyView(this.activeViewInstance);
474
+ // (strategy?.destroy ?? this.defaultRouteStrategy.destroy!)(
475
+ // this.activeViewInstance,
476
+ // this._ui
477
+ // );
478
+ if (strategy === null || strategy === void 0 ? void 0 : strategy.destroy) {
479
+ strategy.destroy(this.activeViewInstance, this._ui);
480
+ }
481
+ else {
482
+ this.defaultRouteStrategy.destroy(this.activeViewInstance, this._ui);
483
+ }
484
+ this.activeViewInstance = null;
485
+ }
486
+ this.viewStack.pop();
487
+ let layerInstance = this._ui.getLayer(this.layer);
488
+ if (!layerInstance)
489
+ throw `Layer [${this.layer}] not found`;
490
+ // this.activeViewInstance = (
491
+ // strategy?.create ?? this.defaultRouteStrategy.create!
492
+ // )(view_name, this.layer, prop, this._ui, this.isSave);
493
+ if (strategy === null || strategy === void 0 ? void 0 : strategy.create) {
494
+ this.activeViewInstance = strategy.create(view_name, this.layer, prop, this._ui, this.isSave);
495
+ }
496
+ else {
497
+ this.activeViewInstance = this.defaultRouteStrategy.create(view_name, this.layer, prop, this._ui, this.isSave);
498
+ }
499
+ this.viewStack.push([view_name, prop]);
500
+ }
501
+ update(prop, strategy) {
502
+ if (!this.activeViewInstance)
503
+ throw `Update View but activeViewInstance is null`;
504
+ this.viewStack[this.viewStack.length - 1][1] = prop;
505
+ // (strategy?.update ?? this.defaultRouteStrategy.update!)(
506
+ // this.activeViewInstance,
507
+ // prop,
508
+ // this._ui
509
+ // );
510
+ if (strategy === null || strategy === void 0 ? void 0 : strategy.update) {
511
+ strategy.update(this.activeViewInstance, prop, this._ui);
512
+ }
513
+ else {
514
+ this.defaultRouteStrategy.update(this.activeViewInstance, prop, this._ui);
515
+ }
516
+ }
517
+ clear(strategy) {
518
+ if (this.activeViewInstance) {
519
+ // (strategy?.destroy ?? this.defaultRouteStrategy.destroy!)(
520
+ // this.activeViewInstance,
521
+ // this._ui
522
+ // );
523
+ if (strategy === null || strategy === void 0 ? void 0 : strategy.destroy) {
524
+ strategy.destroy(this.activeViewInstance, this._ui);
525
+ }
526
+ else {
527
+ this.defaultRouteStrategy.destroy(this.activeViewInstance, this._ui);
528
+ }
529
+ this.activeViewInstance = null;
530
+ }
531
+ this.viewStack = [];
532
+ }
533
+ }
534
+ UI.Router = Router;
535
+ })(UI || (UI = {}));