mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
move some files around
This commit is contained in:
@@ -3,7 +3,7 @@ import { Widget } from "../core/widget";
|
||||
import { Env } from "../env";
|
||||
import { Registry } from "./registry";
|
||||
import { Type } from "../types";
|
||||
import { IRouter, Query } from "./router";
|
||||
import { IRouter, Query } from "../core/router";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Types
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Types
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export interface RPCQuery {
|
||||
model: string;
|
||||
method: string;
|
||||
args: any;
|
||||
}
|
||||
export interface IAjax {
|
||||
rpc(rpc: RPCQuery): Promise<any>;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Ajax
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export class Ajax implements IAjax {
|
||||
rpc(rpc: RPCQuery): Promise<any> {
|
||||
return Promise.resolve(1);
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import { EventBus as Bus } from "../core/event_bus";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Types
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export interface INotification {
|
||||
id: number;
|
||||
title: string;
|
||||
message: string;
|
||||
type: "notification" | "warning";
|
||||
sticky: boolean;
|
||||
}
|
||||
|
||||
export type NotificationEvent = "notifications_updated";
|
||||
|
||||
export type Callback = (notifs: INotification[]) => void;
|
||||
|
||||
export interface INotificationManager {
|
||||
add(notif: Partial<INotification>): number;
|
||||
close(id: number): void;
|
||||
on(event: NotificationEvent, owner: any, callback: Callback): void;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Notification Manager
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export class NotificationManager extends Bus implements INotificationManager {
|
||||
nextID = 1;
|
||||
notifications: INotification[] = [];
|
||||
|
||||
add(notif: Partial<INotification>): number {
|
||||
const id = this.nextID++;
|
||||
const defaultVals = {
|
||||
title: "",
|
||||
message: "",
|
||||
type: "notification",
|
||||
sticky: false
|
||||
};
|
||||
const notification = Object.assign(defaultVals, notif, { id });
|
||||
this.notifications.push(notification);
|
||||
this.trigger("notifications_updated", this.notifications);
|
||||
if (!notification.sticky) {
|
||||
setTimeout(() => this.close(id), 2500);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
close(id: number) {
|
||||
this.notifications = this.notifications.filter(n => n.id !== id);
|
||||
this.trigger("notifications_updated", this.notifications);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
import { EventBus, Callback } from "../core/event_bus";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Types and helpers
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export type Query = { [key: string]: string };
|
||||
|
||||
function clearSlashes(s: string): string {
|
||||
return s.replace(/\/$/, "").replace(/^\//, "");
|
||||
}
|
||||
|
||||
export type RouterEvent = "query_changed";
|
||||
|
||||
export interface IRouter {
|
||||
navigate(query: Query): void;
|
||||
on(event: RouterEvent, owner: any, callback: Callback): void;
|
||||
getQuery(): Query;
|
||||
formatURL(path: string, query: Query): string;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Router
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
export class Router extends EventBus implements IRouter {
|
||||
currentQuery: Query;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.currentQuery = this.getQuery();
|
||||
window.onhashchange = () => {
|
||||
this.currentQuery = this.getQuery();
|
||||
this.trigger("query_changed", this.currentQuery);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Query} query
|
||||
*/
|
||||
navigate(query: Query) {
|
||||
const url = this.formatURL("", query);
|
||||
window.history.replaceState(null, document.title, url);
|
||||
}
|
||||
|
||||
formatQuery(query: Query): string {
|
||||
let parts: string[] = [];
|
||||
for (let key in query) {
|
||||
parts.push(`${key}=${query[key]}`);
|
||||
}
|
||||
return parts.join("&");
|
||||
}
|
||||
|
||||
formatURL(path: string, query: Query): string {
|
||||
let url = clearSlashes(path);
|
||||
return `/${url}#${this.formatQuery(query)}`;
|
||||
}
|
||||
|
||||
getQuery(): Query {
|
||||
const query = {};
|
||||
for (let part of window.location.hash.slice(1).split("?")) {
|
||||
let [key, value] = part.split("=");
|
||||
query[key] = value;
|
||||
}
|
||||
return query;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user