restore odoo behaviour on home url

This commit is contained in:
Géry Debongnie
2019-03-01 11:03:58 +01:00
parent 724181a5ba
commit 3a44d41654
4 changed files with 42 additions and 12 deletions
+10 -6
View File
@@ -4,11 +4,7 @@ import { EventBus, Callback } from "../core/event_bus";
// Types and helpers
//------------------------------------------------------------------------------
export type Query = { [key: string]: string };
function clearSlashes(s: string): string {
return s.replace(/\/$/, "").replace(/^\//, "");
}
export type Query = { [key: string]: string | true };
export type RouterEvent = "query_changed";
@@ -23,6 +19,10 @@ export interface IRouter {
// Router
//------------------------------------------------------------------------------
function clearSlashes(s: string): string {
return s.replace(/\/$/, "").replace(/^\//, "");
}
export class Router extends EventBus implements IRouter {
currentQuery: Query;
@@ -46,7 +46,11 @@ export class Router extends EventBus implements IRouter {
formatQuery(query: Query): string {
let parts: string[] = [];
for (let key in query) {
parts.push(`${key}=${query[key]}`);
if (query[key] === true) {
parts.push(key);
} else {
parts.push(`${key}=${query[key]}`);
}
}
return parts.join("&");
}
+17 -4
View File
@@ -62,6 +62,7 @@ export class BaseStore extends EventBus {
menuInfo: MenuInfo;
services: Services;
actionRegistry: Registry<ActionWidget>;
currentQuery: Query;
constructor(
services: Services,
@@ -72,6 +73,7 @@ export class BaseStore extends EventBus {
this.services = services;
this.menuInfo = menuInfo;
this.actionRegistry = actionRegistry;
this.currentQuery = {};
}
update(nextState: Partial<State>) {
@@ -84,6 +86,16 @@ export class BaseStore extends EventBus {
return;
}
this.update({ inHome: !this.state.inHome });
if (this.state.inHome) {
this.services.router.navigate({ home: true });
} else {
this.updateQuery(this.currentQuery);
}
}
updateQuery(query: Query) {
this.currentQuery = query;
this.services.router.navigate(query);
}
}
@@ -101,6 +113,7 @@ export class Store extends actionManagerMixin(
this.state.currentApp = app;
if (!actionId) {
this.state.inHome = true;
this.services.router.navigate({ home: true });
}
this.services.router.on("query_changed", this, this.updateAction);
@@ -131,8 +144,8 @@ export class Store extends actionManagerMixin(
if (app) {
this.update({ currentApp: app });
}
this.services.router.navigate(query);
return this.doAction(actionId);
await this.doAction(actionId);
this.updateQuery(query);
} else {
this.update({ inHome: true, currentApp: newApp });
}
@@ -144,14 +157,14 @@ export class Store extends actionManagerMixin(
const menuInfo = this.menuInfo;
let app: MenuItem | null = null;
let actionId: number | null = null;
if ("action_id" in query) {
if (typeof query.action_id === "string") {
actionId = parseInt(query.action_id, 10);
if (menuInfo.actionMap[actionId]) {
const menu = menuInfo.actionMap[actionId]!;
app = menu.app;
}
}
if ("menu_id" in query) {
if (typeof query.menu_id === "string") {
const menuId = parseInt(query.menu_id, 10);
const menu = menuInfo.menus[menuId];
if (menu) {
+14 -1
View File
@@ -107,13 +107,26 @@ describe("state transitions", () => {
// should still be in home menu since no app is currently active
expect(store.state.inHome).toBe(true);
expect(store.services.router.getQuery()).toEqual({ home: true });
await store.activateMenuItem(96);
const promise = store.activateMenuItem(96);
expect(store.services.router.getQuery()).toEqual({ home: true });
await promise;
expect(store.state.inHome).toBe(false);
expect(store.services.router.getQuery()).toEqual({
action_id: "131",
menu_id: "96"
});
store.toggleHomeMenu();
expect(store.state.inHome).toBe(true);
expect(store.services.router.getQuery()).toEqual({ home: true });
store.toggleHomeMenu();
expect(store.state.inHome).toBe(false);
expect(store.services.router.getQuery()).toEqual({
action_id: "131",
menu_id: "96"
});
});
});
+1 -1
View File
@@ -57,7 +57,7 @@ test("start with no action => clicks on client action => discuss is rendered", a
const root = new Root(env, store);
await root.mount(fixture);
expect(env.services.router.getQuery()).toEqual({});
expect(env.services.router.getQuery()).toEqual({ home: true });
// discuss menu item
await (<any>document.querySelector('[data-menu="96"]')).click();