*crm, project With owl2, the `el` of components is no longer available. It still works in Odoo on LegacyComponent, which has been introduced to ease the switch from owl1 to owl2, and we're now incrementally removing it. With this commit, the action scrolling helpers no longer rely on component.el, meaning that views using them (through the hook `useSetupAction`) no longer need to extend LegacyComponent. More specifically, we move the action scrolling helpers from core/ to webclient/actions, as those helpers make no sense outside the context of actions (and thus are unnecessary in the frontend). Tests have been adapted as well. closes odoo/odoo#85631 Related: odoo/enterprise#24902 Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
98 lines
2.5 KiB
JavaScript
98 lines
2.5 KiB
JavaScript
/** @odoo-module **/
|
|
|
|
import { getScrollPosition, setScrollPosition } from "./scrolling";
|
|
|
|
const { useComponent, useEffect } = owl;
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Action hook
|
|
// -----------------------------------------------------------------------------
|
|
const scrollSymbol = Symbol("scroll");
|
|
|
|
export class CallbackRecorder {
|
|
constructor() {
|
|
this.setup();
|
|
}
|
|
setup() {
|
|
this._callbacks = [];
|
|
}
|
|
/**
|
|
* @returns {Function[]}
|
|
*/
|
|
get callbacks() {
|
|
return this._callbacks.map(({ callback }) => callback);
|
|
}
|
|
/**
|
|
* @param {any} owner
|
|
* @param {Function} callback
|
|
*/
|
|
add(owner, callback) {
|
|
if (!callback) {
|
|
throw new Error("Missing callback");
|
|
}
|
|
this._callbacks.push({ owner, callback });
|
|
}
|
|
/**
|
|
* @param {any} owner
|
|
*/
|
|
remove(owner) {
|
|
this._callbacks = this._callbacks.filter((s) => s.owner !== owner);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {CallbackRecorder} callbackRecorder
|
|
* @param {Function} callback
|
|
*/
|
|
export function useCallbackRecorder(callbackRecorder, callback) {
|
|
const component = useComponent();
|
|
useEffect(
|
|
() => {
|
|
callbackRecorder.add(component, callback);
|
|
return () => callbackRecorder.remove(component);
|
|
},
|
|
() => []
|
|
);
|
|
}
|
|
|
|
/**
|
|
*/
|
|
export function useSetupAction(params = {}) {
|
|
const component = useComponent();
|
|
const {
|
|
__beforeLeave__,
|
|
__getGlobalState__,
|
|
__getLocalState__,
|
|
__getContext__,
|
|
} = component.env;
|
|
|
|
if (__beforeLeave__ && params.beforeLeave) {
|
|
useCallbackRecorder(__beforeLeave__, params.beforeLeave);
|
|
}
|
|
if (__getGlobalState__ && params.getGlobalState) {
|
|
useCallbackRecorder(__getGlobalState__, params.getGlobalState);
|
|
}
|
|
if (__getLocalState__) {
|
|
useCallbackRecorder(__getLocalState__, () => {
|
|
const state = {};
|
|
state[scrollSymbol] = getScrollPosition(component.env);
|
|
if (params.getLocalState) {
|
|
Object.assign(state, params.getLocalState());
|
|
}
|
|
return state;
|
|
});
|
|
}
|
|
if (__getContext__ && params.getContext) {
|
|
useCallbackRecorder(__getContext__, params.getContext);
|
|
}
|
|
|
|
useEffect(
|
|
() => {
|
|
if (component.props.state && component.props.state[scrollSymbol]) {
|
|
setScrollPosition(component.env, component.props.state[scrollSymbol]);
|
|
}
|
|
},
|
|
() => []
|
|
);
|
|
}
|