small refactoring...

This commit is contained in:
Géry Debongnie
2019-01-31 13:19:30 +01:00
parent bac075dbbd
commit 92bea3df52
17 changed files with 199 additions and 158 deletions
+54 -53
View File
@@ -1,47 +1,50 @@
import { Callback, EventBus } from "../core/event_bus";
import { EventBus } from "../core/event_bus";
import { Widget } from "../core/widget";
import { Env } from "../env";
import { Registry } from "../registry";
import { Type } from "../types";
import { CRM } from "../widgets/crm";
import { Discuss } from "../widgets/discuss";
import { IRouter, Query } from "./router";
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
export interface ClientAction {
export type ActionRequest = string | number;
export type Context = { [key: string]: any };
export interface CommonActionInfo {
id: number;
context: Context;
title: string;
target: "current" | "new";
}
export interface ClientActionInfo extends CommonActionInfo {
type: "client";
name: string;
Widget: Type<Widget<Env, {}>>;
}
export interface ActWindowAction {
export interface ActWindowInfo extends CommonActionInfo {
type: "act_window";
views: string[];
view: string;
}
export type ActionEvent = "action_ready";
export type ActionInfo = ClientActionInfo | ActWindowInfo;
export type ActionStack = ActionInfo[];
export type ActionEvent = "action_stack_updated";
type Callback = (stack: ActionStack) => void;
export interface IActionManager {
doAction(actionID: number): void;
activate(): void;
doAction(request: ActionRequest): void;
on(event: ActionEvent, owner: any, callback: Callback): void;
getCurrentAction(): ActionWidget | null;
getStack(): ActionStack;
}
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 }
];
//------------------------------------------------------------------------------
// Action Manager
//------------------------------------------------------------------------------
@@ -49,49 +52,47 @@ const actions: any[] = [
export class ActionManager extends EventBus implements IActionManager {
router: IRouter;
registry: Registry;
currentAction: ActionWidget | null = null;
stack: ActionStack;
constructor(router: IRouter, registry: Registry) {
super();
this.router = router;
this.registry = registry;
const query = this.router.getQuery();
this.update(query);
this.stack = [];
}
activate() {
this.router.on("query_changed", this, this.update);
if (!this.currentAction) {
const action = actions.find(a => a.default);
if (action) {
this.doAction(action.id);
}
this.update(this.router.getQuery());
}
doAction(request: ActionRequest) {
if (typeof request === "number") {
// this is an action ID
let name = request === 1 ? 'discuss' : 'crm';
let title = request === 1 ? 'Discuss': 'CRM';
let Widget = this.registry.getAction(name);
this.stack = [
{
id: 1,
context: {},
target: "current",
type: "client",
name,
title,
Widget: Widget
}
];
this.trigger("action_stack_updated", this.stack);
}
}
getStack(): ActionStack {
return [];
}
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;
}
}
+8
View File
@@ -1,3 +1,7 @@
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
export interface RPCQuery {
model: string;
method: string;
@@ -7,6 +11,10 @@ export interface IAjax {
rpc(rpc: RPCQuery): Promise<any>;
}
//------------------------------------------------------------------------------
// Ajax
//------------------------------------------------------------------------------
export class Ajax implements IAjax {
rpc(rpc: RPCQuery): Promise<any> {
return Promise.resolve(1);
+8 -10
View File
@@ -12,9 +12,9 @@ export interface INotification {
sticky: boolean;
}
export type NotificationEvent = "notification_added" | "notification_removed";
export type NotificationEvent = "notifications_updated";
export type Callback = (notif: INotification) => void;
export type Callback = (notifs: INotification[]) => void;
export interface INotificationManager {
add(notif: Partial<INotification>): number;
@@ -25,9 +25,10 @@ export interface INotificationManager {
//------------------------------------------------------------------------------
// Notification Manager
//------------------------------------------------------------------------------
export class NotificationManager extends Bus implements INotificationManager {
nextID = 1;
notifications: { [key: number]: INotification } = {};
notifications: INotification[] = [];
add(notif: Partial<INotification>): number {
const id = this.nextID++;
@@ -38,18 +39,15 @@ export class NotificationManager extends Bus implements INotificationManager {
sticky: false
};
const notification = Object.assign(defaultVals, notif, { id });
this.notifications[id] = notification;
this.trigger("notification_added", notification);
this.notifications.push(notification);
this.trigger("notifications_updated", this.notifications);
if (!notification.sticky) {
setTimeout(() => this.close(id), 2500);
}
return id;
}
close(id: number) {
let notification = this.notifications[id];
if (notification) {
delete this.notifications[id];
this.trigger("notification_removed", notification);
}
this.notifications = this.notifications.filter(n => n.id !== id);
this.trigger("notifications_updated", this.notifications);
}
}
+2
View File
@@ -3,6 +3,7 @@ import { EventBus, Callback } from "../core/event_bus";
//------------------------------------------------------------------------------
// Types and helpers
//------------------------------------------------------------------------------
export type Query = { [key: string]: string };
function clearSlashes(s: string): string {
@@ -21,6 +22,7 @@ export interface IRouter {
//------------------------------------------------------------------------------
// Router
//------------------------------------------------------------------------------
export class Router extends EventBus implements IRouter {
currentQuery: Query;