@cocreate/pwa 1.21.0 → 1.22.1

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,18 @@
1
+ ## [1.22.1](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.22.0...v1.22.1) (2025-04-11)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * dispatch end event on action element rather than document ([c14b2f3](https://github.com/CoCreate-app/CoCreate-pwa/commit/c14b2f3e92e50f3487aefd66a479e4222c835ab6))
7
+ * update observer observe param to type and and attributeName to attributeFilter ([5545765](https://github.com/CoCreate-app/CoCreate-pwa/commit/554576546507eda1c7577ae593e5c660572ba774))
8
+
9
+ # [1.22.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.21.0...v1.22.0) (2025-01-18)
10
+
11
+
12
+ ### Features
13
+
14
+ * geolocation intergration ([4bc95fb](https://github.com/CoCreate-app/CoCreate-pwa/commit/4bc95fb1192efec090da1a45698f347376427d44))
15
+
1
16
  # [1.21.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.20.0...v1.21.0) (2024-12-14)
2
17
 
3
18
 
@@ -0,0 +1,73 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Geolocation Web API Demo</title>
7
+ </head>
8
+ <body>
9
+ <form id="locationForm">
10
+ <input
11
+ geolocation="getCurrentPosition"
12
+ geolocation-key="coords.latitude"
13
+ placeholder="Latitude" />
14
+ <input
15
+ geolocation="getCurrentPosition"
16
+ geolocation-key="coords.longitude"
17
+ placeholder="Longitude" />
18
+ <input
19
+ geolocation="getCurrentPosition"
20
+ geolocation-key="coords.accuracy"
21
+ placeholder="Accuracy (meters)" />
22
+ <button actions="geoLocation.getCurrentPosition">
23
+ Get Location
24
+ </button>
25
+ </form>
26
+
27
+ <form id="trackLocationForm">
28
+ <input
29
+ geolocation="watchPosition"
30
+ geolocation-key="id"
31
+ placeholder="id"
32
+ input-next
33
+ input-attribute="value" />
34
+ <input
35
+ geolocation="clearWatch"
36
+ geolocation-key="id"
37
+ placeholder="id" />
38
+ <input
39
+ geolocation="watchPosition"
40
+ geolocation-key="coords.latitude"
41
+ placeholder="Latitude" />
42
+ <input
43
+ geolocation="watchPosition"
44
+ geolocation-key="coords.longitude"
45
+ placeholder="Longitude" />
46
+ <input
47
+ geolocation="watchPosition"
48
+ geolocation-key="coords.speed"
49
+ placeholder="Speed (m/s)" />
50
+ <button actions="geoLocation.watchPosition">Start Tracking</button>
51
+ <button actions="geoLocation.clearWatch">Stop Tracking</button>
52
+ </form>
53
+
54
+ <form id="errorForm">
55
+ <input
56
+ geolocation="getCurrentPosition"
57
+ geolocation-key="coords.latitude"
58
+ placeholder="Latitude" />
59
+ <input
60
+ geolocation="getCurrentPosition"
61
+ geolocation-key="coords.longitude"
62
+ placeholder="Longitude" />
63
+ <input
64
+ geolocation="getCurrentPosition"
65
+ geolocation-key="error.message"
66
+ placeholder="Error Message" />
67
+ <button actions="geoLocation.getCurrentPosition">
68
+ Get Location
69
+ </button>
70
+ </form>
71
+ <script src="./dist/CoCreate.js"></script>
72
+ </body>
73
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/pwa",
3
- "version": "1.21.0",
3
+ "version": "1.22.1",
4
4
  "description": "A simple pwa component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "pwa",
@@ -0,0 +1,92 @@
1
+ import Actions from "@cocreate/actions";
2
+ import Api from "@cocreate/api";
3
+
4
+ const geolocationHandlers = {
5
+ getCurrentPosition,
6
+ watchPosition,
7
+ clearWatch
8
+ };
9
+
10
+ // Get Current Position
11
+ async function getCurrentPosition(action) {
12
+ if (action.element) action.options = await Api.getData(action);
13
+
14
+ navigator.geolocation.getCurrentPosition(
15
+ (position) => {
16
+ action.data = {
17
+ geoLocation: { coords: position.coords }
18
+ };
19
+ Api.setData(action);
20
+ dispatchEvent(action);
21
+ },
22
+ (error) => handleError(error, action),
23
+ action.options
24
+ );
25
+ }
26
+
27
+ // Watch Position
28
+ async function watchPosition(action) {
29
+ if (action.element) action.options = await Api.getData(action);
30
+
31
+ const watchId = navigator.geolocation.watchPosition(
32
+ (position) => {
33
+ // Use a closure to access `watchId`
34
+ action.data = {
35
+ geoLocation: {
36
+ id: watchId, // Access `watchId` from outer scope
37
+ coords: position.coords
38
+ }
39
+ };
40
+
41
+ Api.setData(action);
42
+ dispatchEvent(action);
43
+ },
44
+ (error) => handleError(error, action),
45
+ action.options
46
+ );
47
+ }
48
+
49
+ // Clear Watch
50
+ async function clearWatch(action) {
51
+ if (action.element) action.options = await Api.getData(action);
52
+
53
+ if (action.options.id) {
54
+ navigator.geolocation.clearWatch(Number(action.options.id));
55
+ dispatchEvent(action);
56
+ }
57
+ }
58
+
59
+ // Handle Errors
60
+ function handleError(error, action) {
61
+ if (!action.data) action.data = { error };
62
+ else action.data.error = error;
63
+ console.error(`Geolocation error: ${error.message}`);
64
+ Api.setData(action);
65
+ }
66
+
67
+ function dispatchEvent(action) {
68
+ action.element.dispatchEvent(
69
+ new CustomEvent(action.endEvent, {
70
+ detail: {
71
+ data: action
72
+ }
73
+ })
74
+ );
75
+ }
76
+
77
+ // Actions Integration
78
+ Actions.init([
79
+ {
80
+ name: "geoLocation",
81
+ endEvent: "geolocation",
82
+ callback(action) {
83
+ geolocationHandlers[action.method](action);
84
+ }
85
+ }
86
+ ]);
87
+
88
+ export default {
89
+ getCurrentPosition,
90
+ watchPosition,
91
+ clearWatch
92
+ };
package/src/index.js CHANGED
@@ -25,6 +25,7 @@
25
25
  import observer from "@cocreate/observer";
26
26
  import localStorage from "@cocreate/local-storage";
27
27
  import "./web-share";
28
+ import "./geolocation";
28
29
  // import "./theme";
29
30
 
30
31
  /**
@@ -144,7 +145,7 @@ async function persistData() {
144
145
 
145
146
  observer.init({
146
147
  name: "initPwa",
147
- observe: ["addedNodes"],
148
+ types: ["addedNodes"],
148
149
  selector: '[actions="install"]',
149
150
  callback: (mutation) => {
150
151
  init();
package/src/web-share.js CHANGED
@@ -23,60 +23,68 @@
23
23
  * For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
24
24
  */
25
25
 
26
- import Observer from '@cocreate/observer'
27
- import Actions from '@cocreate/actions'
26
+ import Observer from "@cocreate/observer";
27
+ import Actions from "@cocreate/actions";
28
28
 
29
29
  const selector = "[share]";
30
30
 
31
31
  async function init(element) {
32
- if (element && !(element instanceof HTMLCollection) && !Array.isArray(element))
33
- element = [element]
34
- else if (!element) {
35
- element = document.querySelectorAll(selector)
36
- }
37
-
38
- for (let i = 0; i < element.length; i++)
39
- element[i].addEventListener('click', function (e) {
40
- share(e.target);
41
- });
32
+ if (
33
+ element &&
34
+ !(element instanceof HTMLCollection) &&
35
+ !Array.isArray(element)
36
+ )
37
+ element = [element];
38
+ else if (!element) {
39
+ element = document.querySelectorAll(selector);
40
+ }
42
41
 
42
+ for (let i = 0; i < element.length; i++)
43
+ element[i].addEventListener("click", function (e) {
44
+ share(e.target);
45
+ });
43
46
  }
44
47
 
45
48
  function share(element) {
46
- if (navigator.share && element) {
47
- let title = element.getAttribute('share-title') || document.title;
48
- let text = element.getAttribute('share-text') || document.querySelector('meta[name="description"]')?.content || '';
49
- let url = element.getAttribute('share-url') || window.location.href;
49
+ if (navigator.share && element) {
50
+ let title = element.getAttribute("share-title") || document.title;
51
+ let text =
52
+ element.getAttribute("share-text") ||
53
+ document.querySelector('meta[name="description"]')?.content ||
54
+ "";
55
+ let url = element.getAttribute("share-url") || window.location.href;
50
56
 
51
- navigator.share({
52
- title: title,
53
- text: text,
54
- url: url
55
- })
56
- .then(() => console.log('Successful share'))
57
- .catch((error) => console.log('Error sharing', error));
58
- } else {
59
- console.log('Web Share API is not supported or element is not provided.');
60
- }
57
+ navigator
58
+ .share({
59
+ title: title,
60
+ text: text,
61
+ url: url
62
+ })
63
+ .then(() => console.log("Successful share"))
64
+ .catch((error) => console.log("Error sharing", error));
65
+ } else {
66
+ console.log(
67
+ "Web Share API is not supported or element is not provided."
68
+ );
69
+ }
61
70
  }
62
71
 
63
72
  Observer.init({
64
- name: 'webshare',
65
- observe: ['addedNodes'],
66
- selector: '[share]',
67
- callback: (mutation) => {
68
- init(mutation.target)
69
- }
73
+ name: "webshare",
74
+ types: ["addedNodes"],
75
+ selector: "[share]",
76
+ callback: (mutation) => {
77
+ init(mutation.target);
78
+ }
70
79
  });
71
80
 
72
81
  Actions.init({
73
- name: "share",
74
- callback: (action) => {
75
- share(action.element);
76
- }
77
- })
78
-
79
- init()
82
+ name: "share",
83
+ callback: (action) => {
84
+ share(action.element);
85
+ }
86
+ });
80
87
 
81
- export default { init }
88
+ init();
82
89
 
90
+ export default { init };