|
1 |
| -import Gleap, { GleapFeedbackButtonManager, GleapConfigManager, GleapFrameManager, GleapSession } from "./Gleap"; |
| 1 | +import Gleap, { |
| 2 | + GleapFeedbackButtonManager, |
| 3 | + GleapConfigManager, |
| 4 | + GleapFrameManager, |
| 5 | + GleapSession, |
| 6 | +} from "./Gleap"; |
2 | 7 | import { loadFromGleapCache, saveToGleapCache } from "./GleapHelper";
|
3 | 8 | import { loadIcon } from "./UI";
|
4 | 9 |
|
5 | 10 | export default class GleapNotificationManager {
|
6 |
| - notificationContainer = null; |
7 |
| - notifications = []; |
8 |
| - unreadCount = 0; |
9 |
| - unreadNotificationsKey = "unread-notifications"; |
10 |
| - |
11 |
| - // GleapNotificationManager singleton |
12 |
| - static instance; |
13 |
| - static getInstance() { |
14 |
| - if (!this.instance) { |
15 |
| - this.instance = new GleapNotificationManager(); |
16 |
| - } |
17 |
| - return this.instance; |
| 11 | + notificationContainer = null; |
| 12 | + notifications = []; |
| 13 | + unreadCount = 0; |
| 14 | + unreadNotificationsKey = "unread-notifications"; |
| 15 | + |
| 16 | + // GleapNotificationManager singleton |
| 17 | + static instance; |
| 18 | + static getInstance() { |
| 19 | + if (!this.instance) { |
| 20 | + this.instance = new GleapNotificationManager(); |
| 21 | + } |
| 22 | + return this.instance; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Injects the feedback button into the current DOM. |
| 27 | + */ |
| 28 | + injectNotificationUI() { |
| 29 | + if (this.notificationContainer) { |
| 30 | + return; |
18 | 31 | }
|
19 | 32 |
|
20 |
| - /** |
21 |
| - * Injects the feedback button into the current DOM. |
22 |
| - */ |
23 |
| - injectNotificationUI() { |
24 |
| - if (this.notificationContainer) { |
25 |
| - return; |
26 |
| - } |
27 |
| - |
28 |
| - var elem = document.createElement("div"); |
29 |
| - elem.className = "gleap-notification-container"; |
30 |
| - document.body.appendChild(elem); |
31 |
| - this.notificationContainer = elem; |
32 |
| - |
33 |
| - this.updateContainerStyle(); |
34 |
| - |
35 |
| - // Load persisted notifications. |
36 |
| - const notificationsFromCache = loadFromGleapCache(this.unreadNotificationsKey); |
37 |
| - if (notificationsFromCache && notificationsFromCache.length > 0) { |
38 |
| - this.notifications = notificationsFromCache; |
39 |
| - this.renderNotifications(); |
40 |
| - } |
| 33 | + var elem = document.createElement("div"); |
| 34 | + elem.className = "gleap-notification-container"; |
| 35 | + document.body.appendChild(elem); |
| 36 | + this.notificationContainer = elem; |
| 37 | + |
| 38 | + this.updateContainerStyle(); |
| 39 | + this.reloadNotificationsFromCache(); |
| 40 | + } |
| 41 | + |
| 42 | + reloadNotificationsFromCache() { |
| 43 | + // Load persisted notifications. |
| 44 | + const notificationsFromCache = loadFromGleapCache( |
| 45 | + this.unreadNotificationsKey |
| 46 | + ); |
| 47 | + if (notificationsFromCache && notificationsFromCache.length > 0) { |
| 48 | + this.notifications = notificationsFromCache; |
| 49 | + this.renderNotifications(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + setNotificationCount(unreadCount) { |
| 54 | + if (GleapFrameManager.getInstance().isOpened()) { |
| 55 | + this.unreadCount = 0; |
| 56 | + return; |
| 57 | + } else { |
| 58 | + this.unreadCount = unreadCount; |
41 | 59 | }
|
42 | 60 |
|
43 |
| - setNotificationCount(unreadCount) { |
44 |
| - if (GleapFrameManager.getInstance().isOpened()) { |
45 |
| - this.unreadCount = 0; |
46 |
| - return; |
47 |
| - } else { |
48 |
| - this.unreadCount = unreadCount; |
49 |
| - } |
| 61 | + // Update the badge counter. |
| 62 | + GleapFeedbackButtonManager.getInstance().updateNotificationBadge( |
| 63 | + this.unreadCount |
| 64 | + ); |
| 65 | + } |
50 | 66 |
|
51 |
| - // Update the badge counter. |
52 |
| - GleapFeedbackButtonManager.getInstance().updateNotificationBadge(this.unreadCount); |
| 67 | + showNotification(notification) { |
| 68 | + if (!(this.notificationContainer && notification && notification.data)) { |
| 69 | + return; |
53 | 70 | }
|
54 | 71 |
|
55 |
| - showNotification(notification) { |
56 |
| - if (!(this.notificationContainer && notification && notification.data)) { |
57 |
| - return; |
58 |
| - } |
| 72 | + const notificationsForOutbound = this.notifications.find( |
| 73 | + (e) => notification.outbound === e.outbound |
| 74 | + ); |
| 75 | + if (!notificationsForOutbound) { |
| 76 | + this.notifications.push(notification); |
| 77 | + } |
| 78 | + if (this.notifications.length > 3) { |
| 79 | + this.notifications.shift(); |
| 80 | + } |
59 | 81 |
|
60 |
| - const notificationsForOutbound = this.notifications.find((e) => notification.outbound === e.outbound); |
61 |
| - if (!notificationsForOutbound) { |
62 |
| - this.notifications.push(notification); |
63 |
| - } |
64 |
| - if (this.notifications.length > 2) { |
65 |
| - this.notifications.shift(); |
66 |
| - } |
| 82 | + // Persist notifications. |
| 83 | + saveToGleapCache(this.unreadNotificationsKey, this.notifications); |
67 | 84 |
|
68 |
| - // Persist notifications. |
69 |
| - saveToGleapCache(this.unreadNotificationsKey, this.notifications); |
| 85 | + this.renderNotifications(); |
| 86 | + } |
70 | 87 |
|
71 |
| - this.renderNotifications(); |
| 88 | + renderNotifications() { |
| 89 | + if (!this.notificationContainer) { |
| 90 | + return; |
72 | 91 | }
|
73 | 92 |
|
74 |
| - renderNotifications() { |
75 |
| - if (!this.notificationContainer) { |
76 |
| - return; |
| 93 | + // Clear the existing notifications. |
| 94 | + this.clearAllNotifications(true); |
| 95 | + |
| 96 | + // Append close button. |
| 97 | + const clearElem = document.createElement("div"); |
| 98 | + clearElem.onclick = () => { |
| 99 | + this.clearAllNotifications(); |
| 100 | + }; |
| 101 | + clearElem.className = "gleap-notification-close"; |
| 102 | + clearElem.innerHTML = loadIcon("dismiss"); |
| 103 | + this.notificationContainer.appendChild(clearElem); |
| 104 | + |
| 105 | + // Render the notifications. |
| 106 | + for (var i = 0; i < this.notifications.length; i++) { |
| 107 | + const notification = this.notifications[i]; |
| 108 | + |
| 109 | + var content = notification.data.text; |
| 110 | + |
| 111 | + // Try replacing the session name. |
| 112 | + content = content.replaceAll( |
| 113 | + "{{name}}", |
| 114 | + GleapSession.getInstance().getName() |
| 115 | + ); |
| 116 | + |
| 117 | + const elem = document.createElement("div"); |
| 118 | + elem.onclick = () => { |
| 119 | + if (notification.data.conversation) { |
| 120 | + Gleap.openConversation(notification.data.conversation.shareToken); |
| 121 | + } else if (notification.data.news) { |
| 122 | + Gleap.openNewsArticle(notification.data.news.id); |
| 123 | + } else { |
| 124 | + Gleap.open(); |
77 | 125 | }
|
78 |
| - |
79 |
| - // Clear the existing notifications. |
80 |
| - this.clearAllNotifications(true); |
81 |
| - |
82 |
| - // Append close button. |
83 |
| - const clearElem = document.createElement("div"); |
84 |
| - clearElem.onclick = () => { |
85 |
| - this.clearAllNotifications(); |
86 |
| - }; |
87 |
| - clearElem.className = "gleap-notification-close"; |
88 |
| - clearElem.innerHTML = loadIcon("dismiss"); |
89 |
| - this.notificationContainer.appendChild(clearElem); |
90 |
| - |
91 |
| - // Render the notifications. |
92 |
| - for (var i = 0; i < this.notifications.length; i++) { |
93 |
| - const notification = this.notifications[i]; |
94 |
| - |
95 |
| - var content = notification.data.text; |
96 |
| - |
97 |
| - // Try replacing the session name. |
98 |
| - content = content.replaceAll("{{name}}", GleapSession.getInstance().getName()); |
99 |
| - |
100 |
| - const elem = document.createElement("div"); |
101 |
| - elem.onclick = () => { |
102 |
| - if (notification.data.conversation) { |
103 |
| - Gleap.openConversation(notification.data.conversation.shareToken); |
104 |
| - } else if (notification.data.news) { |
105 |
| - Gleap.openNewsArticle(notification.data.news.id); |
106 |
| - } else { |
107 |
| - Gleap.open(); |
108 |
| - } |
109 |
| - }; |
110 |
| - elem.className = "gleap-notification-item"; |
111 |
| - elem.innerHTML = ` |
112 |
| - ${notification.data.sender && notification.data.sender.profileImageUrl && `<img src="${notification.data.sender.profileImageUrl}" />`} |
| 126 | + }; |
| 127 | + elem.className = "gleap-notification-item"; |
| 128 | + elem.innerHTML = ` |
| 129 | + ${ |
| 130 | + notification.data.sender && |
| 131 | + notification.data.sender.profileImageUrl && |
| 132 | + `<img src="${notification.data.sender.profileImageUrl}" />` |
| 133 | + } |
113 | 134 | <div class="gleap-notification-item-container">
|
114 |
| - ${notification.data.sender ? `<div class="gleap-notification-item-sender">${notification.data.sender.name}</div>` : ""} |
| 135 | + ${ |
| 136 | + notification.data.sender |
| 137 | + ? `<div class="gleap-notification-item-sender">${notification.data.sender.name}</div>` |
| 138 | + : "" |
| 139 | + } |
115 | 140 | <div class="gleap-notification-item-content">${content}</div>
|
116 | 141 | </div>`;
|
117 |
| - this.notificationContainer.appendChild(elem); |
118 |
| - } |
| 142 | + this.notificationContainer.appendChild(elem); |
119 | 143 | }
|
| 144 | + } |
120 | 145 |
|
121 |
| - clearAllNotifications(uiOnly = false) { |
122 |
| - if (!this.notificationContainer) { |
123 |
| - return; |
124 |
| - } |
| 146 | + clearAllNotifications(uiOnly = false) { |
| 147 | + if (!this.notificationContainer) { |
| 148 | + return; |
| 149 | + } |
125 | 150 |
|
126 |
| - if (!uiOnly) { |
127 |
| - this.notifications = []; |
128 |
| - saveToGleapCache(this.unreadNotificationsKey, this.notifications); |
129 |
| - } |
| 151 | + if (!uiOnly) { |
| 152 | + this.notifications = []; |
| 153 | + saveToGleapCache(this.unreadNotificationsKey, this.notifications); |
| 154 | + } |
130 | 155 |
|
131 |
| - while (this.notificationContainer.firstChild) { |
132 |
| - this.notificationContainer.removeChild(this.notificationContainer.firstChild); |
133 |
| - } |
| 156 | + while (this.notificationContainer.firstChild) { |
| 157 | + this.notificationContainer.removeChild( |
| 158 | + this.notificationContainer.firstChild |
| 159 | + ); |
134 | 160 | }
|
| 161 | + } |
135 | 162 |
|
136 |
| - updateContainerStyle() { |
137 |
| - if (!this.notificationContainer) { |
138 |
| - return; |
139 |
| - } |
| 163 | + updateContainerStyle() { |
| 164 | + if (!this.notificationContainer) { |
| 165 | + return; |
| 166 | + } |
140 | 167 |
|
141 |
| - const flowConfig = GleapConfigManager.getInstance().getFlowConfig(); |
142 |
| - const classLeft = "gleap-notification-container--left"; |
143 |
| - const classNoButton = "gleap-notification-container--no-button"; |
144 |
| - this.notificationContainer.classList.remove(classLeft); |
145 |
| - this.notificationContainer.classList.remove(classNoButton); |
146 |
| - if ( |
147 |
| - flowConfig.feedbackButtonPosition === GleapFeedbackButtonManager.FEEDBACK_BUTTON_CLASSIC_LEFT || |
148 |
| - flowConfig.feedbackButtonPosition === GleapFeedbackButtonManager.FEEDBACK_BUTTON_BOTTOM_LEFT |
149 |
| - ) { |
150 |
| - this.notificationContainer.classList.add(classLeft); |
151 |
| - } |
| 168 | + const flowConfig = GleapConfigManager.getInstance().getFlowConfig(); |
| 169 | + const classLeft = "gleap-notification-container--left"; |
| 170 | + const classNoButton = "gleap-notification-container--no-button"; |
| 171 | + this.notificationContainer.classList.remove(classLeft); |
| 172 | + this.notificationContainer.classList.remove(classNoButton); |
| 173 | + if ( |
| 174 | + flowConfig.feedbackButtonPosition === |
| 175 | + GleapFeedbackButtonManager.FEEDBACK_BUTTON_CLASSIC_LEFT || |
| 176 | + flowConfig.feedbackButtonPosition === |
| 177 | + GleapFeedbackButtonManager.FEEDBACK_BUTTON_BOTTOM_LEFT |
| 178 | + ) { |
| 179 | + this.notificationContainer.classList.add(classLeft); |
| 180 | + } |
152 | 181 |
|
153 |
| - if (GleapFeedbackButtonManager.getInstance().buttonHidden === null) { |
154 |
| - if (flowConfig.feedbackButtonPosition === GleapFeedbackButtonManager.FEEDBACK_BUTTON_NONE) { |
155 |
| - this.notificationContainer.classList.add(classNoButton); |
156 |
| - } |
157 |
| - } else { |
158 |
| - if (GleapFeedbackButtonManager.getInstance().buttonHidden) { |
159 |
| - this.notificationContainer.classList.add(classNoButton); |
160 |
| - } |
161 |
| - } |
| 182 | + if (GleapFeedbackButtonManager.getInstance().buttonHidden === null) { |
| 183 | + if ( |
| 184 | + flowConfig.feedbackButtonPosition === |
| 185 | + GleapFeedbackButtonManager.FEEDBACK_BUTTON_NONE |
| 186 | + ) { |
| 187 | + this.notificationContainer.classList.add(classNoButton); |
| 188 | + } |
| 189 | + } else { |
| 190 | + if (GleapFeedbackButtonManager.getInstance().buttonHidden) { |
| 191 | + this.notificationContainer.classList.add(classNoButton); |
| 192 | + } |
162 | 193 | }
|
| 194 | + } |
163 | 195 | }
|
0 commit comments