refactoring, work on environment

This commit is contained in:
Géry Debongnie
2019-01-23 14:28:41 +01:00
parent 08938d2334
commit 76fedae5a9
18 changed files with 138 additions and 63 deletions
@@ -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
}
}
+10 -2
View File
@@ -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) {
// }
// }
+2
View File
@@ -0,0 +1,2 @@
export default class Ajax{}
+27 -20
View File
@@ -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 };
}
}