@geophrase/core 1.0.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/package.json +6 -0
- package/src/index.js +142 -0
package/package.json
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
class Geophrase {
|
|
2
|
+
constructor(options = {}) {
|
|
3
|
+
if (!options.key || typeof document === 'undefined') {
|
|
4
|
+
if (typeof document !== 'undefined') console.error("Geophrase: 'key' is required.");
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
this.apiKey = options.key;
|
|
9
|
+
this.orderId = options.order_id;
|
|
10
|
+
this.phone = options.phone;
|
|
11
|
+
|
|
12
|
+
this.onSuccess = options.onSuccess;
|
|
13
|
+
this.onError = options.onError;
|
|
14
|
+
this.onClose = options.onClose;
|
|
15
|
+
|
|
16
|
+
this.iframeId = 'geophrase-iframe-connect';
|
|
17
|
+
this.overlayId = 'geophrase-overlay-container';
|
|
18
|
+
this.styleId = 'geophrase-connect-styles';
|
|
19
|
+
this.widgetOrigin = 'https://connect.geophrase.com';
|
|
20
|
+
this.apiBase = 'https://api.geophrase.com';
|
|
21
|
+
|
|
22
|
+
this._boundHandleMessage = this._handleMessage.bind(this);
|
|
23
|
+
|
|
24
|
+
this._injectDOM();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
_injectDOM() {
|
|
28
|
+
if (document.getElementById(this.overlayId)) return;
|
|
29
|
+
|
|
30
|
+
if (!document.getElementById(this.styleId)) {
|
|
31
|
+
const style = document.createElement('style');
|
|
32
|
+
style.id = this.styleId;
|
|
33
|
+
style.innerHTML = `
|
|
34
|
+
#${this.overlayId} {
|
|
35
|
+
position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
|
|
36
|
+
background-color: rgba(0, 0, 0, 0.6); z-index: 999999;
|
|
37
|
+
display: flex; justify-content: center; align-items: center;
|
|
38
|
+
opacity: 0; visibility: hidden; transition: opacity 0.3s ease;
|
|
39
|
+
}
|
|
40
|
+
#${this.overlayId}.geophrase-active { opacity: 1; visibility: visible; }
|
|
41
|
+
#${this.iframeId} { width: 100%; height: 100%; border: none; background-color: #fff; }
|
|
42
|
+
@media (min-width: 768px) {
|
|
43
|
+
#${this.iframeId} {
|
|
44
|
+
width: 375px; height: 812px; max-height: 85vh;
|
|
45
|
+
border-radius: 20px; box-shadow: 0 15px 35px rgba(0, 0, 0, 0.25);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
`;
|
|
49
|
+
document.head.appendChild(style);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const overlay = document.createElement('div');
|
|
53
|
+
overlay.id = this.overlayId;
|
|
54
|
+
|
|
55
|
+
const iframe = document.createElement('iframe');
|
|
56
|
+
iframe.id = this.iframeId;
|
|
57
|
+
iframe.allow = "geolocation";
|
|
58
|
+
|
|
59
|
+
const url = new URL(this.widgetOrigin);
|
|
60
|
+
url.searchParams.append('api-key', this.apiKey);
|
|
61
|
+
if (this.orderId) url.searchParams.append('order-id', this.orderId);
|
|
62
|
+
if (this.phone) url.searchParams.append('phone', this.phone);
|
|
63
|
+
iframe.src = url.toString();
|
|
64
|
+
|
|
65
|
+
overlay.appendChild(iframe);
|
|
66
|
+
document.body.appendChild(overlay);
|
|
67
|
+
|
|
68
|
+
window.addEventListener('message', this._boundHandleMessage);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
open() {
|
|
72
|
+
document.body.style.overflow = 'hidden';
|
|
73
|
+
document.getElementById(this.overlayId)?.classList.add('geophrase-active');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
close() {
|
|
77
|
+
document.body.style.overflow = '';
|
|
78
|
+
document.getElementById(this.overlayId)?.classList.remove('geophrase-active');
|
|
79
|
+
|
|
80
|
+
const iframe = document.getElementById(this.iframeId);
|
|
81
|
+
if (iframe) iframe.src = iframe.src;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
destroy() {
|
|
85
|
+
const overlay = document.getElementById(this.overlayId);
|
|
86
|
+
if (overlay) overlay.remove();
|
|
87
|
+
|
|
88
|
+
document.body.style.overflow = '';
|
|
89
|
+
|
|
90
|
+
window.removeEventListener('message', this._boundHandleMessage);
|
|
91
|
+
|
|
92
|
+
this.onSuccess = null;
|
|
93
|
+
this.onError = null;
|
|
94
|
+
this.onClose = null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async _handleMessage(event) {
|
|
98
|
+
if (event.origin !== this.widgetOrigin) return;
|
|
99
|
+
|
|
100
|
+
if (event.data?.type === 'GEOPHRASE_CLOSE_WIDGET') {
|
|
101
|
+
this.close();
|
|
102
|
+
if (this.onClose) this.onClose();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (event.data?.type === 'GEOPHRASE_RESOLUTION_TOKEN') {
|
|
106
|
+
this.close();
|
|
107
|
+
try {
|
|
108
|
+
const response = await fetch(`${this.apiBase}/business/resolve/`, {
|
|
109
|
+
method: "POST",
|
|
110
|
+
headers: { "X-API-Key": this.apiKey, "Content-Type": "application/json" },
|
|
111
|
+
body: JSON.stringify({ token: event.data.token }),
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
const errorData = await response.json().catch(() => ({}));
|
|
116
|
+
if (this.onError) {
|
|
117
|
+
this.onError({
|
|
118
|
+
type: 'API_ERROR',
|
|
119
|
+
status: response.status,
|
|
120
|
+
message: errorData.message || 'Geophrase API resolution failed'
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const data = await response.json();
|
|
127
|
+
if (this.onSuccess) this.onSuccess(data);
|
|
128
|
+
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error("Geophrase Resolution Error:", error);
|
|
131
|
+
if (this.onError) {
|
|
132
|
+
this.onError({
|
|
133
|
+
type: 'NETWORK_ERROR',
|
|
134
|
+
message: error.message || 'Failed to connect to Geophrase API'
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export default Geophrase;
|