saasufy-components 2.12.0 → 2.13.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/collection-adder.js +6 -54
- package/model-input.js +15 -1
- package/package.json +1 -1
- package/utils.js +51 -0
package/collection-adder.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SocketConsumer } from './socket.js';
|
|
2
|
+
import { convertStringToFieldParams, getTypeCastFunction } from './utils.js';
|
|
2
3
|
import AGCollection from '/node_modules/ag-collection/ag-collection.js';
|
|
3
4
|
|
|
4
5
|
class CollectionAdder extends SocketConsumer {
|
|
@@ -6,18 +7,7 @@ class CollectionAdder extends SocketConsumer {
|
|
|
6
7
|
super();
|
|
7
8
|
this.options = options || {};
|
|
8
9
|
|
|
9
|
-
this.typeCastFunctions = {
|
|
10
|
-
text: String,
|
|
11
|
-
textarea: String,
|
|
12
|
-
checkbox: Boolean,
|
|
13
|
-
number: Number,
|
|
14
|
-
radio: String,
|
|
15
|
-
select: String,
|
|
16
|
-
'text-select': String
|
|
17
|
-
};
|
|
18
|
-
this.fieldPartsRegExp = /("[^"]*"|'[^']*'|\([^)]*\)|[^,()"']+)+/g;
|
|
19
10
|
this.inputTypeWithParamsRegExp = /^([^()]*)(\(([^)]*)\))?/;
|
|
20
|
-
this.quotedContentRegExp = /^\s*["']?(.*?)["']?\s*$/;
|
|
21
11
|
this.isReady = false;
|
|
22
12
|
}
|
|
23
13
|
|
|
@@ -52,44 +42,6 @@ class CollectionAdder extends SocketConsumer {
|
|
|
52
42
|
this.render();
|
|
53
43
|
}
|
|
54
44
|
|
|
55
|
-
getTypeCastFunction(type) {
|
|
56
|
-
return this.typeCastFunctions[type] || ((value) => value);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
getFieldDetails(string) {
|
|
60
|
-
let parts = ((string || '').match(this.fieldPartsRegExp) || []).map(field => field.trim());
|
|
61
|
-
let fieldTypeValues = parts.map((part) => {
|
|
62
|
-
let subParts = part.split('=');
|
|
63
|
-
let nameType = (subParts[0] || '').split(':');
|
|
64
|
-
let name = nameType[0];
|
|
65
|
-
let type = nameType[1] || 'text';
|
|
66
|
-
let value = subParts.slice(1).join('=').replace(this.quotedContentRegExp, '$1');
|
|
67
|
-
return {
|
|
68
|
-
name,
|
|
69
|
-
type,
|
|
70
|
-
value
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
let fieldNames = fieldTypeValues.map(item => item.name);
|
|
74
|
-
let fieldTypes = Object.fromEntries(
|
|
75
|
-
fieldTypeValues.map(item => [ item.name, item.type ])
|
|
76
|
-
);
|
|
77
|
-
let fieldValues = Object.fromEntries(
|
|
78
|
-
fieldTypeValues.map(
|
|
79
|
-
(item) => {
|
|
80
|
-
let type = fieldTypes[item.name];
|
|
81
|
-
let Type = this.getTypeCastFunction(type);
|
|
82
|
-
return [ item.name, Type(item.value) ];
|
|
83
|
-
}
|
|
84
|
-
)
|
|
85
|
-
);
|
|
86
|
-
return {
|
|
87
|
-
fieldNames,
|
|
88
|
-
fieldTypes,
|
|
89
|
-
fieldValues
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
45
|
async submit() {
|
|
94
46
|
if (!this.isReady) {
|
|
95
47
|
throw new Error('Collection adder form is not ready to be submitted');
|
|
@@ -119,7 +71,7 @@ class CollectionAdder extends SocketConsumer {
|
|
|
119
71
|
.filter(input => input.value !== '')
|
|
120
72
|
.map((input) => {
|
|
121
73
|
let fieldType = this.fieldTypes[input.name];
|
|
122
|
-
let Type =
|
|
74
|
+
let Type = getTypeCastFunction(fieldType);
|
|
123
75
|
let value;
|
|
124
76
|
if (fieldType === 'checkbox') {
|
|
125
77
|
value = input.checked;
|
|
@@ -180,22 +132,22 @@ class CollectionAdder extends SocketConsumer {
|
|
|
180
132
|
let {
|
|
181
133
|
fieldNames,
|
|
182
134
|
fieldTypes
|
|
183
|
-
} =
|
|
135
|
+
} = convertStringToFieldParams(this.getAttribute('collection-fields'));
|
|
184
136
|
|
|
185
137
|
this.fieldTypes = fieldTypes;
|
|
186
138
|
|
|
187
139
|
let {
|
|
188
140
|
fieldNames: modelFieldNames,
|
|
189
141
|
fieldValues: modelFieldValues
|
|
190
|
-
} =
|
|
142
|
+
} = convertStringToFieldParams(this.getAttribute('model-values'));
|
|
191
143
|
|
|
192
144
|
let {
|
|
193
145
|
fieldValues: modelFieldLabels
|
|
194
|
-
} =
|
|
146
|
+
} = convertStringToFieldParams(this.getAttribute('field-labels'));
|
|
195
147
|
|
|
196
148
|
let {
|
|
197
149
|
fieldValues: optionLabels
|
|
198
|
-
} =
|
|
150
|
+
} = convertStringToFieldParams(this.getAttribute('option-labels'));
|
|
199
151
|
|
|
200
152
|
let autocapitalize = this.getAttribute('autocapitalize');
|
|
201
153
|
let autocorrect = this.getAttribute('autocorrect');
|
package/model-input.js
CHANGED
|
@@ -61,6 +61,14 @@ class ModelInput extends SocketConsumer {
|
|
|
61
61
|
|
|
62
62
|
set value(newValue) {
|
|
63
63
|
if (this.inputElement) {
|
|
64
|
+
if (this.inputElement.type === 'checkbox') {
|
|
65
|
+
let oldValue = this.inputElement.checked;
|
|
66
|
+
this.inputElement.checked = newValue;
|
|
67
|
+
if (oldValue !== newValue) {
|
|
68
|
+
this.triggerChange();
|
|
69
|
+
}
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
64
72
|
let oldValue = this.inputElement.value;
|
|
65
73
|
this.inputElement.value = newValue;
|
|
66
74
|
if (oldValue !== newValue) {
|
|
@@ -70,7 +78,13 @@ class ModelInput extends SocketConsumer {
|
|
|
70
78
|
}
|
|
71
79
|
|
|
72
80
|
get value() {
|
|
73
|
-
|
|
81
|
+
if (!this.inputElement) {
|
|
82
|
+
return '';
|
|
83
|
+
}
|
|
84
|
+
if (this.inputElement.type === 'checkbox') {
|
|
85
|
+
return this.inputElement.checked;
|
|
86
|
+
}
|
|
87
|
+
return this.inputElement.value;
|
|
74
88
|
}
|
|
75
89
|
|
|
76
90
|
render() {
|
package/package.json
CHANGED
package/utils.js
CHANGED
|
@@ -291,6 +291,57 @@ export function generateRandomHexString(byteLength) {
|
|
|
291
291
|
}).join('');
|
|
292
292
|
}
|
|
293
293
|
|
|
294
|
+
export const fieldPartsRegExp = /("[^"]*"|'[^']*'|\([^)]*\)|[^,()"']+)+/g;
|
|
295
|
+
export const quotedContentRegExp = /^\s*["']?(.*?)["']?\s*$/;
|
|
296
|
+
|
|
297
|
+
export const typeCastFunctions = {
|
|
298
|
+
text: String,
|
|
299
|
+
textarea: String,
|
|
300
|
+
checkbox: Boolean,
|
|
301
|
+
number: Number,
|
|
302
|
+
radio: String,
|
|
303
|
+
select: String,
|
|
304
|
+
'text-select': String
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
export function getTypeCastFunction(type) {
|
|
308
|
+
return typeCastFunctions[type] || ((value) => value);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function convertStringToFieldParams(string) {
|
|
312
|
+
let parts = ((string || '').match(fieldPartsRegExp) || []).map(field => field.trim());
|
|
313
|
+
let fieldTypeValues = parts.map((part) => {
|
|
314
|
+
let subParts = part.split('=');
|
|
315
|
+
let nameType = (subParts[0] || '').split(':');
|
|
316
|
+
let name = nameType[0];
|
|
317
|
+
let type = nameType[1] || 'text';
|
|
318
|
+
let value = subParts.slice(1).join('=').replace(quotedContentRegExp, '$1');
|
|
319
|
+
return {
|
|
320
|
+
name,
|
|
321
|
+
type,
|
|
322
|
+
value
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
let fieldNames = fieldTypeValues.map(item => item.name);
|
|
326
|
+
let fieldTypes = Object.fromEntries(
|
|
327
|
+
fieldTypeValues.map(item => [ item.name, item.type ])
|
|
328
|
+
);
|
|
329
|
+
let fieldValues = Object.fromEntries(
|
|
330
|
+
fieldTypeValues.map(
|
|
331
|
+
(item) => {
|
|
332
|
+
let type = fieldTypes[item.name];
|
|
333
|
+
let Type = getTypeCastFunction(type);
|
|
334
|
+
return [ item.name, Type(item.value) ];
|
|
335
|
+
}
|
|
336
|
+
)
|
|
337
|
+
);
|
|
338
|
+
return {
|
|
339
|
+
fieldNames,
|
|
340
|
+
fieldTypes,
|
|
341
|
+
fieldValues
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
294
345
|
export function wait(duration) {
|
|
295
346
|
return new Promise((resolve) => setTimeout(resolve, duration));
|
|
296
347
|
}
|