refactoring: implement actionmanager, and router

So we now have a working SPA!
This commit is contained in:
Géry Debongnie
2019-01-24 10:58:46 +01:00
parent 443f5e210c
commit c6b83b1aed
10 changed files with 193 additions and 144 deletions
+76 -17
View File
@@ -1,22 +1,81 @@
import { Action } from "./actions";
import { Bus } from "../core/bus";
import { Widget } from "../core/widget";
import { Env } from "../env";
import { Router, Query } from "./router";
import { Discuss } from "../widgets/discuss";
import { CRM } from "../widgets/crm";
// export interface Action {
// id: number;
// title: string;
// Widget: Type<Widget<Env>>;
// default?: boolean;
// }
interface Type<T> extends Function {
new (...args: any[]): T;
}
// const actions: Action[] = [
// { id: 1, title: "Discuss", Widget: Discuss, default: true },
// { id: 2, title: "CRM", Widget: CRM }
// ];
interface ClientAction {
type: "client";
name: string;
}
export class ActionManager {
doAction(action: Action) {
console.log(action);
// load data (??)
// trigger action somewhere
// upload url with router
interface ActWindowAction {
type: "act_window";
views: string[];
}
export type Action = ClientAction | ActWindowAction;
export interface ActionWidget {
id: number;
Widget: Type<Widget<Env>>;
props: any;
}
const actions: any[] = [
{ id: 1, title: "Discuss", Widget: Discuss, default: true },
{ id: 2, title: "CRM", Widget: CRM }
];
export class ActionManager extends Bus {
router: Router;
currentAction: ActionWidget | null = null;
constructor(router: Router) {
super();
this.router = router;
const query = this.router.getQuery();
this.update(query);
this.router.on("query_changed", this, this.update);
if (!this.currentAction) {
const action = actions.find(a => a.default);
if (action) {
this.doAction(action.id);
}
}
}
update(query: Query) {
const initialAction = this.currentAction;
if ("action_id" in query) {
const actionID = parseInt(query.action_id);
this.doAction(actionID);
}
if (this.currentAction && initialAction !== this.currentAction) {
}
}
doAction(actionID: number) {
const action = actions.find(a => a.id === actionID);
if (action) {
this.currentAction = {
id: action.id,
Widget: action.Widget,
props: {}
};
}
this.trigger("action_ready", this.currentAction);
this.router.navigate({ action_id: String(actionID) });
}
registerAction(action: Action) {}
getCurrentAction(): ActionWidget | null {
return this.currentAction;
}
}