From 1cd484e1bce2b9dee5c77a93667f53e650c111a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sun, 11 Dec 2022 21:19:44 -0300 Subject: [PATCH] Extract Functions.handleRedirectAndReload() into a module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This removes the dependency on Functions in navigation.js. Signed-off-by: MaurĂ­cio Meneghini Fauth --- js/src/modules/ajax.js | 5 +++-- js/src/modules/functions.js | 22 ++----------------- .../functions/handleRedirectAndReload.js | 20 +++++++++++++++++ js/src/modules/navigation.js | 4 ++-- 4 files changed, 27 insertions(+), 24 deletions(-) create mode 100644 js/src/modules/functions/handleRedirectAndReload.js diff --git a/js/src/modules/ajax.js b/js/src/modules/ajax.js index 5e02027ad1..f9b37c1ace 100644 --- a/js/src/modules/ajax.js +++ b/js/src/modules/ajax.js @@ -7,6 +7,7 @@ import { ajaxRemoveMessage, ajaxShowMessage } from './ajax-message.js'; import { escapeHtml } from './functions/escape.js'; import getImageTag from './functions/getImageTag.js'; import { ignorePhpErrors } from './functions/ignorePhpErrors.js'; +import handleRedirectAndReload from './functions/handleRedirectAndReload.js'; /** * This object handles ajax requests for pages. It also @@ -453,7 +454,7 @@ const AJAX = { ajaxShowMessage(data.error, false); AJAX.active = false; AJAX.xhr = null; - Functions.handleRedirectAndReload(data); + handleRedirectAndReload(data); if (data.fieldWithError) { $(':input.error').removeClass('error'); $('#' + data.fieldWithError).addClass('error'); @@ -624,7 +625,7 @@ const AJAX = { $('html, body').animate({ scrollTop: $(document).height() }, 200); AJAX.active = false; AJAX.xhr = null; - Functions.handleRedirectAndReload(data); + handleRedirectAndReload(data); if (data.fieldWithError) { $(':input.error').removeClass('error'); $('#' + data.fieldWithError).addClass('error'); diff --git a/js/src/modules/functions.js b/js/src/modules/functions.js index 87dcb2c4c3..fa6e28247a 100644 --- a/js/src/modules/functions.js +++ b/js/src/modules/functions.js @@ -10,6 +10,7 @@ import { ajaxRemoveMessage, ajaxShowMessage } from './ajax-message.js'; import handleCreateViewModal from './functions/handleCreateViewModal.js'; import { escapeHtml } from './functions/escape.js'; import getImageTag from './functions/getImageTag.js'; +import handleRedirectAndReload from './functions/handleRedirectAndReload.js'; /* global DatabaseStructure */ // js/database/structure.js /* global firstDayOfCalendar, maxInputVars, themeImagePath */ // templates/javascript/variables.twig @@ -210,25 +211,6 @@ Functions.addDateTimePicker = function () { } }; -/** - * Handle redirect and reload flags sent as part of AJAX requests - * - * @param data ajax response data - */ -Functions.handleRedirectAndReload = function (data) { - if (parseInt(data.redirect_flag) === 1) { - // add one more GET param to display session expiry msg - if (window.location.href.indexOf('?') === -1) { - window.location.href += '?session_expired=1'; - } else { - window.location.href += CommonParams.get('arg_separator') + 'session_expired=1'; - } - window.location.reload(); - } else if (parseInt(data.reload_flag) === 1) { - window.location.reload(); - } -}; - /** * Creates an SQL editor which supports auto completing etc. * @@ -864,7 +846,7 @@ Functions.onloadIdleEvent = function () { $('input[name=token]').val(data.new_token); } idleSecondsCounter = 0; - Functions.handleRedirectAndReload(data); + handleRedirectAndReload(data); } } }); diff --git a/js/src/modules/functions/handleRedirectAndReload.js b/js/src/modules/functions/handleRedirectAndReload.js new file mode 100644 index 0000000000..97a36512ee --- /dev/null +++ b/js/src/modules/functions/handleRedirectAndReload.js @@ -0,0 +1,20 @@ +import { CommonParams } from '../common.js'; + +/** + * Handle redirect and reload flags sent as part of AJAX requests + * + * @param {Object} data ajax response data + */ +export default function handleRedirectAndReload (data) { + if (parseInt(data.redirect_flag) === 1) { + // add one more GET param to display session expiry msg + if (window.location.href.indexOf('?') === -1) { + window.location.href += '?session_expired=1'; + } else { + window.location.href += CommonParams.get('arg_separator') + 'session_expired=1'; + } + window.location.reload(); + } else if (parseInt(data.reload_flag) === 1) { + window.location.reload(); + } +} diff --git a/js/src/modules/navigation.js b/js/src/modules/navigation.js index cc67e0884e..2875b34dd0 100644 --- a/js/src/modules/navigation.js +++ b/js/src/modules/navigation.js @@ -1,11 +1,11 @@ import $ from 'jquery'; -import { Functions } from './functions.js'; import { CommonParams } from './common.js'; import { Config } from './config.js'; import tooltip from './tooltip.js'; import { ajaxRemoveMessage, ajaxShowMessage } from './ajax-message.js'; import handleCreateViewModal from './functions/handleCreateViewModal.js'; import { getConfigValue, setConfigValue } from './functions/config.js'; +import handleRedirectAndReload from './functions/handleRedirectAndReload.js'; /** * function used in or for navigation panel @@ -1039,7 +1039,7 @@ Navigation.treePagination = function ($this) { } } else { ajaxShowMessage(data.error); - Functions.handleRedirectAndReload(data); + handleRedirectAndReload(data); } Navigation.treeStateUpdate(); });