# Dialog Module data flow

**Purpose:** Concise data-flow summary for dialog.

**Primary role:** Provide a lightweight wrapper around `@syncfusion/ej2-popups` `Dialog` for the `Spreadsheet` host — create, show, hide, and destroy modal dialogs while wiring localization, focus management, and parent callbacks.

- **Constructor:** `new Dialog(parent)` — stores `parent` reference.
- **Public API:** `show(dialogModel, cancelBtn?)`, `hide(disableAnimation?)`, `destroyDialog()`, `destroy()`.

**show(dialogModel, cancelBtn?)**
- Builds a baseline `DialogModel` with `header`, `cssClass` (from `parent.cssClass`), `target` (`parent.element`), `allowDragging`, and `buttons`.
- Wraps `dialogModel.beforeOpen` to call any existing handler, cancels opening when handler sets `args.cancel`, calls `hide(true)` and focuses `parent.element` (unless `preventFocus`).
- Wraps `dialogModel.close` to call `destroyDialog()` and then the original close handler.
- If `cancelBtn` is true (default), appends a localized Cancel/Ok button using the `locale` service from `parent.serviceLocator`.
- Creates a DOM `div`, appends to `document.body`, instantiates `DialogComponent`, assigns `parent.createElement` as factory, and `appendTo(div)`.
- Applies RTL modal positioning fix and calls `refreshPosition()`.

**destroyDialog()**
- Calls `this.dialogInstance.destroy()`, removes the dialog element from DOM, and nulls `dialogInstance`.

**hide(disableAnimation?)**
- If `disableAnimation` is true, sets `animationSettings.effect = 'None'` and `dataBind()` to immediately apply change, then calls `this.dialogInstance.hide()`.

**destroy()**
- Clears `parent` reference to avoid leaks.

**Side effects & invariants**
- Appends a container `div` to `document.body` for each shown dialog; `destroyDialog()` must be called to avoid orphaned nodes.
- Relies on `parent.createElement` (injected DOM factory) for server-friendly element creation.
- Uses `parent.serviceLocator.getService(locale)` for localized button labels.
- `beforeOpen` wrapper still invokes original handler and respects `args.cancel` and `preventFocus` semantics.

**Dependencies**
- Imports: `Dialog` / `DialogModel` / `BeforeOpenEventArgs` from `@syncfusion/ej2-popups`; helpers from `@syncfusion/ej2-base`; `focus`, `locale` from internal `../common/index`.

**Where it's used**
- Instantiated by `Spreadsheet` to show confirmation, prompt, and error dialogs; centralizes the lifecycle and cleanup of transient dialog UI.

**Quick maintainer notes**
- Always call `destroyDialog()` when preventing open or on host teardown to prevent DOM leaks.
- Verify `dialogInstance` null checks before calling `hide()`/`destroy()`.