saasufy-components 3.12.0 → 3.14.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/README.md +23 -1
- package/collection-browser.js +14 -4
- package/input-provider.js +27 -4
- package/model-input.js +8 -3
- package/package.json +3 -2
- package/sha256.js +113 -0
- package/utils.js +25 -1
package/README.md
CHANGED
|
@@ -95,6 +95,21 @@ This project does not impose any specific approach to styling components, howeve
|
|
|
95
95
|
<link href="https://saasufy.com/node_modules/saasufy-components/styles.css" rel="stylesheet" />
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
+
### Utility functions
|
|
99
|
+
|
|
100
|
+
The following utility functions can be used anywhere inside template `{{expression}}` placeholders:
|
|
101
|
+
|
|
102
|
+
- Generate a unique random ID in UUID format: `{{uuid.v4()}}`
|
|
103
|
+
- Generate a deterministic ID in UUID-compatible format: `{{computeId('value1', 'value2')}}`
|
|
104
|
+
- Convert text to a format suitable for use inside a URL: `{{url('This is a test')}}`
|
|
105
|
+
- Convert text to lower case: `{{lowerCase('TEST')}}`
|
|
106
|
+
- Convert text to upper case: `{{upperCase('test')}}`
|
|
107
|
+
- Convert first letter of text to upper case: `{{capitalize('test')}}`
|
|
108
|
+
- Remove leading and trailing spaces from text: `{{trim(' test ')}}`
|
|
109
|
+
- Specify one or more fallback values in case values are null or undefined: `{{fallback(Product.imageSrc, 'path/to/default-image.png')}}`
|
|
110
|
+
- Format UNIX timestamp as a human-readable date: `{{date(Product.updatedAt)}}`
|
|
111
|
+
- Given an array of objects, extract the values from the specified field and join them together into a single comma-separated string: `{{joinFields(Product, 'name')}}`
|
|
112
|
+
|
|
98
113
|
## Components
|
|
99
114
|
|
|
100
115
|
### app-router
|
|
@@ -263,7 +278,7 @@ A form component for inserting data into collections.
|
|
|
263
278
|
|
|
264
279
|
- `collection-type` (required): Specifies the type of collection to add the resource to when the form is submitted. This should match a `Model` available in your Saasufy service.
|
|
265
280
|
- `collection-fields`: A comma-separated list of fields from the `Model` to display as input elements inside the form for the user to fill in. Each field name in this list can optionally be followed by an input element type after a `:` character. For example `collection-fields="qty:number, size:select(small,medium,large)` will create one input element with `type="number"` and one with `type=select` with options `small`, `medium` or `large`. To make a select field optional, you can prefix the list of options with a comma; e.g. `size:select(,small,medium,large)`.
|
|
266
|
-
- `model-values`: An optional list of key-value pairs in the format `field1=value1,field2=value2` to add to the newly created resource alongside the values collected from the user via the form. For non-string values, the type should be provided in the format `fieldName:type=value`; supported types are `string`, `number` and `boolean`.
|
|
281
|
+
- `model-values`: An optional list of key-value pairs in the format `field1=value1,field2=value2` to add to the newly created resource alongside the values collected from the user via the form. For non-string values, the type should be provided in the format `fieldName:type=value`; supported types are `string`, `number` and `boolean`. Unlike `collection-fields` which are rendered as input elements in the form, `model-values` are not shown to the user.
|
|
267
282
|
- `submit-button-label`: Text to display on the submit button. If not specified, defaults to `Submit`.
|
|
268
283
|
- `hide-submit-button`: Adding this attribute will hide the submit button from the form.
|
|
269
284
|
- `success-message`: A message to show the user if the resource has been successfully added to the collection after submitting the form.
|
|
@@ -284,6 +299,13 @@ It supports either immediate deletion or deletion upon confirmation; in the latt
|
|
|
284
299
|
|
|
285
300
|
**Example usage**
|
|
286
301
|
|
|
302
|
+
```html
|
|
303
|
+
<!-- Must be placed somewhere inside a collection-browser component, typically inside the slotted item template. -->
|
|
304
|
+
<collection-deleter model-id="{{Product.id}}" onclick="deleteItem()">✕</collection-deleter>
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
OR (with confirmation step)
|
|
308
|
+
|
|
287
309
|
```html
|
|
288
310
|
<!-- Must be placed somewhere inside a collection-browser component, typically inside the slotted item template. -->
|
|
289
311
|
<collection-deleter model-id="{{Product.id}}" confirm-message="Are you sure you want to delete the {{Product.name}} product?" onclick="confirmDeleteItem()">✕</collection-deleter>
|
package/collection-browser.js
CHANGED
|
@@ -202,14 +202,24 @@ class CollectionBrowser extends SocketConsumer {
|
|
|
202
202
|
|
|
203
203
|
let items = [];
|
|
204
204
|
|
|
205
|
+
let type = this.getAttribute('type-alias') || this.collection.type;
|
|
206
|
+
|
|
205
207
|
if (firstItemTemplate) {
|
|
206
|
-
|
|
208
|
+
let itemString = renderTemplate(
|
|
209
|
+
firstItemTemplate.innerHTML,
|
|
210
|
+
{ [`$${type}`]: this.collection.meta },
|
|
211
|
+
this.socket
|
|
212
|
+
);
|
|
213
|
+
items.push(itemString);
|
|
207
214
|
}
|
|
208
215
|
if (noItemTemplate && !this.collection.value.length) {
|
|
209
|
-
|
|
216
|
+
let itemString = renderTemplate(
|
|
217
|
+
noItemTemplate.innerHTML,
|
|
218
|
+
{ [`$${type}`]: this.collection.meta },
|
|
219
|
+
this.socket
|
|
220
|
+
);
|
|
221
|
+
items.push(itemString);
|
|
210
222
|
} else if (itemTemplate) {
|
|
211
|
-
let type = this.getAttribute('type-alias') || this.collection.type;
|
|
212
|
-
|
|
213
223
|
for (let modelItem of this.collection.value) {
|
|
214
224
|
let itemString = renderTemplate(
|
|
215
225
|
itemTemplate.innerHTML,
|
package/input-provider.js
CHANGED
|
@@ -40,8 +40,12 @@ class InputProvider extends HTMLElement {
|
|
|
40
40
|
|
|
41
41
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
42
42
|
if (!this.isReady) return;
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
if (name === 'value') {
|
|
44
|
+
this.value = newValue;
|
|
45
|
+
} else {
|
|
46
|
+
this.destroy();
|
|
47
|
+
this.render();
|
|
48
|
+
}
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
triggerChange() {
|
|
@@ -53,7 +57,20 @@ class InputProvider extends HTMLElement {
|
|
|
53
57
|
if (newValue && this.hasAttribute('computable-value')) {
|
|
54
58
|
newValue = renderTemplate(newValue);
|
|
55
59
|
}
|
|
56
|
-
let oldValue
|
|
60
|
+
let oldValue;
|
|
61
|
+
if (this.inputElement.type === 'checkbox') {
|
|
62
|
+
oldValue = this.inputElement.checked;
|
|
63
|
+
if (typeof newValue === 'string') {
|
|
64
|
+
this.inputElement.checked = newValue !== 'false' && newValue !== '';
|
|
65
|
+
} else {
|
|
66
|
+
this.inputElement.checked = newValue;
|
|
67
|
+
}
|
|
68
|
+
if (oldValue !== newValue) {
|
|
69
|
+
this.triggerChange();
|
|
70
|
+
}
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
oldValue = this.inputElement.value;
|
|
57
74
|
this.inputElement.value = newValue;
|
|
58
75
|
if (oldValue !== newValue) {
|
|
59
76
|
this.triggerChange();
|
|
@@ -62,7 +79,13 @@ class InputProvider extends HTMLElement {
|
|
|
62
79
|
}
|
|
63
80
|
|
|
64
81
|
get value() {
|
|
65
|
-
|
|
82
|
+
if (!this.inputElement) {
|
|
83
|
+
return '';
|
|
84
|
+
}
|
|
85
|
+
if (this.inputElement.type === 'checkbox') {
|
|
86
|
+
return this.inputElement.checked;
|
|
87
|
+
}
|
|
88
|
+
return this.inputElement.value || '';
|
|
66
89
|
}
|
|
67
90
|
|
|
68
91
|
render() {
|
package/model-input.js
CHANGED
|
@@ -63,15 +63,20 @@ class ModelInput extends SocketConsumer {
|
|
|
63
63
|
if (newValue && this.hasAttribute('computable-value')) {
|
|
64
64
|
newValue = renderTemplate(newValue);
|
|
65
65
|
}
|
|
66
|
+
let oldValue;
|
|
66
67
|
if (this.inputElement.type === 'checkbox') {
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
oldValue = this.inputElement.checked;
|
|
69
|
+
if (typeof newValue === 'string') {
|
|
70
|
+
this.inputElement.checked = newValue !== 'false' && newValue !== '';
|
|
71
|
+
} else {
|
|
72
|
+
this.inputElement.checked = newValue;
|
|
73
|
+
}
|
|
69
74
|
if (oldValue !== newValue) {
|
|
70
75
|
this.triggerChange();
|
|
71
76
|
}
|
|
72
77
|
return;
|
|
73
78
|
}
|
|
74
|
-
|
|
79
|
+
oldValue = this.inputElement.value;
|
|
75
80
|
this.inputElement.value = newValue;
|
|
76
81
|
if (oldValue !== newValue) {
|
|
77
82
|
this.triggerChange();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "saasufy-components",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.14.0",
|
|
4
4
|
"description": "Components for Saasufy.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
"ag-collection": "^5.0.2",
|
|
21
21
|
"ag-model": "^3.0.1",
|
|
22
22
|
"capitalisk-auth-client": "^1.5.1",
|
|
23
|
-
"socketcluster-client": "^19.1.0"
|
|
23
|
+
"socketcluster-client": "^19.1.0",
|
|
24
|
+
"uuid": "^8.3.2"
|
|
24
25
|
},
|
|
25
26
|
"bugs": {
|
|
26
27
|
"url": "https://github.com/Saasufy/saasufy-components/issues"
|
package/sha256.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2022 Andrea Griffini
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
a copy of this software and associated documentation files (the
|
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
// sha256(data) returns the digest
|
|
25
|
+
// sha256() returns an object you can call .add(data) zero or more time and .digest() at the end
|
|
26
|
+
// digest is a 32-byte Uint8Array instance with an added .hex() function.
|
|
27
|
+
// Input should be either a string (that will be encoded as UTF-8) or an array-like object with values 0..255.
|
|
28
|
+
export function sha256(data) {
|
|
29
|
+
let h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a,
|
|
30
|
+
h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19,
|
|
31
|
+
tsz = 0, bp = 0;
|
|
32
|
+
const k = [
|
|
33
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
34
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
35
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
36
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
37
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
38
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
39
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
40
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
41
|
+
],
|
|
42
|
+
rrot = (x, n) => (x >>> n) | (x << (32 - n)),
|
|
43
|
+
w = new Uint32Array(64),
|
|
44
|
+
buf = new Uint8Array(64),
|
|
45
|
+
process = () => {
|
|
46
|
+
for (let j = 0, r = 0; j < 16; j++, r += 4) {
|
|
47
|
+
w[j] = (buf[r] << 24) | (buf[r + 1] << 16) | (buf[r + 2] << 8) | buf[r + 3];
|
|
48
|
+
}
|
|
49
|
+
for (let j = 16; j < 64; j++) {
|
|
50
|
+
let s0 = rrot(w[j - 15], 7) ^ rrot(w[j - 15], 18) ^ (w[j - 15] >>> 3);
|
|
51
|
+
let s1 = rrot(w[j - 2], 17) ^ rrot(w[j - 2], 19) ^ (w[j - 2] >>> 10);
|
|
52
|
+
w[j] = (w[j - 16] + s0 + w[j - 7] + s1) | 0;
|
|
53
|
+
}
|
|
54
|
+
let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
|
|
55
|
+
for (let j = 0; j < 64; j++) {
|
|
56
|
+
let S1 = rrot(e, 6) ^ rrot(e, 11) ^ rrot(e, 25),
|
|
57
|
+
ch = (e & f) ^ ((~e) & g),
|
|
58
|
+
t1 = (h + S1 + ch + k[j] + w[j]) | 0,
|
|
59
|
+
S0 = rrot(a, 2) ^ rrot(a, 13) ^ rrot(a, 22),
|
|
60
|
+
maj = (a & b) ^ (a & c) ^ (b & c),
|
|
61
|
+
t2 = (S0 + maj) | 0;
|
|
62
|
+
h = g; g = f; f = e; e = (d + t1) | 0; d = c; c = b; b = a; a = (t1 + t2) | 0;
|
|
63
|
+
}
|
|
64
|
+
h0 = (h0 + a) | 0; h1 = (h1 + b) | 0; h2 = (h2 + c) | 0; h3 = (h3 + d) | 0;
|
|
65
|
+
h4 = (h4 + e) | 0; h5 = (h5 + f) | 0; h6 = (h6 + g) | 0; h7 = (h7 + h) | 0;
|
|
66
|
+
bp = 0;
|
|
67
|
+
},
|
|
68
|
+
add = data => {
|
|
69
|
+
if (typeof data === 'string') {
|
|
70
|
+
data = typeof TextEncoder === 'undefined' ? Buffer.from(data) : (new TextEncoder).encode(data);
|
|
71
|
+
}
|
|
72
|
+
for (let i = 0; i < data.length; i++) {
|
|
73
|
+
buf[bp++] = data[i];
|
|
74
|
+
if (bp === 64) process();
|
|
75
|
+
}
|
|
76
|
+
tsz += data.length;
|
|
77
|
+
},
|
|
78
|
+
digest = () => {
|
|
79
|
+
buf[bp++] = 0x80; if (bp == 64) process();
|
|
80
|
+
if (bp + 8 > 64) {
|
|
81
|
+
while (bp < 64) buf[bp++] = 0x00;
|
|
82
|
+
process();
|
|
83
|
+
}
|
|
84
|
+
while (bp < 58) buf[bp++] = 0x00;
|
|
85
|
+
// Max number of bytes is 35,184,372,088,831
|
|
86
|
+
let L = tsz * 8;
|
|
87
|
+
buf[bp++] = (L / 1099511627776.) & 255;
|
|
88
|
+
buf[bp++] = (L / 4294967296.) & 255;
|
|
89
|
+
buf[bp++] = L >>> 24;
|
|
90
|
+
buf[bp++] = (L >>> 16) & 255;
|
|
91
|
+
buf[bp++] = (L >>> 8) & 255;
|
|
92
|
+
buf[bp++] = L & 255;
|
|
93
|
+
process();
|
|
94
|
+
let reply = new Uint8Array(32);
|
|
95
|
+
reply[0] = h0 >>> 24; reply[1] = (h0 >>> 16) & 255; reply[2] = (h0 >>> 8) & 255; reply[3] = h0 & 255;
|
|
96
|
+
reply[4] = h1 >>> 24; reply[5] = (h1 >>> 16) & 255; reply[6] = (h1 >>> 8) & 255; reply[7] = h1 & 255;
|
|
97
|
+
reply[8] = h2 >>> 24; reply[9] = (h2 >>> 16) & 255; reply[10] = (h2 >>> 8) & 255; reply[11] = h2 & 255;
|
|
98
|
+
reply[12] = h3 >>> 24; reply[13] = (h3 >>> 16) & 255; reply[14] = (h3 >>> 8) & 255; reply[15] = h3 & 255;
|
|
99
|
+
reply[16] = h4 >>> 24; reply[17] = (h4 >>> 16) & 255; reply[18] = (h4 >>> 8) & 255; reply[19] = h4 & 255;
|
|
100
|
+
reply[20] = h5 >>> 24; reply[21] = (h5 >>> 16) & 255; reply[22] = (h5 >>> 8) & 255; reply[23] = h5 & 255;
|
|
101
|
+
reply[24] = h6 >>> 24; reply[25] = (h6 >>> 16) & 255; reply[26] = (h6 >>> 8) & 255; reply[27] = h6 & 255;
|
|
102
|
+
reply[28] = h7 >>> 24; reply[29] = (h7 >>> 16) & 255; reply[30] = (h7 >>> 8) & 255; reply[31] = h7 & 255;
|
|
103
|
+
reply.hex = () => {
|
|
104
|
+
let res = '';
|
|
105
|
+
reply.forEach(x => res += ('0' + x.toString(16)).slice(-2));
|
|
106
|
+
return res;
|
|
107
|
+
};
|
|
108
|
+
return reply;
|
|
109
|
+
};
|
|
110
|
+
if (data === undefined) return { add, digest };
|
|
111
|
+
add(data);
|
|
112
|
+
return digest();
|
|
113
|
+
}
|
package/utils.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
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 * as uuid from '/node_modules/uuid/dist/esm-browser/index.js';
|
|
4
|
+
import { sha256 } from './sha256.js';
|
|
3
5
|
|
|
4
6
|
const DEFAULT_DEBOUNCE_DELAY = 300;
|
|
5
7
|
const DEFAULT_RELOAD_DELAY = 0;
|
|
@@ -181,6 +183,26 @@ export function createModel(modelOptions) {
|
|
|
181
183
|
return model;
|
|
182
184
|
}
|
|
183
185
|
|
|
186
|
+
function computeId(...parts) {
|
|
187
|
+
let encoder = new TextEncoder();
|
|
188
|
+
let data = encoder.encode(parts.join('-'));
|
|
189
|
+
let start = Date.now();
|
|
190
|
+
let hashBuffer = sha256(data);
|
|
191
|
+
let hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
192
|
+
let hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
|
|
193
|
+
let idHex = hashHex.slice(0, 32);
|
|
194
|
+
|
|
195
|
+
let third = (
|
|
196
|
+
(parseInt(idHex.slice(12, 16), 16) & 0x0fff) | 0x4000
|
|
197
|
+
).toString(16);
|
|
198
|
+
|
|
199
|
+
let fourth = (
|
|
200
|
+
(parseInt(idHex.slice(16, 20), 16) & 0x3fff) | 0x8000
|
|
201
|
+
).toString(16);
|
|
202
|
+
|
|
203
|
+
return `${idHex.slice(0, 8)}-${idHex.slice(8, 12)}-${third}-${fourth}-${idHex.slice(20, 32)}`;
|
|
204
|
+
}
|
|
205
|
+
|
|
184
206
|
let templateFormatters = {
|
|
185
207
|
url: (value) => String(value).toLowerCase().replace(/ /g, '-'),
|
|
186
208
|
lowerCase: (value) => String(value).toLowerCase(),
|
|
@@ -207,7 +229,8 @@ let templateFormatters = {
|
|
|
207
229
|
},
|
|
208
230
|
combineFilters: (filterMap, useOr) => {
|
|
209
231
|
return Object.values(filterMap || {}).join(useOr ? ' %OR% ' : ' %AND% ');
|
|
210
|
-
}
|
|
232
|
+
},
|
|
233
|
+
computeId
|
|
211
234
|
};
|
|
212
235
|
|
|
213
236
|
let templateTagsRegExp = /{{.*?}}/gs;
|
|
@@ -228,6 +251,7 @@ export function renderTemplate(templateString, data, socket) {
|
|
|
228
251
|
let expString = match.slice(2, -2);
|
|
229
252
|
let options = {
|
|
230
253
|
...templateFormatters,
|
|
254
|
+
uuid,
|
|
231
255
|
socket: socket ? {
|
|
232
256
|
state: socket.state,
|
|
233
257
|
pendingReconnect: socket.pendingReconnect,
|