diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-06-25 01:54:09 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-06-25 01:54:38 +0300 |
commit | 664811562a4d5b8b777b3ff7d3585793a9888490 (patch) | |
tree | 1bb409363f316a43b4a26b77150a3065bbdc2e4c | |
parent | 1c2b6111736a436007a9756c12a36302c5695532 (diff) |
Extract functions in ajax.js
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
-rw-r--r-- | js/src/ajax.js | 303 |
1 files changed, 158 insertions, 145 deletions
diff --git a/js/src/ajax.js b/js/src/ajax.js index 35cb626f02..f07e03072f 100644 --- a/js/src/ajax.js +++ b/js/src/ajax.js @@ -775,113 +775,174 @@ window.AJAX = { $(document).off('submit', 'form').on('submit', 'form', window.AJAX.requestHandler); callback(); } - } -}; - -/** - * Here we register a function that will remove the onsubmit event from all - * forms that will be handled by the generic page loader. We then save this - * event handler in the "jQuery data", so that we can fire it up later in - * window.AJAX.requestHandler(). - * - * See bug #3583316 - */ -window.AJAX.registerOnload('functions.js', function () { - // Registering the onload event for functions.js - // ensures that it will be fired for all pages - $('form').not('.ajax').not('.disableAjax').each(function () { - if ($(this).attr('onsubmit')) { - $(this).data('onsubmit', this.onsubmit).attr('onsubmit', ''); - } - }); + }, - var $pageContent = $('#page_content'); /** - * Workaround for passing submit button name,value on ajax form submit - * by appending hidden element with submit button name and value. + * Here we register a function that will remove the onsubmit event from all + * forms that will be handled by the generic page loader. We then save this + * event handler in the "jQuery data", so that we can fire it up later in + * window.AJAX.requestHandler(). + * + * See bug #3583316 */ - $pageContent.on('click', 'form input[type=submit]', function () { - var buttonName = $(this).attr('name'); - if (typeof buttonName === 'undefined') { - return; - } - $(this).closest('form').append($('<input>', { - 'type' : 'hidden', - 'name' : buttonName, - 'value': $(this).val() - })); - }); + removeSubmitEvents: function () { + // Registering the onload event for functions.js + // ensures that it will be fired for all pages + $('form').not('.ajax').not('.disableAjax').each(function () { + if ($(this).attr('onsubmit')) { + $(this).data('onsubmit', this.onsubmit).attr('onsubmit', ''); + } + }); + + var $pageContent = $('#page_content'); + /** + * Workaround for passing submit button name,value on ajax form submit + * by appending hidden element with submit button name and value. + */ + $pageContent.on('click', 'form input[type=submit]', function () { + var buttonName = $(this).attr('name'); + if (typeof buttonName === 'undefined') { + return; + } + $(this).closest('form').append($('<input>', { + 'type': 'hidden', + 'name': buttonName, + 'value': $(this).val() + })); + }); + + /** + * Attach event listener to events when user modify visible + * Input,Textarea and select fields to make changes in forms + */ + $pageContent.on( + 'keyup change', + 'form.lock-page textarea, ' + + 'form.lock-page input[type="text"], ' + + 'form.lock-page input[type="number"], ' + + 'form.lock-page select', + { value: 1 }, + window.AJAX.lockPageHandler + ); + $pageContent.on( + 'change', + 'form.lock-page input[type="checkbox"], ' + + 'form.lock-page input[type="radio"]', + { value: 2 }, + window.AJAX.lockPageHandler + ); + /** + * Reset lock when lock-page form reset event is fired + * Note: reset does not bubble in all browser so attach to + * form directly. + */ + $('form.lock-page').on('reset', function () { + window.AJAX.resetLock(); + }); + }, /** - * Attach event listener to events when user modify visible - * Input,Textarea and select fields to make changes in forms + * Page load event handler + * @return {function} */ - $pageContent.on( - 'keyup change', - 'form.lock-page textarea, ' + - 'form.lock-page input[type="text"], ' + - 'form.lock-page input[type="number"], ' + - 'form.lock-page select', - { value:1 }, - window.AJAX.lockPageHandler - ); - $pageContent.on( - 'change', - 'form.lock-page input[type="checkbox"], ' + - 'form.lock-page input[type="radio"]', - { value:2 }, - window.AJAX.lockPageHandler - ); + loadEventHandler: function () { + return function () { + var menuContent = $('<div></div>') + .append($('#server-breadcrumb').clone()) + .append($('#topmenucontainer').clone()) + .html(); + + // set initial state reload + var initState = ('state' in window.history && window.history.state !== null); + var initURL = $('#selflink').find('> a').attr('href') || location.href; + var state = { + url: initURL, + menu: menuContent + }; + history.replaceState(state, null); + + $(window).on('popstate', function (event) { + var initPop = (!initState && location.href === initURL); + initState = true; + // check if popstate fired on first page itself + if (initPop) { + return; + } + var state = event.originalEvent.state; + if (state && state.menu) { + window.AJAX.$msgbox = Functions.ajaxShowMessage(); + var params = 'ajax_request=true' + window.CommonParams.get('arg_separator') + 'ajax_page_request=true'; + var url = state.url || location.href; + $.get(url, params, window.AJAX.responseHandler); + // TODO: Check if sometimes menu is not retrieved from server, + // Not sure but it seems menu was missing only for printview which + // been removed lately, so if it's right some dead menu checks/fallbacks + // may need to be removed from this file and Header.php + // window.AJAX.handleMenu.replace(event.originalEvent.state.menu); + } + }); + }; + }, + /** - * Reset lock when lock-page form reset event is fired - * Note: reset does not bubble in all browser so attach to - * form directly. + * Gracefully handle fatal server errors (e.g: 500 - Internal server error) + * @return {function} */ - $('form.lock-page').on('reset', function () { - window.AJAX.resetLock(); - }); -}); + getFatalErrorHandler: function () { + return function (event, request) { + if (window.AJAX.debug) { + // eslint-disable-next-line no-console + console.log('AJAX error: status=' + request.status + ', text=' + request.statusText); + } + // Don't handle aborted requests + if (request.status !== 0 || request.statusText !== 'abort') { + var details = ''; + var state = request.state(); + + if ( + 'responseJSON' in request && + 'isErrorResponse' in request.responseJSON && + request.responseJSON.isErrorResponse + ) { + Functions.ajaxShowMessage( + '<div class="alert alert-danger" role="alert">' + + Functions.escapeHtml(request.responseJSON.error) + + '</div>', + false + ); + window.AJAX.active = false; + window.AJAX.xhr = null; -/** - * Page load event handler - */ -$(function () { - var menuContent = $('<div></div>') - .append($('#server-breadcrumb').clone()) - .append($('#topmenucontainer').clone()) - .html(); - - // set initial state reload - var initState = ('state' in window.history && window.history.state !== null); - var initURL = $('#selflink').find('> a').attr('href') || location.href; - var state = { - url : initURL, - menu : menuContent - }; - history.replaceState(state, null); - - $(window).on('popstate', function (event) { - var initPop = (! initState && location.href === initURL); - initState = true; - // check if popstate fired on first page itself - if (initPop) { - return; - } - var state = event.originalEvent.state; - if (state && state.menu) { - window.AJAX.$msgbox = Functions.ajaxShowMessage(); - var params = 'ajax_request=true' + window.CommonParams.get('arg_separator') + 'ajax_page_request=true'; - var url = state.url || location.href; - $.get(url, params, window.AJAX.responseHandler); - // TODO: Check if sometimes menu is not retrieved from server, - // Not sure but it seems menu was missing only for printview which - // been removed lately, so if it's right some dead menu checks/fallbacks - // may need to be removed from this file and Header.php - // window.AJAX.handleMenu.replace(event.originalEvent.state.menu); - } - }); + return; + } + + if (request.status !== 0) { + details += '<div>' + Functions.escapeHtml(Functions.sprintf(Messages.strErrorCode, request.status)) + '</div>'; + } + details += '<div>' + Functions.escapeHtml(Functions.sprintf(Messages.strErrorText, request.statusText + ' (' + state + ')')) + '</div>'; + if (state === 'rejected' || state === 'timeout') { + details += '<div>' + Functions.escapeHtml(Messages.strErrorConnection) + '</div>'; + } + Functions.ajaxShowMessage( + '<div class="alert alert-danger" role="alert">' + + Messages.strErrorProcessingRequest + + details + + '</div>', + false + ); + window.AJAX.active = false; + window.AJAX.xhr = null; + } + }; + } +}; + +window.AJAX.registerOnload('functions.js', function () { + window.AJAX.removeSubmitEvents(); }); +$(window.AJAX.loadEventHandler()); + /** * Attach a generic event handler to clicks * on pages and submissions of forms @@ -889,52 +950,4 @@ $(function () { $(document).on('click', 'a', window.AJAX.requestHandler); $(document).on('submit', 'form', window.AJAX.requestHandler); -/** - * Gracefully handle fatal server errors - * (e.g: 500 - Internal server error) - */ -$(document).on('ajaxError', function (event, request) { - if (window.AJAX.debug) { - // eslint-disable-next-line no-console - console.log('AJAX error: status=' + request.status + ', text=' + request.statusText); - } - // Don't handle aborted requests - if (request.status !== 0 || request.statusText !== 'abort') { - var details = ''; - var state = request.state(); - - if ( - 'responseJSON' in request && - 'isErrorResponse' in request.responseJSON && - request.responseJSON.isErrorResponse - ) { - Functions.ajaxShowMessage( - '<div class="alert alert-danger" role="alert">' + - Functions.escapeHtml(request.responseJSON.error) + - '</div>', - false - ); - window.AJAX.active = false; - window.AJAX.xhr = null; - - return; - } - - if (request.status !== 0) { - details += '<div>' + Functions.escapeHtml(Functions.sprintf(Messages.strErrorCode, request.status)) + '</div>'; - } - details += '<div>' + Functions.escapeHtml(Functions.sprintf(Messages.strErrorText, request.statusText + ' (' + state + ')')) + '</div>'; - if (state === 'rejected' || state === 'timeout') { - details += '<div>' + Functions.escapeHtml(Messages.strErrorConnection) + '</div>'; - } - Functions.ajaxShowMessage( - '<div class="alert alert-danger" role="alert">' + - Messages.strErrorProcessingRequest + - details + - '</div>', - false - ); - window.AJAX.active = false; - window.AJAX.xhr = null; - } -}); +$(document).on('ajaxError', window.AJAX.getFatalErrorHandler()); |