Skip to content

Commit

Permalink
Extract Functions.config* to a module
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Dec 9, 2022
1 parent 5348e09 commit 75dbb2c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 104 deletions.
3 changes: 2 additions & 1 deletion js/src/modules/console.js
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +63,7 @@ var Console = {
return;
}

Functions.configGet('Console', false, (data) => {
getConfigValue('Console', false, (data) => {
Config.init(data);
Console.setupAfterInit();
}, () => {
Expand Down
4 changes: 2 additions & 2 deletions 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
Expand Down Expand Up @@ -69,7 +69,7 @@ export const Config = {
*/
set: function (key, value) {
this[key] = value;
Functions.configSet('Console/' + key, value);
setConfigValue('Console/' + key, value);
},

/**
Expand Down
98 changes: 0 additions & 98 deletions js/src/modules/functions.js
Expand Up @@ -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
*
Expand Down
103 changes: 103 additions & 0 deletions 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)));
}
}
});
}
7 changes: 4 additions & 3 deletions js/src/modules/navigation.js
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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();
};
Expand Down

0 comments on commit 75dbb2c

Please sign in to comment.