@cocreate/elements 1.7.5 → 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 +7 -0
- package/package.json +1 -1
- package/src/fetchSrc.js +56 -0
- package/src/index.js +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
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
|
+
|
|
1
8
|
## [1.7.5](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.7.4...v1.7.5) (2022-06-02)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/elements",
|
|
3
|
-
"version": "1.
|
|
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",
|
package/src/fetchSrc.js
ADDED
|
@@ -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,6 +3,7 @@ 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]";
|
|
8
9
|
const initializing = new Map();
|