@cocreate/lazy-loader 1.23.0 → 1.23.2

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,17 @@
1
+ ## [1.23.2](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.23.1...v1.23.2) (2024-12-22)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * if (!window.CoCreate[name]) ([06f52b2](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/06f52b2f203d3860db065a786c095e8900d41508))
7
+
8
+ ## [1.23.1](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.23.0...v1.23.1) (2024-12-09)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * formating ([45845ab](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/45845abc2c4371b0952c5b747df10b55f3e90d6f))
14
+
1
15
  # [1.23.0](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.22.1...v1.23.0) (2024-11-04)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/lazy-loader",
3
- "version": "1.23.0",
3
+ "version": "1.23.2",
4
4
  "description": "A simple lazy-loader component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "lazy-loader",
package/src/client.js CHANGED
@@ -1,80 +1,75 @@
1
- import observer from '@cocreate/observer';
1
+ import observer from "@cocreate/observer";
2
2
 
3
3
  function listen(name, callback, selector) {
4
+ async function observerCallback({ target }) {
5
+ if (!window.CoCreate) window.CoCreate = {};
4
6
 
5
- async function observerCallback({ target }) {
6
- if (!window.CoCreate)
7
- window.CoCreate = {}
7
+ if (window.CoCreate[name]) return;
8
+ window.CoCreate[name] = {};
9
+ observer.uninit(observerCallback);
8
10
 
9
- if (window.CoCreate[name])
10
- return
11
- window.CoCreate[name] = {}
12
- observer.uninit(observerCallback)
11
+ const module = await callback();
12
+ observer.uninit(observerCallback);
13
+ window.CoCreate[name] = module.default || module;
13
14
 
14
- const module = await callback()
15
- observer.uninit(observerCallback)
16
- window.CoCreate[name] = module.default || module
15
+ dispatchComponentLoaded(name);
16
+ }
17
17
 
18
- dispatchComponentLoaded(name)
19
- }
20
-
21
- observer.init({
22
- name: 'lazyloadObserver',
23
- observe: ['childList'],
24
- selector,
25
- callback: observerCallback
26
- })
27
-
28
- let selectorAttributes = [];
29
- let attributes = selector.split(",")
30
- for (let attribute of attributes) {
31
- let attr = attribute.trim()
32
- if (attr.startsWith("[")) {
33
- let pos = attr.indexOf("*")
34
- if (pos == -1)
35
- pos = attr.indexOf("=")
36
- if (pos !== -1) {
37
- attr = attr.slice(1, pos)
38
- } else {
39
- attr = attr.slice(1, -1)
40
- }
41
- selectorAttributes.push(attr)
42
- }
43
-
44
- }
45
- if (selectorAttributes.length > 0)
46
- observer.init({
47
- name: 'lazyloadAttributeObserver',
48
- observe: ['attributes'],
49
- attributeName: selectorAttributes,
50
- selector,
51
- callback: observerCallback
52
- });
18
+ observer.init({
19
+ name: "lazyloadObserver",
20
+ observe: ["childList"],
21
+ selector,
22
+ callback: observerCallback
23
+ });
53
24
 
25
+ let selectorAttributes = [];
26
+ let attributes = selector.split(",");
27
+ for (let attribute of attributes) {
28
+ let attr = attribute.trim();
29
+ if (attr.startsWith("[")) {
30
+ let pos = attr.indexOf("*");
31
+ if (pos == -1) pos = attr.indexOf("=");
32
+ if (pos !== -1) {
33
+ attr = attr.slice(1, pos);
34
+ } else {
35
+ attr = attr.slice(1, -1);
36
+ }
37
+ selectorAttributes.push(attr);
38
+ }
39
+ }
40
+ if (selectorAttributes.length > 0)
41
+ observer.init({
42
+ name: "lazyloadAttributeObserver",
43
+ observe: ["attributes"],
44
+ attributeName: selectorAttributes,
45
+ selector,
46
+ callback: observerCallback
47
+ });
54
48
  }
55
49
 
56
50
  export async function lazyLoad(name, selector, callback) {
57
- if (document.querySelector(selector))
58
- await dependency(name, await callback())
59
- else
60
- listen(name, callback, selector)
51
+ if (document.querySelector(selector))
52
+ await dependency(name, await callback());
53
+ else listen(name, callback, selector);
61
54
  }
62
55
 
63
56
  export async function dependency(name, promise) {
64
- try {
65
- let component = await promise;
66
- if (!window.CoCreate)
67
- window.CoCreate = {}
68
-
69
- window.CoCreate[name] = component.default || component
70
- dispatchComponentLoaded(name)
71
- } catch (error) {
72
- console.error('error loading chunck: ', error)
73
- }
57
+ try {
58
+ let component = await promise;
59
+ if (!window.CoCreate) window.CoCreate = {};
60
+ if (!window.CoCreate[name]) {
61
+ window.CoCreate[name] = component.default || component;
62
+ dispatchComponentLoaded(name);
63
+ }
64
+ } catch (error) {
65
+ console.error("error loading chunck: ", error);
66
+ }
74
67
  }
75
68
 
76
69
  function dispatchComponentLoaded(name) {
77
- document.dispatchEvent(new CustomEvent(name + 'Loaded', {
78
- detail: { name }
79
- }));
70
+ document.dispatchEvent(
71
+ new CustomEvent(name + "Loaded", {
72
+ detail: { name }
73
+ })
74
+ );
80
75
  }