1// eslint-disable-next-line @typescript-eslint/no-unused-vars 2const ajaxAction = function () { 3 $(document).on('click', '.jsAjaxAction', function (e) { 4 e.preventDefault(); 5 6 const $button = $(this); 7 let data = null; 8 9 if ($button.data('serialize') !== undefined) { 10 data = $($button.data('serialize')).serializeArray(); 11 } else if ($button.closest('.modal').length) { 12 data = $button.closest('.modal').find('.modal-body form').serializeArray(); 13 } 14 15 waitOnButton($button); 16 loadAjaxAction($button.data('action-url'), data); 17 }); 18 19 function hideAllModals() { 20 $('.modal').modal('hide'); 21 } 22 23 function showError(errorMessage) { 24 const $modal = $('#errorModal'); 25 26 $modal.find('.errorMessage').html(errorMessage); 27 $modal.modal('show'); 28 } 29 30 function waitOnButton(button) { 31 button.addClass('jsPleaseWait'); 32 33 //## Hide existing icons, so it's replace with... 34 button.find('.svg-inline--fa').hide(); 35 36 const html = '<i class="fas fa-cog fa-spin fa-fw"></i>'; 37 38 $(button).prepend(html); 39 40 if ($(button).data('please-wait')) { 41 cmsModal.info( 42 '<i class="fas fa-cog fa-spin fa-fw"></i> Your request is being processed.', 43 { 44 'title': 'Please Wait', 45 'dialogClasses': 'modal-dialog-centered' 46 } 47 ); 48 } 49 } 50 51 function stopWaitingOnButton() { 52 const $button = $('.jsPleaseWait'); 53 54 $button.find('.svg-inline--fa').show(); 55 $button.find('.fa-cog').hide(); 56 } 57 58 function loadAjaxAction(url, data, ajaxOptions) { 59 if (data === null || typeof data === 'undefined') { 60 data = []; 61 } 62 63 //## Always send along the page we are on, for state update purposes. 64 if (data instanceof FormData) { 65 data.append('stateUpdatePageId', $(document.body).data('id')); 66 } else { 67 data.push({'name': 'stateUpdatePageId', 'value': $(document.body).data('id')}); 68 } 69 70 const defaultAjaxOptions = { 71 url, 72 data, 73 xhrFields: { 74 withCredentials: true, 75 }, 76 crossDomain: true, 77 type: 'POST', 78 error(xhr) { 79 let message = 'A server error occurred while making that request.'; 80 81 switch (xhr.status) { 82 case 403: 83 message = 'Access denied.'; 84 break; 85 case 404: 86 message = 'Page not found.'; 87 break; 88 } 89 90 hideAllModals(); 91 showError(message); 92 }, 93 success(data) { 94 if (data.hideAllModals) { 95 hideAllModals(); 96 } 97 98 if (!data.actions) { 99 //## Not sure what happened, but the AJAX did not return what we were expecting. 100 let error = 'A server error occurred while making that request.'; 101 102 //## This probably means they got logged out, maybe in another tab, and now the AJAX requests 103 //## are redirecting to the SSAP login page. Display a custom message for that. 104 if (data.indexOf('SSAP') !== -1) { 105 error = 'You are no longer logged in. Please refresh the page and login.'; 106 } 107 108 hideAllModals(); 109 showError(error); 110 } 111 112 for (let i = 0; i < data.actions.length; i++) { 113 handleAjaxAction(data.actions[i]); 114 } 115 116 stopWaitingOnButton(); 117 }, 118 }; 119 120 if (!ajaxOptions) { 121 ajaxOptions = {}; 122 } 123 124 $.extend(ajaxOptions, defaultAjaxOptions); 125 126 $.ajax(ajaxOptions); 127 } 128 129 function handleAjaxAction(action) { 130 const $content = $(action.html); 131 const $location = $(action.location).first(); 132 133 if (action.appendToLocation) { 134 $location.append($content); 135 } else { 136 $location.replaceWith($content); 137 } 138 } 139 140 return { 141 handleAjaxAction, 142 loadAjaxAction, 143 showError, 144 stopWaitingOnButton, 145 waitOnButton, 146 }; 147}(); 148 149// TODO: uncomment for webpack: 150// export default ajaxAction;
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.