saasufy-components 3.13.0 → 3.15.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 CHANGED
@@ -77,6 +77,7 @@ To use components, you just need to include them into your `.html` file inside y
77
77
  <script src="https://saasufy.com/node_modules/saasufy-components/collection-adder-group.js" type="module" defer></script>
78
78
  <script src="https://saasufy.com/node_modules/saasufy-components/confirm-modal.js" type="module" defer></script>
79
79
  <script src="https://saasufy.com/node_modules/saasufy-components/overlay-modal.js" type="module" defer></script>
80
+ <script src="https://saasufy.com/node_modules/saasufy-components/download-link.js" type="module" defer></script>
80
81
  ```
81
82
 
82
83
  You should only add the script tags for the components which your page uses to avoid wasting bandwidth and unnecessarily delaying page load for your users.
@@ -95,6 +96,21 @@ This project does not impose any specific approach to styling components, howeve
95
96
  <link href="https://saasufy.com/node_modules/saasufy-components/styles.css" rel="stylesheet" />
96
97
  ```
97
98
 
99
+ ### Utility functions
100
+
101
+ The following utility functions can be used anywhere inside template `{{expression}}` placeholders:
102
+
103
+ - Generate a unique random ID in UUID format: `{{uuid.v4()}}`
104
+ - Generate a deterministic ID in UUID-compatible format derived from one or more values: `{{computeId('value1', 'value2')}}`
105
+ - Convert text to a format suitable for use inside a URL: `{{url('This is a test')}}`
106
+ - Convert text to lower case: `{{lowerCase('TEST')}}`
107
+ - Convert text to upper case: `{{upperCase('test')}}`
108
+ - Convert first letter of text to upper case: `{{capitalize('test')}}`
109
+ - Remove leading and trailing spaces from text: `{{trim(' test ')}}`
110
+ - Specify one or more fallback values in case values are null or undefined: `{{fallback(Product.imageSrc, 'path/to/default-image.png')}}`
111
+ - Format UNIX timestamp as a human-readable date: `{{date(Product.updatedAt)}}`
112
+ - 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')}}`
113
+
98
114
  ## Components
99
115
 
100
116
  ### app-router
@@ -880,3 +896,28 @@ A modal component to prompt the user for confirmation before performing sensitiv
880
896
  ### overlay-modal
881
897
 
882
898
  A general purpose modal component.
899
+
900
+ ### download-link
901
+
902
+ A component to download a file from a model. The file must be stored on the model instance in base64 format.
903
+
904
+ **Import**
905
+
906
+ ```html
907
+ <script src="https://saasufy.com/node_modules/saasufy-components/download-link.js" type="module" defer></script>
908
+ ```
909
+
910
+ **Example usage**
911
+
912
+ ```html
913
+ <download-link model-type="Image" model-id="dce962f8-34f1-4a82-95b9-377e42b57e2d" model-field="src" file-name="my-image.png">
914
+ <a href="javascript: void(0)">Download image</a>
915
+ </download-link>
916
+ ```
917
+
918
+ **Attributes**
919
+
920
+ - `model-type` (required): Specifies the type of model to download the file from. This should match a `Model` available in your Saasufy service.
921
+ - `model-id` (required): The id of the model instance/record to download the file from.
922
+ - `model-field` (required): The field of the model instance/record where the file is stored in base64 format (with content type).
923
+ - `file-name` (required): The default name of the file to save as. The file extension is optional.
@@ -0,0 +1,94 @@
1
+ import { SocketConsumer } from './socket.js';
2
+
3
+ class DownloadLink extends SocketConsumer {
4
+ constructor() {
5
+ super();
6
+ let dataTypeRegExp = /data:([^;]+);base64,(.*)/;
7
+
8
+ this.handleLinkClickEvent = async (event) => {
9
+ let socket = this.getSocket();
10
+
11
+ let modelType = this.getAttribute('model-type');
12
+ let modelId = this.getAttribute('model-id');
13
+ let modelField = this.getAttribute('model-field');
14
+
15
+ this.setAttribute('is-loading', '');
16
+ this.removeAttribute('failed');
17
+
18
+ let data;
19
+ try {
20
+ data = await socket.invoke('crud', {
21
+ action: 'read',
22
+ type: modelType,
23
+ id: modelId,
24
+ field: modelField
25
+ });
26
+ } catch (error) {
27
+ this.setAttribute('failed', '');
28
+ if (!this.hasAttribute('hide-error-logs')) {
29
+ console.error(
30
+ `Failed to download file because of error: ${error.message}`
31
+ );
32
+ }
33
+ }
34
+
35
+ this.removeAttribute('is-loading');
36
+
37
+ if (typeof data !== 'string') {
38
+ this.setAttribute('failed', '');
39
+ if (!this.hasAttribute('hide-error-logs')) {
40
+ console.error(
41
+ 'Failed to download file because it did not exist'
42
+ );
43
+ }
44
+ return;
45
+ };
46
+
47
+ let dataParts = data.match(dataTypeRegExp);
48
+ if (!dataParts) {
49
+ this.setAttribute('failed', '');
50
+ if (!this.hasAttribute('hide-error-logs')) {
51
+ console.error(
52
+ 'Failed to process downloaded file because format was invalid'
53
+ );
54
+ }
55
+ return;
56
+ }
57
+
58
+ let contentType = dataParts[1];
59
+ let content = dataParts[2];
60
+
61
+ let binaryString = atob(content);
62
+ let length = binaryString.length;
63
+ let bytes = new Uint8Array(length);
64
+ for (let i = 0; i < length; i++) {
65
+ bytes[i] = binaryString.charCodeAt(i);
66
+ }
67
+ let blob = new Blob([ bytes ], { type: contentType });
68
+
69
+ let fileName = this.getAttribute('file-name');
70
+ let fileDataURL = URL.createObjectURL(blob);
71
+
72
+ let downloadElement = document.createElement('a');
73
+ downloadElement.style.display = 'none';
74
+ downloadElement.setAttribute('href', fileDataURL);
75
+ downloadElement.setAttribute('download', fileName);
76
+ document.body.appendChild(downloadElement);
77
+ downloadElement.click();
78
+ downloadElement.remove();
79
+ URL.revokeObjectURL(fileDataURL);
80
+ };
81
+ }
82
+
83
+ connectedCallback() {
84
+ this.removeAttribute('failed');
85
+ this.removeAttribute('is-loading');
86
+ this.addEventListener('click', this.handleLinkClickEvent);
87
+ }
88
+
89
+ disconnectedCallback() {
90
+ this.removeEventListener('click', this.handleLinkClickEvent);
91
+ }
92
+ }
93
+
94
+ window.customElements.define('download-link', DownloadLink);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasufy-components",
3
- "version": "3.13.0",
3
+ "version": "3.15.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,