( element: LitElement & ProvideHassElement, dialogTag: string, dialogParams: unknown, dialogImport?: () => Promise<unknown>, parentElement?: LitElement, addHistory = true, dialogAnchor?: Element )
| 79 | * @returns `true` if the dialog was shown (or could be shown), `false` if it could not be loaded. |
| 80 | */ |
| 81 | export const showDialog = async ( |
| 82 | element: LitElement & ProvideHassElement, |
| 83 | dialogTag: string, |
| 84 | dialogParams: unknown, |
| 85 | dialogImport?: () => Promise<unknown>, |
| 86 | parentElement?: LitElement, |
| 87 | addHistory = true, |
| 88 | dialogAnchor?: Element |
| 89 | ): Promise<boolean> => { |
| 90 | if (!(dialogTag in LOADED)) { |
| 91 | if (!dialogImport) { |
| 92 | if (__DEV__) { |
| 93 | // eslint-disable-next-line |
| 94 | console.warn( |
| 95 | "Asked to show dialog that's not loaded and can't be imported" |
| 96 | ); |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | LOADED[dialogTag] = { |
| 101 | element: dialogImport().then(() => { |
| 102 | const dialogEl = document.createElement(dialogTag) as |
| 103 | | HassDialogNext |
| 104 | | HassDialog; |
| 105 | |
| 106 | if ("showDialog" in dialogEl) { |
| 107 | // provide hass for legacy persistent dialogs |
| 108 | element.provideHass(dialogEl); |
| 109 | } |
| 110 | |
| 111 | dialogEl.addEventListener("dialog-closed", _handleClosed); |
| 112 | dialogEl.addEventListener("dialog-closed", _handleClosedFocus); |
| 113 | |
| 114 | return dialogEl; |
| 115 | }), |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | if (addHistory) { |
| 120 | const { history } = mainWindow; |
| 121 | if (history.state?.dialog && !OPEN_DIALOG_STACK.length) { |
| 122 | // theres is a dialog state in history, but no dialogs open |
| 123 | // wait for history.back() to update the state |
| 124 | await new Promise((resolve) => { |
| 125 | setTimeout(resolve); |
| 126 | }); |
| 127 | return showDialog( |
| 128 | element, |
| 129 | dialogTag, |
| 130 | dialogParams, |
| 131 | dialogImport, |
| 132 | parentElement, |
| 133 | addHistory, |
| 134 | dialogAnchor |
| 135 | ); |
| 136 | } |
| 137 | const dialogIndex = OPEN_DIALOG_STACK.findIndex( |
| 138 | (state) => state.dialogTag === dialogTag |
no test coverage detected
searching dependent graphs…