Skip to content

Commit 648ee27

Browse files
committed
Extract Config.isStorageSupported() to a module
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent a90229f commit 648ee27

6 files changed

Lines changed: 58 additions & 55 deletions

File tree

js/src/modules/config.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import $ from 'jquery';
22
import { Functions } from './functions.js';
33
import { CommonParams } from './common.js';
44
import { ajaxShowMessage } from './ajax-message.js';
5+
import isStorageSupported from './functions/isStorageSupported.js';
56

67
/**
78
* Functions used in configuration forms and on user preferences pages
@@ -11,32 +12,6 @@ const Config = {};
1112
window.configInlineParams;
1213
window.configScriptLoaded;
1314

14-
/**
15-
* checks whether browser supports web storage
16-
*
17-
* @param {'localStorage' | 'sessionStorage'} type the type of storage i.e. localStorage or sessionStorage
18-
* @param {boolean} warn Wether to show a warning on error
19-
*
20-
* @return {boolean}
21-
*/
22-
Config.isStorageSupported = (type, warn = false) => {
23-
try {
24-
window[type].setItem('PMATest', 'test');
25-
// Check whether key-value pair was set successfully
26-
if (window[type].getItem('PMATest') === 'test') {
27-
// Supported, remove test variable from storage
28-
window[type].removeItem('PMATest');
29-
return true;
30-
}
31-
} catch (error) {
32-
// Not supported
33-
if (warn) {
34-
ajaxShowMessage(window.Messages.strNoLocalStorage, false);
35-
}
36-
}
37-
return false;
38-
};
39-
4015
// default values for fields
4116
window.defaultValues = {};
4217

@@ -694,7 +669,7 @@ function updatePrefsDate () {
694669
* Prepares message which informs that localStorage preferences are available and can be imported or deleted
695670
*/
696671
function offerPrefsAutoimport () {
697-
var hasConfig = (Config.isStorageSupported('localStorage')) && (window.localStorage.config || false);
672+
var hasConfig = (isStorageSupported('localStorage')) && (window.localStorage.config || false);
698673
var $cnt = $('#prefs_autoload');
699674
if (! $cnt.length || ! hasConfig) {
700675
return;
@@ -784,7 +759,7 @@ Config.on = function () {
784759
});
785760

786761
// detect localStorage state
787-
var lsSupported = Config.isStorageSupported('localStorage', true);
762+
var lsSupported = isStorageSupported('localStorage', true);
788763
var lsExists = lsSupported ? (window.localStorage.config || false) : false;
789764
$('div.localStorage-' + (lsSupported ? 'un' : '') + 'supported').hide();
790765
$('div.localStorage-' + (lsExists ? 'empty' : 'exists')).hide();

js/src/modules/functions.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import $ from 'jquery';
22
import { AJAX } from './ajax.js';
33
import { Navigation } from './navigation.js';
44
import { CommonParams } from './common.js';
5-
import { Config } from './config.js';
65
import tooltip from './tooltip.js';
76
import highlightSql from './sql-highlight.js';
87
import { ajaxRemoveMessage, ajaxShowMessage } from './ajax-message.js';
@@ -14,6 +13,7 @@ import refreshMainContent from './functions/refreshMainContent.js';
1413
import checkIndexType from './indexes/checkIndexType.js';
1514
import checkIndexName from './indexes/checkIndexName.js';
1615
import mainMenuResizerCallback from './functions/mainMenuResizerCallback.js';
16+
import isStorageSupported from './functions/isStorageSupported.js';
1717

1818
/* global DatabaseStructure */ // js/database/structure.js
1919
/* global firstDayOfCalendar, themeImagePath */ // templates/javascript/variables.twig
@@ -799,7 +799,7 @@ Functions.onloadIdleEvent = function () {
799799
function UpdateIdleTime () {
800800
var href = 'index.php?route=/';
801801
var guid = 'default';
802-
if (Config.isStorageSupported('sessionStorage')) {
802+
if (isStorageSupported('sessionStorage')) {
803803
guid = window.sessionStorage.guid;
804804
}
805805
var params = {
@@ -834,7 +834,7 @@ Functions.onloadIdleEvent = function () {
834834
updateTimeout = window.setTimeout(UpdateIdleTime, interval);
835835
} else { // timeout occurred
836836
clearInterval(incInterval);
837-
if (Config.isStorageSupported('sessionStorage')) {
837+
if (isStorageSupported('sessionStorage')) {
838838
window.sessionStorage.clear();
839839
}
840840
// append the login form on the page, disable all the forms which were not disabled already, close all the open jqueryui modal boxes
@@ -862,7 +862,7 @@ Functions.onloadIdleEvent = function () {
862862
CommonParams.get('LoginCookieValidity'),
863863
CommonParams.get('session_gc_maxlifetime')
864864
);
865-
if (Config.isStorageSupported('sessionStorage')) {
865+
if (isStorageSupported('sessionStorage')) {
866866
window.sessionStorage.setItem('guid', guid());
867867
}
868868
var interval = (sessionTimeout - 5) * 1000;
@@ -3000,15 +3000,15 @@ Functions.onloadRecentFavoriteTables = () => {
30003000
cache: false,
30013001
type: 'POST',
30023002
data: {
3003-
'favoriteTables': (Config.isStorageSupported('localStorage') && typeof window.localStorage.favoriteTables !== 'undefined')
3003+
'favoriteTables': (isStorageSupported('localStorage') && typeof window.localStorage.favoriteTables !== 'undefined')
30043004
? window.localStorage.favoriteTables
30053005
: '',
30063006
'server': CommonParams.get('server'),
30073007
'no_debug': true
30083008
},
30093009
success: function (data) {
30103010
// Update localStorage.
3011-
if (Config.isStorageSupported('localStorage')) {
3011+
if (isStorageSupported('localStorage')) {
30123012
window.localStorage.favoriteTables = data.favoriteTables;
30133013
}
30143014
$('#pma_favorite_list').html(data.list);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ajaxShowMessage } from '../ajax-message.js';
2+
3+
/**
4+
* checks whether browser supports web storage
5+
*
6+
* @param {'localStorage' | 'sessionStorage'} type the type of storage i.e. localStorage or sessionStorage
7+
* @param {boolean} warn Wether to show a warning on error
8+
*
9+
* @return {boolean}
10+
*/
11+
export default function isStorageSupported (type, warn = false) {
12+
try {
13+
window[type].setItem('PMATest', 'test');
14+
// Check whether key-value pair was set successfully
15+
if (window[type].getItem('PMATest') === 'test') {
16+
// Supported, remove test variable from storage
17+
window[type].removeItem('PMATest');
18+
return true;
19+
}
20+
} catch (error) {
21+
// Not supported
22+
if (warn) {
23+
ajaxShowMessage(window.Messages.strNoLocalStorage, false);
24+
}
25+
}
26+
return false;
27+
}

js/src/modules/navigation.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ajaxRemoveMessage, ajaxShowMessage } from './ajax-message.js';
66
import handleCreateViewModal from './functions/handleCreateViewModal.js';
77
import { getConfigValue, setConfigValue } from './functions/config.js';
88
import handleRedirectAndReload from './functions/handleRedirectAndReload.js';
9+
import isStorageSupported from './functions/isStorageSupported.js';
910

1011
/**
1112
* function used in or for navigation panel
@@ -21,7 +22,7 @@ const Navigation = {};
2122
*/
2223
Navigation.treeStateUpdate = function () {
2324
// update if session storage is supported
24-
if (Config.isStorageSupported('sessionStorage')) {
25+
if (isStorageSupported('sessionStorage')) {
2526
var storage = window.sessionStorage;
2627
// try catch necessary here to detect whether
2728
// content to be stored exceeds storage capacity
@@ -48,7 +49,7 @@ Navigation.treeStateUpdate = function () {
4849
* @return {void}
4950
*/
5051
Navigation.filterStateUpdate = function (filterName, filterValue) {
51-
if (Config.isStorageSupported('sessionStorage')) {
52+
if (isStorageSupported('sessionStorage')) {
5253
var storage = window.sessionStorage;
5354
try {
5455
var currentFilter = $.extend({}, JSON.parse(storage.getItem('navTreeSearchFilters')));
@@ -68,7 +69,7 @@ Navigation.filterStateUpdate = function (filterName, filterValue) {
6869
* @return {void}
6970
*/
7071
Navigation.filterStateRestore = function () {
71-
if (Config.isStorageSupported('sessionStorage')
72+
if (isStorageSupported('sessionStorage')
7273
&& typeof window.sessionStorage.navTreeSearchFilters !== 'undefined'
7374
) {
7475
var searchClauses = JSON.parse(window.sessionStorage.navTreeSearchFilters);
@@ -536,7 +537,7 @@ Navigation.onload = () => function () {
536537
}
537538
}
538539

539-
var hasLocalStorage = Config.isStorageSupported('localStorage') &&
540+
var hasLocalStorage = isStorageSupported('localStorage') &&
540541
typeof window.localStorage.favoriteTables !== 'undefined';
541542
$.ajax({
542543
url: $self.attr('href'),
@@ -552,7 +553,7 @@ Navigation.onload = () => function () {
552553
$('#' + anchorId).parent().html(data.anchor);
553554
tooltip($('#' + anchorId), 'a', $('#' + anchorId).attr('title'));
554555
// Update localStorage.
555-
if (Config.isStorageSupported('localStorage')) {
556+
if (isStorageSupported('localStorage')) {
556557
window.localStorage.favoriteTables = data.favoriteTables;
557558
}
558559
} else {
@@ -562,7 +563,7 @@ Navigation.onload = () => function () {
562563
});
563564
});
564565
// Check if session storage is supported
565-
if (Config.isStorageSupported('sessionStorage')) {
566+
if (isStorageSupported('sessionStorage')) {
566567
var storage = window.sessionStorage;
567568
// remove tree from storage if Navi_panel config form is submitted
568569
$(document).on('submit', 'form.config-form', function () {

js/src/server/status/monitor.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import $ from 'jquery';
22
import { AJAX } from '../../modules/ajax.js';
33
import { Functions } from '../../modules/functions.js';
44
import { CommonParams } from '../../modules/common.js';
5-
import { Config } from '../../modules/config.js';
65
import tooltip from '../../modules/tooltip.js';
76
import createProfilingChart from '../../modules/functions/createProfilingChart.js';
87
import { escapeHtml } from '../../modules/functions/escape.js';
98
import getImageTag from '../../modules/functions/getImageTag.js';
9+
import isStorageSupported from '../../modules/functions/isStorageSupported.js';
1010

1111
/**
1212
* @fileoverview Javascript functions used in server status monitor page
@@ -716,15 +716,15 @@ AJAX.registerOnload('server/status/monitor.js', function () {
716716

717717
// If json ok, try applying config
718718
try {
719-
if (Config.isStorageSupported('localStorage')) {
719+
if (isStorageSupported('localStorage')) {
720720
window.localStorage.monitorCharts = JSON.stringify(json.monitorCharts);
721721
window.localStorage.monitorSettings = JSON.stringify(json.monitorSettings);
722722
}
723723
rebuildGrid();
724724
} catch (err) {
725725
alert(window.Messages.strFailedBuildingGrid);
726726
// If an exception is thrown, load default again
727-
if (Config.isStorageSupported('localStorage')) {
727+
if (isStorageSupported('localStorage')) {
728728
window.localStorage.removeItem('monitorCharts');
729729
window.localStorage.removeItem('monitorSettings');
730730
}
@@ -752,7 +752,7 @@ AJAX.registerOnload('server/status/monitor.js', function () {
752752

753753
$('a[href="#clearMonitorConfig"]').on('click', function (event) {
754754
event.preventDefault();
755-
if (Config.isStorageSupported('localStorage')) {
755+
if (isStorageSupported('localStorage')) {
756756
window.localStorage.removeItem('monitorCharts');
757757
window.localStorage.removeItem('monitorSettings');
758758
window.localStorage.removeItem('monitorVersion');
@@ -1047,7 +1047,7 @@ AJAX.registerOnload('server/status/monitor.js', function () {
10471047
var i;
10481048

10491049
/* Apply default values & config */
1050-
if (Config.isStorageSupported('localStorage')) {
1050+
if (isStorageSupported('localStorage')) {
10511051
if (typeof window.localStorage.monitorCharts !== 'undefined') {
10521052
runtime.charts = JSON.parse(window.localStorage.monitorCharts);
10531053
}
@@ -2319,7 +2319,7 @@ AJAX.registerOnload('server/status/monitor.js', function () {
23192319
gridCopy[key].maxYLabel = elem.maxYLabel;
23202320
});
23212321

2322-
if (Config.isStorageSupported('localStorage')) {
2322+
if (isStorageSupported('localStorage')) {
23232323
window.localStorage.monitorCharts = JSON.stringify(gridCopy);
23242324
window.localStorage.monitorSettings = JSON.stringify(monitorSettings);
23252325
window.localStorage.monitorVersion = monitorProtocolVersion;

js/src/sql.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { AJAX } from './modules/ajax.js';
33
import { Functions } from './modules/functions.js';
44
import { Navigation } from './modules/navigation.js';
55
import { CommonParams } from './modules/common.js';
6-
import { Config } from './modules/config.js';
76
import highlightSql from './modules/sql-highlight.js';
87
import { ajaxRemoveMessage, ajaxShowMessage } from './modules/ajax-message.js';
98
import createProfilingChart from './modules/functions/createProfilingChart.js';
109
import { escapeHtml } from './modules/functions/escape.js';
1110
import refreshMainContent from './modules/functions/refreshMainContent.js';
11+
import isStorageSupported from './modules/functions/isStorageSupported.js';
1212

1313
/**
1414
* @fileoverview functions used wherever an sql query form is used
@@ -54,7 +54,7 @@ Sql.urlEncode = function (str) {
5454
Sql.autoSave = function (query) {
5555
if (query) {
5656
var key = Sql.getAutoSavedKey();
57-
if (Config.isStorageSupported('localStorage')) {
57+
if (isStorageSupported('localStorage')) {
5858
window.localStorage.setItem(key, query);
5959
} else {
6060
window.Cookies.set(key, query, { path: CommonParams.get('rootPath') });
@@ -76,7 +76,7 @@ Sql.showThisQuery = function (db, table, query) {
7676
'table': table,
7777
'query': query
7878
};
79-
if (Config.isStorageSupported('localStorage')) {
79+
if (isStorageSupported('localStorage')) {
8080
window.localStorage.showThisQuery = 1;
8181
window.localStorage.showThisQueryObject = JSON.stringify(showThisQueryObject);
8282
} else {
@@ -92,7 +92,7 @@ Sql.showThisQuery = function (db, table, query) {
9292
Sql.setShowThisQuery = function () {
9393
var db = $('input[name="db"]').val();
9494
var table = $('input[name="table"]').val();
95-
if (Config.isStorageSupported('localStorage')) {
95+
if (isStorageSupported('localStorage')) {
9696
if (window.localStorage.showThisQueryObject !== undefined) {
9797
var storedDb = JSON.parse(window.localStorage.showThisQueryObject).db;
9898
var storedTable = JSON.parse(window.localStorage.showThisQueryObject).table;
@@ -122,7 +122,7 @@ Sql.setShowThisQuery = function () {
122122
*/
123123
Sql.autoSaveWithSort = function (query) {
124124
if (query) {
125-
if (Config.isStorageSupported('localStorage')) {
125+
if (isStorageSupported('localStorage')) {
126126
window.localStorage.setItem('autoSavedSqlSort', query);
127127
} else {
128128
window.Cookies.set('autoSavedSqlSort', query, { path: CommonParams.get('rootPath') });
@@ -136,7 +136,7 @@ Sql.autoSaveWithSort = function (query) {
136136
* @return {void}
137137
*/
138138
Sql.clearAutoSavedSort = function () {
139-
if (Config.isStorageSupported('localStorage')) {
139+
if (isStorageSupported('localStorage')) {
140140
window.localStorage.removeItem('autoSavedSqlSort');
141141
} else {
142142
window.Cookies.set('autoSavedSqlSort', '', { path: CommonParams.get('rootPath') });
@@ -309,7 +309,7 @@ const insertQuery = function (queryType) {
309309
key += '.' + table;
310310
}
311311
key = 'autoSavedSql_' + key;
312-
if (Config.isStorageSupported('localStorage') &&
312+
if (isStorageSupported('localStorage') &&
313313
typeof window.localStorage.getItem(key) === 'string') {
314314
setQuery(window.localStorage.getItem(key));
315315
} else if (window.Cookies.get(key, { path: CommonParams.get('rootPath') })) {
@@ -471,7 +471,7 @@ AJAX.registerOnload('sql.js', function () {
471471
$('#sqlquery').on('input propertychange', function () {
472472
Sql.autoSave($('#sqlquery').val());
473473
});
474-
var useLocalStorageValue = Config.isStorageSupported('localStorage') && typeof window.localStorage.autoSavedSqlSort !== 'undefined';
474+
var useLocalStorageValue = isStorageSupported('localStorage') && typeof window.localStorage.autoSavedSqlSort !== 'undefined';
475475
// Save sql query with sort
476476
if ($('#RememberSorting') !== undefined && $('#RememberSorting').is(':checked')) {
477477
$('select[name="sql_query"]').on('change', function () {
@@ -1219,7 +1219,7 @@ Sql.getAutoSavedKey = function () {
12191219
Sql.checkSavedQuery = function () {
12201220
var key = Sql.getAutoSavedKey();
12211221

1222-
if (Config.isStorageSupported('localStorage') &&
1222+
if (isStorageSupported('localStorage') &&
12231223
typeof window.localStorage.getItem(key) === 'string') {
12241224
ajaxShowMessage(window.Messages.strPreviousSaveQuery);
12251225
} else if (window.Cookies.get(key, { path: CommonParams.get('rootPath') })) {

0 commit comments

Comments
 (0)