diff --git a/js/src/console.js b/js/src/console.js index c82ac341af..f67b0f5b88 100644 --- a/js/src/console.js +++ b/js/src/console.js @@ -64,6 +64,10 @@ var Console = { Functions.configGet('Console', false, (data) => { Console.config = data; Console.setupAfterInit(); + }, () => { + Console.config = {};// Avoid null pointers in setupAfterInit() + // Fetching data failed, still perform the console init + Console.setupAfterInit(); }); }, diff --git a/js/src/functions.js b/js/src/functions.js index d2b3179e40..064c34431c 100644 --- a/js/src/functions.js +++ b/js/src/functions.js @@ -4537,8 +4537,10 @@ Functions.getImage = function (image, alternate, attributes) { * @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', @@ -4550,15 +4552,12 @@ Functions.configSet = function (key, value) { value: serialized, }, success: function (data) { - // Updating value in local storage. - if (! data.success) { - if (data.error) { - Functions.ajaxShowMessage(data.error); - } else { - Functions.ajaxShowMessage(data.message); + if (data.success !== true) { + // Try to find a message to display + if (data.error || data.message || false) { + Functions.ajaxShowMessage(data.error || data.message); } } - // Eventually, call callback. } }); }; @@ -4573,11 +4572,12 @@ Functions.configSet = function (key, value) { * * @param {string} key Configuration key. * @param {boolean} cached Configuration type. - * @param {Function} successCallback The callback to call after the value is received + * @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) { +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) { @@ -4596,12 +4596,23 @@ Functions.configGet = function (key, cached, successCallback) { key: key }, success: function (data) { - // Updating value in local storage. - if (data.success) { - localStorage.setItem(key, JSON.stringify(data.value)); - } else { - Functions.ajaxShowMessage(data.message); + if (data.success !== true) { + // Try to find a message to display + if (data.error || data.message || false) { + Functions.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