releasebird-javascript-sdk 1.0.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.
@@ -0,0 +1,300 @@
1
+ import RbirdUtils from "./RbirdUtils";
2
+ import ReleasebirdImageViewer from "./ReleasebirdImageViewer";
3
+ import {
4
+ hideWidgetImage,
5
+ launcher0,
6
+ launcher1,
7
+ launcher2,
8
+ launcher3, launcher4,
9
+ launcher5,
10
+ launcher5Open,
11
+ launcherOpen
12
+ } from "./Styles";
13
+ import RbirdSessionManager from "./RbirdSessionManager";
14
+ import RbirdScreenshotManager from "./RbirdScreenshotManager";
15
+ import RbirdVideoRecorder from "./RbirdVideoRecorder";
16
+
17
+ export default class RbirdWebsiteWidget {
18
+
19
+ websiteWidget;
20
+
21
+ websiteWidgetVisible = false;
22
+
23
+ static instance;
24
+
25
+ widgetContent;
26
+
27
+ countBadge;
28
+
29
+ escListener;
30
+
31
+ iframe;
32
+
33
+ hideWidgetButton;
34
+
35
+ static getInstance() {
36
+ if (!this.instance) {
37
+ this.instance = new RbirdWebsiteWidget();
38
+ }
39
+ return this.instance;
40
+ }
41
+
42
+ isMobileDevice = () => {
43
+ const mobileMaxWidth = 768; // Definiert die maximale Breite für mobile Geräte, z.B. Tablets und Smartphones
44
+ return window.innerWidth <= mobileMaxWidth;
45
+ }
46
+
47
+ renderWebsiteWidget() {
48
+ if (this.websiteWidgetVisible) {
49
+ return;
50
+ }
51
+ this.websiteWidgetVisible = true;
52
+
53
+ let elem = document.createElement("div");
54
+ elem.onclick = () => {
55
+ this.openWebsiteWidget();
56
+ };
57
+ document.body.appendChild(elem);
58
+ this.websiteWidget = elem;
59
+
60
+ if (RbirdSessionManager.getInstance().widgetSettings?.allowClose) {
61
+ this.hideWidgetButton = document.createElement("div");
62
+ this.hideWidgetButton.className = "hideWidgetButton";
63
+ this.hideWidgetButton.innerHTML = hideWidgetImage;
64
+ this.hideWidgetButton.onclick = () => this.handleHideWidgetButton();
65
+ this.hideWidgetButton.style.display = "block";
66
+ document.body.appendChild(this.hideWidgetButton);
67
+ }
68
+
69
+ this.countBadge = document.createElement("span");
70
+ this.countBadge.className = "rbird_badge";
71
+ this.countBadge.style.display = "none";
72
+ document.body.appendChild(this.countBadge);
73
+
74
+ this.styleWidget();
75
+ }
76
+
77
+ isOpen() {
78
+ return RbirdUtils.hasClass(this.widgetContent, "cta__modal--visible");
79
+ }
80
+
81
+ openWebsiteWidget() {
82
+ RbirdUtils.addClass(this.widgetContent, "cta__modal--visible");
83
+ this.registerListeners();
84
+ if (this.hideWidgetButton) {
85
+ this.hideWidgetButton.style.display = "none";
86
+ }
87
+
88
+ if (this.isMobileDevice()) {
89
+ this.hideWidgetButton.style.display = 'none';
90
+ this.websiteWidget.style.display = 'none';
91
+ }
92
+
93
+ if (RbirdSessionManager.getInstance().widgetSettings.launcher === 5) {
94
+ this.websiteWidget.innerHTML = launcher5Open(RbirdSessionManager.getInstance().widgetSettings.launcherColor);
95
+ } else {
96
+ this.websiteWidget.innerHTML = launcherOpen(RbirdSessionManager.getInstance().widgetSettings.launcherColor);
97
+ }
98
+
99
+ this.websiteWidget.onclick = () => this.closeWebsiteWidget();
100
+
101
+ const http = new XMLHttpRequest();
102
+ http.open("POST", `${API}/track/${RbirdSessionManager.getInstance().widgetSettings.subdomain}/trackView?type=WIDGET`);
103
+ http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
104
+ http.setRequestHeader("timezone", Intl.DateTimeFormat().resolvedOptions().timeZone);
105
+ http.onerror = (error) => {
106
+ //handle error
107
+ };
108
+ http.onreadystatechange = function (e) {
109
+ if (http.readyState === 4) {
110
+ if (http.status === 200 || http.status === 201) {
111
+ try {
112
+ //handle success
113
+ } catch (exp) {
114
+ //handle error
115
+ }
116
+ } else {
117
+ //handle error
118
+ }
119
+ }
120
+ };
121
+ http.send(JSON.stringify({}));
122
+ }
123
+
124
+ handleHideWidgetButton() {
125
+ if (this.hideWidgetButton) {
126
+ this.hideWidgetButton.style.display = 'none';
127
+ this.websiteWidget.style.display = 'none';
128
+ }
129
+ }
130
+
131
+ countNotifications() {
132
+ if (RbirdSessionManager.getInstance().identify?.people || RbirdSessionManager.getInstance().anonymousIdentifier) {
133
+ const http = new XMLHttpRequest();
134
+ http.open("GET", `${API}/ewidget/unread`);
135
+ http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
136
+ http.setRequestHeader("apiKey", RbirdSessionManager.getInstance().apiKey);
137
+ http.setRequestHeader("peopleId", RbirdSessionManager.getInstance().identify.people);
138
+ http.setRequestHeader("ai", RbirdSessionManager.getInstance().anonymousIdentifier);
139
+ const lastView = window.localStorage.getItem("lastPageView");
140
+ http.onerror = function () {
141
+ //handle error
142
+ };
143
+ const that = this;
144
+ http.onreadystatechange = function (e) {
145
+ if (http.readyState === XMLHttpRequest.DONE) {
146
+ if (http.status === 200 || http.status === 201) {
147
+ try {
148
+ let response = JSON.parse(http.responseText);
149
+ //const lastView = getValue("lastPageView");
150
+ if (response.messageCount > 0) {
151
+ that.addBadgeCount(response.messageCount);
152
+ } else if (response.lastReleaseNote && lastView < response.lastReleaseNote) {
153
+ that.addBadgeCount("1");
154
+ } else {
155
+ that.addBadgeCount(0);
156
+ }
157
+ } catch (e) {
158
+ console.error(e);
159
+ }
160
+ }
161
+ //reject();
162
+ }
163
+ };
164
+ http.send();
165
+ }
166
+ }
167
+
168
+ addBadgeCount(count) {
169
+ if (count === 0) {
170
+ if (this.countBadge) {
171
+ this.countBadge.innerText = "0";
172
+ this.countBadge.style.display = "none";
173
+ if (!this.isOpen() && this.hideWidgetButton) {
174
+ this.hideWidgetButton.style.display = "block";
175
+ }
176
+ }
177
+ } else {
178
+ this.countBadge.innerText = count;
179
+ this.countBadge.style.display = "block";
180
+ if (this.hideWidgetButton) {
181
+ this.hideWidgetButton.style.display = "none";
182
+ }
183
+
184
+ }
185
+ }
186
+
187
+ closeWebsiteWidget() {
188
+ if (this.iframe) {
189
+ this.iframe.contentWindow?.postMessage({
190
+ type: 'close',
191
+ }, '*');
192
+ }
193
+ if (this.hideWidgetButton && this.countBadge.style.display === "none") {
194
+ this.hideWidgetButton.style.display = "block";
195
+ }
196
+ RbirdUtils.removeClass(this.widgetContent, "cta__modal--visible");
197
+
198
+ this.initButton();
199
+ this.websiteWidget.style.display = 'block';
200
+
201
+ this.websiteWidget.onclick = () => this.openWebsiteWidget();
202
+ this.unregisterListeners();
203
+ }
204
+
205
+ initButton() {
206
+ if (RbirdSessionManager.getInstance().widgetSettings.launcher === 0) {
207
+ this.websiteWidget.innerHTML = launcher0(RbirdSessionManager.getInstance().widgetSettings.launcherColor);
208
+ } else if (RbirdSessionManager.getInstance().widgetSettings.launcher === 1) {
209
+ this.websiteWidget.innerHTML = launcher1(RbirdSessionManager.getInstance().widgetSettings.launcherColor);
210
+ } else if (RbirdSessionManager.getInstance().widgetSettings.launcher === 2) {
211
+ this.websiteWidget.innerHTML = launcher2(RbirdSessionManager.getInstance().widgetSettings.launcherColor);
212
+ } else if (RbirdSessionManager.getInstance().widgetSettings.launcher === 3) {
213
+ this.websiteWidget.innerHTML = launcher3(RbirdSessionManager.getInstance().widgetSettings.launcherColor);
214
+ } else if (RbirdSessionManager.getInstance().widgetSettings.launcher === 4) {
215
+ this.websiteWidget.innerHTML = launcher4(RbirdSessionManager.getInstance().widgetSettings.launcherColor);
216
+ } else if (RbirdSessionManager.getInstance().widgetSettings.launcher === 5) {
217
+ this.websiteWidget.innerHTML = launcher5(RbirdSessionManager.getInstance().widgetSettings.launcherColor);
218
+ }
219
+ }
220
+
221
+ handleEvent(e, that) {
222
+ if (e.data === 'close') {
223
+ that.closeWebsiteWidget();
224
+ }
225
+ if (e.data === 'increaseSize') {
226
+ RbirdUtils.removeClass(that.widgetContent, "cta__modal--standard");
227
+ RbirdUtils.addClass(that.widgetContent, "cta__modal--big");
228
+ }
229
+ if (e.data === 'decreaseSize') {
230
+ RbirdUtils.removeClass(that.widgetContent, "cta__modal--big");
231
+ RbirdUtils.addClass(that.widgetContent, "cta__modal--standard");
232
+ }
233
+
234
+ if (e.data?.key === 'showImage') {
235
+ ReleasebirdImageViewer.showImage(e.data?.url);
236
+ }
237
+
238
+ if (e.data === 'newMessageArrived') {
239
+ this.countNotifications();
240
+ if (!RbirdUtils.hasClass(this.widgetContent, 'cta__modal--visible')) {
241
+ if (this.iframe) {
242
+ this.iframe.contentWindow?.postMessage({
243
+ type: 'showMessageTab',
244
+ }, '*');
245
+ }
246
+ }
247
+ }
248
+
249
+ if (e.data === 'start_screenshot') {
250
+ RbirdScreenshotManager.getInstance().activateMarker(true);
251
+ }
252
+
253
+ if (e.data === 'start_recording') {
254
+ RbirdVideoRecorder.getInstance().startRecording(false);
255
+ }
256
+ }
257
+
258
+ styleWidget() {
259
+ this.websiteWidget.className=RbirdSessionManager.getInstance().widgetSettings.launcher === 5 ? 'launcherButton5' : 'launcherButton' ;
260
+ this.initButton();
261
+ this.widgetContent = document.createElement("div");
262
+ this.widgetContent.className = "cta__modal";
263
+ this.updateIframe(`${CONTENT_URL}/widget?apiKey=${RbirdSessionManager.getInstance().apiKey}&tab=HOME&ai=${RbirdSessionManager.getInstance().anonymousIdentifier}`);
264
+ document.body.appendChild(this.widgetContent);
265
+ window.onmessage = (e) => this.handleEvent(e, this);
266
+
267
+ this.countNotifications();
268
+ }
269
+
270
+ updateIframe(iframeSrc) {
271
+ this.widgetContent.innerHTML = `<iframe src=${iframeSrc} class="rbird-content-iframe" scrolling="yes" title="Releasebird" allow="autoplay; encrypted-media; fullscreen;" frameborder="0"></iframe>`;
272
+ this.iframe = this.widgetContent.firstChild;
273
+ this.iframe.scrolling="no";
274
+ }
275
+
276
+ unregisterListeners() {
277
+ if (this.escListener) {
278
+ document.removeEventListener("keydown", this.escListener);
279
+ }
280
+ }
281
+
282
+ registerListeners() {
283
+ const that = this;
284
+
285
+ // Esc listener
286
+ this.escListener = function (evt) {
287
+ evt = evt || window.event;
288
+ let isEscape = false;
289
+ if ("key" in evt) {
290
+ isEscape = evt.key === "Escape";
291
+ }
292
+ if (isEscape) {
293
+ that.closeWebsiteWidget();
294
+ }
295
+ };
296
+ document.addEventListener("keydown", this.escListener);
297
+ }
298
+
299
+
300
+ }
@@ -0,0 +1,24 @@
1
+ export default class ReleasebirdImageViewer {
2
+
3
+ static showImage(imageUrl) {
4
+ let elem = document.createElement("div");
5
+ elem.id="myModal";
6
+ elem.className="modalImageViewer";
7
+
8
+ let spanElem = document.createElement('span');
9
+ spanElem.className="rbird_close";
10
+ spanElem.onclick= () => document.body.removeChild(elem);
11
+ spanElem.textContent = 'x';
12
+
13
+ let img = document.createElement("img");
14
+ img.className = "modal-content-imageViewer";
15
+ img.src=imageUrl;
16
+
17
+ elem.appendChild(spanElem);
18
+ elem.appendChild(img);
19
+
20
+ document.body.appendChild(elem);
21
+
22
+ }
23
+
24
+ }