1// TODO: For conversion, this will be global since referenced in twig. 2// eslint-disable-next-line @typescript-eslint/no-unused-vars 3const notification = (function () { 4 let isSsap; 5 let enabled = true; 6 7 if (typeof pageLoadAjaxInDev !== 'undefined' 8 && typeof pageLoadAjaxInDev.isEnabled === 'function' 9 && !pageLoadAjaxInDev.isEnabled()) { 10 enabled = false; 11 } 12 13 $(document).ready(function () { 14 isSsap = $('.app-drawer-link').length === 0; 15 16 if (isSsap) { 17 ssap.init(); 18 } 19 20 //## Notifications aren't important, so let other AJAX go first. 21 window.setTimeout(getNotifications, 2500); 22 }); 23 24 function getNotifications() { 25 if (!enabled) { 26 return; 27 } 28 29 const url = $('*[data-notification-url]').data('notification-url'); 30 31 if (!url) { 32 return; 33 } 34 35 $.ajax({ 36 type: 'POST', 37 url, 38 ignoreErrors: true, 39 success(response) { 40 if (isSsap) { 41 ssap.handleSuccess(response); 42 } else { 43 cms.handleSuccess(response); 44 } 45 }, 46 error(jqXHR) { 47 if (jqXHR.status === 0) { 48 // This is from them navigating away from the page while the AJAX is loading. 49 return; 50 } 51 52 semModal.error('Error checking for notifications.'); 53 }, 54 }); 55 } 56 57 function disable() { 58 enabled = false; 59 } 60 61 function enable() { 62 enabled = true; 63 } 64 65 const cms = { 66 handleSuccess(response) { 67 let count = 0; 68 const $label = $('.app-drawer-link .label'); 69 const $list = $('.jsNotificationsHolder').html(''); 70 71 $.each(response, function (index, notification) { 72 $('<li><a href="' + notification.url + '">' + 73 '<span class="label">' + notification.count + '</span> ' + 74 notification.message + '</a></li>').appendTo($list); 75 76 count += notification.count; 77 }); 78 79 const $notificationHeading = $('#heading-notifications'); 80 81 $notificationHeading.closest('.card').removeClass('d-none'); 82 83 const $notificationsHeadingLink = $notificationHeading.find('a'); 84 85 $label 86 .toggleClass('notificationNumber', count > 0) 87 .find('.notificationCount').toggleClass('d-none').html(count); 88 89 if (count === 0) { 90 $notificationsHeadingLink.addClass('disabled').html('No Notifications'); 91 $label.find('svg').show(); 92 $label.find('.notificationCount').hide(); 93 94 return; 95 } 96 97 const notificationLabel = 'Notification' + (count !== 1 ? 's' : ''); 98 99 $notificationsHeadingLink 100 .removeClass('disabled') 101 .html( 102 notificationLabel + '<span class="label">' + count + '</span> <i class="far fa-chevron-down"></i>'); 103 $list.prepend('<li>You have ' + count + ' ' + notificationLabel.toLowerCase() + '</li>'); 104 $label.find('svg').hide(); 105 $label.find('.notificationCount').show(); 106 }, 107 }; 108 const ssap = { 109 init() { 110 $('.sidebar-menu a').on('click', function () { 111 //## This handles when a menu is opening, as the sub menu is visible right away. 112 ssap.addToCollapsedParent(); 113 //## This handles when a menu is closing, as it's not hidden for 500 ms. 114 window.setTimeout(ssap.addToCollapsedParent, 600); 115 }); 116 }, 117 118 /** 119 * Copies the notification on the link to parent menu items, if the children are hidden. 120 */ 121 addToCollapsedParent() { 122 $('.semNotificationClone').each(function (index, cloned) { 123 cloned = $(cloned); 124 cloned.next().show(); 125 cloned.remove(); 126 }); 127 128 $('.semNotification').each(function () { 129 const $notification = $(this); 130 131 $notification.parents('li').each(function (index) { 132 const $li = $(this); 133 134 //## The fist one is the li for the notification, so don't add it. 135 if (index === 0) { 136 return !$li.is(':visible'); 137 } 138 139 const $link = $li.find('> a'); 140 const $clone = $link.find('> .semNotificationClone'); 141 142 $link.find('> i').hide(); 143 144 if ($clone.length > 0) { 145 //## Already a notification here, add to it. 146 $clone.html(Number($clone.html()) + Number($notification.html())); 147 } else { 148 $link.prepend($notification.clone().addClass('semNotificationClone')); 149 } 150 151 //## If this one is visible, break the each loop. 152 if ($li.is(':visible')) { 153 return false;
154 } 155 }); 156 }); 157 }, 158 159 handleSuccess(response) { 160 if ($('.main-sidebar .dashboardPleaseWait').length) { 161 //## The menu is loading, don't add the notifications until after the menu is loaded. 162 window.setTimeout(function () { 163 ssap.handleSuccess(response); 164 }, 100); 165 166 return; 167 } 168 169 ssap.addNotifications(response); 170 }, 171 172 addNotifications(notifications) { 173 let count = 0; 174 const $notification = $('.notifications-menu'); 175 const $label = $notification.find('.dropdown-toggle .label'); 176 const $outlineIcon = $notification.find('.outlineIcon'); 177 const $solidIcon = $notification.find('.solidIcon'); 178 const $header = $notification.find('.header'); 179 const $list = $notification.find('.notifcationsHolder ul').html(''); 180 181 $.each(notifications, function (index, notification) { 182 // Construct a real URL so we can strip any anchors or query strings off. 183 const notificationPath = (new URL(notification.url, location.origin)).pathname; 184 185 $('<li><a href="' + notificationPath + '" title="' + notification.message + '">' + 186 '<span class="label label-danger">' + notification.count + '</span> ' + 187 notification.message + '</a></li>').appendTo($list); 188 189 const $menuItem = $('.sidebar-menu').find('a[href="' + notificationPath + '"]'); 190 const $semNotification = $menuItem.find('.semNotification'); 191 192 if ($semNotification.length) { 193 $semNotification.html(Number($semNotification.html()) + notification.count); 194 } else { 195 $menuItem.children('i').remove(); 196 $('<small class="semNotification label float-left bg-danger">' + notification.count + '</small>') 197 .prependTo($menuItem); 198 } 199 200 count += notification.count; 201 }); 202 203 if (count === 0) { 204 $label.remove() 205 $header.html('<i class="fas fa-check-circle mr-1"></i> You don\'t have any notifications'); 206 return; 207 } 208 209 $header.toggleClass('noNotifications').html('You have ' + (count === 1 ? 'one notification' : count + ' notifications.')); 210 $outlineIcon.toggleClass('d-none'); 211 $solidIcon.toggleClass('d-none'); 212 $label.addClass('bg-danger').html(count); 213 $notification.fadeIn(); 214 ssap.addToCollapsedParent(); 215 }, 216 }; 217 218 return { 219 'addSsapNotifications': ssap.addNotifications, 220 disable, 221 enable, 222 getNotifications, 223 }; 224})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.