aprog 2.2.94 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aprog",
3
- "version": "2.2.94",
3
+ "version": "2.3.0",
4
4
  "description": "Допоміжна JavaScript-бібліотека для проєктів",
5
5
  "main": "index.js",
6
6
  "exports": {
package/src/NotFound.js CHANGED
@@ -1,11 +1,22 @@
1
1
  import {div} from "./general.js";
2
2
 
3
- const NotFound = ({ message = "Page Not Found" }) => {
3
+ const NotFound = ({ colors = {
4
+ numberColor: "#ff2800",
5
+ textColor: "#c3e4fc",
6
+ backgroundColor: "#043458",
7
+ }, message = "Page Not Found" }) => {
4
8
  return div({
5
- style: style,
9
+ style: {
10
+ ...style,
11
+ color: colors.textColor,
12
+ backgroundColor: colors.backgroundColor
13
+ },
6
14
  value: [
7
15
  div({
8
- style: style2,
16
+ style: {
17
+ ...style2,
18
+ color: colors.numberColor
19
+ },
9
20
  value: "404"
10
21
  }),
11
22
  div({value: message})
@@ -20,15 +31,12 @@ const style = {
20
31
  flexDirection: "column",
21
32
  alignItems: "center",
22
33
  justifyContent: "center",
23
- fontSize: "1.875rem",
24
- color: "#c3e4fc",
25
- backgroundColor: "#043458"
34
+ fontSize: "1.875rem"
26
35
  };
27
36
 
28
37
  const style2 = {
29
38
  fontSize: "3rem",
30
- fontWeight: "bold",
31
- color: "#ff2800"
39
+ fontWeight: "bold"
32
40
  }
33
41
 
34
42
  export default NotFound;
package/src/router.js CHANGED
@@ -1,13 +1,24 @@
1
1
  import NotFound from "./NotFound.js";
2
2
 
3
- export const router = async ({ routes, main }) => {
3
+ /**
4
+ * Ініціалізація роутера.
5
+ */
6
+ export const router = async ({routes, main, message = "", props = {}}) => {
4
7
  const renderRoute = async () => {
5
- const { routeComponent, props } = await useRoutes(routes);
8
+ const {routeComponent, props: routeProps} = useRoutes(routes);
6
9
  main.innerHTML = "";
7
- if (routeComponent) {
8
- const component = await routeComponent(props);
9
- main.appendChild(component);
10
+
11
+ if (!routeComponent) {
12
+ return;
10
13
  }
14
+
15
+ const component = await routeComponent({
16
+ ...props,
17
+ ...routeProps,
18
+ message,
19
+ });
20
+
21
+ main.appendChild(component);
11
22
  };
12
23
 
13
24
  window.addEventListener('popstate', renderRoute);
@@ -16,21 +27,84 @@ export const router = async ({ routes, main }) => {
16
27
  await renderRoute();
17
28
  };
18
29
 
30
+ /**
31
+ * Перехід без рендеру.
32
+ */
19
33
  export const navigateTo = (path, props = {}) => {
20
34
  window.history.pushState(props, "", path);
21
35
  };
22
36
 
23
- export const navi = (path, props = {}) => {
37
+ /**
38
+ * Перехід + рендер.
39
+ */
40
+ export const navi = async (path, props = {}) => {
24
41
  window.history.pushState(props, "", path);
25
- window.renderRoute().then();
42
+
43
+ await window.renderRoute();
26
44
  };
27
45
 
28
- const useRoutes = async (routes) => {
46
+ /**
47
+ * Порівнює URL з шаблоном route
48
+ * і повертає параметри.
49
+ *
50
+ * /category/{id}
51
+ * /category/15
52
+ *
53
+ * => { id: "15" }
54
+ *
55
+ * /category/{category_id}/product/{product_id}
56
+ * /category/15/product/128
57
+ *
58
+ * => {
59
+ * category_id: "15",
60
+ * product_id: "128"
61
+ * }
62
+ */
63
+ const matchRoute = (routePath, path) => {
64
+ const keys = [];
65
+
66
+ const pattern = routePath.replace(/\{([^}]+)}/g, (_, key) => {
67
+ keys.push(key);
68
+
69
+ return '([^/]+)';
70
+ });
71
+
72
+ const match = path.match(new RegExp(`^${pattern}$`));
73
+
74
+ if (!match) {
75
+ return null;
76
+ }
77
+
78
+ return keys.reduce((params, key, index) => {
79
+ params[key] = decodeURIComponent(match[index + 1]);
80
+
81
+ return params;
82
+ }, {});
83
+ };
84
+
85
+ /**
86
+ * Знаходить поточний route.
87
+ */
88
+ const useRoutes = (routes) => {
29
89
  const path = window.location.pathname;
30
- const matchingRoute = routes.find(route => route.path === path);
31
- const props = window.history.state || {};
32
90
 
33
- return matchingRoute
34
- ? { routeComponent: matchingRoute.component, props }
35
- : { routeComponent: NotFound, props };
91
+ for (const route of routes) {
92
+ const params = matchRoute(route.path, path);
93
+ if (params !== null) {
94
+ return {
95
+ routeComponent: route.component,
96
+ props: {
97
+ ...(window.history.state || {}),
98
+ ...params,
99
+ },
100
+ };
101
+ }
102
+ }
103
+
104
+ return {
105
+ routeComponent: NotFound,
106
+ props: {
107
+ ...(window.history.state || {}),
108
+ },
109
+ };
36
110
  };
package/src/svg.js CHANGED
@@ -232,7 +232,8 @@ export const svg = ({
232
232
  stroke="${stroke}"
233
233
  stroke-width="${strokeWidth}"
234
234
  viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
235
- <path d="M2 6C2 4.11438 2 3.17157 2.58579 2.58579C3.17157 2 4.11438 2 6 2C7.88562 2 8.82843 2 9.41421 2.58579C10 3.17157 10 4.11438 10 6V18C10 19.8856 10 20.8284 9.41421 21.4142C8.82843 22 7.88562 22 6 22C4.11438 22 3.17157 22 2.58579 21.4142C2 20.8284 2 19.8856 2 18V6Z" fill="#1C274C"></path> <path d="M14 6C14 4.11438 14 3.17157 14.5858 2.58579C15.1716 2 16.1144 2 18 2C19.8856 2 20.8284 2 21.4142 2.58579C22 3.17157 22 4.11438 22 6V18C22 19.8856 22 20.8284 21.4142 21.4142C20.8284 22 19.8856 22 18 22C16.1144 22 15.1716 22 14.5858 21.4142C14 20.8284 14 19.8856 14 18V6Z"></path>
235
+ <path d="M2 6C2 4.11438 2 3.17157 2.58579 2.58579C3.17157 2 4.11438 2 6 2C7.88562 2 8.82843 2 9.41421 2.58579C10 3.17157 10 4.11438 10 6V18C10 19.8856 10 20.8284 9.41421 21.4142C8.82843 22 7.88562 22 6 22C4.11438 22 3.17157 22 2.58579 21.4142C2 20.8284 2 19.8856 2 18V6Z"></path>
236
+ <path d="M14 6C14 4.11438 14 3.17157 14.5858 2.58579C15.1716 2 16.1144 2 18 2C19.8856 2 20.8284 2 21.4142 2.58579C22 3.17157 22 4.11438 22 6V18C22 19.8856 22 20.8284 21.4142 21.4142C20.8284 22 19.8856 22 18 22C16.1144 22 15.1716 22 14.5858 21.4142C14 20.8284 14 19.8856 14 18V6Z"></path>
236
237
  </svg>
237
238
  `
238
239
  }