diff --git a/addons/web/static/src/legacy/legacy_client_actions.js b/addons/web/static/src/legacy/legacy_client_actions.js index fc5f734b2a0..d2a48c5f2dc 100644 --- a/addons/web/static/src/legacy/legacy_client_actions.js +++ b/addons/web/static/src/legacy/legacy_client_actions.js @@ -50,9 +50,17 @@ function registerClientAction(name, action) { this.widget = this.props.state && this.props.state.__legacy_widget__; this.onReverseBreadcrumb = this.props.state && this.props.state.__on_reverse_breadcrumb__; + + const rootRef = { + get el() { + return legacyRefs.widget && legacyRefs.widget.el; + }, + }; + useSetupAction({ beforeLeave: () => legacyRefs.widget.canBeRemoved(), getLocalState: () => legacyRefs.component.exportState(), + rootRef, }); } } diff --git a/addons/web/static/tests/webclient/actions/client_action_tests.js b/addons/web/static/tests/webclient/actions/client_action_tests.js index db0c8593a85..065e540bac6 100644 --- a/addons/web/static/tests/webclient/actions/client_action_tests.js +++ b/addons/web/static/tests/webclient/actions/client_action_tests.js @@ -350,6 +350,60 @@ QUnit.module("ActionManager", (hooks) => { delete core.action_registry.map.ClientAction2; }); + QUnit.test("client action restore scrollbar (legacy)", async function (assert) { + assert.expect(7); + const ClientAction = AbstractAction.extend({ + hasControlPanel: true, + init(parent, action) { + action.display_name = "Title1"; + this._super.apply(this, arguments); + }, + async start() { + for (let i = 0; i < 100; i++) { + const content = document.createElement("div"); + content.innerText = "Paper company"; + content.className = "lorem"; + this.el.querySelector(".o_content").appendChild(content); + } + await this._super(arguments); + }, + }); + const ClientAction2 = AbstractAction.extend({ + hasControlPanel: true, + init(parent, action) { + action.display_name = "Title2"; + this._super.apply(this, arguments); + }, + start() { + return this._super.apply(this, arguments); + }, + }); + core.action_registry.add("ClientAction", ClientAction); + core.action_registry.add("ClientAction2", ClientAction2); + const webClient = await createWebClient({ serverData }); + await doAction(webClient, "ClientAction"); + assert.containsOnce(target, ".breadcrumb-item"); + assert.strictEqual(target.querySelector(".breadcrumb-item.active").textContent, "Title1"); + + target.querySelector(".lorem:last-child").scrollIntoView(); + const scrollPosition = target.querySelector(".o_content").scrollTop; + assert.ok(scrollPosition > 0); + await doAction(webClient, "ClientAction2", { clearBreadcrumbs: false }); + assert.containsN(target, ".breadcrumb-item", 2); + assert.strictEqual(target.querySelector(".breadcrumb-item.active").textContent, "Title2"); + + await click(target.querySelector(".breadcrumb-item:first-child")); + assert.strictEqual(target.querySelector(".breadcrumb-item.active").textContent, "Title1"); + + assert.strictEqual( + target.querySelector(".o_content").scrollTop, + scrollPosition, + "Should restore the scroll" + ); + delete core.action_registry.map.ClientAction; + delete core.action_registry.map.ClientAction2; + }); + QUnit.test("ClientAction receives breadcrumbs and exports title (wowl)", async (assert) => { assert.expect(4); class ClientAction extends Component {