Extract Functions.config* to a module
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
parent
5348e09627
commit
75dbb2cfd2
@ -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();
|
||||
}, () => {
|
||||
|
||||
@ -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);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@ -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
|
||||
*
|
||||
|
||||
103
js/src/modules/functions/config.js
Normal file
103
js/src/modules/functions/config.js
Normal file
@ -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)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -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();
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user