@cocreate/elements 1.7.3 → 1.8.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 CHANGED
@@ -1,3 +1,24 @@
1
+ # [1.8.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.7.5...v1.8.0) (2022-06-04)
2
+
3
+
4
+ ### Features
5
+
6
+ * supports adding src attribute to any element to fetch the file and render the results in the element ([0507159](https://github.com/CoCreate-app/CoCreate-elements/commit/0507159aa0a9311a2bbc7fd89e17b20d39d7622c))
7
+
8
+ ## [1.7.5](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.7.4...v1.7.5) (2022-06-02)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * add element to map to reduce repetitive reads. ([8d4e2e9](https://github.com/CoCreate-app/CoCreate-elements/commit/8d4e2e9d1c2a544e2eef8b72fbfe97a62bd7452f))
14
+
15
+ ## [1.7.4](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.7.3...v1.7.4) (2022-05-23)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * bump all dependencies ([c837d78](https://github.com/CoCreate-app/CoCreate-elements/commit/c837d78ef5ecd9b05d38b4b5553583ef6763e569))
21
+
1
22
  ## [1.7.3](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.7.2...v1.7.3) (2022-05-19)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/elements",
3
- "version": "1.7.3",
3
+ "version": "1.8.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",
@@ -61,9 +61,9 @@
61
61
  "webpack-log": "^3.0.1"
62
62
  },
63
63
  "dependencies": {
64
- "@cocreate/actions": "^1.3.33",
65
- "@cocreate/crud-client": "^1.4.47",
66
- "@cocreate/docs": "^1.2.69",
67
- "@cocreate/observer": "^1.4.4"
64
+ "@cocreate/actions": "^1.4.1",
65
+ "@cocreate/crud-client": "^1.6.2",
66
+ "@cocreate/docs": "^1.3.1",
67
+ "@cocreate/observer": "^1.5.1"
68
68
  }
69
69
  }
@@ -0,0 +1,56 @@
1
+ import observer from '@cocreate/observer';
2
+
3
+ const selector = '[src]:not(img, video, script, input, iframe, frame)'
4
+ const initializing = new Map();
5
+
6
+
7
+ function init() {
8
+ var elements = document.querySelectorAll(selector);
9
+ initElements(elements);
10
+ };
11
+
12
+ function initElements(elements) {
13
+ for (let element of elements)
14
+ initElement(element);
15
+ };
16
+
17
+ async function initElement(element) {
18
+ let src = element.getAttribute('src');
19
+ let initialize = initializing.get(element)
20
+ if (!initialize || initialize.src != src){
21
+ initializing.set(element, {src});
22
+ if (src)
23
+ try {
24
+ let response = await fetch(src); // Gets a promise
25
+ let text = await response.text(); // Replaces body with response
26
+ if (text){
27
+ element.innerHTML = text; // Replaces body with response
28
+ initializing.delete(el)
29
+ }
30
+ } catch (err) {
31
+ console.log('Fetch error:' + err); // Error handling
32
+ }
33
+ }
34
+ };
35
+
36
+ observer.init({
37
+ name: 'CoCreateSrc',
38
+ observe: ['addedNodes'],
39
+ target: selector,
40
+ callback: function(mutation) {
41
+ initElement(mutation.target);
42
+ }
43
+ });
44
+
45
+ observer.init({
46
+ name: 'CoCreateSrcAttributes',
47
+ observe: ['attributes'],
48
+ attributeName: ['src'],
49
+ target: selector,
50
+ callback: function(mutation) {
51
+ initElement(mutation.target);
52
+ }
53
+ });
54
+
55
+ init()
56
+
package/src/index.js CHANGED
@@ -3,8 +3,10 @@ import action from '@cocreate/actions';
3
3
  import crud from '@cocreate/crud-client';
4
4
  import { initSetValues, setValue } from './setValue';
5
5
  import { initGetValues, getValue } from './getValue';
6
+ import './fetchSrc';
6
7
 
7
8
  const selector = "[collection][document_id][name]:not(cocreate-select, link), input, textarea, select, [contenteditable]";
9
+ const initializing = new Map();
8
10
 
9
11
  function init() {
10
12
  let elements = document.querySelectorAll(selector);
@@ -19,9 +21,14 @@ function initElements(elements) {
19
21
  if (doc) {
20
22
  let collection = doc.collection;
21
23
  let document_id = doc.document_id;
22
- documents.set(`${collection}${document_id}`, {collection, document_id});
24
+ let initialize = initializing.get(element)
25
+ if (!initialize || initialize.collection != collection && initialize.document_id != document_id){
26
+ initializing.set(element, {collection, document_id});
27
+ documents.set(`${collection}${document_id}`, {collection, document_id});
28
+ }
23
29
  }
24
30
  }
31
+
25
32
  read(documents, elements);
26
33
  }
27
34
 
@@ -85,6 +92,8 @@ function setData(elements, data) {
85
92
 
86
93
  isRendered = true;
87
94
  }
95
+ initializing.delete(el)
96
+
88
97
  });
89
98
 
90
99
  if (isRendered) {