mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
refactoring, work on environment
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { Action } from "./actions";
|
||||
|
||||
// export interface Action {
|
||||
// id: number;
|
||||
// title: string;
|
||||
// Widget: Type<Widget<Env>>;
|
||||
// default?: boolean;
|
||||
// }
|
||||
|
||||
// const actions: Action[] = [
|
||||
// { id: 1, title: "Discuss", Widget: Discuss, default: true },
|
||||
// { id: 2, title: "CRM", Widget: CRM }
|
||||
// ];
|
||||
|
||||
|
||||
|
||||
export default class ActionManager {
|
||||
doAction(action: Action) {
|
||||
console.log(action);
|
||||
// load data (??)
|
||||
// trigger action somewhere
|
||||
// upload url with router
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import CRM from "../widgets/crm";
|
||||
import Discuss from "../widgets/discuss";
|
||||
import Widget from "../core/widget";
|
||||
import { Env, Type } from "../types";
|
||||
|
||||
import { Env } from "../env";
|
||||
|
||||
interface Type<T> extends Function {
|
||||
new (...args: any[]): T;
|
||||
}
|
||||
|
||||
export interface Action {
|
||||
id: number;
|
||||
@@ -19,3 +21,9 @@ const actions: Action[] = [
|
||||
|
||||
export default actions;
|
||||
|
||||
// class ActionManager {
|
||||
|
||||
// doAction(action: Action) {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
|
||||
export default class Ajax{}
|
||||
@@ -1,39 +1,46 @@
|
||||
export type Route = string;
|
||||
export type Query = { [key: string]: string };
|
||||
|
||||
export interface RouteInfo {
|
||||
route: Route;
|
||||
export interface Route {
|
||||
// this is the part before the hash: www.something.com/web#action=1 => web
|
||||
path: string;
|
||||
query: Query;
|
||||
title: string;
|
||||
}
|
||||
|
||||
function clearSlaches(s: string): string {
|
||||
return s.replace(/\/$/, "").replace(/^\//, "");
|
||||
}
|
||||
|
||||
export default class Router {
|
||||
listeners: { owner: any; callback: (info: RouteInfo) => void }[] = [];
|
||||
listeners: { owner: any; callback: (info: Route) => void }[] = [];
|
||||
|
||||
constructor() {
|
||||
window.addEventListener("popstate", this.onUrlChange.bind(this));
|
||||
window.onhashchange = function () { console.log('aaaa'); debugger; }
|
||||
}
|
||||
|
||||
onUrlChange() {
|
||||
const info = this.getRouteInfo();
|
||||
for (let listener of this.listeners) {
|
||||
listener.callback.call(listener.owner, info);
|
||||
}
|
||||
onUrlChange(event: PopStateEvent) {
|
||||
debugger
|
||||
event.preventDefault();
|
||||
const info = this.getRoute();
|
||||
for (let listener of this.listeners) {
|
||||
listener.callback.call(listener.owner, info);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Route} route relative route: for example, /web/
|
||||
* @param {Query} query
|
||||
*/
|
||||
navigate(info: Partial<RouteInfo>) {
|
||||
const currentRouteInfo = this.getRouteInfo();
|
||||
const route = info.route || currentRouteInfo.route;
|
||||
navigate(info: Partial<Route>) {
|
||||
const currentRoute = this.getRoute();
|
||||
const route = info.path || currentRoute.path;
|
||||
const query = info.query || {};
|
||||
const title = info.title || currentRouteInfo.title;
|
||||
const title = info.title || currentRoute.title;
|
||||
const url = this.formatURL(route, query);
|
||||
window.history.pushState(null, title, url);
|
||||
}
|
||||
register(owner: any, callback: (info: RouteInfo) => void) {
|
||||
register(owner: any, callback: (info: Route) => void) {
|
||||
this.listeners.push({ owner, callback });
|
||||
}
|
||||
|
||||
@@ -41,24 +48,24 @@ export default class Router {
|
||||
this.listeners = this.listeners.filter(l => l.owner !== owner);
|
||||
}
|
||||
|
||||
formatURL(route: Route, query: Query): string {
|
||||
let url = route;
|
||||
formatURL(path: string, query: Query): string {
|
||||
let url = clearSlaches(path);
|
||||
let hasHash = false;
|
||||
for (let key in query) {
|
||||
url = url + (hasHash ? "&" : "#");
|
||||
url = url + `${key}=${query[key]}`;
|
||||
hasHash = true;
|
||||
}
|
||||
return url;
|
||||
return "/" + url;
|
||||
}
|
||||
|
||||
getRouteInfo(): RouteInfo {
|
||||
const route = window.location.pathname.slice(1);
|
||||
getRoute(): Route {
|
||||
const path = clearSlaches(window.location.pathname);
|
||||
const query = {};
|
||||
for (let part of window.location.hash.slice(1).split("?")) {
|
||||
let [key, value] = part.split("=");
|
||||
query[key] = value;
|
||||
}
|
||||
return { route, query, title: document.title };
|
||||
return { path, query, title: document.title };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user