diff --git a/js/src/modules/console.js b/js/src/modules/console.js index 0caafa2147bf..115e788167e7 100644 --- a/js/src/modules/console.js +++ b/js/src/modules/console.js @@ -5,6 +5,7 @@ import { Functions } from './functions.js'; import { CommonParams } from './common.js'; import { Navigation } from './navigation.js'; import { Config } from './console/config.js'; +import { getConfigValue } from './functions/config.js'; /** * Console object @@ -62,7 +63,7 @@ var Console = { return; } - Functions.configGet('Console', false, (data) => { + getConfigValue('Console', false, (data) => { Config.init(data); Console.setupAfterInit(); }, () => { diff --git a/js/src/modules/console/config.js b/js/src/modules/console/config.js index ca29e60a7647..bcee02acaa6a 100644 --- a/js/src/modules/console/config.js +++ b/js/src/modules/console/config.js @@ -1,4 +1,4 @@ -import { Functions } from '../functions.js'; +import { setConfigValue } from '../functions/config.js'; /** * @link https://docs.phpmyadmin.net/en/latest/config.html#console-settings @@ -69,7 +69,7 @@ export const Config = { */ set: function (key, value) { this[key] = value; - Functions.configSet('Console/' + key, value); + setConfigValue('Console/' + key, value); }, /** diff --git a/js/src/modules/functions.js b/js/src/modules/functions.js index c2177793b1b4..833e35c72fc0 100644 --- a/js/src/modules/functions.js +++ b/js/src/modules/functions.js @@ -3926,104 +3926,6 @@ Functions.getImage = function (image, alternate, attributes) { return retval; }; -/** - * Sets a configuration value. - * - * A configuration value may be set in both browser's local storage and - * remotely in server's configuration table. - * - * NOTE: Depending on server's configuration, the configuration table may be or - * not persistent. - * - * @param {string} key Configuration key. - * @param {object} value Configuration value. - */ -Functions.configSet = function (key, value) { - // Updating value in local storage. - var serialized = JSON.stringify(value); - localStorage.setItem(key, serialized); - - $.ajax({ - url: 'index.php?route=/config/set', - type: 'POST', - dataType: 'json', - data: { - 'ajax_request': true, - key: key, - server: CommonParams.get('server'), - value: serialized, - }, - success: function (data) { - if (data.success !== true) { - // Try to find a message to display - if (data.error || data.message || false) { - ajaxShowMessage(data.error || data.message); - } - } - } - }); -}; - -/** - * Gets a configuration value. A configuration value will be searched in - * browser's local storage first and if not found, a call to the server will be - * made. - * - * If value should not be cached and the up-to-date configuration value from - * right from the server is required, the third parameter should be `false`. - * - * @param {string} key Configuration key. - * @param {boolean} cached Configuration type. - * @param {Function} successCallback The callback to call after the value is successfully received - * @param {Function} failureCallback The callback to call when the value can not be received - * - * @return {void} - */ -Functions.configGet = function (key, cached, successCallback, failureCallback) { - var isCached = (typeof cached !== 'undefined') ? cached : true; - var value = localStorage.getItem(key); - if (isCached && value !== undefined && value !== null) { - return JSON.parse(value); - } - - // Result not found in local storage or ignored. - // Hitting the server. - $.ajax({ - url: 'index.php?route=/config/get', - type: 'POST', - dataType: 'json', - data: { - 'ajax_request': true, - server: CommonParams.get('server'), - key: key - }, - success: function (data) { - if (data.success !== true) { - // Try to find a message to display - if (data.error || data.message || false) { - ajaxShowMessage(data.error || data.message); - } - - // Call the callback if it is defined - if (typeof failureCallback === 'function') { - failureCallback(); - } - - // return here, exit non success mode - return; - } - - // Updating value in local storage. - localStorage.setItem(key, JSON.stringify(data.value)); - // Call the callback if it is defined - if (typeof successCallback === 'function') { - // Feed it the value previously saved like on async mode - successCallback(JSON.parse(localStorage.getItem(key))); - } - } - }); -}; - /** * Return POST data as stored by Generator::linkOrButton * diff --git a/js/src/modules/functions/config.js b/js/src/modules/functions/config.js new file mode 100644 index 000000000000..db2a1e05590b --- /dev/null +++ b/js/src/modules/functions/config.js @@ -0,0 +1,103 @@ +import $ from 'jquery'; +import { ajaxShowMessage } from '../ajax-message.js'; +import { CommonParams } from '../common.js'; + +/** + * Sets a configuration value. + * + * A configuration value may be set in both browser's local storage and + * remotely in server's configuration table. + * + * NOTE: Depending on server's configuration, the configuration table may be or + * not persistent. + * + * @param {string} key Configuration key. + * @param {object} value Configuration value. + * + * @return {void} + */ +export function setConfigValue (key, value) { + // Updating value in local storage. + var serialized = JSON.stringify(value); + localStorage.setItem(key, serialized); + + $.ajax({ + url: 'index.php?route=/config/set', + type: 'POST', + dataType: 'json', + data: { + 'ajax_request': true, + key: key, + server: CommonParams.get('server'), + value: serialized, + }, + success: function (data) { + if (data.success !== true) { + // Try to find a message to display + if (data.error || data.message || false) { + ajaxShowMessage(data.error || data.message); + } + } + } + }); +} + +/** + * Gets a configuration value. A configuration value will be searched in + * browser's local storage first and if not found, a call to the server will be + * made. + * + * If value should not be cached and the up-to-date configuration value from + * right from the server is required, the third parameter should be `false`. + * + * @param {string} key Configuration key. + * @param {boolean} cached Configuration type. + * @param {Function} successCallback The callback to call after the value is successfully received + * @param {Function} failureCallback The callback to call when the value can not be received + * + * @return {void} + */ +export function getConfigValue (key, cached, successCallback, failureCallback) { + var isCached = (typeof cached !== 'undefined') ? cached : true; + var value = localStorage.getItem(key); + if (isCached && value !== undefined && value !== null) { + return JSON.parse(value); + } + + // Result not found in local storage or ignored. + // Hitting the server. + $.ajax({ + url: 'index.php?route=/config/get', + type: 'POST', + dataType: 'json', + data: { + 'ajax_request': true, + server: CommonParams.get('server'), + key: key + }, + success: function (data) { + if (data.success !== true) { + // Try to find a message to display + if (data.error || data.message || false) { + ajaxShowMessage(data.error || data.message); + } + + // Call the callback if it is defined + if (typeof failureCallback === 'function') { + failureCallback(); + } + + // return here, exit non success mode + return; + } + + // Updating value in local storage. + localStorage.setItem(key, JSON.stringify(data.value)); + // Call the callback if it is defined + if (typeof successCallback === 'function') { + // Feed it the value previously saved like on async mode + successCallback(JSON.parse(localStorage.getItem(key))); + } + } + }); +} diff --git a/js/src/modules/navigation.js b/js/src/modules/navigation.js index 06a21cf8ec6e..cc67e0884ea0 100644 --- a/js/src/modules/navigation.js +++ b/js/src/modules/navigation.js @@ -5,6 +5,7 @@ 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'; /** * function used in or for navigation panel @@ -1195,7 +1196,7 @@ Navigation.ResizeHandler = function () { */ this.mouseup = function (event) { $('body').css('cursor', ''); - Functions.configSet('NavigationWidth', event.data.resize_handler.getPos(event)); + setConfigValue('NavigationWidth', event.data.resize_handler.getPos(event)); $('#topmenu').menuResizer('resize'); $(document) .off('mousemove') @@ -1229,7 +1230,7 @@ Navigation.ResizeHandler = function () { if (width === 0 && panelWidth === 0) { panelWidth = 240; } - Functions.configSet('NavigationWidth', panelWidth); + setConfigValue('NavigationWidth', panelWidth); event.data.resize_handler.setWidth(panelWidth); event.data.resize_handler.panelWidth = width; }; @@ -1291,7 +1292,7 @@ Navigation.ResizeHandler = function () { const initialResizeValue = $('#pma_navigation').data('config-navigation-width'); callbackSuccessGetConfigValue(initialResizeValue); } - Functions.configGet('NavigationWidth', false, callbackSuccessGetConfigValue); + getConfigValue('NavigationWidth', false, callbackSuccessGetConfigValue); }; this.treeInit(); };