|
2 | 2 | let currentPageLink = null; |
3 | 3 | let getNotificationsStatusErrorShowed = false; |
4 | 4 | let lastFailedCountFetchNotificationNode = null; |
| 5 | + let selectedNotificationId = null; |
5 | 6 | const SELECTOR_MODAL_ITEM = '.ibexa-notifications-modal__item'; |
6 | 7 | const SELECTOR_MODAL_RESULTS = '.ibexa-notifications-modal__results .ibexa-scrollable-wrapper'; |
7 | 8 | const SELECTOR_MODAL_TITLE = '.ibexa-side-panel__header'; |
8 | 9 | const SELECTOR_LIST = '.ibexa-list--notifications'; |
9 | 10 | const CLASS_MODAL_LOADING = 'ibexa-notifications-modal--loading'; |
10 | 11 | const INTERVAL = 30000; |
11 | 12 | const panel = doc.querySelector('.ibexa-notifications-modal'); |
| 13 | + const deleteConfirmationModal = doc.querySelector('#delete-notification-modal'); |
12 | 14 | const { showErrorNotification, showWarningNotification } = ibexa.helpers.notification; |
13 | 15 | const { getJsonFromResponse, getTextFromResponse } = ibexa.helpers.request; |
| 16 | + const { controlManyZIndexes } = ibexa.helpers.modal; |
14 | 17 | const handleNotificationClickRequest = (notification, response) => { |
15 | 18 | if (response.status === 'success') { |
16 | 19 | notification.classList.add('ibexa-notifications-modal__item--read'); |
|
214 | 217 |
|
215 | 218 | currentTarget.textContent.trim() === markAsReadLabel ? markAsRead({ currentTarget }) : markAsUnread({ currentTarget }); |
216 | 219 | }; |
217 | | - const deleteNotification = ({ currentTarget }) => { |
218 | | - const { notificationId } = currentTarget.dataset; |
219 | | - const deleteLink = Routing.generate('ibexa.notifications.delete', { notificationId }); |
| 220 | + const hidePopupMenu = (btn) => { |
| 221 | + const menuBranch = btn.closest('.ibexa-multilevel-popup-menu__branch'); |
| 222 | + |
| 223 | + if (!menuBranch?.menuInstanceElement) { |
| 224 | + return; |
| 225 | + } |
| 226 | + |
| 227 | + const menuInstance = ibexa.helpers.objectInstances.getInstance(menuBranch.menuInstanceElement); |
| 228 | + |
| 229 | + menuInstance.closeMenu(); |
| 230 | + }; |
| 231 | + const deleteNotification = () => { |
| 232 | + const deleteLink = Routing.generate('ibexa.notifications.delete', { notificationId: selectedNotificationId }); |
220 | 233 | const message = Translator.trans( |
221 | 234 | /* @Desc("Cannot delete notification") */ 'notifications.modal.message.error.delete', |
222 | 235 | {}, |
|
227 | 240 | .then(getJsonFromResponse) |
228 | 241 | .then((response) => { |
229 | 242 | if (response.status === 'success') { |
230 | | - const notification = doc.querySelector(`.ibexa-notifications-modal__item[data-notification-id="${notificationId}"]`); |
231 | | - const menuBranch = currentTarget.closest('.ibexa-multilevel-popup-menu__branch'); |
232 | | - const menuInstance = ibexa.helpers.objectInstances.getInstance(menuBranch.menuInstanceElement); |
| 243 | + const notification = doc.querySelector( |
| 244 | + `.ibexa-notifications-modal__item[data-notification-id="${selectedNotificationId}"]`, |
| 245 | + ); |
233 | 246 |
|
234 | | - menuInstance.closeMenu(); |
235 | 247 | notification.remove(); |
236 | 248 | getNotificationsStatus(); |
237 | 249 | } else { |
|
244 | 256 | }; |
245 | 257 | const attachActionsListeners = () => { |
246 | 258 | const attachListener = (node, callback) => node.addEventListener('click', callback, false); |
247 | | - const markAsButtons = doc.querySelectorAll('.ibexa-notifications-modal--mark-as'); |
248 | | - const deleteButtons = doc.querySelectorAll('.ibexa-notifications-modal--delete'); |
249 | | - |
250 | | - markAsButtons.forEach((markAsButton) => { |
251 | | - attachListener(markAsButton, handleMarkAsAction); |
| 259 | + const markAsBtns = doc.querySelectorAll('.ibexa-notifications-modal--mark-as'); |
| 260 | + const deleteBtns = doc.querySelectorAll('.ibexa-notifications-open-modal-button'); |
| 261 | + const setNotificationId = ({ currentTarget }) => { |
| 262 | + hidePopupMenu(currentTarget); |
| 263 | + selectedNotificationId = currentTarget.dataset.notificationId; |
| 264 | + }; |
| 265 | + |
| 266 | + markAsBtns.forEach((markAsBtn) => { |
| 267 | + attachListener(markAsBtn, handleMarkAsAction); |
252 | 268 | }); |
253 | 269 |
|
254 | | - deleteButtons.forEach((deleteButton) => { |
255 | | - attachListener(deleteButton, deleteNotification); |
| 270 | + deleteBtns.forEach((deleteBtn) => { |
| 271 | + attachListener(deleteBtn, setNotificationId); |
256 | 272 | }); |
257 | 273 | }; |
258 | 274 | const showNotificationPage = (pageHtml) => { |
|
308 | 324 | currentPageLink = notificationsTable.dataset.notifications; |
309 | 325 | const interval = Number.parseInt(notificationsTable.dataset.notificationsCountInterval, 10) || INTERVAL; |
310 | 326 |
|
| 327 | + if (deleteConfirmationModal) { |
| 328 | + controlManyZIndexes([{ container: panel, zIndex: '1040' }], deleteConfirmationModal); |
| 329 | + } |
| 330 | + |
311 | 331 | panel.querySelectorAll(SELECTOR_MODAL_RESULTS).forEach((link) => link.addEventListener('click', handleModalResultsClick, false)); |
312 | 332 | markAllAsReadBtn.addEventListener('click', markAllAsRead, false); |
| 333 | + deleteConfirmationModal |
| 334 | + ?.querySelector('.ibexa-notifications-modal--delete--confirm') |
| 335 | + ?.addEventListener('click', deleteNotification, false); |
313 | 336 |
|
314 | 337 | const getNotificationsStatusLoop = () => { |
315 | 338 | getNotificationsStatus().finally(() => { |
|
0 commit comments