Skip to content

feat(manager): warn when closing or leaving with unsaved changes (Resources, Elements, Files)#16848

Open
Ibochkarev wants to merge 2 commits intomodxcms:3.xfrom
Ibochkarev:fix/16277-warn-unsaved-changes-on-close
Open

feat(manager): warn when closing or leaving with unsaved changes (Resources, Elements, Files)#16848
Ibochkarev wants to merge 2 commits intomodxcms:3.xfrom
Ibochkarev:fix/16277-warn-unsaved-changes-on-close

Conversation

@Ibochkarev
Copy link
Copy Markdown
Collaborator

What does it do?

  • Unified beforeunload: In MODx.FormPanel.onReady, when confirm_navigation is on, sets window.onbeforeunload to return the unsaved-changes message only when this.isDirty(), so the warning triggers on any form change (no field blur required). Removes the duplicate handler from the resource panel that relied on warnUnsavedChanges.
  • Left sidebar tab switch: When switching between Resources / Elements / Files, if any content form (resource, chunk, snippet, template, plugin, tv, file edit/create) is dirty, shows a confirm dialog; on "Yes" the tab switch proceeds, on "No" it is cancelled. Uses a new hasDirtyContentForm() helper on the layout.
  • Cancel button: In the toolbar handleClick path for URL redirect (e.g. Cancel), checks the page form panel for isDirty() and shows the same confirm before calling MODx.loadPage, so Elements and Files get the same behavior as Resources.

Why is it needed?

Closing or leaving a changed resource sometimes showed a warning only when the field had lost focus, and the warning did not apply to Elements or Files. This makes the behaviour consistent: any unsaved change triggers a warning when leaving (browser close/refresh), switching the left tab, or clicking Cancel, for Resources, Elements, and Files.

How to test

  1. Resources: Edit a resource (e.g. change title without blurring). Refresh or close the tab → warning. Switch left tab to Elements/Files → confirm. Click Cancel → confirm (existing). Save or Cancel+Yes → no warning.
  2. Elements (Chunk/Snippet/Template/Plugin/TV): Edit an element, then refresh/close tab, switch left tab, or click Cancel → confirm when dirty; no warning after save or Cancel+Yes.
  3. Files: Same for file create/edit.
  4. Ensure system setting "Confirm Navigation with unsaved changes" is enabled where the feature is expected.

Related issue(s)/PR(s)

Resolves #16277

@Ibochkarev Ibochkarev marked this pull request as ready for review February 21, 2026 18:48
@Ibochkarev Ibochkarev force-pushed the fix/16277-warn-unsaved-changes-on-close branch from 43271be to dc5a4e7 Compare February 22, 2026 16:30
…ources, Elements, Files)

- Use isDirty() for beforeunload in MODx.FormPanel onReady so any form change
  triggers the warning without requiring field blur; remove duplicate handler
  from resource panel.
- On left sidebar tab switch (Resources/Elements/Files), confirm when content
  has a dirty form; add hasDirtyContentForm() on layout.
- On Cancel button (toolbar redirect), confirm when form panel is dirty so
  Elements and Files match Resource behavior.

Resolves modxcms#16277
@Ibochkarev Ibochkarev force-pushed the fix/16277-warn-unsaved-changes-on-close branch from dc5a4e7 to a82cbf7 Compare February 22, 2026 17:02
@Ibochkarev Ibochkarev added the feature Request about implementing a brand new function or possibility. label Feb 26, 2026
@Ibochkarev Ibochkarev self-assigned this Feb 26, 2026
@smg6511 smg6511 assigned smg6511 and unassigned Ibochkarev Mar 26, 2026
@smg6511
Copy link
Copy Markdown
Collaborator

smg6511 commented Mar 26, 2026

@Ibochkarev - I'll take a look at this soon. A couple things:

  • I removed you as an Assignee since this is your PR
  • Only use const and let for variable definitions; the global var is almost never appropriate these days. It'll help if you run a javascript linter, as we have a bunch of rules set up that'll catch things like this.

@Ibochkarev
Copy link
Copy Markdown
Collaborator Author

@Ibochkarev - I'll take a look at this soon. A couple things:

  • I removed you as an Assignee since this is your PR
  • Only use const and let for variable definitions; the global var is almost never appropriate these days. It'll help if you run a javascript linter, as we have a bunch of rules set up that'll catch things like this.

Done — replaced var with const/let in the PR-touched code and used === for the trash tab id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Request about implementing a brand new function or possibility.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn when closing all unsaved objects

2 participants