(ref: {
el: HTMLElement;
hasController: boolean;
delegate?: FrameworkDelegate;
})
| 862 | * @param ref The component class instance. |
| 863 | */ |
| 864 | export const createDelegateController = (ref: { |
| 865 | el: HTMLElement; |
| 866 | hasController: boolean; |
| 867 | delegate?: FrameworkDelegate; |
| 868 | }) => { |
| 869 | let inline = false; |
| 870 | let workingDelegate: FrameworkDelegate | undefined; |
| 871 | |
| 872 | const coreDelegate: FrameworkDelegate = CoreDelegate(); |
| 873 | |
| 874 | /** |
| 875 | * Determines whether or not an overlay is being used |
| 876 | * inline or via a controller/JS and returns the correct delegate. |
| 877 | * By default, subsequent calls to getDelegate will use |
| 878 | * a cached version of the delegate. |
| 879 | * This is useful for calling dismiss after present, |
| 880 | * so that the correct delegate is given. |
| 881 | * @param force `true` to force the non-cached version of the delegate. |
| 882 | * @returns The delegate to use and whether or not the overlay is inline. |
| 883 | */ |
| 884 | const getDelegate = (force = false) => { |
| 885 | if (workingDelegate && !force) { |
| 886 | return { |
| 887 | delegate: workingDelegate, |
| 888 | inline, |
| 889 | }; |
| 890 | } |
| 891 | const { el, hasController, delegate } = ref; |
| 892 | /** |
| 893 | * If using overlay inline |
| 894 | * we potentially need to use the coreDelegate |
| 895 | * so that this works in vanilla JS apps. |
| 896 | * If a developer has presented this component |
| 897 | * via a controller, then we can assume |
| 898 | * the component is already in the |
| 899 | * correct place. |
| 900 | */ |
| 901 | const parentEl = el.parentNode as HTMLElement | null; |
| 902 | inline = parentEl !== null && !hasController; |
| 903 | workingDelegate = inline ? delegate || coreDelegate : delegate; |
| 904 | |
| 905 | return { inline, delegate: workingDelegate }; |
| 906 | }; |
| 907 | |
| 908 | /** |
| 909 | * Attaches a component in the DOM. Teleports the component |
| 910 | * to the root of the app. |
| 911 | * @param component The component to optionally construct and append to the element. |
| 912 | */ |
| 913 | const attachViewToDom = async (component?: any) => { |
| 914 | const { delegate } = getDelegate(true); |
| 915 | if (delegate) { |
| 916 | return await delegate.attachViewToDom(ref.el, component); |
| 917 | } |
| 918 | const { hasController } = ref; |
| 919 | if (hasController && component !== undefined) { |
| 920 | throw new Error('framework delegate is missing'); |
| 921 | } |
no test coverage detected