saasufy-components 1.8.0 → 1.10.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/app-router.js +95 -9
- package/collection-browser.js +2 -1
- package/collection-reducer.js +2 -1
- package/model-viewer.js +2 -1
- package/package.json +1 -2
- package/utils.js +31 -4
package/app-router.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
import { toSafeHTML } from './utils.js';
|
|
2
|
-
import {
|
|
1
|
+
import { toSafeHTML, renderTemplate, debouncer } from './utils.js';
|
|
2
|
+
import { SocketConsumer } from './socket.js';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const DEFAULT_DEBOUNCE_DELAY = 100;
|
|
5
|
+
|
|
6
|
+
export class AppRouter extends SocketConsumer {
|
|
5
7
|
constructor() {
|
|
6
8
|
super();
|
|
7
9
|
this.attachShadow({ mode: 'open' });
|
|
8
10
|
|
|
9
11
|
this.pages = {};
|
|
10
12
|
this.pageRouteInfos = [];
|
|
13
|
+
this.debounce = debouncer();
|
|
14
|
+
this.debounceDelay = DEFAULT_DEBOUNCE_DELAY;
|
|
11
15
|
|
|
12
16
|
this.hashStartRegex = /^#/;
|
|
13
17
|
|
|
14
18
|
this.handleHashChangeEvent = () => {
|
|
15
|
-
this.renderCurrentPage
|
|
19
|
+
this.debounce(this.renderCurrentPage, this.debounceDelay);
|
|
16
20
|
};
|
|
17
21
|
|
|
18
|
-
this.handleSlotChangeEvent = () => {
|
|
22
|
+
this.handleSlotChangeEvent = (event) => {
|
|
19
23
|
let pageTemplates = this.shadowRoot.querySelector('slot[name="page"]').assignedNodes();
|
|
20
24
|
|
|
21
25
|
for (let template of pageTemplates) {
|
|
@@ -30,8 +34,9 @@ export class AppRouter extends HTMLElement {
|
|
|
30
34
|
params: [ ...route.matchAll(/:[^\/]*/g) ].map(paramMatch => paramMatch[0].replace(':', ''))
|
|
31
35
|
};
|
|
32
36
|
});
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
if (!this.socket || this.socket.state === this.socket.OPEN) {
|
|
38
|
+
this.debounce(this.renderCurrentPage, this.debounceDelay);
|
|
39
|
+
}
|
|
35
40
|
};
|
|
36
41
|
}
|
|
37
42
|
|
|
@@ -43,15 +48,61 @@ export class AppRouter extends HTMLElement {
|
|
|
43
48
|
return Object.fromEntries(argEntries);
|
|
44
49
|
}
|
|
45
50
|
|
|
51
|
+
static get observedAttributes() {
|
|
52
|
+
return [
|
|
53
|
+
'default-page',
|
|
54
|
+
'debounce-delay'
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
59
|
+
if (name === 'debounce-delay') {
|
|
60
|
+
if (newValue) {
|
|
61
|
+
this.debounceDelay = Number(newValue);
|
|
62
|
+
} else {
|
|
63
|
+
this.debounceDelay = DEFAULT_DEBOUNCE_DELAY;
|
|
64
|
+
}
|
|
65
|
+
} else if (this.isReady) {
|
|
66
|
+
this.renderCurrentPage();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
46
70
|
connectedCallback() {
|
|
47
71
|
window.addEventListener('hashchange', this.handleHashChangeEvent);
|
|
48
72
|
this.shadowRoot.addEventListener('slotchange', this.handleSlotChangeEvent);
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
// Socket is optional as not all routing use cases require authentication.
|
|
76
|
+
this.socket = this.getSocket();
|
|
77
|
+
} catch (error) {}
|
|
78
|
+
if (this.socket) {
|
|
79
|
+
let hasProcessedSocketConnect = this.socket.state === this.socket.OPEN;
|
|
80
|
+
this.authStateChangeConsumer = this.socket.listener('authStateChange').createConsumer();
|
|
81
|
+
(async () => {
|
|
82
|
+
for await (let event of this.authStateChangeConsumer) {
|
|
83
|
+
if (hasProcessedSocketConnect) {
|
|
84
|
+
this.debounce(this.renderCurrentPage, this.debounceDelay);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
})();
|
|
88
|
+
|
|
89
|
+
this.connectConsumer = this.socket.listener('connect').createConsumer();
|
|
90
|
+
(async () => {
|
|
91
|
+
for await (let event of this.connectConsumer) {
|
|
92
|
+
hasProcessedSocketConnect = true;
|
|
93
|
+
this.debounce(this.renderCurrentPage, this.debounceDelay);
|
|
94
|
+
}
|
|
95
|
+
})();
|
|
96
|
+
}
|
|
49
97
|
this.render();
|
|
98
|
+
this.isReady = true;
|
|
50
99
|
}
|
|
51
100
|
|
|
52
101
|
disconnectedCallback() {
|
|
53
102
|
window.removeEventListener('hashchange', this.handleHashChangeEvent);
|
|
54
103
|
this.shadowRoot.removeEventListener('slotchange', this.handleSlotChangeEvent);
|
|
104
|
+
this.authStateChangeConsumer.kill();
|
|
105
|
+
this.connectConsumer.kill();
|
|
55
106
|
}
|
|
56
107
|
|
|
57
108
|
renderCurrentPage() {
|
|
@@ -66,7 +117,38 @@ export class AppRouter extends HTMLElement {
|
|
|
66
117
|
) || {};
|
|
67
118
|
|
|
68
119
|
let pageTemplate = this.pages[route];
|
|
69
|
-
if (!pageTemplate)
|
|
120
|
+
if (!pageTemplate) {
|
|
121
|
+
let defaultPage = this.getAttribute('default-page');
|
|
122
|
+
if (!defaultPage) return;
|
|
123
|
+
pageTemplate = this.pages[defaultPage];
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
let noAuthRedirect = pageTemplate.getAttribute('no-auth-redirect');
|
|
127
|
+
let authRedirect = pageTemplate.getAttribute('auth-redirect');
|
|
128
|
+
let hardRedirect = pageTemplate.hasAttribute('hard-redirect');
|
|
129
|
+
|
|
130
|
+
if (this.socket && (authRedirect || noAuthRedirect)) {
|
|
131
|
+
if (this.socket.authState === 'authenticated') {
|
|
132
|
+
if (authRedirect) {
|
|
133
|
+
if (hardRedirect) {
|
|
134
|
+
location.hash = authRedirect;
|
|
135
|
+
return;
|
|
136
|
+
} else {
|
|
137
|
+
pageTemplate = this.pages[authRedirect];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
if (this.socket.state !== this.socket.OPEN) return;
|
|
142
|
+
if (noAuthRedirect) {
|
|
143
|
+
if (hardRedirect) {
|
|
144
|
+
location.hash = noAuthRedirect;
|
|
145
|
+
return;
|
|
146
|
+
} else {
|
|
147
|
+
pageTemplate = this.pages[noAuthRedirect];
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
70
152
|
|
|
71
153
|
let routeArgs;
|
|
72
154
|
if (route) {
|
|
@@ -75,7 +157,11 @@ export class AppRouter extends HTMLElement {
|
|
|
75
157
|
routeArgs = {};
|
|
76
158
|
}
|
|
77
159
|
|
|
78
|
-
routerViewport.innerHTML = renderTemplate(
|
|
160
|
+
routerViewport.innerHTML = renderTemplate(
|
|
161
|
+
pageTemplate.innerHTML,
|
|
162
|
+
routeArgs,
|
|
163
|
+
this.socket
|
|
164
|
+
);
|
|
79
165
|
}
|
|
80
166
|
|
|
81
167
|
render() {
|
package/collection-browser.js
CHANGED
|
@@ -171,7 +171,8 @@ class CollectionBrowser extends SocketConsumer {
|
|
|
171
171
|
for (let modelItem of this.collection.value) {
|
|
172
172
|
let itemString = renderTemplate(
|
|
173
173
|
itemTemplate.innerHTML,
|
|
174
|
-
{ [type]: modelItem }
|
|
174
|
+
{ [type]: modelItem },
|
|
175
|
+
this.socket
|
|
175
176
|
);
|
|
176
177
|
items.push(itemString);
|
|
177
178
|
}
|
package/collection-reducer.js
CHANGED
|
@@ -139,7 +139,8 @@ class CollectionReducer extends SocketConsumer {
|
|
|
139
139
|
let type = this.getAttribute('type-alias') || this.collection.type;
|
|
140
140
|
let itemString = renderTemplate(
|
|
141
141
|
itemTemplate.innerHTML,
|
|
142
|
-
{ [type]: this.collection.value }
|
|
142
|
+
{ [type]: this.collection.value },
|
|
143
|
+
this.socket
|
|
143
144
|
);
|
|
144
145
|
viewportNode.innerHTML = itemString;
|
|
145
146
|
}
|
package/model-viewer.js
CHANGED
|
@@ -59,7 +59,8 @@ class ModelViewer extends SocketConsumer {
|
|
|
59
59
|
let type = this.getAttribute('type-alias') || this.model.type;
|
|
60
60
|
let itemString = renderTemplate(
|
|
61
61
|
itemTemplate.innerHTML,
|
|
62
|
-
{ [type]: modelValue }
|
|
62
|
+
{ [type]: modelValue },
|
|
63
|
+
this.socket
|
|
63
64
|
);
|
|
64
65
|
viewportNode.innerHTML = itemString;
|
|
65
66
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "saasufy-components",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "Components for Saasufy.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
"ag-collection": "^3.3.0",
|
|
21
21
|
"ag-model": "^1.4.1",
|
|
22
22
|
"capitalisk-auth-client": "^1.5.1",
|
|
23
|
-
"expr-eval": "^2.0.2",
|
|
24
23
|
"socketcluster-client": "^19.1.0"
|
|
25
24
|
},
|
|
26
25
|
"bugs": {
|
package/utils.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import AGCollection from '/node_modules/ag-collection/ag-collection.js';
|
|
2
2
|
import AGModel from '/node_modules/ag-model/ag-model.js';
|
|
3
|
-
import { Parser } from '/node_modules/expr-eval/dist/index.mjs';
|
|
4
3
|
|
|
5
4
|
const DEFAULT_DEBOUNCE_DELAY = 300;
|
|
6
5
|
const DEFAULT_RELOAD_DELAY = 0;
|
|
@@ -11,7 +10,8 @@ export function toSafeHTML(text) {
|
|
|
11
10
|
.replace(/</g, '<')
|
|
12
11
|
.replace(/>/g, '>')
|
|
13
12
|
.replace(/"/g, '"')
|
|
14
|
-
.replace(/'/g, ''')
|
|
13
|
+
.replace(/'/g, ''')
|
|
14
|
+
.replace(/\n/g, '<br />');
|
|
15
15
|
} else if (text != null && typeof text.toString !== 'function') {
|
|
16
16
|
return '[invalid]';
|
|
17
17
|
}
|
|
@@ -170,21 +170,48 @@ let templateFormatters = {
|
|
|
170
170
|
},
|
|
171
171
|
joinFields: (list, field, sep) => {
|
|
172
172
|
return list.map(item => item[field]).join(sep);
|
|
173
|
+
},
|
|
174
|
+
date: (timestamp) => {
|
|
175
|
+
let date = new Date(timestamp);
|
|
176
|
+
let year = date.getFullYear();
|
|
177
|
+
let month = date.toLocaleString('default', { month: 'long' });
|
|
178
|
+
let day = date.getDate();
|
|
179
|
+
let hour = date.getHours();
|
|
180
|
+
let minutes = date.getMinutes();
|
|
181
|
+
return `${month} ${day}, ${year} at ${hour}:${minutes.toString().padStart(2, '0')}`;
|
|
173
182
|
}
|
|
174
183
|
};
|
|
175
184
|
|
|
176
185
|
let templateTagsRegExp = /{{[^}]+}}/g;
|
|
177
186
|
|
|
178
|
-
|
|
187
|
+
function execExpression(expression, options) {
|
|
188
|
+
let keys = Object.keys(options);
|
|
189
|
+
let args = [
|
|
190
|
+
...keys,
|
|
191
|
+
`return (function () {
|
|
192
|
+
return ${expression};
|
|
193
|
+
})();`
|
|
194
|
+
];
|
|
195
|
+
return (new Function(...args))(...keys.map(key => options[key]));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function renderTemplate(templateString, data, socket) {
|
|
179
199
|
return templateString.replace(templateTagsRegExp, (match) => {
|
|
180
200
|
let expString = match.slice(2, -2);
|
|
181
201
|
let options = {
|
|
182
202
|
...templateFormatters,
|
|
203
|
+
socket: socket ? {
|
|
204
|
+
state: socket.state,
|
|
205
|
+
pendingReconnect: socket.pendingReconnect,
|
|
206
|
+
connectAttempts: socket.connectAttempts,
|
|
207
|
+
authState: socket.authState,
|
|
208
|
+
authToken: socket.authToken
|
|
209
|
+
} : undefined,
|
|
183
210
|
...data
|
|
184
211
|
};
|
|
185
212
|
try {
|
|
186
213
|
return toSafeHTML(
|
|
187
|
-
|
|
214
|
+
execExpression(
|
|
188
215
|
expString,
|
|
189
216
|
options
|
|
190
217
|
)
|