From e2fad78eeb6dd36fc17037069c0532a940d40377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 6 Feb 2019 15:08:24 +0100 Subject: [PATCH] move ajax to services, prepare actionmanager to use ajax --- web/static/src/ts/core/ajax.ts | 22 ------- web/static/src/ts/init.ts | 4 +- web/static/src/ts/services/action_manager.ts | 21 ++++++- web/static/src/ts/services/ajax.ts | 62 ++++++++++++++++++++ web/static/src/ts/widgets/widget.ts | 2 +- web/static/tests/helpers.ts | 4 +- 6 files changed, 87 insertions(+), 28 deletions(-) delete mode 100644 web/static/src/ts/core/ajax.ts create mode 100644 web/static/src/ts/services/ajax.ts diff --git a/web/static/src/ts/core/ajax.ts b/web/static/src/ts/core/ajax.ts deleted file mode 100644 index c1fee209..00000000 --- a/web/static/src/ts/core/ajax.ts +++ /dev/null @@ -1,22 +0,0 @@ -//------------------------------------------------------------------------------ -// Types -//------------------------------------------------------------------------------ - -export interface RPCQuery { - model: string; - method: string; - args: any; -} -export interface IAjax { - rpc(rpc: RPCQuery): Promise; -} - -//------------------------------------------------------------------------------ -// Ajax -//------------------------------------------------------------------------------ - -export class Ajax implements IAjax { - rpc(rpc: RPCQuery): Promise { - return Promise.resolve(1); - } -} diff --git a/web/static/src/ts/init.ts b/web/static/src/ts/init.ts index 65fa654c..1413f270 100644 --- a/web/static/src/ts/init.ts +++ b/web/static/src/ts/init.ts @@ -1,4 +1,4 @@ -import { Ajax } from "./core/ajax"; +import { Ajax } from "./services/ajax"; import { NotificationManager } from "./core/notifications"; import { QWeb } from "./core/qweb_vdom"; import { Router } from "./core/router"; @@ -32,7 +32,7 @@ export const init = memoize(async function(): Promise { const qweb = new QWeb(); const router = new Router(); const ajax = new Ajax(); - const actionManager = new ActionManager(actionRegistry); + const actionManager = new ActionManager(actionRegistry, ajax); const notifications = new NotificationManager(); // templates diff --git a/web/static/src/ts/services/action_manager.ts b/web/static/src/ts/services/action_manager.ts index 77445a61..c847aa9c 100644 --- a/web/static/src/ts/services/action_manager.ts +++ b/web/static/src/ts/services/action_manager.ts @@ -1,3 +1,4 @@ +import { IAjax } from "./ajax"; import { Type } from "../core/component"; import { EventBus } from "../core/event_bus"; import { Registry } from "../core/registry"; @@ -31,6 +32,12 @@ export interface ActWindowInfo extends CommonActionInfo { view: string; } +export interface ActionDescription { + id: number; + type: "ir.actions.act_window" | "ir.actions.client"; + target: "current"; +} + export type ActionInfo = ClientActionInfo | ActWindowInfo; export type ActionStack = ActionInfo[]; @@ -50,16 +57,19 @@ export interface IActionManager { export class ActionManager extends EventBus implements IActionManager { registry: Registry; + ajax: IAjax; stack: ActionStack; - constructor(registry: Registry) { + constructor(registry: Registry, ajax: IAjax) { super(); this.registry = registry; + this.ajax = ajax; this.stack = []; } doAction(request: ActionRequest) { if (typeof request === "number") { + this.loadAction(request); // this is an action ID let name = request === 131 ? "discuss" : "crm"; let title = @@ -80,6 +90,15 @@ export class ActionManager extends EventBus implements IActionManager { } } + private loadAction(id: number) { + this.ajax.rpc({ + route: "web/action/load", + params: { + action_id: id + } + }); + } + getStack(): ActionStack { return []; } diff --git a/web/static/src/ts/services/ajax.ts b/web/static/src/ts/services/ajax.ts new file mode 100644 index 00000000..75be25cf --- /dev/null +++ b/web/static/src/ts/services/ajax.ts @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// Types +//------------------------------------------------------------------------------ + +export interface RPCModelQuery { + model: string; + method: string; + args?: any[]; + kwargs?: { [key: string]: any }; + context?: { [key: string]: any }; +} + +export interface RPCControllerQuery { + route: string; + params: { [key: string]: any }; +} + +export type RPCQuery = RPCModelQuery | RPCControllerQuery; + +export interface IAjax { + rpc(rpc: RPCQuery): Promise; +} + +interface RequestParameters { + route: string; + params: { [key: string]: any }; +} + +//------------------------------------------------------------------------------ +// Ajax +//------------------------------------------------------------------------------ + +export class Ajax implements IAjax { + rpc(rpc: RPCQuery): Promise { + const request = this.prepareRequest(rpc); + console.log("RPC", request.route, request.params); + const delay = Math.random() * 150; + return new Promise(resolve => setTimeout(resolve, delay)); + } + + private prepareRequest(query: RPCQuery): RequestParameters { + let route: string; + let params = "params" in query ? query.params : {}; + if ("route" in query) { + route = query.route; + } else if ("model" in query && "method" in query) { + route = `/web/dataset/call_kw/${query.model}/${query.method}`; + params.args = query.args || []; + params.model = query.model; + params.method = query.method; + params.kwargs = Object.assign(params.kwargs || {}, query.kwargs); + params.kwargs.context = + query.context || params.context || params.kwargs.context; + } else { + throw new Error("Invalid Query"); + } + + // doing this remove empty keys, and undefined stuff + const sanitizedParams = JSON.parse(JSON.stringify(params)); + return { route, params: sanitizedParams }; + } +} diff --git a/web/static/src/ts/widgets/widget.ts b/web/static/src/ts/widgets/widget.ts index 5b18e518..2e112333 100644 --- a/web/static/src/ts/widgets/widget.ts +++ b/web/static/src/ts/widgets/widget.ts @@ -1,4 +1,4 @@ -import { IAjax } from "../core/ajax"; +import { IAjax } from "../services/ajax"; import { Component, PureComponent, WEnv } from "../core/component"; import { INotificationManager } from "../core/notifications"; import { Registry } from "../core/registry"; diff --git a/web/static/tests/helpers.ts b/web/static/tests/helpers.ts index cab0158e..e10c6c05 100644 --- a/web/static/tests/helpers.ts +++ b/web/static/tests/helpers.ts @@ -1,5 +1,5 @@ import { readFile } from "fs"; -import { IAjax, RPCQuery } from "../src/ts/core/ajax"; +import { IAjax, RPCQuery } from "../src/ts/services/ajax"; import { WEnv } from "../src/ts/core/component"; import { Callback } from "../src/ts/core/event_bus"; import { NotificationManager } from "../src/ts/core/notifications"; @@ -31,7 +31,7 @@ export interface MockEnv extends Env { export function makeTestEnv(): MockEnv { const ajax = new MockAjax(); - const actionManager = new ActionManager(actionRegistry); + const actionManager = new ActionManager(actionRegistry, ajax); const router = new MockRouter(); const notifications = new NotificationManager(); let { qweb, getID } = makeTestWEnv();