Debian Codegraph @

addons/point_of_sale/static/src/app/services/pos_router_service.js

raw
1 import { registry } from "@web/core/registry";
2 import { Reactive } from "@web/core/utils/reactive";
3 import { browser } from "@web/core/browser/browser";
4 import { escapeRegExp } from "@web/core/utils/strings";
5 import { zip } from "@web/core/utils/arrays";
6
7 const parseParams = (matches, paramSpecs) =>
8 Object.fromEntries(
9 zip(matches, paramSpecs).map(([match, paramSpec]) => {
10 const { type, name } = paramSpec;
11 switch (type) {
12 case "int":
13 return [name, parseInt(match)];
14 case "string":
15 return [name, match];
16 default:
17 throw new Error(`Unknown type ${type}`);
18 }
19 })
20 );
21
22 export class PosRouter extends Reactive {
23 static serviceDependencies = [];
24
25 constructor(...args) {
26 super(...args);
27 this.setup(...args);
28 }
29
30 setup(env) {
31 this.path = window.location.pathname;
32 this.registeredRoutes = {};
33 this.popStateCallback = null;
34 this.state = {
35 params: {},
36 current: null,
37 previous: null,
38 };
39
40 window.addEventListener("popstate", (event) => {
41 this.path = window.location.pathname;
42 this.matchURL();
43 this.popStateCallback && this.popStateCallback(event);
44 });
45
46 this.initRegisteredRoutes();
47 this.matchURL();
48 }
49
50 get page() {
51 const page = registry.category("pos_pages").get(this.state.current);
52 const params = this.state.params;
53 return {
54 name: page.name,
55 component: page.component,
56 params,
57 };
58 }
59
60 initRegisteredRoutes() {
61 const pages = registry.category("pos_pages").getAll();
62 for (const { name, route } of pages) {
63 const paramStrings = route.match(/\{\w+:\w+\}/g);
64
65 if (!paramStrings) {
66 this.registeredRoutes[name] = {
67 route,
68 paramSpecs: [],
69 regex: new RegExp(`^${route}$`),
70 };
71 continue;
72 }
73
74 const paramSpecs = paramStrings.map((paramString) => {
75 const [, type, name] = paramString.match(/(\w+):(\w+)/);
76 return { type, name };
77 });
78
79 const regex = new RegExp(
80 `^${route
81 .split(/\{\w+:\w+\}/)
82 .map((part) => escapeRegExp(part))
83 .join("([^/]+)")}$`
84 );
85
86 this.registeredRoutes[name] = { route, regex, paramSpecs };
87 }
88 }
89
90 back() {
91 if (!this.historyPage.length) {
92 this.navigate("LoginScreen", {
93 configId: odoo.pos_config_id,
94 });
95 return;
96 }
97
98 history.back();
99 this.path = window.location.pathname;
100 this.state.previous = window.location.pathname;
101 }
102
103 close() {
104 window.location.href = `/pos/ui/${odoo.pos_config_id}`;
105 }
106
107 matchURL(props = {}) {
108 const path = this.path;
109
110 for (const [routeName, { regex, paramSpecs }] of Object.entries(this.registeredRoutes)) {
111 const match = path.match(regex);
112 if (match) {
113 const parsedParams = parseParams(match.slice(1), paramSpecs);
114 this.state.current = routeName;
115 this.state.params = { ...props, ...parsedParams };
116 return;
117 }
118 }
119
120 // In case no route matches, we default to the LoginScreen
121 this.state.current = "LoginScreen";
122 }
123
124 getRoute(routeName) {
125 try {
126 const { route } = this.registeredRoutes[routeName];
127 return route;
128 } catch {
129 const { route } = this.registeredRoutes["ProductScreen"];
130 return route;
131 }
132 }
133
134 navigate(routeName, routeParams = {}) {
135 const route = this.getRoute(routeName);
136 const url = new URL(browser.location.href);
137
138 url.pathname = route.replace(
139 /\{\w+:(\w+)\}/g,
140 (match, paramName) => routeParams[paramName]
141 );
142
143 history.pushState({}, "", url);
144 this.path = window.location.pathname;
145 this.historyPage = this.path;
146 this.matchURL(routeParams);
147 }
148
149 registerRoutes(routes) {
150 Object.assign(this.registeredRoutes, routes);
151 }
152 }
153
154 export const PosRouterService = {
155 dependencies: PosRouter.serviceDependencies,
156 async start(env, deps) {
157 return new PosRouter(env, deps);
158 },
159 };
160
161 registry.category("services").add("pos_router", PosRouterService);