@panomc/sdk 1.0.0-dev.8 → 2.0.0-dev.61

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 (162) hide show
  1. package/api/routes.core.json +3702 -0
  2. package/bin/api/__tests__/api-compat.test.mjs +97 -0
  3. package/bin/api/__tests__/check-paths.test.mjs +113 -0
  4. package/bin/api/__tests__/cli.test.mjs +44 -0
  5. package/bin/api/__tests__/extract-routes.test.mjs +132 -0
  6. package/bin/api/__tests__/fixtures/core-new/Pano/api/paths.old-new.json +82 -0
  7. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
  8. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
  9. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
  10. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
  11. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
  12. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
  13. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
  14. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
  15. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
  16. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
  17. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
  18. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
  19. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
  20. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
  21. package/bin/api/__tests__/fixtures/plugin-new/api/paths.old-new.json +66 -0
  22. package/bin/api/__tests__/fixtures/plugin-new/build.gradle.kts +42 -0
  23. package/bin/api/__tests__/fixtures/plugin-new/gradle.properties +4 -0
  24. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
  25. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
  26. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
  27. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
  28. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
  29. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
  30. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
  31. package/bin/api/__tests__/fixtures/plugin-new/src/panel/Things.js +4 -0
  32. package/bin/api/__tests__/fixtures/plugin-new/src/theme/Logo.svelte +9 -0
  33. package/bin/api/__tests__/fixtures/plugin-new/src/theme/api.js +29 -0
  34. package/bin/api/__tests__/fixtures/plugin-old/build.gradle.kts +22 -0
  35. package/bin/api/__tests__/fixtures/plugin-old/gradle.properties +4 -0
  36. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
  37. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
  38. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
  39. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
  40. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
  41. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
  42. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
  43. package/bin/api/__tests__/fixtures/plugin-old/src/panel/Things.js +4 -0
  44. package/bin/api/__tests__/fixtures/plugin-old/src/theme/Logo.svelte +9 -0
  45. package/bin/api/__tests__/fixtures/plugin-old/src/theme/api.js +29 -0
  46. package/bin/api/__tests__/fixtures/refusals/gradle.properties +1 -0
  47. package/bin/api/__tests__/fixtures/refusals/src/main/kotlin/x/Bad.kt +39 -0
  48. package/bin/api/__tests__/fixtures/unresolved/src/main/kotlin/x/Orphan.kt +19 -0
  49. package/bin/api/__tests__/helpers.mjs +48 -0
  50. package/bin/api/__tests__/migrate-v1.test.mjs +377 -0
  51. package/bin/api/__tests__/openapi-snapshot.test.mjs +120 -0
  52. package/bin/api/api-compat.mjs +204 -0
  53. package/bin/api/check-paths.mjs +135 -0
  54. package/bin/api/cli.mjs +83 -0
  55. package/bin/api/extract-routes.mjs +129 -0
  56. package/bin/api/js-scan.mjs +337 -0
  57. package/bin/api/kotlin.mjs +366 -0
  58. package/bin/api/migrate-v1.mjs +330 -0
  59. package/bin/api/openapi-snapshot.mjs +112 -0
  60. package/bin/api/project.mjs +172 -0
  61. package/bin/api/rename-table.mjs +66 -0
  62. package/bin/api/util.mjs +194 -0
  63. package/bin/pano-api.js +5 -0
  64. package/build/plugin-api-rollup.js +46 -0
  65. package/core/css/pano-tokens.css +225 -0
  66. package/core/favicon/apple-touch-icon.png +0 -0
  67. package/core/favicon/favicon-96x96.png +0 -0
  68. package/core/favicon/favicon.ico +0 -0
  69. package/core/favicon/favicon.svg +1 -0
  70. package/core/favicon/site.webmanifest +21 -0
  71. package/core/favicon/web-app-manifest-192x192.png +0 -0
  72. package/core/favicon/web-app-manifest-512x512.png +0 -0
  73. package/core/favicon_old/android-icon-144x144.png +0 -0
  74. package/core/favicon_old/android-icon-192x192.png +0 -0
  75. package/core/favicon_old/android-icon-36x36.png +0 -0
  76. package/core/favicon_old/android-icon-48x48.png +0 -0
  77. package/core/favicon_old/android-icon-72x72.png +0 -0
  78. package/core/favicon_old/android-icon-96x96.png +0 -0
  79. package/core/favicon_old/apple-icon-114x114.png +0 -0
  80. package/core/favicon_old/apple-icon-120x120.png +0 -0
  81. package/core/favicon_old/apple-icon-144x144.png +0 -0
  82. package/core/favicon_old/apple-icon-152x152.png +0 -0
  83. package/core/favicon_old/apple-icon-180x180.png +0 -0
  84. package/core/favicon_old/apple-icon-57x57.png +0 -0
  85. package/core/favicon_old/apple-icon-60x60.png +0 -0
  86. package/core/favicon_old/apple-icon-72x72.png +0 -0
  87. package/core/favicon_old/apple-icon-76x76.png +0 -0
  88. package/core/favicon_old/apple-icon-precomposed.png +0 -0
  89. package/core/favicon_old/apple-icon.png +0 -0
  90. package/core/favicon_old/browserconfig.xml +11 -0
  91. package/core/favicon_old/favicon-16x16.png +0 -0
  92. package/core/favicon_old/favicon-32x32.png +0 -0
  93. package/core/favicon_old/favicon-96x96.png +0 -0
  94. package/core/favicon_old/favicon.ico +0 -0
  95. package/core/favicon_old/manifest.json +41 -0
  96. package/core/favicon_old/ms-icon-144x144.png +0 -0
  97. package/core/favicon_old/ms-icon-150x150.png +0 -0
  98. package/core/favicon_old/ms-icon-310x310.png +0 -0
  99. package/core/favicon_old/ms-icon-70x70.png +0 -0
  100. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.eot +0 -0
  101. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.svg +483 -0
  102. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.ttf +0 -0
  103. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff +0 -0
  104. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff2 +0 -0
  105. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.eot +0 -0
  106. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.svg +486 -0
  107. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.ttf +0 -0
  108. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff +0 -0
  109. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff2 +0 -0
  110. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.eot +0 -0
  111. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.svg +484 -0
  112. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.ttf +0 -0
  113. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff +0 -0
  114. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff2 +0 -0
  115. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.eot +0 -0
  116. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.svg +486 -0
  117. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.ttf +0 -0
  118. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff +0 -0
  119. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff2 +0 -0
  120. package/core/js/ControllerRegistry.js +421 -0
  121. package/core/js/NotificationManager.js +62 -0
  122. package/core/js/PluginAPI.js +39 -0
  123. package/core/js/PluginManager.js +851 -0
  124. package/core/js/RouteMatcher.js +142 -0
  125. package/core/js/__tests__/ControllerRegistry.test.js +378 -0
  126. package/core/js/__tests__/NotificationManager.test.js +57 -0
  127. package/core/js/__tests__/PluginAPI.test.js +25 -0
  128. package/core/js/__tests__/RouteMatcher.test.js +33 -0
  129. package/core/js/__tests__/accessClient.test.js +582 -0
  130. package/core/js/__tests__/api.util.v1.test.js +336 -0
  131. package/core/js/__tests__/devBounce.test.js +28 -0
  132. package/core/js/__tests__/html.util.test.js +18 -0
  133. package/core/js/__tests__/pluginFolder.util.test.js +106 -0
  134. package/core/js/__tests__/pluginPackage.test.js +134 -0
  135. package/core/js/__tests__/pluginUiZip.test.js +47 -0
  136. package/core/js/__tests__/sdkLevel.test.js +34 -0
  137. package/core/js/api.util.js +433 -0
  138. package/core/js/html.util.js +26 -0
  139. package/core/js/pluginFolder.util.js +95 -0
  140. package/core/js/tooltip.util.js +59 -0
  141. package/core/js/variables.js +95 -0
  142. package/core/js/version.util.js +100 -0
  143. package/core/scss/_pano-tokens.scss +51 -0
  144. package/core/scss/custom-bootstrap.scss +196 -0
  145. package/core/scss/fonts.scss +85 -0
  146. package/core/scss/main.scss +389 -0
  147. package/core/scss/themes.scss +120 -0
  148. package/package.json +50 -10
  149. package/src/__tests__/controllers.test.js +220 -0
  150. package/src/__tests__/views.spike.test.js +472 -0
  151. package/src/components/{index.js → panel.js} +9 -6
  152. package/src/components/theme.js +18 -0
  153. package/src/controllers.js +127 -0
  154. package/src/index.js +1 -1
  155. package/src/svelte.js +3 -2
  156. package/src/types.js +5 -3
  157. package/src/utils/api.js +42 -1
  158. package/src/utils/auth.js +8 -0
  159. package/src/utils/component.js +4 -2
  160. package/src/utils/route.js +16 -0
  161. package/src/utils/text.js +8 -0
  162. package/src/views.js +29 -0
@@ -0,0 +1,421 @@
1
+ /**
2
+ * Controller registry (doc 02 §2).
3
+ *
4
+ * State lives on `globalThis.__PANO_CONTROLLERS__` so a plugin bundle that carries its own copy of this file
5
+ * (the plugin server bundle has no externals) still talks to the same registry. Nothing request-bound is ever
6
+ * stored here: on the server a host is created per call from `opts.event` and nothing is cached.
7
+ *
8
+ * A definition is what `defineController()` of `@panomc/plugin-kit` returns:
9
+ * `{ name, version, scope?, eager?, load?, create(host, params?, initial?, ctx?) }`
10
+ * `create` returns a Controller `{ name, version, get, subscribe, actions, destroy }`. The registry passes a fourth
11
+ * argument `ctx = { use(siblingName) }` so a controller can reach a sibling of the same plugin on the same host.
12
+ *
13
+ * Unknown names and version mismatches never throw: `use()` returns null and `onMismatch` listeners are told.
14
+ */
15
+
16
+ const GLOBAL_KEY = '__PANO_CONTROLLERS__';
17
+
18
+ /**
19
+ * @typedef {Object} ControllerDefinition
20
+ * @property {string} name
21
+ * @property {number} version
22
+ * @property {'app'|'instance'} [scope]
23
+ * @property {boolean} [eager]
24
+ * @property {(c:{host:any, params:object}) => Promise<object>} [load]
25
+ * @property {(host:any, params?:object, initial?:object, ctx?:{use:(name:string)=>any}) => any} create
26
+ * @property {(patch:object) => any} [createSample]
27
+ */
28
+
29
+ /**
30
+ * @typedef {Object} UseOptions
31
+ * @property {number} [version]
32
+ * @property {object} [params]
33
+ * @property {object} [initial]
34
+ * @property {any} [event]
35
+ */
36
+
37
+ function getState() {
38
+ let s = globalThis[GLOBAL_KEY];
39
+ if (!s) {
40
+ s = {
41
+ /** @type {Map<string, {def: ControllerDefinition, pluginId: string, namespace: string, key: string}>} */
42
+ defs: new Map(),
43
+ /** @type {Map<string, string>} namespace -> pluginId */
44
+ plugins: new Map(),
45
+ /** @type {Map<string, any>} browser app-scope cache by full name */
46
+ cache: new Map(),
47
+ /** @type {Array<string>} eager names waiting for a host factory */
48
+ pendingEager: [],
49
+ /** @type {Record<string, number>} */
50
+ pins: {},
51
+ /** @type {Record<string, object> | null} */
52
+ samples: null,
53
+ /** @type {Map<string, any>} */
54
+ sampleCache: new Map(),
55
+ /** @type {null | ((event?: any) => any)} */
56
+ hostFactory: null,
57
+ browserHost: null,
58
+ /** @type {Array<(m: object) => void>} */
59
+ mismatch: [],
60
+ /** @type {Set<string>} */
61
+ warned: new Set(),
62
+ };
63
+ globalThis[GLOBAL_KEY] = s;
64
+ }
65
+ return s;
66
+ }
67
+
68
+ function isBrowser() {
69
+ return typeof window !== 'undefined' && typeof document !== 'undefined';
70
+ }
71
+
72
+ function warnOnce(key, message) {
73
+ const s = getState();
74
+ if (s.warned.has(key)) return;
75
+ s.warned.add(key);
76
+ console.warn(message);
77
+ }
78
+
79
+ function scopeOf(def) {
80
+ return def.scope === 'instance' ? 'instance' : 'app';
81
+ }
82
+
83
+ function safeDestroy(controller) {
84
+ try {
85
+ controller?.destroy?.();
86
+ } catch (e) {
87
+ console.error('[PanoSDK] controller destroy failed', e);
88
+ }
89
+ }
90
+
91
+ function hostFor(event) {
92
+ const s = getState();
93
+ if (!s.hostFactory) return null;
94
+ if (isBrowser()) {
95
+ if (!s.browserHost) s.browserHost = s.hostFactory(event);
96
+ return s.browserHost;
97
+ }
98
+ return s.hostFactory(event);
99
+ }
100
+
101
+ function report(info) {
102
+ const s = getState();
103
+ for (const fn of [...s.mismatch]) {
104
+ try {
105
+ fn(info);
106
+ } catch (e) {
107
+ console.error('[PanoSDK] onMismatch listener failed', e);
108
+ }
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Looks the name up and applies the version rule. Returns the entry or null (already reported).
114
+ * @param {string} name
115
+ * @param {UseOptions} [opts]
116
+ */
117
+ function resolveEntry(name, opts) {
118
+ const s = getState();
119
+ const entry = s.defs.get(name);
120
+
121
+ if (!entry) {
122
+ warnOnce(
123
+ `missing:${name}`,
124
+ `controller '${name}' is not registered - is ${pluginHint(name)} installed and enabled?`,
125
+ );
126
+ return null;
127
+ }
128
+
129
+ const wanted = opts?.version ?? s.pins[name];
130
+
131
+ if (wanted !== undefined && wanted !== null && wanted !== entry.def.version) {
132
+ warnOnce(
133
+ `mismatch:${name}:${wanted}:${entry.def.version}`,
134
+ `'${name}' is at version ${entry.def.version}, this theme was written for ${wanted} - run theme-core check`,
135
+ );
136
+ report({ kind: 'controller', name, wanted, available: entry.def.version });
137
+ return null;
138
+ }
139
+
140
+ return entry;
141
+ }
142
+
143
+ function pluginHint(name) {
144
+ const ns = String(name).split('/')[0];
145
+ const s = getState();
146
+ return s.plugins.get(ns) ?? `pano-plugin-${ns}`;
147
+ }
148
+
149
+ function makeCtx(entry, host) {
150
+ return {
151
+ use(sibling) {
152
+ const full = `${entry.namespace}/${sibling}`;
153
+ const found = getState().defs.get(full);
154
+ if (!found) return null;
155
+ return acquire(found, full, {}, host, true);
156
+ },
157
+ };
158
+ }
159
+
160
+ /**
161
+ * Creates or fetches the controller for an entry on a given host.
162
+ * `cacheable` is false on the server (nothing is ever cached there).
163
+ */
164
+ function acquire(entry, full, opts, host, cacheable) {
165
+ const s = getState();
166
+ const def = entry.def;
167
+ const canCache = cacheable && isBrowser() && scopeOf(def) === 'app';
168
+
169
+ if (canCache && s.cache.has(full)) return s.cache.get(full);
170
+
171
+ const controller = def.create(host, opts?.params ?? {}, opts?.initial, makeCtx(entry, host));
172
+
173
+ if (canCache) s.cache.set(full, controller);
174
+ return controller;
175
+ }
176
+
177
+ function createSampleController(entry, full, patch) {
178
+ const def = entry.def;
179
+ if (typeof def.createSample === 'function') return def.createSample(patch ?? {});
180
+
181
+ const guest = {
182
+ baseUrl: '',
183
+ browser: false,
184
+ request: async () => ({ error: { code: 'NULL_HOST' } }),
185
+ session: () => ({ user: null, csrfToken: null }),
186
+ locale: () => undefined,
187
+ onSession: () => () => {},
188
+ t: (key) => key,
189
+ toast: () => {},
190
+ storage: () => null,
191
+ now: () => Date.now(),
192
+ navigate: () => {},
193
+ feature: () => false,
194
+ loginUrl: () => '/login',
195
+ registerUrl: () => '/register',
196
+ };
197
+
198
+ let base = null;
199
+ let initial = {};
200
+ let actionNames = [];
201
+ try {
202
+ base = def.create(guest, {}, undefined, { use: () => null });
203
+ initial = base.get() ?? {};
204
+ actionNames = Object.keys(base.actions ?? {});
205
+ } catch (e) {
206
+ console.warn(`[PanoSDK] sample state of '${full}' could not read the initial state`, e);
207
+ } finally {
208
+ safeDestroy(base);
209
+ }
210
+
211
+ const state = { ...initial, ...(patch ?? {}) };
212
+ const calls = [];
213
+ const actions = {};
214
+ for (const n of actionNames) {
215
+ actions[n] = (...args) => {
216
+ calls.push({ name: n, args });
217
+ return { ok: true };
218
+ };
219
+ }
220
+
221
+ return {
222
+ name: full,
223
+ version: def.version,
224
+ get: () => state,
225
+ subscribe(run) {
226
+ run(state);
227
+ return () => {};
228
+ },
229
+ actions,
230
+ calls,
231
+ destroy() {},
232
+ };
233
+ }
234
+
235
+ function startEager(full) {
236
+ const s = getState();
237
+ const entry = s.defs.get(full);
238
+ if (!entry || !entry.def.eager || scopeOf(entry.def) !== 'app') return;
239
+
240
+ const host = hostFor();
241
+ if (!host) {
242
+ if (!s.pendingEager.includes(full)) s.pendingEager.push(full);
243
+ return;
244
+ }
245
+
246
+ try {
247
+ acquire(entry, full, {}, host, true);
248
+ } catch (e) {
249
+ console.error(`[PanoSDK] eager controller '${full}' failed to start`, e);
250
+ }
251
+ }
252
+
253
+ /**
254
+ * @param {string} pluginId
255
+ * @param {string} namespace
256
+ * @param {Record<string, ControllerDefinition> | ControllerDefinition[]} definitions
257
+ */
258
+ export function register(pluginId, namespace, definitions) {
259
+ const s = getState();
260
+ s.plugins.set(namespace, pluginId);
261
+
262
+ const list = Array.isArray(definitions)
263
+ ? definitions.map((def) => [def?.name, def])
264
+ : Object.entries(definitions ?? {}).map(([key, def]) => [def?.name ?? key, def]);
265
+
266
+ for (const [key, def] of list) {
267
+ if (!def || typeof def.create !== 'function' || !key) {
268
+ console.warn(`[PanoSDK] ${pluginId}: skipped a controller definition without name or create()`);
269
+ continue;
270
+ }
271
+
272
+ const full = `${namespace}/${key}`;
273
+
274
+ if (s.cache.has(full)) {
275
+ safeDestroy(s.cache.get(full));
276
+ s.cache.delete(full);
277
+ }
278
+
279
+ s.defs.set(full, { def, pluginId, namespace, key });
280
+ s.warned.delete(`missing:${full}`);
281
+
282
+ if (isBrowser()) startEager(full);
283
+ }
284
+ }
285
+
286
+ /** Stops cached (eager and used) controllers, clears definitions and the cache. Host, pins, samples and listeners stay. */
287
+ export function reset() {
288
+ const s = getState();
289
+ for (const controller of s.cache.values()) safeDestroy(controller);
290
+ s.cache.clear();
291
+ s.sampleCache.clear();
292
+ s.defs.clear();
293
+ s.plugins.clear();
294
+ s.pendingEager = [];
295
+ s.warned.clear();
296
+ }
297
+
298
+ /**
299
+ * @param {string} name `<namespace>/<controller>`
300
+ * @param {UseOptions} [opts]
301
+ * @returns {any | null}
302
+ */
303
+ export function use(name, opts) {
304
+ const s = getState();
305
+ const entry = resolveEntry(name, opts);
306
+ if (!entry) return null;
307
+
308
+ if (s.samples) {
309
+ if (!s.sampleCache.has(name)) {
310
+ s.sampleCache.set(name, createSampleController(entry, name, s.samples[name]));
311
+ }
312
+ return s.sampleCache.get(name);
313
+ }
314
+
315
+ const host = hostFor(opts?.event);
316
+ if (!host) {
317
+ warnOnce('nohost', '[PanoSDK] controllers.use() called before a host factory was set');
318
+ return null;
319
+ }
320
+
321
+ return acquire(entry, name, opts, host, true);
322
+ }
323
+
324
+ /**
325
+ * @param {string} name
326
+ * @param {UseOptions} [opts]
327
+ * @returns {Promise<object | null>}
328
+ */
329
+ export async function load(name, opts) {
330
+ const entry = resolveEntry(name, opts);
331
+ if (!entry) return null;
332
+ if (typeof entry.def.load !== 'function') return {};
333
+
334
+ const host = hostFor(opts?.event);
335
+ if (!host) {
336
+ warnOnce('nohost', '[PanoSDK] controllers.load() called before a host factory was set');
337
+ return null;
338
+ }
339
+
340
+ return entry.def.load({ host, params: opts?.params ?? {} });
341
+ }
342
+
343
+ /**
344
+ * @param {string} name
345
+ * @param {number} [version]
346
+ */
347
+ export function has(name, version) {
348
+ const entry = getState().defs.get(name);
349
+ if (!entry) return false;
350
+ return version === undefined || version === null || entry.def.version === version;
351
+ }
352
+
353
+ /** @returns {Array<{name:string, version:number, pluginId:string, scope:'app'|'instance'}>} */
354
+ export function list() {
355
+ return [...getState().defs.entries()].map(([name, e]) => ({
356
+ name,
357
+ version: e.def.version,
358
+ pluginId: e.pluginId,
359
+ scope: scopeOf(e.def),
360
+ }));
361
+ }
362
+
363
+ /** Plugin id registered for a namespace, or undefined. */
364
+ export function pluginIdOf(namespace) {
365
+ return getState().plugins.get(namespace);
366
+ }
367
+
368
+ /** @param {(event?: any) => any} fn */
369
+ export function setHostFactory(fn) {
370
+ const s = getState();
371
+ s.hostFactory = typeof fn === 'function' ? fn : null;
372
+ s.browserHost = null;
373
+
374
+ if (s.hostFactory && isBrowser()) {
375
+ const pending = s.pendingEager;
376
+ s.pendingEager = [];
377
+ for (const full of pending) startEager(full);
378
+ }
379
+ }
380
+
381
+ /** @param {Record<string, number> | null} map */
382
+ export function setThemePins(map) {
383
+ getState().pins = { ...(map ?? {}) };
384
+ }
385
+
386
+ /** @param {Record<string, object> | null} map */
387
+ export function setSamples(map) {
388
+ const s = getState();
389
+ // any map (even an empty one) turns sample mode on for every name; null turns it off
390
+ s.samples = map ? { ...map } : null;
391
+ s.sampleCache.clear();
392
+ }
393
+
394
+ /**
395
+ * @param {(m:{kind:'controller', name:string, wanted:number, available:number}) => void} fn
396
+ * @returns {() => void}
397
+ */
398
+ export function onMismatch(fn) {
399
+ const s = getState();
400
+ s.mismatch.push(fn);
401
+ return () => {
402
+ s.mismatch = s.mismatch.filter((l) => l !== fn);
403
+ };
404
+ }
405
+
406
+ /** The object the engine exposes as `pano.controllers`. */
407
+ export const controllers = {
408
+ register,
409
+ reset,
410
+ use,
411
+ load,
412
+ has,
413
+ list,
414
+ pluginIdOf,
415
+ setHostFactory,
416
+ setThemePins,
417
+ setSamples,
418
+ onMismatch,
419
+ };
420
+
421
+ export default controllers;
@@ -0,0 +1,62 @@
1
+ const actionListeners = {};
2
+ let pluginListeners = {};
3
+
4
+ export function addListener(action, listener) {
5
+ if (!actionListeners[action]) {
6
+ actionListeners[action] = [];
7
+ }
8
+
9
+ actionListeners[action] = [...actionListeners[action], listener];
10
+ }
11
+
12
+ /**
13
+ * Registers a plugin-provided click listener for a notification type.
14
+ * @param {string} type
15
+ * @param {(notification: object) => void} listener
16
+ */
17
+ export function addPluginListener(type, listener) {
18
+ pluginListeners[type] = [...(pluginListeners[type] || []), listener];
19
+ }
20
+
21
+ /** Drops every plugin listener (hosts call this in their `init()`). */
22
+ export function resetPluginListeners() {
23
+ pluginListeners = {};
24
+ }
25
+
26
+ /**
27
+ * Locale key of a notification's text.
28
+ * @param {{ type: string, pluginId?: string }} n
29
+ */
30
+ export function notificationTextKey(n) {
31
+ return n.pluginId ? `plugins.${n.pluginId}.notifications.${n.type}` : `notifications.${n.type}`;
32
+ }
33
+
34
+ function isSafeHref(href) {
35
+ return (
36
+ typeof href === 'string' &&
37
+ href.startsWith('/') &&
38
+ !href.startsWith('//') &&
39
+ !href.includes('\\') &&
40
+ // eslint-disable-next-line no-control-regex
41
+ !/[\u0000-\u001f\u007f]/.test(href)
42
+ );
43
+ }
44
+
45
+ /**
46
+ * Runs core listeners, then plugin listeners; when none exists and `details.href` is a safe
47
+ * local path, calls `navigate(href)`.
48
+ * @param {{ type: string, details?: { href?: string } }} notification
49
+ * @param {(path: string) => void} [navigate]
50
+ */
51
+ export function onNotificationClick(notification, navigate) {
52
+ const core = actionListeners[notification.type] || [];
53
+ const plugin = pluginListeners[notification.type] || [];
54
+
55
+ core.forEach((listener) => listener(notification));
56
+ plugin.forEach((listener) => listener(notification));
57
+
58
+ if (core.length === 0 && plugin.length === 0) {
59
+ const href = notification.details?.href;
60
+ if (typeof navigate === 'function' && isSafeHref(href)) navigate(href);
61
+ }
62
+ }
@@ -0,0 +1,39 @@
1
+ import { base } from '$app/paths';
2
+ import { registeredPages } from './PluginManager.js';
3
+ import { canonicalizeRouteKey } from './RouteMatcher.js';
4
+
5
+ const isPanel = base === '/panel';
6
+
7
+ export const baseAPI = {
8
+ isPanel,
9
+ debug: false,
10
+ };
11
+
12
+ export const pageAPI = {
13
+ page: {
14
+ register(
15
+ page = {
16
+ path: '',
17
+ component: Promise,
18
+ systemLayout: String,
19
+ restLayout: Boolean,
20
+ },
21
+ ) {
22
+ registeredPages[canonicalizeRouteKey(page.path)] = page;
23
+ },
24
+ unregister(path = '') {
25
+ delete registeredPages[canonicalizeRouteKey(path)];
26
+ },
27
+ isPluginPage: (path = '') => registeredPages[canonicalizeRouteKey(path)] !== undefined,
28
+ },
29
+ };
30
+
31
+ /**
32
+ * Generic capability detection. Hosts announce what they support through `pano.features`;
33
+ * plugins test `pano.features?.has('<id>')`.
34
+ * @param {Iterable<string>} ids
35
+ */
36
+ export function createFeatureSet(ids) {
37
+ const s = new Set(ids);
38
+ return Object.freeze({ has: (id) => s.has(id), list: () => [...s].sort() });
39
+ }