@api-client/ui 0.5.36 → 0.5.37
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/build/src/core/Fragment.d.ts +23 -0
- package/build/src/core/Fragment.d.ts.map +1 -1
- package/build/src/core/Fragment.js +44 -0
- package/build/src/core/Fragment.js.map +1 -1
- package/build/src/core/renderer/FragmentRenderer.d.ts.map +1 -1
- package/build/src/core/renderer/FragmentRenderer.js +2 -3
- package/build/src/core/renderer/FragmentRenderer.js.map +1 -1
- package/package.json +1 -1
- package/src/core/Fragment.ts +46 -0
- package/src/core/renderer/FragmentRenderer.ts +2 -3
- package/test/core/fragment.spec.ts +66 -3
|
@@ -40,6 +40,15 @@ export declare class Fragment extends EventTarget {
|
|
|
40
40
|
*/
|
|
41
41
|
pendingActivityResult: Map<number, PendingActivityResult>;
|
|
42
42
|
get renderer(): FragmentRenderer;
|
|
43
|
+
/**
|
|
44
|
+
* The host to be used by the FragmentRenderer to assign it to the Lit's render root.
|
|
45
|
+
* Whatever is assigned to this property will be used as the root element.
|
|
46
|
+
*
|
|
47
|
+
* Fragments should use this property to manipulate the event's target.
|
|
48
|
+
* If a fragment renders multiple children, but only one is visible,
|
|
49
|
+
* it should assign the host value to the currently rendered child.
|
|
50
|
+
*/
|
|
51
|
+
host: Fragment;
|
|
43
52
|
constructor(options?: FragmentOptions);
|
|
44
53
|
/**
|
|
45
54
|
* When implemented, this method returns the class name to be used as a root class for the fragment.
|
|
@@ -181,5 +190,19 @@ export declare class Fragment extends EventTarget {
|
|
|
181
190
|
* @throws An error if the event type is not recognized.
|
|
182
191
|
*/
|
|
183
192
|
handleIntentEvent(event: CustomEvent<ActivityDetail | ActivityWithResultDetail>): Promise<void>;
|
|
193
|
+
/**
|
|
194
|
+
* Shows a registered fragment.
|
|
195
|
+
* @param key The fragment's key.
|
|
196
|
+
* @param options Fragment rendering options.
|
|
197
|
+
*/
|
|
198
|
+
showFragment(key: string, options?: {
|
|
199
|
+
renderTarget?: HTMLElement;
|
|
200
|
+
isHost?: boolean;
|
|
201
|
+
}): Promise<void>;
|
|
202
|
+
/**
|
|
203
|
+
* Hides a fragment by its key.
|
|
204
|
+
* @param key The fragment's key.
|
|
205
|
+
*/
|
|
206
|
+
hideFragment(fragment: Fragment | string): Promise<void>;
|
|
184
207
|
}
|
|
185
208
|
//# sourceMappingURL=Fragment.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Fragment.d.ts","sourceRoot":"","sources":["../../../src/core/Fragment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,KAAK,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,aAAa,EAAE,KAAK,eAAe,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAC3F,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAA;AACjE,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAE3D,OAAO,KAAK,EAAE,cAAc,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAA;AAEzF,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAE1D,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CACrD;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,QAAS,SAAQ,WAAW;;IAChC,KAAK,EAAE,aAAa,CAA4B;IAChD,MAAM,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAA;IACnC,SAAS,CAAC,QAAQ,wBAA8B;IAChD,SAAS,CAAC,eAAe,EAAE,eAAe,CAAA;IAE1C;;OAEG;IACH,WAAW,SAAK;IAEhB;;;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K9E;;;OAGG;IACG,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ7C;;;;;;;;;;;OAWG;IACH,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,CAAC,OAAO,CAAC;IAatD;;;OAGG;IACH,aAAa,CAAC,IAAI,GAAE,aAAkB,GAAG,IAAI;IAS7C,cAAc,IAAI,WAAW,GAAG,SAAS;IAIzC,WAAW,IAAI,QAAQ,GAAG,SAAS;IAInC;;;OAGG;IACG,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ3D;;;OAGG;IACG,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQlD;;;;;;;OAOG;IACG,gBAAgB,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBpG,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO;IAS5C;;;;;;;;;;;;;;OAcG;IAEG,iBAAiB,CAAC,KAAK,EAAE,WAAW,CAAC,cAAc,GAAG,wBAAwB,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBrG;;;;OAIG;IACG,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE;QAAE,YAAY,CAAC,EAAE,WAAW,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAW9G;;;OAGG;IACG,YAAY,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAe/D"}
|
|
@@ -47,6 +47,15 @@ let Fragment = (() => {
|
|
|
47
47
|
get renderer() {
|
|
48
48
|
return this.#renderer;
|
|
49
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* The host to be used by the FragmentRenderer to assign it to the Lit's render root.
|
|
52
|
+
* Whatever is assigned to this property will be used as the root element.
|
|
53
|
+
*
|
|
54
|
+
* Fragments should use this property to manipulate the event's target.
|
|
55
|
+
* If a fragment renders multiple children, but only one is visible,
|
|
56
|
+
* it should assign the host value to the currently rendered child.
|
|
57
|
+
*/
|
|
58
|
+
host = this;
|
|
50
59
|
constructor(options) {
|
|
51
60
|
super();
|
|
52
61
|
this.parent = options?.parent;
|
|
@@ -323,6 +332,41 @@ let Fragment = (() => {
|
|
|
323
332
|
throw new Error(`Unrecognized intent event: ${event.type}`);
|
|
324
333
|
}
|
|
325
334
|
}
|
|
335
|
+
/**
|
|
336
|
+
* Shows a registered fragment.
|
|
337
|
+
* @param key The fragment's key.
|
|
338
|
+
* @param options Fragment rendering options.
|
|
339
|
+
*/
|
|
340
|
+
async showFragment(key, options = {}) {
|
|
341
|
+
if (options.isHost) {
|
|
342
|
+
const fragment = this.fragmentManager.findFragment(key);
|
|
343
|
+
if (!fragment) {
|
|
344
|
+
throw new Error(`Fragment with key ${key} not found.`);
|
|
345
|
+
}
|
|
346
|
+
this.host = fragment;
|
|
347
|
+
}
|
|
348
|
+
await this.fragmentManager.showFragment(key, options.renderTarget);
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Hides a fragment by its key.
|
|
352
|
+
* @param key The fragment's key.
|
|
353
|
+
*/
|
|
354
|
+
async hideFragment(fragment) {
|
|
355
|
+
let ref;
|
|
356
|
+
if (typeof fragment === 'string') {
|
|
357
|
+
ref = this.fragmentManager.findFragment(fragment);
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
ref = fragment;
|
|
361
|
+
}
|
|
362
|
+
if (!ref) {
|
|
363
|
+
throw new Error(`Fragment not found`);
|
|
364
|
+
}
|
|
365
|
+
if (this.host === ref) {
|
|
366
|
+
this.host = this;
|
|
367
|
+
}
|
|
368
|
+
await this.fragmentManager.hideFragment(ref);
|
|
369
|
+
}
|
|
326
370
|
};
|
|
327
371
|
})();
|
|
328
372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AC,MAAM,CAAC,CAAA;YACnD,CAAC;iBAAM,CAAC;gBACN,MAAM,IAAI,KAAK,CAAC,8BAA8B,KAAK,CAAC,IAAI,EAAE,CAAC,CAAA;YAC7D,CAAC;QACH,CAAC;;;SAtUU,QAAQ","sourcesContent":["import { nothing, TemplateResult } from 'lit'\nimport { Activity } from './Activity.js'\nimport { FragmentState, type FragmentOptions, FragmentManager } from './FragmentManager.js'\nimport type { Application, UpdateRequest } from './Application.js'\nimport { FragmentRenderer } from './renderer/FragmentRenderer.js'\nimport { Intent, IntentResult } from './ActivityManager.js'\nimport { bound } from '../decorators/bound.js'\nimport type { ActivityDetail, ActivityWithResultDetail } from '../events/IntentEvents.js'\nimport { EventTypes } from '../events/EventTypes.js'\nimport { type RefOrCallback } from 'lit/directives/ref.js'\n\nexport interface PendingActivityResult {\n onResult(result: IntentResult, intent: Intent): void\n}\n\n/**\n * Similar to Activity, with lifecycle methods (onCreate, onAttach, onDetach, etc.).\n * The crucial difference is that a Fragment is always hosted by an `Activity` or\n * another `Fragment`.\n *\n * A `Fragment` represents a reusable portion of the app's UI.\n * A fragment defines and manages its own layout, has its own lifecycle,\n * and can handle its own input events.\n * Fragments can't live on their own. They must be hosted by an activity or another\n * fragment. The fragment’s view hierarchy becomes part of, or attaches to,\n * the host’s view hierarchy.\n */\nexport class Fragment extends EventTarget {\n public state: FragmentState = FragmentState.Initialized\n public parent?: Activity | Fragment\n protected children = new Map<string, Fragment>()\n protected fragmentManager: FragmentManager\n\n /**\n * The request code used to start an activity for a result.\n */\n requestCode = -1\n\n /**\n * A list of pending activity results that were requested by the components\n * hosted in this fragment.\n * The key is the request code and the value contains the callback\n * that will be called when the activity result is received.\n * The callback is called with the result code and the resulting intent.\n */\n pendingActivityResult: Map<number, PendingActivityResult> = new Map<number, PendingActivityResult>()\n\n #renderer: FragmentRenderer\n\n get renderer(): FragmentRenderer {\n return this.#renderer\n }\n\n constructor(options?: FragmentOptions) {\n super()\n this.parent = options?.parent\n this.fragmentManager = new FragmentManager(this)\n this.#renderer = new FragmentRenderer(this)\n }\n\n /**\n * When implemented, this method returns the class name to be used as a root class for the fragment.\n * This class name can be used to apply styles or identify the fragment in the DOM.\n * @returns The class name to be used as a root class for the fragment.\n */\n public rootClass(): string | undefined {\n return undefined\n }\n\n /**\n * @see {@link ./Renderer.js}\n */\n public setRenderRoot(renderRoot: HTMLElement | string): void {\n this.#renderer.setRenderRoot(renderRoot)\n }\n\n onData(data: unknown): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:data', { detail: data }))\n }\n\n /**\n * Called once when the fragment is created.\n *\n * In the `onCreate()` method, perform basic fragment startup logic that happens only once\n * for the entire life of the fragment.\n * @param data Optional init data.\n */\n onCreate(data?: unknown): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:create', { detail: data }))\n }\n\n /**\n * Called when the fragment is attached to a parent\n */\n onAttach(data?: unknown): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:attach', { detail: data }))\n }\n\n /**\n * Called when the fragment becomes visible.\n */\n onStart(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:start', { detail: null }))\n }\n\n /**\n * Called when the fragment gains focus.\n */\n onResume(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:resume', { detail: null }))\n }\n\n /**\n * Called when the fragment loses focus.\n */\n onPause(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:pause', { detail: null }))\n }\n\n /**\n * Called when the fragment is no longer visible.\n */\n onStop(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:stop', { detail: null }))\n }\n\n /**\n * Called when the fragment is detached from a parent\n */\n onDetach(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:detach', { detail: null }))\n }\n\n /**\n * Called before the fragment is destroyed.\n */\n onDestroy(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:destroy', { detail: null }))\n this.fragmentManager.onDestroy()\n }\n\n /**\n * Called by the renderer when the fragment is rendered for the first time.\n */\n onFirstRender(): void {\n //\n }\n\n /**\n * A function called when it should render the view.\n * By default it renders a fragment view if the fragment hosts other fragments and only one is visible.\n */\n render(): TemplateResult | typeof nothing {\n const fragment = this.getSingleVisibleFragment()\n if (fragment) {\n // we can manage the default rendering.\n // we make an assumption that by default only one fragment is rendered,\n // otherwise the parent activity or fragment would have to setup render roots.\n return fragment.render()\n }\n return nothing\n }\n\n /**\n * Checks whether there's only one `Fragment` that is in the `Resumed` state\n * and returns it. It returns `null` if there is no `Fragment`s or more than a single\n * visible `Fragment`.\n * @returns A Fragment that is the only fragment that should be visible.\n */\n getSingleVisibleFragment(): Fragment | null {\n const fragments = this.getVisibleFragments()\n return fragments.length === 1 ? fragments[0] : null\n }\n\n /**\n * Checks whether this fragment is a host for other fragments.\n * @returns `true` when this fragment hosts other fragments.\n */\n isRenderingFragments(): boolean {\n return this.children.size > 0\n }\n\n /**\n * @returns The list of all fragments that are in the Resumed state.\n */\n getVisibleFragments(): Fragment[] {\n return this.fragmentManager.getVisible()\n }\n\n /**\n * Adds a child fragment to this fragment.\n * The FragmentManager will handle lifecycle.\n * @param key The name of the fragment.\n * @param fragment The fragment to add.\n */\n async addChild(key: string, fragment: Fragment, data?: unknown): Promise<void> {\n this.children.set(key, fragment)\n await this.fragmentManager.attachFragment(key, fragment, this, data)\n }\n\n /**\n * Removes a fragment from this fragment.\n * @param key The name of the fragment to remove.\n */\n async removeChild(key: string): Promise<void> {\n const child = this.children.get(key)\n if (child) {\n await this.fragmentManager.detachFragment(key)\n this.children.delete(key)\n }\n }\n\n /**\n * Creates a RefCallback that can be used with lit's `ref()` directive\n * to automatically show/hide a child fragment when its container element\n * is added or removed from the DOM.\n *\n * @param key The key of the child fragment to manage.\n * @returns A RefCallback to be used with the `ref()` directive.\n *\n * @example\n * // In your parent fragment's render method:\n * html`<div ${ref(this.createFragmentRef('my-child-fragment'))}></div>`\n */\n createFragmentRef(key: string): RefOrCallback<Element> {\n return (element?: Element) => {\n if (element) {\n this.fragmentManager.showFragment(key, element as HTMLElement)\n } else {\n const fragment = this.fragmentManager.findFragment(key)\n if (fragment) {\n this.fragmentManager.hideFragment(fragment)\n }\n }\n }\n }\n\n /**\n * A helper to request an update.\n * Application or parent Fragment will handle rendering.\n */\n requestUpdate(opts: UpdateRequest = {}): void {\n if (opts.fragment !== false) {\n this.renderer.requestUpdate()\n }\n if (this.parent && (opts.activity || opts.app)) {\n this.parent.requestUpdate(opts)\n }\n }\n\n getApplication(): Application | undefined {\n return this.parent?.getApplication()\n }\n\n getActivity(): Activity | undefined {\n return this.parent?.getActivity()\n }\n\n /**\n * Starts an activity for result.\n * @param intent The intent to start.\n */\n async startActivityForResult(intent: Intent): Promise<void> {\n const activity = this.getActivity()\n if (!activity) {\n throw new Error(`The fragment has no activity. Unable to start an intent.`)\n }\n this.requestCode = await activity.startActivityForResult(intent)\n }\n\n /**\n * Starts another activity.\n * @param intent The intent to start.\n */\n async startActivity(intent: Intent): Promise<void> {\n const activity = this.getActivity()\n if (!activity) {\n throw new Error(`The fragment has no activity. Unable to start an intent.`)\n }\n await activity.startActivity(intent)\n }\n\n /**\n * The callback method that is triggered when another activity that was\n * started for result finishes.\n *\n * @param requestCode The request code that was used to start the activity.\n * @param data The data that was passed back.\n * @param intent The intent that was used to start the activity.\n */\n async onActivityResult(requestCode: number, resultCode: IntentResult, intent: Intent): Promise<void> {\n const { pendingActivityResult, children } = this\n if (pendingActivityResult.has(requestCode)) {\n const { onResult } = pendingActivityResult.get(requestCode) as PendingActivityResult\n pendingActivityResult.delete(requestCode)\n onResult(resultCode, intent)\n return\n }\n for (const fragment of children.values()) {\n if (fragment.hasRequestCode(requestCode)) {\n fragment.onActivityResult(requestCode, resultCode, intent)\n return\n }\n }\n this.requestCode = -1\n if (this.constructor === Fragment) {\n // eslint-disable-next-line no-console\n console.info('Fragment#onActivityResult() not implemented', requestCode, resultCode, intent)\n }\n }\n\n hasRequestCode(requestCode: number): boolean {\n for (const fragment of this.children.values()) {\n if (fragment.hasRequestCode(requestCode)) {\n return true\n }\n }\n return this.requestCode === requestCode\n }\n\n /**\n * A handler for the intent event dispatched by web components hosted by this fragment.\n *\n * **Usage example:**\n *\n * ```ts\n * <custom-element @startactivity=\"${this.handleIntentEvent}\"></custom-element>\n * <custom-element @startactivityforresult=\"${this.handleIntentEvent}\"></custom-element>\n * ```\n *\n * @param event The event that was dispatched.\n * @returns A promise that resolves when the intent is handled.\n * @throws An error if the fragment has no activity.\n * @throws An error if the event type is not recognized.\n */\n @bound\n async handleIntentEvent(event: CustomEvent<ActivityDetail | ActivityWithResultDetail>): Promise<void> {\n const activity = this.getActivity()\n if (!activity) {\n throw new Error(`The fragment has no activity. Unable to start an intent.`)\n }\n if (event.type === EventTypes.Intent.startActivityForResult) {\n const info = event.detail as ActivityWithResultDetail\n const code = await activity.startActivityForResult(info.intent)\n this.requestCode = code\n this.pendingActivityResult.set(code, {\n onResult: info.onResult,\n })\n } else if (event.type === EventTypes.Intent.startActivity) {\n await activity.startActivity(event.detail.intent)\n } else {\n throw new Error(`Unrecognized intent event: ${event.type}`)\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"Fragment.js","sourceRoot":"","sources":["../../../src/core/Fragment.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,OAAO,EAAkB,MAAM,KAAK,CAAA;AAE7C,OAAO,EAAE,aAAa,EAAwB,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAE3F,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAA;AAEjE,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAOpD;;;;;;;;;;;GAWG;IACU,QAAQ;sBAAS,WAAW;;;iBAA5B,QAAS,SAAQ,WAAW;;;6CA8TtC,KAAK;YACN,sMAAM,iBAAiB,6DAiBtB;;;QA/UM,KAAK,IADD,mDAAQ,EACW,aAAa,CAAC,WAAW,EAAA;QACh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C,eAAe,CAAC,YAAY,CAAC,GAAG,EAAE,OAAO,CAAC,YAAY,CAAC,CAAA;QACpE,CAAC;QAED;;;WAGG;QACH,KAAK,CAAC,YAAY,CAAC,QAA2B;YAC5C,IAAI,GAAyB,CAAA;YAC7B,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;gBACjC,GAAG,GAAG,IAAI,CAAC,eAAe,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAA;YACnD,CAAC;iBAAM,CAAC;gBACN,GAAG,GAAG,QAAoB,CAAA;YAC5B,CAAC;YACD,IAAI,CAAC,GAAG,EAAE,CAAC;gBACT,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;YACvC,CAAC;YACD,IAAI,IAAI,CAAC,IAAI,KAAK,GAAG,EAAE,CAAC;gBACtB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;YAClB,CAAC;YACD,MAAM,IAAI,CAAC,eAAe,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;QAC9C,CAAC;;;SApXU,QAAQ","sourcesContent":["import { nothing, TemplateResult } from 'lit'\nimport { Activity } from './Activity.js'\nimport { FragmentState, type FragmentOptions, FragmentManager } from './FragmentManager.js'\nimport type { Application, UpdateRequest } from './Application.js'\nimport { FragmentRenderer } from './renderer/FragmentRenderer.js'\nimport { Intent, IntentResult } from './ActivityManager.js'\nimport { bound } from '../decorators/bound.js'\nimport type { ActivityDetail, ActivityWithResultDetail } from '../events/IntentEvents.js'\nimport { EventTypes } from '../events/EventTypes.js'\nimport { type RefOrCallback } from 'lit/directives/ref.js'\n\nexport interface PendingActivityResult {\n onResult(result: IntentResult, intent: Intent): void\n}\n\n/**\n * Similar to Activity, with lifecycle methods (onCreate, onAttach, onDetach, etc.).\n * The crucial difference is that a Fragment is always hosted by an `Activity` or\n * another `Fragment`.\n *\n * A `Fragment` represents a reusable portion of the app's UI.\n * A fragment defines and manages its own layout, has its own lifecycle,\n * and can handle its own input events.\n * Fragments can't live on their own. They must be hosted by an activity or another\n * fragment. The fragment’s view hierarchy becomes part of, or attaches to,\n * the host’s view hierarchy.\n */\nexport class Fragment extends EventTarget {\n public state: FragmentState = FragmentState.Initialized\n public parent?: Activity | Fragment\n protected children = new Map<string, Fragment>()\n protected fragmentManager: FragmentManager\n\n /**\n * The request code used to start an activity for a result.\n */\n requestCode = -1\n\n /**\n * A list of pending activity results that were requested by the components\n * hosted in this fragment.\n * The key is the request code and the value contains the callback\n * that will be called when the activity result is received.\n * The callback is called with the result code and the resulting intent.\n */\n pendingActivityResult: Map<number, PendingActivityResult> = new Map<number, PendingActivityResult>()\n\n #renderer: FragmentRenderer\n\n get renderer(): FragmentRenderer {\n return this.#renderer\n }\n\n /**\n * The host to be used by the FragmentRenderer to assign it to the Lit's render root.\n * Whatever is assigned to this property will be used as the root element.\n *\n * Fragments should use this property to manipulate the event's target.\n * If a fragment renders multiple children, but only one is visible,\n * it should assign the host value to the currently rendered child.\n */\n host: Fragment = this\n\n constructor(options?: FragmentOptions) {\n super()\n this.parent = options?.parent\n this.fragmentManager = new FragmentManager(this)\n this.#renderer = new FragmentRenderer(this)\n }\n\n /**\n * When implemented, this method returns the class name to be used as a root class for the fragment.\n * This class name can be used to apply styles or identify the fragment in the DOM.\n * @returns The class name to be used as a root class for the fragment.\n */\n public rootClass(): string | undefined {\n return undefined\n }\n\n /**\n * @see {@link ./Renderer.js}\n */\n public setRenderRoot(renderRoot: HTMLElement | string): void {\n this.#renderer.setRenderRoot(renderRoot)\n }\n\n onData(data: unknown): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:data', { detail: data }))\n }\n\n /**\n * Called once when the fragment is created.\n *\n * In the `onCreate()` method, perform basic fragment startup logic that happens only once\n * for the entire life of the fragment.\n * @param data Optional init data.\n */\n onCreate(data?: unknown): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:create', { detail: data }))\n }\n\n /**\n * Called when the fragment is attached to a parent\n */\n onAttach(data?: unknown): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:attach', { detail: data }))\n }\n\n /**\n * Called when the fragment becomes visible.\n */\n onStart(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:start', { detail: null }))\n }\n\n /**\n * Called when the fragment gains focus.\n */\n onResume(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:resume', { detail: null }))\n }\n\n /**\n * Called when the fragment loses focus.\n */\n onPause(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:pause', { detail: null }))\n }\n\n /**\n * Called when the fragment is no longer visible.\n */\n onStop(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:stop', { detail: null }))\n }\n\n /**\n * Called when the fragment is detached from a parent\n */\n onDetach(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:detach', { detail: null }))\n }\n\n /**\n * Called before the fragment is destroyed.\n */\n onDestroy(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('fragment:destroy', { detail: null }))\n this.fragmentManager.onDestroy()\n }\n\n /**\n * Called by the renderer when the fragment is rendered for the first time.\n */\n onFirstRender(): void {\n //\n }\n\n /**\n * A function called when it should render the view.\n * By default it renders a fragment view if the fragment hosts other fragments and only one is visible.\n */\n render(): TemplateResult | typeof nothing {\n const fragment = this.getSingleVisibleFragment()\n if (fragment) {\n // we can manage the default rendering.\n // we make an assumption that by default only one fragment is rendered,\n // otherwise the parent activity or fragment would have to setup render roots.\n return fragment.render()\n }\n return nothing\n }\n\n /**\n * Checks whether there's only one `Fragment` that is in the `Resumed` state\n * and returns it. It returns `null` if there is no `Fragment`s or more than a single\n * visible `Fragment`.\n * @returns A Fragment that is the only fragment that should be visible.\n */\n getSingleVisibleFragment(): Fragment | null {\n const fragments = this.getVisibleFragments()\n return fragments.length === 1 ? fragments[0] : null\n }\n\n /**\n * Checks whether this fragment is a host for other fragments.\n * @returns `true` when this fragment hosts other fragments.\n */\n isRenderingFragments(): boolean {\n return this.children.size > 0\n }\n\n /**\n * @returns The list of all fragments that are in the Resumed state.\n */\n getVisibleFragments(): Fragment[] {\n return this.fragmentManager.getVisible()\n }\n\n /**\n * Adds a child fragment to this fragment.\n * The FragmentManager will handle lifecycle.\n * @param key The name of the fragment.\n * @param fragment The fragment to add.\n */\n async addChild(key: string, fragment: Fragment, data?: unknown): Promise<void> {\n this.children.set(key, fragment)\n await this.fragmentManager.attachFragment(key, fragment, this, data)\n }\n\n /**\n * Removes a fragment from this fragment.\n * @param key The name of the fragment to remove.\n */\n async removeChild(key: string): Promise<void> {\n const child = this.children.get(key)\n if (child) {\n await this.fragmentManager.detachFragment(key)\n this.children.delete(key)\n }\n }\n\n /**\n * Creates a RefCallback that can be used with lit's `ref()` directive\n * to automatically show/hide a child fragment when its container element\n * is added or removed from the DOM.\n *\n * @param key The key of the child fragment to manage.\n * @returns A RefCallback to be used with the `ref()` directive.\n *\n * @example\n * // In your parent fragment's render method:\n * html`<div ${ref(this.createFragmentRef('my-child-fragment'))}></div>`\n */\n createFragmentRef(key: string): RefOrCallback<Element> {\n return (element?: Element) => {\n if (element) {\n this.fragmentManager.showFragment(key, element as HTMLElement)\n } else {\n const fragment = this.fragmentManager.findFragment(key)\n if (fragment) {\n this.fragmentManager.hideFragment(fragment)\n }\n }\n }\n }\n\n /**\n * A helper to request an update.\n * Application or parent Fragment will handle rendering.\n */\n requestUpdate(opts: UpdateRequest = {}): void {\n if (opts.fragment !== false) {\n this.renderer.requestUpdate()\n }\n if (this.parent && (opts.activity || opts.app)) {\n this.parent.requestUpdate(opts)\n }\n }\n\n getApplication(): Application | undefined {\n return this.parent?.getApplication()\n }\n\n getActivity(): Activity | undefined {\n return this.parent?.getActivity()\n }\n\n /**\n * Starts an activity for result.\n * @param intent The intent to start.\n */\n async startActivityForResult(intent: Intent): Promise<void> {\n const activity = this.getActivity()\n if (!activity) {\n throw new Error(`The fragment has no activity. Unable to start an intent.`)\n }\n this.requestCode = await activity.startActivityForResult(intent)\n }\n\n /**\n * Starts another activity.\n * @param intent The intent to start.\n */\n async startActivity(intent: Intent): Promise<void> {\n const activity = this.getActivity()\n if (!activity) {\n throw new Error(`The fragment has no activity. Unable to start an intent.`)\n }\n await activity.startActivity(intent)\n }\n\n /**\n * The callback method that is triggered when another activity that was\n * started for result finishes.\n *\n * @param requestCode The request code that was used to start the activity.\n * @param data The data that was passed back.\n * @param intent The intent that was used to start the activity.\n */\n async onActivityResult(requestCode: number, resultCode: IntentResult, intent: Intent): Promise<void> {\n const { pendingActivityResult, children } = this\n if (pendingActivityResult.has(requestCode)) {\n const { onResult } = pendingActivityResult.get(requestCode) as PendingActivityResult\n pendingActivityResult.delete(requestCode)\n onResult(resultCode, intent)\n return\n }\n for (const fragment of children.values()) {\n if (fragment.hasRequestCode(requestCode)) {\n fragment.onActivityResult(requestCode, resultCode, intent)\n return\n }\n }\n this.requestCode = -1\n if (this.constructor === Fragment) {\n // eslint-disable-next-line no-console\n console.info('Fragment#onActivityResult() not implemented', requestCode, resultCode, intent)\n }\n }\n\n hasRequestCode(requestCode: number): boolean {\n for (const fragment of this.children.values()) {\n if (fragment.hasRequestCode(requestCode)) {\n return true\n }\n }\n return this.requestCode === requestCode\n }\n\n /**\n * A handler for the intent event dispatched by web components hosted by this fragment.\n *\n * **Usage example:**\n *\n * ```ts\n * <custom-element @startactivity=\"${this.handleIntentEvent}\"></custom-element>\n * <custom-element @startactivityforresult=\"${this.handleIntentEvent}\"></custom-element>\n * ```\n *\n * @param event The event that was dispatched.\n * @returns A promise that resolves when the intent is handled.\n * @throws An error if the fragment has no activity.\n * @throws An error if the event type is not recognized.\n */\n @bound\n async handleIntentEvent(event: CustomEvent<ActivityDetail | ActivityWithResultDetail>): Promise<void> {\n const activity = this.getActivity()\n if (!activity) {\n throw new Error(`The fragment has no activity. Unable to start an intent.`)\n }\n if (event.type === EventTypes.Intent.startActivityForResult) {\n const info = event.detail as ActivityWithResultDetail\n const code = await activity.startActivityForResult(info.intent)\n this.requestCode = code\n this.pendingActivityResult.set(code, {\n onResult: info.onResult,\n })\n } else if (event.type === EventTypes.Intent.startActivity) {\n await activity.startActivity(event.detail.intent)\n } else {\n throw new Error(`Unrecognized intent event: ${event.type}`)\n }\n }\n\n /**\n * Shows a registered fragment.\n * @param key The fragment's key.\n * @param options Fragment rendering options.\n */\n async showFragment(key: string, options: { renderTarget?: HTMLElement; isHost?: boolean } = {}): Promise<void> {\n if (options.isHost) {\n const fragment = this.fragmentManager.findFragment(key)\n if (!fragment) {\n throw new Error(`Fragment with key ${key} not found.`)\n }\n this.host = fragment\n }\n await this.fragmentManager.showFragment(key, options.renderTarget)\n }\n\n /**\n * Hides a fragment by its key.\n * @param key The fragment's key.\n */\n async hideFragment(fragment: Fragment | string): Promise<void> {\n let ref: Fragment | undefined\n if (typeof fragment === 'string') {\n ref = this.fragmentManager.findFragment(fragment)\n } else {\n ref = fragment as Fragment\n }\n if (!ref) {\n throw new Error(`Fragment not found`)\n }\n if (this.host === ref) {\n this.host = this\n }\n await this.fragmentManager.hideFragment(ref)\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FragmentRenderer.d.ts","sourceRoot":"","sources":["../../../../src/core/renderer/FragmentRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC;;;;;;GAMG;AACH,qBAAa,gBAAiB,SAAQ,QAAQ;IAChC,SAAS,CAAC,QAAQ,EAAE,QAAQ;gBAAlB,QAAQ,EAAE,QAAQ;IAIxC,SAAS,CAAC,QAAQ,IAAI,IAAI;
|
|
1
|
+
{"version":3,"file":"FragmentRenderer.d.ts","sourceRoot":"","sources":["../../../../src/core/renderer/FragmentRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC;;;;;;GAMG;AACH,qBAAa,gBAAiB,SAAQ,QAAQ;IAChC,SAAS,CAAC,QAAQ,EAAE,QAAQ;gBAAlB,QAAQ,EAAE,QAAQ;IAIxC,SAAS,CAAC,QAAQ,IAAI,IAAI;CAa3B"}
|
|
@@ -18,13 +18,12 @@ export class FragmentRenderer extends Renderer {
|
|
|
18
18
|
// return quietly. Fragments can be rendered directly.
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
|
-
const host = this.fragment;
|
|
22
21
|
if (!this.firstRendered) {
|
|
23
22
|
this.firstRenderedFlag = true;
|
|
24
23
|
this._clearRoot(root);
|
|
25
|
-
queueMicrotask(() =>
|
|
24
|
+
queueMicrotask(() => this.fragment.onFirstRender());
|
|
26
25
|
}
|
|
27
|
-
this._render(
|
|
26
|
+
this._render(this.fragment.render(), root, this.fragment.host);
|
|
28
27
|
}
|
|
29
28
|
}
|
|
30
29
|
//# sourceMappingURL=FragmentRenderer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FragmentRenderer.js","sourceRoot":"","sources":["../../../../src/core/renderer/FragmentRenderer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC;;;;;;GAMG;AACH,MAAM,OAAO,gBAAiB,SAAQ,QAAQ;IACtB;IAAtB,YAAsB,QAAkB;QACtC,KAAK,EAAE,CAAA;QADa,aAAQ,GAAR,QAAQ,CAAU;IAExC,CAAC;IAES,QAAQ;QAChB,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAA;QAC5B,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,sDAAsD;YACtD,OAAM;QACR,CAAC;QACD,
|
|
1
|
+
{"version":3,"file":"FragmentRenderer.js","sourceRoot":"","sources":["../../../../src/core/renderer/FragmentRenderer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC;;;;;;GAMG;AACH,MAAM,OAAO,gBAAiB,SAAQ,QAAQ;IACtB;IAAtB,YAAsB,QAAkB;QACtC,KAAK,EAAE,CAAA;QADa,aAAQ,GAAR,QAAQ,CAAU;IAExC,CAAC;IAES,QAAQ;QAChB,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAA;QAC5B,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,sDAAsD;YACtD,OAAM;QACR,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;YACxB,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAA;YAC7B,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;YACrB,cAAc,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,CAAC,CAAA;QACrD,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;IAChE,CAAC;CACF","sourcesContent":["import type { Fragment } from '../Fragment.js'\nimport { Renderer } from './Renderer.js'\n\n/**\n * FragmentRenderer is responsible for rendering a Fragment instance.\n * It extends the Renderer class and implements the rendering logic specific to Fragments.\n *\n * The fragment must decide how to render itself. Event when a fragment is a host\n * and it only shows a single child, it still needs to render that child.\n */\nexport class FragmentRenderer extends Renderer {\n constructor(protected fragment: Fragment) {\n super()\n }\n\n protected renderer(): void {\n const root = this.renderRoot\n if (!root) {\n // return quietly. Fragments can be rendered directly.\n return\n }\n if (!this.firstRendered) {\n this.firstRenderedFlag = true\n this._clearRoot(root)\n queueMicrotask(() => this.fragment.onFirstRender())\n }\n this._render(this.fragment.render(), root, this.fragment.host)\n }\n}\n"]}
|
package/package.json
CHANGED
package/src/core/Fragment.ts
CHANGED
|
@@ -51,6 +51,16 @@ export class Fragment extends EventTarget {
|
|
|
51
51
|
return this.#renderer
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* The host to be used by the FragmentRenderer to assign it to the Lit's render root.
|
|
56
|
+
* Whatever is assigned to this property will be used as the root element.
|
|
57
|
+
*
|
|
58
|
+
* Fragments should use this property to manipulate the event's target.
|
|
59
|
+
* If a fragment renders multiple children, but only one is visible,
|
|
60
|
+
* it should assign the host value to the currently rendered child.
|
|
61
|
+
*/
|
|
62
|
+
host: Fragment = this
|
|
63
|
+
|
|
54
64
|
constructor(options?: FragmentOptions) {
|
|
55
65
|
super()
|
|
56
66
|
this.parent = options?.parent
|
|
@@ -352,4 +362,40 @@ export class Fragment extends EventTarget {
|
|
|
352
362
|
throw new Error(`Unrecognized intent event: ${event.type}`)
|
|
353
363
|
}
|
|
354
364
|
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Shows a registered fragment.
|
|
368
|
+
* @param key The fragment's key.
|
|
369
|
+
* @param options Fragment rendering options.
|
|
370
|
+
*/
|
|
371
|
+
async showFragment(key: string, options: { renderTarget?: HTMLElement; isHost?: boolean } = {}): Promise<void> {
|
|
372
|
+
if (options.isHost) {
|
|
373
|
+
const fragment = this.fragmentManager.findFragment(key)
|
|
374
|
+
if (!fragment) {
|
|
375
|
+
throw new Error(`Fragment with key ${key} not found.`)
|
|
376
|
+
}
|
|
377
|
+
this.host = fragment
|
|
378
|
+
}
|
|
379
|
+
await this.fragmentManager.showFragment(key, options.renderTarget)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Hides a fragment by its key.
|
|
384
|
+
* @param key The fragment's key.
|
|
385
|
+
*/
|
|
386
|
+
async hideFragment(fragment: Fragment | string): Promise<void> {
|
|
387
|
+
let ref: Fragment | undefined
|
|
388
|
+
if (typeof fragment === 'string') {
|
|
389
|
+
ref = this.fragmentManager.findFragment(fragment)
|
|
390
|
+
} else {
|
|
391
|
+
ref = fragment as Fragment
|
|
392
|
+
}
|
|
393
|
+
if (!ref) {
|
|
394
|
+
throw new Error(`Fragment not found`)
|
|
395
|
+
}
|
|
396
|
+
if (this.host === ref) {
|
|
397
|
+
this.host = this
|
|
398
|
+
}
|
|
399
|
+
await this.fragmentManager.hideFragment(ref)
|
|
400
|
+
}
|
|
355
401
|
}
|
|
@@ -19,12 +19,11 @@ export class FragmentRenderer extends Renderer {
|
|
|
19
19
|
// return quietly. Fragments can be rendered directly.
|
|
20
20
|
return
|
|
21
21
|
}
|
|
22
|
-
const host = this.fragment
|
|
23
22
|
if (!this.firstRendered) {
|
|
24
23
|
this.firstRenderedFlag = true
|
|
25
24
|
this._clearRoot(root)
|
|
26
|
-
queueMicrotask(() =>
|
|
25
|
+
queueMicrotask(() => this.fragment.onFirstRender())
|
|
27
26
|
}
|
|
28
|
-
this._render(
|
|
27
|
+
this._render(this.fragment.render(), root, this.fragment.host)
|
|
29
28
|
}
|
|
30
29
|
}
|
|
@@ -8,8 +8,8 @@ import { FragmentState, FragmentManager } from '../../src/core/FragmentManager.j
|
|
|
8
8
|
import { IntentResult, type Intent, ActivityLifecycle } from '../../src/core/ActivityManager.js'
|
|
9
9
|
import { FragmentRenderer } from '../../src/core/renderer/FragmentRenderer.js'
|
|
10
10
|
import { EventTypes } from '../../src/events/EventTypes.js'
|
|
11
|
-
import { nothing, render } from 'lit'
|
|
12
|
-
import type { ActivityDetail } from '../../src/events/IntentEvents.js'
|
|
11
|
+
import { nothing, render, TemplateResult } from 'lit'
|
|
12
|
+
import type { ActivityDetail, ActivityWithResultDetail } from '../../src/events/IntentEvents.js'
|
|
13
13
|
import { ref } from 'lit/directives/ref.js'
|
|
14
14
|
|
|
15
15
|
class TestActivity extends Activity {
|
|
@@ -466,7 +466,7 @@ describe('Fragment', () => {
|
|
|
466
466
|
it('throws error if no activity for startActivityForResult', async () => {
|
|
467
467
|
fragment.parent = undefined
|
|
468
468
|
const event = new CustomEvent(EventTypes.Intent.startActivityForResult, {
|
|
469
|
-
detail: {} as unknown as
|
|
469
|
+
detail: {} as unknown as ActivityWithResultDetail,
|
|
470
470
|
})
|
|
471
471
|
try {
|
|
472
472
|
await fragment.handleIntentEvent(event)
|
|
@@ -487,6 +487,69 @@ describe('Fragment', () => {
|
|
|
487
487
|
})
|
|
488
488
|
})
|
|
489
489
|
|
|
490
|
+
describe('Child Fragment Hosting', () => {
|
|
491
|
+
it('sets the host when showing a child with isHost: true', async () => {
|
|
492
|
+
const parentFragment = new TestFragment({ parent: activity })
|
|
493
|
+
const childFragment = new TestFragment({ parent: parentFragment })
|
|
494
|
+
await parentFragment.addChild('childKey', childFragment)
|
|
495
|
+
|
|
496
|
+
await parentFragment.showFragment('childKey', { isHost: true })
|
|
497
|
+
assert.strictEqual(parentFragment.host, childFragment)
|
|
498
|
+
})
|
|
499
|
+
|
|
500
|
+
it('resets the host when hiding a host fragment', async () => {
|
|
501
|
+
const parentFragment = new TestFragment({ parent: activity })
|
|
502
|
+
const childFragment = new TestFragment({ parent: parentFragment })
|
|
503
|
+
|
|
504
|
+
await parentFragment.addChild('childKey', childFragment)
|
|
505
|
+
await parentFragment.showFragment('childKey', { isHost: true })
|
|
506
|
+
|
|
507
|
+
assert.strictEqual(parentFragment.host, childFragment, 'Pre-condition: host should be child')
|
|
508
|
+
|
|
509
|
+
await parentFragment.hideFragment('childKey')
|
|
510
|
+
|
|
511
|
+
assert.strictEqual(parentFragment.host, parentFragment, 'Host should be reset to the parent fragment itself')
|
|
512
|
+
})
|
|
513
|
+
|
|
514
|
+
it('correctly scopes event handlers to the child fragment when using isHost', async () => {
|
|
515
|
+
// A child fragment that has a template with an event handler.
|
|
516
|
+
class ChildWithHandlerFragment extends TestFragment {
|
|
517
|
+
handlerContext: Fragment | null = null
|
|
518
|
+
|
|
519
|
+
handleButtonClick(): void {
|
|
520
|
+
this.handlerContext = this
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
override render(): TemplateResult {
|
|
524
|
+
super.render() // for the renderSpy
|
|
525
|
+
return html`<button @click=${this.handleButtonClick}></button>`
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const parent = new TestFragment({ parent: activity })
|
|
529
|
+
const child = new ChildWithHandlerFragment({ parent })
|
|
530
|
+
await parent.addChild('childKey', child)
|
|
531
|
+
|
|
532
|
+
// Show with isHost: true
|
|
533
|
+
await parent.showFragment('childKey', { isHost: true })
|
|
534
|
+
|
|
535
|
+
// Render the parent fragment
|
|
536
|
+
const container = await fixture<HTMLElement>(html`<div></div>`)
|
|
537
|
+
parent.setRenderRoot(container)
|
|
538
|
+
parent.requestUpdate()
|
|
539
|
+
await parent.renderer.updateComplete
|
|
540
|
+
|
|
541
|
+
const button = container.querySelector('button')
|
|
542
|
+
assert.isNotNull(button, 'Button from child fragment should be rendered')
|
|
543
|
+
button!.click()
|
|
544
|
+
|
|
545
|
+
assert.strictEqual(
|
|
546
|
+
child.handlerContext,
|
|
547
|
+
child,
|
|
548
|
+
'Event handler `this` context should be the child fragment instance'
|
|
549
|
+
)
|
|
550
|
+
})
|
|
551
|
+
})
|
|
552
|
+
|
|
490
553
|
describe('Misc', () => {
|
|
491
554
|
it('rootClass returns undefined by default', () => {
|
|
492
555
|
assert.isUndefined(fragment.rootClass())
|