mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
small refactoring...
This commit is contained in:
@@ -7,6 +7,7 @@ import { QWeb } from "./qweb_vdom";
|
||||
//------------------------------------------------------------------------------
|
||||
// Types/helpers
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export interface WEnv {
|
||||
qweb: QWeb;
|
||||
getID(): number;
|
||||
@@ -35,6 +36,7 @@ const patch = init([sdListeners, sdAttrs]);
|
||||
//------------------------------------------------------------------------------
|
||||
// Widget
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export class Widget<T extends WEnv, Props> {
|
||||
__widget__: Meta<WEnv>;
|
||||
name: string = "widget";
|
||||
@@ -53,7 +55,7 @@ export class Widget<T extends WEnv, Props> {
|
||||
// Lifecycle
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
constructor(parent: Widget<T, {}> | T, props?: Props) {
|
||||
constructor(parent: Widget<T, any> | T, props?: Props) {
|
||||
wl.push(this);
|
||||
// is this a good idea?
|
||||
// Pro: if props is empty, we can create easily a widget
|
||||
|
||||
@@ -20,6 +20,7 @@ export interface Subscription {
|
||||
//------------------------------------------------------------------------------
|
||||
// EventBus
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export class EventBus {
|
||||
private subscriptions: { [eventType: string]: Subscription[] } = {};
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ const RESERVED_WORDS = "true,false,NaN,null,undefined,debugger,console,window,in
|
||||
//------------------------------------------------------------------------------
|
||||
// Compilation Context
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export class Context {
|
||||
nextID: number = 1;
|
||||
code: string[] = [];
|
||||
@@ -98,6 +99,7 @@ export class Context {
|
||||
//------------------------------------------------------------------------------
|
||||
// QWeb rendering engine
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export class QWeb {
|
||||
rawTemplates: { [name: string]: RawTemplate } = {};
|
||||
parsedTemplates: { [name: string]: ParsedTemplate } = {};
|
||||
@@ -461,6 +463,7 @@ export class QWeb {
|
||||
//------------------------------------------------------------------------------
|
||||
// QWeb Directives
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
interface CompilationInfo {
|
||||
nodeID?: number;
|
||||
node: Element;
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
///<amd-module name="main" />
|
||||
|
||||
import { debounce } from "./core/utils";
|
||||
import { makeEnvironment } from "./env";
|
||||
import { registry } from "./registry";
|
||||
import { Discuss } from "./widgets/discuss";
|
||||
import { Root } from "./root";
|
||||
import { debounce } from "./core/utils";
|
||||
import { CRM } from "./widgets/crm";
|
||||
import { Discuss } from "./widgets/discuss";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Prepare application registry
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
registry.add("action", "discuss", Discuss);
|
||||
registry.add("action", "crm", CRM);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Application bootstrapping
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async function() {
|
||||
const env = makeEnvironment();
|
||||
const rootWidget = new Root(env);
|
||||
|
||||
@@ -23,6 +23,10 @@ export class Registry {
|
||||
this.registries[type][name] = action;
|
||||
return this;
|
||||
}
|
||||
|
||||
getAction(name: string): ActionWidget {
|
||||
return this.registries.action[name];
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
+36
-46
@@ -1,80 +1,70 @@
|
||||
import { Widget } from "./core/widget";
|
||||
import { Env } from "./env";
|
||||
import { ActionStack } from "./services/action_manager";
|
||||
import { INotification } from "./services/notifications";
|
||||
import { Action } from "./widgets/action";
|
||||
import { HomeMenu } from "./widgets/home_menu";
|
||||
import { Navbar } from "./widgets/navbar";
|
||||
import { Notification } from "./widgets/notification";
|
||||
|
||||
const template = `
|
||||
<div class="o_web_client">
|
||||
<t t-widget="Navbar" t-props="{toggleHomeMenu:toggleHomeMenu,inMenu:state.inMenu}"/>
|
||||
<t t-if="state.inMenu">
|
||||
<t t-widget="HomeMenu" t-props="{toggleHomeMenu:toggleHomeMenu}"/>
|
||||
</t>
|
||||
<t t-else="1">
|
||||
<t t-widget="Action" t-keep-alive="1"/>
|
||||
</t>
|
||||
<div class="o_notification_container">
|
||||
<t t-foreach="state.notifications" t-as="notif">
|
||||
<t t-widget="Notification" t-props="notif"/>
|
||||
</t>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
//------------------------------------------------------------------------------
|
||||
// Types
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
interface State {
|
||||
notifications: INotification[];
|
||||
stack: ActionStack;
|
||||
inMenu: boolean;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Root Widget
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const template = `
|
||||
<div class="o_web_client">
|
||||
<t t-widget="Navbar" t-props="{toggleHome:toggleHome,inMenu:state.inMenu}"/>
|
||||
<t t-if="state.inMenu">
|
||||
<t t-widget="HomeMenu" t-keep-alive="1"/>
|
||||
</t>
|
||||
<t t-else="1">
|
||||
<t t-widget="Action" t-props="{stack:state.stack}" t-keep-alive="1"/>
|
||||
</t>
|
||||
<div class="o_notification_container">
|
||||
<t t-foreach="state.notifications" t-as="notif">
|
||||
<t t-widget="Notification" t-props="notif"/>
|
||||
</t>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
export class Root extends Widget<Env, {}> {
|
||||
name = "root";
|
||||
template = template;
|
||||
widgets = { Navbar, Notification, HomeMenu, Action };
|
||||
content: Widget<Env, {}> | null = null;
|
||||
|
||||
state: State = {
|
||||
notifications: [],
|
||||
stack: [],
|
||||
inMenu: false
|
||||
};
|
||||
|
||||
constructor(env: Env) {
|
||||
super(env);
|
||||
this.toggleHomeMenu = this.toggleHomeMenu.bind(this);
|
||||
this.toggleHome = this.toggleHome.bind(this);
|
||||
}
|
||||
|
||||
mounted() {
|
||||
// this.env.actionManager.on("action_ready", this, this.setContentWidget);
|
||||
this.env.notifications.on("notification_added", this, this.addNotif);
|
||||
this.env.notifications.on("notification_removed", this, this.removeNotif);
|
||||
|
||||
// const actionWidget = this.env.actionManager.getCurrentAction();
|
||||
// if (actionWidget) {
|
||||
// this.setContentWidget(actionWidget);
|
||||
// }
|
||||
this.env.notifications.on("notifications_updated", this, notifs =>
|
||||
this.updateState({ notifications: notifs })
|
||||
);
|
||||
this.env.actionManager.on("action_stack_updated", this, stack =>
|
||||
this.updateState({ stack })
|
||||
);
|
||||
this.env.actionManager.activate();
|
||||
}
|
||||
|
||||
// async setContentWidget(actionWidget: ActionWidget) {
|
||||
// const currentWidget = this.content;
|
||||
// const newWidget = new actionWidget.Widget(this, actionWidget.props);
|
||||
// await newWidget.mount(<HTMLElement>this.refs.content);
|
||||
// if (currentWidget) {
|
||||
// currentWidget.destroy();
|
||||
// }
|
||||
// this.content = newWidget;
|
||||
// }
|
||||
|
||||
addNotif(notif: INotification) {
|
||||
const notifications = this.state.notifications.concat(notif);
|
||||
this.updateState({ notifications });
|
||||
}
|
||||
removeNotif(notif: INotification) {
|
||||
const notifs = this.state.notifications.filter(f => f.id !== notif.id);
|
||||
this.updateState({ notifications: notifs });
|
||||
}
|
||||
|
||||
toggleHomeMenu() {
|
||||
toggleHome() {
|
||||
this.updateState({ inMenu: !this.state.inMenu });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -1,14 +1,36 @@
|
||||
import { Widget } from "../core/widget";
|
||||
import { Env } from "../env";
|
||||
import { ActionStack } from "../services/action_manager";
|
||||
|
||||
const template = `
|
||||
<div class="o_content">
|
||||
<span>MAIN ACTION</span>
|
||||
<input/>
|
||||
</div>
|
||||
`;
|
||||
|
||||
export class Action extends Widget<Env, {}> {
|
||||
name = "action";
|
||||
template = template;
|
||||
export interface Props {
|
||||
stack: ActionStack;
|
||||
}
|
||||
|
||||
export class Action extends Widget<Env, Props> {
|
||||
name = "action";
|
||||
template = `<div class="o_content"/>`;
|
||||
currentWidget: any;
|
||||
|
||||
mounted() {
|
||||
this.setContentWidget();
|
||||
}
|
||||
|
||||
shouldUpdate(nextProps: Props) {
|
||||
this.props = nextProps;
|
||||
this.setContentWidget();
|
||||
return false;
|
||||
}
|
||||
|
||||
async setContentWidget() {
|
||||
const info = this.props.stack[this.props.stack.length - 1];
|
||||
if (info && info.type === "client") {
|
||||
const Widget = info.Widget;
|
||||
let widget = new Widget(this, {});
|
||||
await widget.mount(this.el!);
|
||||
if (this.currentWidget) {
|
||||
this.currentWidget.destroy();
|
||||
}
|
||||
this.currentWidget = widget;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,19 +3,20 @@ import { Env, Menu } from "../env";
|
||||
|
||||
const template = `
|
||||
<div class="o_navbar">
|
||||
<a aria-label="Applications" class="o_title fa fa-th" href="#" title="Applications" accesskey="h" t-on-click="toggleHomeMenu"/>
|
||||
<a aria-label="Applications" class="o_title fa fa-th" href="#" title="Applications" accesskey="h" t-on-click="toggleHome"/>
|
||||
<ul t-if="!props.inMenu">
|
||||
<li t-foreach="env.menus" t-as="menu">
|
||||
<a t-att-href="getUrl(menu)">
|
||||
<t t-esc="menu.title"/>
|
||||
</a>
|
||||
</li>
|
||||
<li t-if="env.isMobile">MOBILEMODE</li>
|
||||
</ul>
|
||||
</div>
|
||||
`;
|
||||
|
||||
export interface Props {
|
||||
toggleHomeMenu: () => void;
|
||||
toggleHome: () => void;
|
||||
inMenu: boolean;
|
||||
}
|
||||
|
||||
@@ -28,8 +29,8 @@ export class Navbar extends Widget<Env, Props> {
|
||||
return this.env.router.formatURL("", { action_id });
|
||||
}
|
||||
|
||||
toggleHomeMenu(ev: MouseEvent) {
|
||||
toggleHome(ev: MouseEvent) {
|
||||
ev.preventDefault();
|
||||
this.props.toggleHomeMenu();
|
||||
this.props.toggleHome();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user