@cocreate/elements 1.23.4 → 1.24.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/CHANGELOG.md +29 -0
- package/package.json +8 -8
- package/src/form.js +1 -1
- package/src/index.js +208 -63
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
# [1.24.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.23.5...v1.24.0) (2023-10-16)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* data[i].method ([71c0b7c](https://github.com/CoCreate-app/CoCreate-elements/commit/71c0b7ccd28d0f4bbf03b7d8cec615f21479af18))
|
|
7
|
+
* delete for loop ([5507a86](https://github.com/CoCreate-app/CoCreate-elements/commit/5507a86526bdee6b7ac8f74d6ae051068ff9e736))
|
|
8
|
+
* export getObject ([42bd4b3](https://github.com/CoCreate-app/CoCreate-elements/commit/42bd4b3fa66de724a94230c1c3ccfa7faea3a8b8))
|
|
9
|
+
* replaced deleteDocuments with delete ([8e06633](https://github.com/CoCreate-app/CoCreate-elements/commit/8e0663351d459bd79a9f955a0a296df46ed3d580))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
* delete-selector w/ fallback to clicked el ([4143fbb](https://github.com/CoCreate-app/CoCreate-elements/commit/4143fbbfade97c3073e8df6b1ce5b8051b5d5fe0))
|
|
15
|
+
|
|
16
|
+
## [1.23.5](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.23.4...v1.23.5) (2023-10-14)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Bug Fixes
|
|
20
|
+
|
|
21
|
+
* added other crud types to attribute queries ([513472c](https://github.com/CoCreate-app/CoCreate-elements/commit/513472c768412e8b7ed5b205a48d16a49349cfaa))
|
|
22
|
+
* attribute mutation not firing when selector applied. temporarily removed selector ([3b5551f](https://github.com/CoCreate-app/CoCreate-elements/commit/3b5551ff0e7c3838b7079e6d14574831ad6a7ba5))
|
|
23
|
+
* bump dependencies ([1aa9443](https://github.com/CoCreate-app/CoCreate-elements/commit/1aa944336afdcd38b4f0915c6146d89d0ea56b1c))
|
|
24
|
+
* data.filter updated to data$filter ([5a64fbf](https://github.com/CoCreate-app/CoCreate-elements/commit/5a64fbfe0d5e580633aa0deed131619c070e0fac))
|
|
25
|
+
* getData ([e74675e](https://github.com/CoCreate-app/CoCreate-elements/commit/e74675ea5ed0f9b800fb18874864c93e8a8a5353))
|
|
26
|
+
* improved setting method conditions ([013753a](https://github.com/CoCreate-app/CoCreate-elements/commit/013753a541eca3f88b4d5350763f52f38bef2932))
|
|
27
|
+
* replaced action with data.method ([bfe513c](https://github.com/CoCreate-app/CoCreate-elements/commit/bfe513c5c498c84d2f11ce7d07ce71095dd232bf))
|
|
28
|
+
* save and delete action ([f32e9b6](https://github.com/CoCreate-app/CoCreate-elements/commit/f32e9b6e661211b8fb0548aaa03ffd28b4bd1edd))
|
|
29
|
+
|
|
1
30
|
## [1.23.4](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.23.3...v1.23.4) (2023-10-09)
|
|
2
31
|
|
|
3
32
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/elements",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.24.0",
|
|
4
4
|
"description": "Fetch content into any html element by using html5 attributes to query data stored in CoCreate headless CMS. If data is changed it will instantly reflect in html tags as changes are broadcasted in realtime",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"elements",
|
|
@@ -58,12 +58,12 @@
|
|
|
58
58
|
"webpack-log": "^3.0.1"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@cocreate/actions": "^1.11.
|
|
62
|
-
"@cocreate/crud-client": "^1.28.
|
|
63
|
-
"@cocreate/element-prototype": "^1.11.
|
|
64
|
-
"@cocreate/filter": "^1.23.
|
|
65
|
-
"@cocreate/observer": "^1.11.
|
|
66
|
-
"@cocreate/render": "^1.28.
|
|
67
|
-
"@cocreate/utils": "^1.25.
|
|
61
|
+
"@cocreate/actions": "^1.11.6",
|
|
62
|
+
"@cocreate/crud-client": "^1.28.4",
|
|
63
|
+
"@cocreate/element-prototype": "^1.11.5",
|
|
64
|
+
"@cocreate/filter": "^1.23.3",
|
|
65
|
+
"@cocreate/observer": "^1.11.5",
|
|
66
|
+
"@cocreate/render": "^1.28.3",
|
|
67
|
+
"@cocreate/utils": "^1.25.3"
|
|
68
68
|
}
|
|
69
69
|
}
|
package/src/form.js
CHANGED
|
@@ -48,7 +48,7 @@ function runObjectId(form) {
|
|
|
48
48
|
* @param form
|
|
49
49
|
*/
|
|
50
50
|
function setAttribute(form) {
|
|
51
|
-
let elements = form.querySelectorAll('[key]');
|
|
51
|
+
let elements = form.querySelectorAll('[storage], [database], [array], [index], [object], [key]');
|
|
52
52
|
|
|
53
53
|
for (let attribute of form.attributes) {
|
|
54
54
|
let variable = window.CoCreateConfig.attributes[attribute.name]
|
package/src/index.js
CHANGED
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
import Observer from '@cocreate/observer';
|
|
27
27
|
import Actions from '@cocreate/actions';
|
|
28
28
|
import CRUD from '@cocreate/crud-client';
|
|
29
|
-
import { dotNotationToObject, queryData, sortData, getAttributes, getAttributeNames, checkValue } from '@cocreate/utils';
|
|
30
|
-
import '@cocreate/filter';
|
|
31
|
-
import
|
|
29
|
+
import { dotNotationToObject, queryElements, queryData, sortData, getAttributes, getAttributeNames, checkValue } from '@cocreate/utils';
|
|
30
|
+
import filter from '@cocreate/filter';
|
|
31
|
+
import render from '@cocreate/render';
|
|
32
32
|
import '@cocreate/element-prototype';
|
|
33
33
|
import './fetchSrc';
|
|
34
34
|
import { reset } from './form'
|
|
@@ -39,7 +39,7 @@ const keys = new Map();
|
|
|
39
39
|
const forms = new Map(); // form, [elements]
|
|
40
40
|
const debounce = new Map();
|
|
41
41
|
|
|
42
|
-
function init(element) {
|
|
42
|
+
async function init(element) {
|
|
43
43
|
if (element && !(element instanceof HTMLCollection) && !Array.isArray(element))
|
|
44
44
|
element = [element]
|
|
45
45
|
else if (!element) {
|
|
@@ -52,7 +52,7 @@ function init(element) {
|
|
|
52
52
|
if (elements.has(element[i]) || element.tagName === 'FORM')
|
|
53
53
|
continue
|
|
54
54
|
|
|
55
|
-
let data = initElement(element[i]);
|
|
55
|
+
let data = await initElement(element[i]);
|
|
56
56
|
if (data) {
|
|
57
57
|
let dataKey = initDataKey(element[i], data)
|
|
58
58
|
dataObjects.set(dataKey.string, data)
|
|
@@ -67,7 +67,7 @@ function init(element) {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
function initElement(el) {
|
|
70
|
+
async function initElement(el) {
|
|
71
71
|
if (el.hasAttribute('render-json')) {
|
|
72
72
|
// TODO find the json template in the text or attributes
|
|
73
73
|
// if found add the node as an element, if the element has crud attributes also add as the element
|
|
@@ -119,9 +119,12 @@ function initElement(el) {
|
|
|
119
119
|
});
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
-
|
|
122
|
+
// let attributes = ['filter-key', 'filter-search', 'filter-sort-key', 'filter-on', 'filter-limit']
|
|
123
|
+
// if (el.getFilter || attributes.some(attr => el.hasAttribute(attr))) {
|
|
124
|
+
await filter.init()
|
|
123
125
|
if (el.getFilter) {
|
|
124
126
|
data.$filter = el.getFilter();
|
|
127
|
+
|
|
125
128
|
el.setFilter = (filter) => {
|
|
126
129
|
data.$filter = filter
|
|
127
130
|
let dataKey = initDataKey(el, data)
|
|
@@ -227,10 +230,13 @@ async function read(element, data, dataKey) {
|
|
|
227
230
|
delayTimer = setTimeout(function () {
|
|
228
231
|
debounce.delete(dataKey.string)
|
|
229
232
|
// TODO: should server support string and string array for type object, methods create, read, delete
|
|
230
|
-
if (data.type === 'object') {
|
|
233
|
+
if (!data.$filter && data.type === 'object') {
|
|
231
234
|
if (typeof data.object === 'string')
|
|
232
235
|
data.object = { _id: data.object }
|
|
233
236
|
|
|
237
|
+
if (Array.isArray(data.object))
|
|
238
|
+
data.object = { _id: data.object[0]._id }
|
|
239
|
+
|
|
234
240
|
if (!data.object._id.match(/^[0-9a-fA-F]{24}$/))
|
|
235
241
|
return
|
|
236
242
|
}
|
|
@@ -245,6 +251,8 @@ async function read(element, data, dataKey) {
|
|
|
245
251
|
}
|
|
246
252
|
|
|
247
253
|
function setData(element, data) {
|
|
254
|
+
if (data.method === 'delete.object')
|
|
255
|
+
console.log('test')
|
|
248
256
|
if (!element) {
|
|
249
257
|
element = getDataElements(data)
|
|
250
258
|
if (!element.length) return
|
|
@@ -272,7 +280,7 @@ function setData(element, data) {
|
|
|
272
280
|
|
|
273
281
|
const { key, isRead, isListen, isCrdt } = getAttributes(el);
|
|
274
282
|
if (el.getFilter || el.renderValue)
|
|
275
|
-
filterData(el, data, type, key
|
|
283
|
+
filterData(el, data, type, key)
|
|
276
284
|
else {
|
|
277
285
|
let value = CRUD.getValueFromObject(data[type][0], key);
|
|
278
286
|
if (key) {
|
|
@@ -286,7 +294,7 @@ function setData(element, data) {
|
|
|
286
294
|
}
|
|
287
295
|
}
|
|
288
296
|
|
|
289
|
-
function filterData(element, data, type, key
|
|
297
|
+
function filterData(element, data, type, key) {
|
|
290
298
|
if (key) {
|
|
291
299
|
if (!data.type) return
|
|
292
300
|
if (Array.isArray(data[type])) {
|
|
@@ -305,8 +313,8 @@ function filterData(element, data, type, key, action) {
|
|
|
305
313
|
}
|
|
306
314
|
}
|
|
307
315
|
|
|
308
|
-
if (element.getFilter &&
|
|
309
|
-
checkFilters(element, data, type
|
|
316
|
+
if (element.getFilter && data.method && !data.method.startsWith('read'))
|
|
317
|
+
checkFilters(element, data, type)
|
|
310
318
|
else if (element.renderValue)
|
|
311
319
|
element.renderValue(data);
|
|
312
320
|
// render({ element, data, key: type });
|
|
@@ -317,7 +325,7 @@ function filterData(element, data, type, key, action) {
|
|
|
317
325
|
element.dispatchEvent(evt);
|
|
318
326
|
}
|
|
319
327
|
|
|
320
|
-
function checkFilters(element, data, type
|
|
328
|
+
function checkFilters(element, data, type) {
|
|
321
329
|
let Data = element.getData()
|
|
322
330
|
if (!Data) return
|
|
323
331
|
|
|
@@ -329,25 +337,31 @@ function checkFilters(element, data, type, action) {
|
|
|
329
337
|
newData = data
|
|
330
338
|
|
|
331
339
|
let filter = element.getFilter()
|
|
332
|
-
if (filter && filter.query)
|
|
333
|
-
|
|
334
|
-
|
|
340
|
+
if (filter && filter.query) {
|
|
341
|
+
for (let i = 0; i < newData.length; i++) {
|
|
342
|
+
let isMatch = queryData(newData[i], filter.query)
|
|
343
|
+
if (!isMatch)
|
|
344
|
+
newData.slice(i, 1)
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (!newData || !newData.length)
|
|
335
349
|
return
|
|
336
350
|
|
|
337
351
|
if (Array.isArray(Data)) {
|
|
338
352
|
if (Array.isArray(newData)) {
|
|
339
353
|
for (let i = 0; i < newData.length; i++) {
|
|
340
|
-
checkIndex(element, data, Data, newData[i], type, filter
|
|
354
|
+
checkIndex(element, data, Data, newData[i], type, filter)
|
|
341
355
|
}
|
|
342
356
|
} else {
|
|
343
|
-
checkIndex(element, data, Data, newData, type, filter
|
|
357
|
+
checkIndex(element, data, Data, newData, type, filter)
|
|
344
358
|
}
|
|
345
359
|
} else {
|
|
346
360
|
let primaryKey
|
|
347
361
|
if (type === 'object') {
|
|
348
362
|
primaryKey = '_id';
|
|
349
363
|
} else {
|
|
350
|
-
primaryKey = '
|
|
364
|
+
primaryKey = 'name';
|
|
351
365
|
}
|
|
352
366
|
|
|
353
367
|
if (Data[primaryKey] === newData[primaryKey]) {
|
|
@@ -361,7 +375,7 @@ function checkFilters(element, data, type, action) {
|
|
|
361
375
|
}
|
|
362
376
|
}
|
|
363
377
|
|
|
364
|
-
function checkIndex(element, data, Data, newData, type, filter
|
|
378
|
+
function checkIndex(element, data, Data, newData, type, filter) {
|
|
365
379
|
let index
|
|
366
380
|
if (type === 'object') {
|
|
367
381
|
index = Data.findIndex(obj => obj._id === newData._id);
|
|
@@ -372,7 +386,7 @@ function checkIndex(element, data, Data, newData, type, filter, action) {
|
|
|
372
386
|
if (!data.$filter)
|
|
373
387
|
data.$filter = {}
|
|
374
388
|
|
|
375
|
-
if (
|
|
389
|
+
if (data.method.startsWith('delete')) {
|
|
376
390
|
if (!index && index !== 0)
|
|
377
391
|
return
|
|
378
392
|
data.$filter.remove = true
|
|
@@ -387,28 +401,27 @@ function checkIndex(element, data, Data, newData, type, filter, action) {
|
|
|
387
401
|
}
|
|
388
402
|
|
|
389
403
|
if (filter && filter.sort) {
|
|
390
|
-
|
|
404
|
+
Data[index].isNewData = true
|
|
391
405
|
Data = sortData(Data, filter.sort)
|
|
392
406
|
index = Data.findIndex(obj => obj.isNewData);
|
|
393
407
|
}
|
|
408
|
+
}
|
|
394
409
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
element.setValue(data)
|
|
404
|
-
}
|
|
405
|
-
|
|
410
|
+
if (index >= 0) {
|
|
411
|
+
if (data.$filter.currentIndex === index)
|
|
412
|
+
delete data.$filter.currentIndex
|
|
413
|
+
data.$filter.index = index
|
|
414
|
+
if (element.renderValue)
|
|
415
|
+
element.renderValue(data);
|
|
416
|
+
else if (data)
|
|
417
|
+
element.setValue(data)
|
|
406
418
|
}
|
|
419
|
+
|
|
407
420
|
}
|
|
408
421
|
|
|
409
422
|
function getDataKey(data) {
|
|
410
423
|
let dataKey = {};
|
|
411
|
-
let attributes = ["host", "organization_id", "apikey", "method", "type", "storage", "database", "array", "index", "object", "key", "updateKey", "filter", "upsert", "namespace", "room", "broadcast", "broadcastSender", "broadcastBrowser"];
|
|
424
|
+
let attributes = ["host", "organization_id", "apikey", "method", "type", "storage", "database", "array", "index", "object", "key", "updateKey", "$filter", "upsert", "namespace", "room", "broadcast", "broadcastSender", "broadcastBrowser"];
|
|
412
425
|
|
|
413
426
|
for (let attribute of attributes) {
|
|
414
427
|
if (attribute === 'key' && data.type === 'object')
|
|
@@ -444,7 +457,7 @@ function getDataElements(data) {
|
|
|
444
457
|
|
|
445
458
|
function getDataKeys(data) {
|
|
446
459
|
const matchingKeyStrings = [];
|
|
447
|
-
const targetKeys = ["type", "storage", "database", "array", "index", "object", 'filter'];
|
|
460
|
+
const targetKeys = ["type", "storage", "database", "array", "index", "object", '$filter'];
|
|
448
461
|
|
|
449
462
|
for (const [keyString, sortedKey] of keys.entries()) {
|
|
450
463
|
let hasMatch = true;
|
|
@@ -488,10 +501,17 @@ function getData(form) {
|
|
|
488
501
|
let formObject = forms.get(form)
|
|
489
502
|
for (let type of formObject.types.values()) {
|
|
490
503
|
for (let [element, data] of type.entries()) {
|
|
504
|
+
if (!element.hasAttribute('key'))
|
|
505
|
+
continue
|
|
506
|
+
if (element.hasAttribute('actions')) {
|
|
507
|
+
let attribute = element.getAttribute('actions')
|
|
508
|
+
if (attribute.includes('save', 'delete'))
|
|
509
|
+
continue
|
|
510
|
+
}
|
|
491
511
|
let Data = { ...data }
|
|
492
512
|
let dataKey = elements.get(element)
|
|
493
513
|
let value = element.getValue()
|
|
494
|
-
console.log(type, value, data)
|
|
514
|
+
// console.log(type, value, data)
|
|
495
515
|
|
|
496
516
|
if (!Data[Data.type] && Data.key) {
|
|
497
517
|
Data.method = 'create.' + Data.type
|
|
@@ -507,19 +527,29 @@ function getData(form) {
|
|
|
507
527
|
|
|
508
528
|
} else if (Data[Data.type] && Data.key) {
|
|
509
529
|
let attributes = element.attributes
|
|
510
|
-
if (
|
|
530
|
+
if (Data.key.startsWith('$')) {
|
|
511
531
|
for (let i = 0; i < attributes.length; i++) {
|
|
512
532
|
let operators = ['$rename', '$inc', '$push', '$each', '$splice', '$unset', '$delete', '$slice', '$pop', '$shift', '$addToSet', '$pull']
|
|
513
|
-
if (
|
|
533
|
+
if (operators.includes(attributes[i].name)) {
|
|
514
534
|
Data.key = attributes[i].name + '.' + Data.key
|
|
515
535
|
break;
|
|
516
536
|
}
|
|
517
537
|
}
|
|
518
538
|
}
|
|
519
539
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
540
|
+
|
|
541
|
+
if (Data.type = 'object') {
|
|
542
|
+
if (typeof Data[Data.type] === 'string')
|
|
543
|
+
if (Data.key == '{}')
|
|
544
|
+
Data[Data.type] = { _id: Data[Data.type], ...value }
|
|
545
|
+
else
|
|
546
|
+
Data[Data.type] = { _id: Data[Data.type], [Data.key]: value }
|
|
547
|
+
else if (typeof Data[Data.type] === 'object')
|
|
548
|
+
if (Data.key == '{}')
|
|
549
|
+
Data[Data.type] = { ...Data[Data.type], ...value }
|
|
550
|
+
else
|
|
551
|
+
Data[Data.type][Data.key] = value
|
|
552
|
+
} else {
|
|
523
553
|
Data[Data.type] = { [Data[Data.type]]: value }
|
|
524
554
|
}
|
|
525
555
|
|
|
@@ -556,22 +586,27 @@ function getData(form) {
|
|
|
556
586
|
|
|
557
587
|
//dataKey should be used to group
|
|
558
588
|
let key = dataKey + Data.method
|
|
559
|
-
if (dataKeys.has(key))
|
|
560
|
-
dataKeys.get(key).
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
589
|
+
if (dataKeys.has(key)) {
|
|
590
|
+
let storedData = dataKeys.get(key)[Data.type]
|
|
591
|
+
dataKeys.get(key)[Data.type] = { ...storedData, ...Data[Data.type] }
|
|
592
|
+
} else {
|
|
593
|
+
dataKeys.set(key, Data)
|
|
594
|
+
}
|
|
564
595
|
}
|
|
565
596
|
}
|
|
566
597
|
|
|
567
598
|
// TODO: group by methods so we can make one crud request per method
|
|
568
|
-
let dataArray =
|
|
599
|
+
let dataArray = Array.from(dataKeys.values())
|
|
600
|
+
|
|
569
601
|
return dataArray
|
|
570
602
|
}
|
|
571
603
|
|
|
572
604
|
async function save(element) {
|
|
573
605
|
if (!element) return;
|
|
574
606
|
let data, value
|
|
607
|
+
let upsert = element.getAttribute('upsert')
|
|
608
|
+
if (upsert !== undefined || upsert !== null)
|
|
609
|
+
upsert = true
|
|
575
610
|
if (element.tagName === "FORM") {
|
|
576
611
|
data = element.getData()
|
|
577
612
|
} else {
|
|
@@ -622,12 +657,40 @@ async function save(element) {
|
|
|
622
657
|
data = [data]
|
|
623
658
|
}
|
|
624
659
|
|
|
660
|
+
|
|
661
|
+
|
|
625
662
|
for (let i = 0; i < data.length; i++) {
|
|
626
|
-
data[i].
|
|
663
|
+
if (data[i].type === 'object' && !data[i].method) {
|
|
664
|
+
if (typeof data[i].object === 'string') {
|
|
665
|
+
if (!data[i]._id)
|
|
666
|
+
data[i].method = 'create.object'
|
|
667
|
+
else {
|
|
668
|
+
data[i].method = 'update.object'
|
|
669
|
+
if (upsert)
|
|
670
|
+
data[i].upsert = true
|
|
671
|
+
}
|
|
672
|
+
} else if (Array.isArray(data[i].object)) {
|
|
673
|
+
if (!data[i].object[0]._id)
|
|
674
|
+
data[i].method = 'create.object'
|
|
675
|
+
else {
|
|
676
|
+
data[i].method = 'update.object'
|
|
677
|
+
if (upsert)
|
|
678
|
+
data[i].upsert = true
|
|
679
|
+
}
|
|
680
|
+
} else if (typeof data[i].object === 'object') {
|
|
681
|
+
if (!data[i].object._id)
|
|
682
|
+
data[i].method = 'create.object'
|
|
683
|
+
else {
|
|
684
|
+
data[i].method = 'update.object'
|
|
685
|
+
if (upsert)
|
|
686
|
+
data[i].upsert = true
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
627
690
|
|
|
628
|
-
if (data[i].method && data[i].method.startsWith('create'))
|
|
691
|
+
if (data[i].method && data[i].method.startsWith('create')) {
|
|
629
692
|
element.setAttribute(data[i].type, 'pending');
|
|
630
|
-
else if (data[i].method && data[i].method.startsWith('update') && data[i].type == 'object' && typeof value == 'string' && window.CoCreate.crdt && !'crdt') {
|
|
693
|
+
} else if (data[i].method && data[i].method.startsWith('update') && data[i].type == 'object' && typeof value == 'string' && window.CoCreate.crdt && !'crdt') {
|
|
631
694
|
return window.CoCreate.crdt.replaceText({
|
|
632
695
|
array: data[i].array,
|
|
633
696
|
key: data[i].key,
|
|
@@ -640,7 +703,10 @@ async function save(element) {
|
|
|
640
703
|
|
|
641
704
|
if (data[i] && (data[i].method.startsWith('create') || data[i].type !== 'object' && data[i].method.startsWith('update'))) {
|
|
642
705
|
setTypeValue(element, data[i])
|
|
643
|
-
}
|
|
706
|
+
} else if (data[i])
|
|
707
|
+
document.dispatchEvent(new CustomEvent('saved', {
|
|
708
|
+
detail: data[i]
|
|
709
|
+
}));
|
|
644
710
|
|
|
645
711
|
}
|
|
646
712
|
|
|
@@ -731,19 +797,46 @@ function setTypeValue(element, data) {
|
|
|
731
797
|
// }
|
|
732
798
|
// }
|
|
733
799
|
}
|
|
800
|
+
|
|
801
|
+
document.dispatchEvent(new CustomEvent('saved', {
|
|
802
|
+
detail: data
|
|
803
|
+
}));
|
|
804
|
+
|
|
734
805
|
}
|
|
735
806
|
|
|
736
807
|
async function remove(element) {
|
|
737
808
|
if (element && !(element instanceof HTMLCollection) && !Array.isArray(element))
|
|
738
809
|
element = [element]
|
|
739
810
|
for (let i = 0; i < element.length; i++) {
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
let
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
811
|
+
if (element[i].tagName === 'FORM') {
|
|
812
|
+
let form = forms.get(element[i]).elements
|
|
813
|
+
for (let el of form.keys()) {
|
|
814
|
+
let key = elements.get(el)
|
|
815
|
+
if (key) {
|
|
816
|
+
keys.get(key).elements.delete(el)
|
|
817
|
+
elements.delete(el)
|
|
818
|
+
// debounce.delete(key)
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
let key = elements.get(element[i])
|
|
822
|
+
if (key) {
|
|
823
|
+
keys.get(key).elements.delete(element[i])
|
|
824
|
+
}
|
|
825
|
+
elements.delete(element[i])
|
|
826
|
+
forms.delete(element[i])
|
|
827
|
+
} else {
|
|
828
|
+
|
|
829
|
+
let key = elements.get(element[i])
|
|
830
|
+
if (key) {
|
|
831
|
+
keys.get(key).elements.delete(element[i])
|
|
832
|
+
elements.delete(element[i])
|
|
833
|
+
// debounce.delete(key)
|
|
834
|
+
let form = element[i].closest('form')
|
|
835
|
+
form = forms.get(form)
|
|
836
|
+
if (form) {
|
|
837
|
+
form.elements.delete(element[i])
|
|
838
|
+
}
|
|
839
|
+
}
|
|
747
840
|
}
|
|
748
841
|
}
|
|
749
842
|
}
|
|
@@ -768,7 +861,11 @@ Observer.init({
|
|
|
768
861
|
observe: ['addedNodes'],
|
|
769
862
|
target: '[render-clone]',
|
|
770
863
|
callback: function (mutation) {
|
|
771
|
-
let
|
|
864
|
+
let delayTimer = debounce.get(mutation)
|
|
865
|
+
clearTimeout(delayTimer);
|
|
866
|
+
debounce.delete(mutation.target)
|
|
867
|
+
|
|
868
|
+
let renderedNode = render.renderedNodes.get(mutation.target)
|
|
772
869
|
if (!renderedNode) return
|
|
773
870
|
|
|
774
871
|
if (!mutation.movedFrom) return
|
|
@@ -911,7 +1008,15 @@ Observer.init({
|
|
|
911
1008
|
observe: ['removedNodes'],
|
|
912
1009
|
target: selector,
|
|
913
1010
|
callback: function (mutation) {
|
|
914
|
-
if (mutation.target.parentElement) return
|
|
1011
|
+
// if (mutation.target.parentElement) return
|
|
1012
|
+
// if (mutation.target.parentElement) {
|
|
1013
|
+
// let delayTimer = setTimeout(function () {
|
|
1014
|
+
// debounce.delete(mutation.target)
|
|
1015
|
+
// remove(mutation.target)
|
|
1016
|
+
// }, 3000);
|
|
1017
|
+
// debounce.set(mutation.target, delayTimer)
|
|
1018
|
+
// } else
|
|
1019
|
+
if (mutation.target.hasAttribute('render-clone')) return
|
|
915
1020
|
remove(mutation.target)
|
|
916
1021
|
}
|
|
917
1022
|
});
|
|
@@ -919,8 +1024,8 @@ Observer.init({
|
|
|
919
1024
|
Observer.init({
|
|
920
1025
|
name: 'CoCreateElementsAttributes',
|
|
921
1026
|
observe: ['attributes'],
|
|
922
|
-
attributeName:
|
|
923
|
-
target: selector,
|
|
1027
|
+
attributeName: ['storage', 'database', 'array', 'index', 'object', 'key'],
|
|
1028
|
+
// target: selector, // blocks mutations when applied
|
|
924
1029
|
callback: function (mutation) {
|
|
925
1030
|
remove(mutation.target)
|
|
926
1031
|
init([mutation.target]);
|
|
@@ -936,6 +1041,46 @@ Actions.init({
|
|
|
936
1041
|
}
|
|
937
1042
|
});
|
|
938
1043
|
|
|
1044
|
+
Actions.init({
|
|
1045
|
+
name: "delete",
|
|
1046
|
+
endEvent: "deleted",
|
|
1047
|
+
callback: async (action) => {
|
|
1048
|
+
let elements = queryElements({ element: action.element, prefix: 'delete' });
|
|
1049
|
+
if (elements === false)
|
|
1050
|
+
elements = [action.element]
|
|
1051
|
+
|
|
1052
|
+
for (let i = 0; i < elements.length; i++) {
|
|
1053
|
+
const data = getObject(elements[i]);
|
|
1054
|
+
if (!data) return
|
|
1055
|
+
data.method = 'delete.' + data.type
|
|
1056
|
+
|
|
1057
|
+
if (elements[i].renderValue) {
|
|
1058
|
+
let selected = elements[i].querySelectorAll('.selected')
|
|
1059
|
+
data[data.type] = []
|
|
1060
|
+
for (let j = 0; j < selected.length; j++) {
|
|
1061
|
+
let attribute = selected[j].getAttribute(data.type)
|
|
1062
|
+
if (attribute) {
|
|
1063
|
+
attribute = attribute.split(',')
|
|
1064
|
+
for (let k = 0; k < attribute.length; k++) {
|
|
1065
|
+
if (data.type === 'object')
|
|
1066
|
+
data[data.type].push({ _id: attribute[k] })
|
|
1067
|
+
else {
|
|
1068
|
+
data[data.type].push(attribute[k])
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
} else if (data.type === 'object' && typeof data[data.type] === 'string')
|
|
1074
|
+
data[data.type] = { _id: data[data.type] }
|
|
1075
|
+
|
|
1076
|
+
let response = await CRUD.send(data)
|
|
1077
|
+
document.dispatchEvent(new CustomEvent('deleted', {
|
|
1078
|
+
detail: response
|
|
1079
|
+
}));
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
});
|
|
1083
|
+
|
|
939
1084
|
init();
|
|
940
1085
|
|
|
941
|
-
export default { init, read, save, getData, reset, elements, keys, forms, debounce, getAttributes };
|
|
1086
|
+
export default { init, read, save, getData, getObject, reset, elements, keys, forms, debounce, getAttributes };
|