From fbbbc5e93bb55ddcb8c8bec01b77b67f1b87aa5a Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Tue, 24 Sep 2024 13:23:27 +0200 Subject: [PATCH] move discard changes logic --- .../workspace/entity/entity-workspace-base.ts | 60 +++++++++++++++++++ .../workspace/data-type-workspace.context.ts | 36 ----------- 2 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/entity/entity-workspace-base.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/entity/entity-workspace-base.ts index 7d7f911e46..90062ec260 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/entity/entity-workspace-base.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/entity/entity-workspace-base.ts @@ -1,9 +1,69 @@ import { UmbSubmittableWorkspaceContextBase } from '../submittable/index.js'; import { UmbEntityWorkspaceDataManager } from './entity-workspace-data-manager.js'; +import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; import type { UmbEntityModel } from '@umbraco-cms/backoffice/entity'; +import { UMB_DISCARD_CHANGES_MODAL, UMB_MODAL_MANAGER_CONTEXT } from '@umbraco-cms/backoffice/modal'; export abstract class UmbEntityWorkspaceContextBase< EntityModelType extends UmbEntityModel, > extends UmbSubmittableWorkspaceContextBase { + /** + * @description Data manager for the workspace. + * @protected + * @memberof UmbEntityWorkspaceContextBase + */ protected readonly _data = new UmbEntityWorkspaceDataManager(this); + + constructor(host: UmbControllerHost, workspaceAlias: string) { + super(host, workspaceAlias); + window.addEventListener('willchangestate', this.#onWillNavigate); + } + + /** + * @description method to check if the workspace is about to navigate away. + * @protected + * @param {string} newUrl + * @returns {*} + * @memberof UmbEntityWorkspaceContextBase + */ + protected _checkWillNavigateAway(newUrl: string) { + let willNavigateAway = false; + + if (this.getIsNew()) { + willNavigateAway = !newUrl.includes(`${this.getEntityType()}/create`); + } else { + willNavigateAway = !newUrl.includes(this.getUnique()!); + } + + return willNavigateAway; + } + + #onWillNavigate = async (e: CustomEvent) => { + const newUrl = e.detail.url; + + if (this._checkWillNavigateAway(newUrl) && this._data.hasUnpersistedChanges()) { + e.preventDefault(); + const modalManager = await this.getContext(UMB_MODAL_MANAGER_CONTEXT); + const modal = modalManager.open(this, UMB_DISCARD_CHANGES_MODAL); + + try { + // navigate to the new url when discarding changes + await modal.onSubmit(); + // Reset the current data so we don't end in a endless loop of asking to discard changes. + this._data.resetCurrentData(); + history.pushState({}, '', e.detail.url); + return true; + } catch { + return false; + } + } + + return true; + }; + + public override destroy(): void { + this._data.destroy(); + window.removeEventListener('willchangestate', this.#onWillNavigate); + super.destroy(); + } } diff --git a/src/Umbraco.Web.UI.Client/src/packages/data-type/workspace/data-type-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/packages/data-type/workspace/data-type-workspace.context.ts index 653da46375..a087c9709f 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/data-type/workspace/data-type-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/data-type/workspace/data-type-workspace.context.ts @@ -98,7 +98,6 @@ export class UmbDataTypeWorkspaceContext constructor(host: UmbControllerHost) { super(host, 'Umb.Workspace.DataType'); - window.addEventListener('willchangestate', this.#onWillNavigate); this.addValidationContext(new UmbValidationContext(this)); this.#observePropertyEditorSchemaAlias(); @@ -429,46 +428,11 @@ export class UmbDataTypeWorkspaceContext await this.repository.delete(unique); } - #willNavigateAway(newUrl: string) { - let willNavigateAway = false; - - if (this.getIsNew()) { - willNavigateAway = !newUrl.includes(`${this.getEntityType()}/create`); - } else { - willNavigateAway = !newUrl.includes(this.getUnique()!); - } - - return willNavigateAway; - } - - #onWillNavigate = async (e: CustomEvent) => { - const newUrl = e.detail.url; - - if (this.#willNavigateAway(newUrl) && this._data.hasUnpersistedChanges()) { - e.preventDefault(); - const modalManager = await this.getContext(UMB_MODAL_MANAGER_CONTEXT); - const modal = modalManager.open(this, UMB_DISCARD_CHANGES_MODAL); - - try { - // navigate to the new url when discarding changes - await modal.onSubmit(); - // Reset the current data so we don't end in a endless loop of asking to discard changes. - this._data.resetCurrentData(); - history.pushState({}, '', e.detail.url); - return true; - } catch { - return false; - } - } - }; - public override destroy(): void { - this._data.destroy(); this.#properties.destroy(); this.#propertyEditorUiIcon.destroy(); this.#propertyEditorUiName.destroy(); this.repository.destroy(); - window.removeEventListener('willchangestate', this.#onWillNavigate); super.destroy(); } }