saasufy-components 1.1.0 → 1.1.1
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/{static-router.js → app-router.js} +4 -9
- package/collection-browser.js +1 -1
- package/package.json +1 -1
- package/utils.js +3 -3
- package/collection-router.js +0 -156
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getSafeHTML } from './utils.js';
|
|
2
|
+
import { renderTemplate } from './utils.js';
|
|
2
3
|
|
|
3
|
-
export class
|
|
4
|
+
export class AppRouter extends HTMLElement {
|
|
4
5
|
constructor() {
|
|
5
6
|
super();
|
|
6
7
|
this.attachShadow({ mode: 'open' });
|
|
@@ -74,13 +75,7 @@ export class StaticRouter extends HTMLElement {
|
|
|
74
75
|
routeArgs = {};
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
|
|
78
|
-
for (let [ key, value ] of Object.entries(routeArgs)) {
|
|
79
|
-
let regExp = new RegExp(`{{${key}}}`, 'g');
|
|
80
|
-
templateHTML = templateHTML.replace(regExp, getSafeHTML(value));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
routerViewport.innerHTML = templateHTML;
|
|
78
|
+
routerViewport.innerHTML = renderTemplate(pageTemplate.innerHTML, routeArgs);
|
|
84
79
|
}
|
|
85
80
|
|
|
86
81
|
render() {
|
|
@@ -91,4 +86,4 @@ export class StaticRouter extends HTMLElement {
|
|
|
91
86
|
}
|
|
92
87
|
}
|
|
93
88
|
|
|
94
|
-
window.customElements.define('
|
|
89
|
+
window.customElements.define('app-router', AppRouter);
|
package/collection-browser.js
CHANGED
|
@@ -155,7 +155,7 @@ class CollectionBrowser extends SocketConsumer {
|
|
|
155
155
|
let items = [];
|
|
156
156
|
|
|
157
157
|
for (let modelItem of this.collection.value) {
|
|
158
|
-
let itemString = renderTemplate(itemTemplate.innerHTML, this.collection.type
|
|
158
|
+
let itemString = renderTemplate(itemTemplate.innerHTML, modelItem, this.collection.type);
|
|
159
159
|
items.push(itemString);
|
|
160
160
|
}
|
|
161
161
|
let noItemTemplate = this.shadowRoot.querySelector('slot[name="no-item"]').assignedNodes()[0];
|
package/package.json
CHANGED
package/utils.js
CHANGED
|
@@ -165,14 +165,14 @@ let templateFormatters = {
|
|
|
165
165
|
}
|
|
166
166
|
};
|
|
167
167
|
|
|
168
|
-
export function renderTemplate(templateString,
|
|
168
|
+
export function renderTemplate(templateString, data, dataType) {
|
|
169
169
|
for (let [ field, value ] of Object.entries(data)) {
|
|
170
170
|
let safeValue = getSafeHTML(value);
|
|
171
171
|
for (let [ formatName, formatFunction ] of Object.entries(templateFormatters)) {
|
|
172
|
-
let formatRegExp = new RegExp(`{{${formatName}:${dataType}
|
|
172
|
+
let formatRegExp = new RegExp(`{{${formatName}:${dataType ? dataType + '.' : ''}${field}}}`, 'g');
|
|
173
173
|
templateString = templateString.replace(formatRegExp, formatFunction(safeValue));
|
|
174
174
|
}
|
|
175
|
-
let regExp = new RegExp(`{{${dataType}
|
|
175
|
+
let regExp = new RegExp(`{{${dataType ? dataType + '.' : ''}${field}}}`, 'g');
|
|
176
176
|
templateString = templateString.replace(regExp, safeValue);
|
|
177
177
|
}
|
|
178
178
|
return templateString;
|
package/collection-router.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { SocketConsumer } from './socket.js';
|
|
2
|
-
import AGCollection from '/node_modules/ag-collection/ag-collection.js';
|
|
3
|
-
import { renderTemplate } from './utils.js';
|
|
4
|
-
import './static-router.js';
|
|
5
|
-
|
|
6
|
-
const DEFAULT_RELOAD_DELAY = 0;
|
|
7
|
-
|
|
8
|
-
export class CollectionRouter extends SocketConsumer {
|
|
9
|
-
constructor() {
|
|
10
|
-
super();
|
|
11
|
-
this.attachShadow({ mode: 'open' });
|
|
12
|
-
this.pages = {};
|
|
13
|
-
|
|
14
|
-
this.handleSlotChangeEvent = () => {
|
|
15
|
-
let pageTemplates = this.shadowRoot.querySelector('slot[name="page"]').assignedNodes();
|
|
16
|
-
|
|
17
|
-
for (let template of pageTemplates) {
|
|
18
|
-
let routePath = template.getAttribute('route-path');
|
|
19
|
-
this.pages[routePath] = template;
|
|
20
|
-
}
|
|
21
|
-
if (this.collection && this.collection.isLoaded) {
|
|
22
|
-
this.renderRoutes();
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
connectedCallback() {
|
|
28
|
-
this.isReady = true;
|
|
29
|
-
this.socket = this.getSocket();
|
|
30
|
-
this.shadowRoot.addEventListener('slotchange', this.handleSlotChangeEvent);
|
|
31
|
-
this.render();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
disconnectedCallback() {
|
|
35
|
-
this.shadowRoot.removeEventListener('slotchange', this.handleSlotChangeEvent);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
static get observedAttributes() {
|
|
39
|
-
return [
|
|
40
|
-
'collection-type',
|
|
41
|
-
'collection-fields',
|
|
42
|
-
'collection-view',
|
|
43
|
-
'collection-view-params',
|
|
44
|
-
'collection-page-size',
|
|
45
|
-
'collection-page-offset',
|
|
46
|
-
'hide-route-logs',
|
|
47
|
-
'hide-error-logs'
|
|
48
|
-
];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
attributeChangedCallback(name, oldValue, newValue) {
|
|
52
|
-
if (!this.isReady) return;
|
|
53
|
-
if (this.collection && name === 'collection-page-offset') {
|
|
54
|
-
let newOffset = Number(newValue);
|
|
55
|
-
if (newOffset !== this.collection.meta.pageOffset) {
|
|
56
|
-
this.collection.fetchPage(newOffset);
|
|
57
|
-
}
|
|
58
|
-
} else {
|
|
59
|
-
this.render();
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
renderRoutes() {
|
|
64
|
-
let hideRouteLogs = this.hasAttribute('hide-route-logs');
|
|
65
|
-
let viewport = this.shadowRoot.querySelector('slot[name="viewport"]').assignedNodes()[0];
|
|
66
|
-
if (!viewport) return;
|
|
67
|
-
|
|
68
|
-
let pageEntries = Object.entries(this.pages);
|
|
69
|
-
if (!pageEntries.length) return;
|
|
70
|
-
|
|
71
|
-
let routesList = [];
|
|
72
|
-
|
|
73
|
-
viewport.innerHTML = `
|
|
74
|
-
<static-router>
|
|
75
|
-
${
|
|
76
|
-
pageEntries.map(
|
|
77
|
-
([ route, template ]) => {
|
|
78
|
-
let routePath = template.getAttribute('route-path');
|
|
79
|
-
let pageTemplates = this.collection.value.map(
|
|
80
|
-
(modelItem) => {
|
|
81
|
-
let pagePath = renderTemplate(routePath, this.collection.type, modelItem);
|
|
82
|
-
let templateHTML = renderTemplate(template.innerHTML, this.collection.type, modelItem);
|
|
83
|
-
routesList.push(pagePath);
|
|
84
|
-
return `<template slot="page" route-path="${pagePath}">${templateHTML}</template>`
|
|
85
|
-
}
|
|
86
|
-
);
|
|
87
|
-
return pageTemplates.join('');
|
|
88
|
-
}
|
|
89
|
-
).join('')
|
|
90
|
-
}
|
|
91
|
-
<div slot="viewport"></div>
|
|
92
|
-
</static-router>
|
|
93
|
-
`;
|
|
94
|
-
|
|
95
|
-
if (!hideRouteLogs) console.log(`The following routes have been registered: ${routesList.join(', ')}`);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
render() {
|
|
99
|
-
let collectionType = this.getAttribute('collection-type');
|
|
100
|
-
let collectionFields = this.getAttribute('collection-fields');
|
|
101
|
-
let collectionView = this.getAttribute('collection-view');
|
|
102
|
-
let collectionPageSize = this.getAttribute('collection-page-size');
|
|
103
|
-
let collectionViewParams = this.getAttribute('collection-view-params');
|
|
104
|
-
let collectionPageOffset = this.getAttribute('collection-page-offset');
|
|
105
|
-
let hideErrorLogs = this.hasAttribute('hide-error-logs');
|
|
106
|
-
let collectionReloadDelay = Number(
|
|
107
|
-
this.getAttribute('collection-reload-delay') || DEFAULT_RELOAD_DELAY
|
|
108
|
-
);
|
|
109
|
-
let viewParams = Object.fromEntries(collectionViewParams.split(',').map((pair) => pair.split('=')));
|
|
110
|
-
if (this.collection) this.collection.destroy();
|
|
111
|
-
|
|
112
|
-
this.collection = new AGCollection({
|
|
113
|
-
socket: this.socket,
|
|
114
|
-
type: collectionType,
|
|
115
|
-
fields: collectionFields.split(',').map(field => field.trim()),
|
|
116
|
-
view: collectionView,
|
|
117
|
-
viewParams,
|
|
118
|
-
pageSize: Number(collectionPageSize || 10),
|
|
119
|
-
pageOffset: Number(collectionPageOffset || 0),
|
|
120
|
-
changeReloadDelay: collectionReloadDelay
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
this.shadowRoot.innerHTML = `
|
|
124
|
-
<slot name="page"></slot>
|
|
125
|
-
<slot name="viewport"></slot>
|
|
126
|
-
`;
|
|
127
|
-
|
|
128
|
-
(async () => {
|
|
129
|
-
await this.collection.listener('load').once();
|
|
130
|
-
if (!this.collection.value.length) {
|
|
131
|
-
this.renderRoutes();
|
|
132
|
-
}
|
|
133
|
-
this.setAttribute('is-loaded', '');
|
|
134
|
-
this.dispatchEvent(new CustomEvent('load', { bubbles: true }));
|
|
135
|
-
})();
|
|
136
|
-
|
|
137
|
-
(async () => {
|
|
138
|
-
for await (let event of this.collection.listener('change')) {
|
|
139
|
-
if (!this.collection.isLoaded) continue;
|
|
140
|
-
this.renderRoutes();
|
|
141
|
-
}
|
|
142
|
-
})();
|
|
143
|
-
|
|
144
|
-
if (!hideErrorLogs) {
|
|
145
|
-
(async () => {
|
|
146
|
-
for await (let { error } of this.collection.listener('error')) {
|
|
147
|
-
console.error(
|
|
148
|
-
`Collection router encountered an error: ${error.message}`
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
})();
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
window.customElements.define('collection-router', CollectionRouter);
|