[REF] web: new view service API
We simplify and document the view service API.
This commit is contained in:
@@ -324,8 +324,8 @@ export class ViewAdapter extends ActionAdapter {
|
||||
return Object.assign({}, state, widgetState);
|
||||
}
|
||||
|
||||
async loadViews(model, context, views) {
|
||||
return (await this.vm.loadViews({ model, views, context }, {})).fields_views;
|
||||
async loadViews(resModel, context, views) {
|
||||
return (await this.vm.loadViews({ resModel, views, context }, {})).__legacy__.fields_views;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -80,17 +80,18 @@ function registerView(name, LegacyView) {
|
||||
|
||||
async willStart() {
|
||||
const params = {
|
||||
model: this.props.resModel,
|
||||
resModel: this.props.resModel,
|
||||
views: this.props.views,
|
||||
context: this.props.context,
|
||||
};
|
||||
const options = {
|
||||
actionId: this.props.actionId,
|
||||
context: this.props.context,
|
||||
withActionMenus: this.props.withActionMenus,
|
||||
withFilters: this.props.withFilters,
|
||||
loadActionMenus: this.props.loadActionMenus,
|
||||
loadIrFilters: this.props.loadIrFilters,
|
||||
};
|
||||
const result = await this.vm.loadViews(params, options);
|
||||
const viewDescriptions = await this.vm.loadViews(params, options);
|
||||
const result = viewDescriptions.__legacy__;
|
||||
const fieldsInfo = result.fields_views[this.props.type];
|
||||
const jsClass = getJsClassWidget(fieldsInfo);
|
||||
this.View = jsClass || this.View;
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import { registry } from "../core/registry";
|
||||
|
||||
/**
|
||||
* @typedef {Object} Fields
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {string} ViewType // to define
|
||||
*/
|
||||
import { registry } from "@web/core/registry";
|
||||
|
||||
/**
|
||||
* @typedef {Object} IrFilter
|
||||
@@ -26,24 +18,27 @@ import { registry } from "../core/registry";
|
||||
/**
|
||||
* @typedef {Object} ViewDescription
|
||||
* @property {string} arch
|
||||
* @property {Fields} fields
|
||||
* @property {ViewType} type
|
||||
* @property {number} view_id
|
||||
* @property {IrFilter[]} [irFilters]
|
||||
* @property {Object} fields
|
||||
* @property {string} model
|
||||
* @property {string} [name] is returned by the server ("default" or real name)
|
||||
* @property {string} type
|
||||
* @property {number} [viewId]
|
||||
* @property {Object} [actionMenus] // for views other than search
|
||||
* @property {IrFilter[]} [irFilters] // for search view
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} LoadViewsParams
|
||||
* @property {string} model
|
||||
* @property {[number, ViewType][]} views
|
||||
* @property {string} resModel
|
||||
* @property {[number, string][]} views
|
||||
* @property {Object} context
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} LoadViewsOptions
|
||||
* @property {number} actionId
|
||||
* @property {boolean} withActionMenus
|
||||
* @property {boolean} withFilters
|
||||
* @property {number|false} actionId
|
||||
* @property {boolean} loadActionMenus
|
||||
* @property {boolean} loadIrFilters
|
||||
*/
|
||||
|
||||
export const viewService = {
|
||||
@@ -65,29 +60,40 @@ export const viewService = {
|
||||
* @returns {Promise<ViewDescriptions>}
|
||||
*/
|
||||
async function loadViews(params, options) {
|
||||
const key = JSON.stringify([params.model, params.views, params.context, options]);
|
||||
const key = JSON.stringify([params.resModel, params.views, params.context, options]);
|
||||
if (!cache[key]) {
|
||||
cache[key] = orm
|
||||
.call(params.model, "load_views", [], {
|
||||
.call(params.resModel, "load_views", [], {
|
||||
views: params.views,
|
||||
options: {
|
||||
action_id: options.actionId || false,
|
||||
load_filters: options.withFilters || false,
|
||||
toolbar: options.withActionMenus || false,
|
||||
load_filters: options.loadIrFilters || false,
|
||||
toolbar: options.loadActionMenus || false,
|
||||
},
|
||||
context: params.context,
|
||||
})
|
||||
.then((result) => {
|
||||
const viewDescriptions = result; // for legacy purpose, keys in result are left in viewDescriptions
|
||||
|
||||
const viewDescriptions = {
|
||||
__legacy__: result,
|
||||
}; // for legacy purpose, keys in result are left in viewDescriptions
|
||||
for (const [_, viewType] of params.views) {
|
||||
const viewDescription = result.fields_views[viewType];
|
||||
const viewDescription = JSON.parse(
|
||||
JSON.stringify(result.fields_views[viewType])
|
||||
);
|
||||
viewDescription.viewId = viewDescription.view_id;
|
||||
delete viewDescription.view_id;
|
||||
if (viewDescription.toolbar) {
|
||||
viewDescription.actionMenus = viewDescription.toolbar;
|
||||
delete viewDescription.toolbar;
|
||||
}
|
||||
viewDescription.fields = Object.assign(
|
||||
{},
|
||||
result.fields,
|
||||
viewDescription.fields
|
||||
); // before a deep freeze was done.
|
||||
if (viewType === "search" && options.withFilters) {
|
||||
delete viewDescription.base_model; // unused
|
||||
delete viewDescription.field_parent; // unused
|
||||
if (viewType === "search" && options.loadIrFilters) {
|
||||
viewDescription.irFilters = result.filters;
|
||||
}
|
||||
viewDescriptions[viewType] = viewDescription;
|
||||
|
||||
@@ -387,8 +387,8 @@ function makeActionManager(env) {
|
||||
type: view.type,
|
||||
views: action.views,
|
||||
viewSwitcherEntries,
|
||||
withActionMenus: target !== "new" && target !== "inline",
|
||||
withFilters: action.views.some((v) => v[1] === "search"),
|
||||
loadActionMenus: target !== "new" && target !== "inline",
|
||||
loadIrFilters: action.views.some((v) => v[1] === "search"),
|
||||
});
|
||||
if (action.res_id) {
|
||||
viewProps.resId = action.res_id;
|
||||
|
||||
@@ -38,14 +38,14 @@ QUnit.module("View service", (hooks) => {
|
||||
|
||||
await env.services.views.loadViews(
|
||||
{
|
||||
model: "take.five",
|
||||
resModel: "take.five",
|
||||
views: [[99, "list"]],
|
||||
},
|
||||
{}
|
||||
);
|
||||
await env.services.views.loadViews(
|
||||
{
|
||||
model: "take.five",
|
||||
resModel: "take.five",
|
||||
views: [[99, "list"]],
|
||||
},
|
||||
{}
|
||||
@@ -70,7 +70,7 @@ QUnit.module("View service", (hooks) => {
|
||||
try {
|
||||
await env.services.views.loadViews(
|
||||
{
|
||||
model: "take.five",
|
||||
resModel: "take.five",
|
||||
views: [[99, "list"]],
|
||||
},
|
||||
{}
|
||||
@@ -82,7 +82,7 @@ QUnit.module("View service", (hooks) => {
|
||||
try {
|
||||
await env.services.views.loadViews(
|
||||
{
|
||||
model: "take.five",
|
||||
resModel: "take.five",
|
||||
views: [[99, "list"]],
|
||||
},
|
||||
{}
|
||||
|
||||
Reference in New Issue
Block a user