move ajax to services, prepare actionmanager to use ajax

This commit is contained in:
Géry Debongnie
2019-02-06 15:08:24 +01:00
parent a33a077173
commit e2fad78eeb
6 changed files with 87 additions and 28 deletions
-22
View File
@@ -1,22 +0,0 @@
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
export interface RPCQuery {
model: string;
method: string;
args: any;
}
export interface IAjax {
rpc(rpc: RPCQuery): Promise<any>;
}
//------------------------------------------------------------------------------
// Ajax
//------------------------------------------------------------------------------
export class Ajax implements IAjax {
rpc(rpc: RPCQuery): Promise<any> {
return Promise.resolve(1);
}
}
+2 -2
View File
@@ -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<InitializedData> {
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
+20 -1
View File
@@ -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<ActionWidget>;
ajax: IAjax;
stack: ActionStack;
constructor(registry: Registry<ActionWidget>) {
constructor(registry: Registry<ActionWidget>, 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 [];
}
+62
View File
@@ -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<any>;
}
interface RequestParameters {
route: string;
params: { [key: string]: any };
}
//------------------------------------------------------------------------------
// Ajax
//------------------------------------------------------------------------------
export class Ajax implements IAjax {
rpc(rpc: RPCQuery): Promise<any> {
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 };
}
}
+1 -1
View File
@@ -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";
+2 -2
View File
@@ -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();