Skip to content

Commit 4e40aef

Browse files
committed
Extract CommonActions.refreshMain into a module
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 94b9c9b commit 4e40aef

File tree

12 files changed

+72
-77
lines changed

12 files changed

+72
-77
lines changed

js/src/database/operations.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import $ from 'jquery';
22
import { AJAX } from '../modules/ajax.js';
33
import { Functions } from '../modules/functions.js';
44
import { Navigation } from '../modules/navigation.js';
5-
import { CommonActions, CommonParams } from '../modules/common.js';
5+
import { CommonParams } from '../modules/common.js';
66
import { ajaxShowMessage } from '../modules/ajax-message.js';
77
import getJsConfirmCommonParam from '../modules/functions/getJsConfirmCommonParam.js';
88
import { escapeHtml } from '../modules/functions/escape.js';
9+
import refreshMainContent from '../modules/functions/refreshMainContent.js';
910

1011
/**
1112
* @fileoverview function used in server privilege pages
@@ -106,7 +107,7 @@ AJAX.registerOnload('database/operations.js', function () {
106107
if (typeof data !== 'undefined' && data.success === true) {
107108
if ($('#checkbox_switch').is(':checked')) {
108109
CommonParams.set('db', data.newname);
109-
CommonActions.refreshMain(false);
110+
refreshMainContent(false);
110111
AJAX.callback = () => {
111112
ajaxShowMessage(data.message);
112113
};
@@ -169,7 +170,7 @@ AJAX.registerOnload('database/operations.js', function () {
169170
// Database deleted successfully, refresh both the frames
170171
Navigation.reload();
171172
CommonParams.set('db', '');
172-
CommonActions.refreshMain('index.php?route=/server/databases');
173+
refreshMainContent('index.php?route=/server/databases');
173174
AJAX.callback = () => {
174175
ajaxShowMessage(data.message);
175176
};

js/src/designer/move.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import $ from 'jquery';
22
import { AJAX } from '../modules/ajax.js';
33
import { Functions } from '../modules/functions.js';
4-
import { CommonActions, CommonParams } from '../modules/common.js';
4+
import { CommonParams } from '../modules/common.js';
55
import { ajaxRemoveMessage, ajaxShowMessage } from '../modules/ajax-message.js';
6+
import refreshMainContent from '../modules/functions/refreshMainContent.js';
67

78
/**
89
* @package PhpMyAdmin-Designer
@@ -1340,13 +1341,13 @@ DesignerMove.newRelation = function () {
13401341
// -------------------------- create tables -------------------------------------
13411342
DesignerMove.startTableNew = function () {
13421343
CommonParams.set('table', '');
1343-
CommonActions.refreshMain('index.php?route=/table/create');
1344+
refreshMainContent('index.php?route=/table/create');
13441345
};
13451346

13461347
DesignerMove.startTabUpd = function (db, table) {
13471348
CommonParams.set('db', db);
13481349
CommonParams.set('table', table);
1349-
CommonActions.refreshMain('index.php?route=/table/structure');
1350+
refreshMainContent('index.php?route=/table/structure');
13501351
};
13511352

13521353
// --------------------------- hide tables --------------------------------------

js/src/modules/ajax.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const AJAX = {
3636
// eslint-disable-next-line valid-jsdoc
3737
/**
3838
* @var {Function} callback Callback to execute after a successful request
39-
* Used by CommonActions from common.js
4039
*/
4140
callback: function () {
4241
},

js/src/modules/common.js

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -104,52 +104,6 @@ const CommonParams = (function () {
104104
};
105105
}());
106106

107-
/**
108-
* Holds common parameters such as server, db, table, etc
109-
*
110-
* The content for this is normally loaded from Header.php or
111-
* Response.php and executed by ajax.js
112-
*/
113-
const CommonActions = {
114-
/**
115-
* Saves the database name when it's changed
116-
* and reloads the query window, if necessary
117-
*
118-
* @param {string} newDb new_db The name of the new database
119-
*
120-
* @return {void}
121-
*/
122-
setDb: function (newDb) {
123-
if (newDb !== CommonParams.get('db')) {
124-
CommonParams.setAll({ 'db': newDb, 'table': '' });
125-
}
126-
},
127-
/**
128-
* Refreshes the main frame
129-
*
130-
* @param {any} url Undefined to refresh to the same page
131-
* String to go to a different page, e.g: 'index.php'
132-
*
133-
* @return {void}
134-
*/
135-
refreshMain: function (url) {
136-
var newUrl = url;
137-
if (! newUrl) {
138-
newUrl = $('#selflink').find('a').attr('href') || window.location.pathname;
139-
newUrl = newUrl.substring(0, newUrl.indexOf('?'));
140-
}
141-
if (newUrl.indexOf('?') !== -1) {
142-
newUrl += CommonParams.getUrlQuery(CommonParams.get('arg_separator'));
143-
} else {
144-
newUrl += CommonParams.getUrlQuery('?');
145-
}
146-
$('<a></a>', { href: newUrl })
147-
.appendTo('body')
148-
.trigger('click')
149-
.remove();
150-
}
151-
};
152-
153107
window.CommonParams = CommonParams;
154108

155-
export { CommonActions, CommonParams };
109+
export { CommonParams };

js/src/modules/functions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import $ from 'jquery';
22
import { AJAX } from './ajax.js';
33
import { Navigation } from './navigation.js';
4-
import { CommonActions, CommonParams } from './common.js';
4+
import { CommonParams } from './common.js';
55
import { Indexes } from './indexes.js';
66
import { Config } from './config.js';
77
import tooltip from './tooltip.js';
@@ -11,6 +11,7 @@ import handleCreateViewModal from './functions/handleCreateViewModal.js';
1111
import { escapeHtml } from './functions/escape.js';
1212
import getImageTag from './functions/getImageTag.js';
1313
import handleRedirectAndReload from './functions/handleRedirectAndReload.js';
14+
import refreshMainContent from './functions/refreshMainContent.js';
1415

1516
/* global DatabaseStructure */ // js/database/structure.js
1617
/* global firstDayOfCalendar, themeImagePath */ // templates/javascript/variables.twig
@@ -1815,7 +1816,7 @@ Functions.onloadCreateTableEvents = function () {
18151816
var tablesTable = $('#tablesForm').find('tbody').not('#tbl_summary_row');
18161817
// this is the first table created in this db
18171818
if (tablesTable.length === 0) {
1818-
CommonActions.refreshMain(CommonParams.get('opendb_url'));
1819+
refreshMainContent(CommonParams.get('opendb_url'));
18191820
} else {
18201821
/**
18211822
* @var curr_last_row Object referring to the last <tr> element in {@link tablesTable}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import $ from 'jquery';
2+
import { CommonParams } from '../common.js';
3+
4+
/**
5+
* Refreshes the main frame
6+
*
7+
* @param {any} url Undefined to refresh to the same page
8+
* String to go to a different page, e.g: 'index.php'
9+
*
10+
* @return {void}
11+
*/
12+
export default function refreshMainContent (url) {
13+
var newUrl = url;
14+
if (! newUrl) {
15+
newUrl = $('#selflink').find('a').attr('href') || window.location.pathname;
16+
newUrl = newUrl.substring(0, newUrl.indexOf('?'));
17+
}
18+
if (newUrl.indexOf('?') !== -1) {
19+
newUrl += CommonParams.getUrlQuery(CommonParams.get('arg_separator'));
20+
} else {
21+
newUrl += CommonParams.getUrlQuery('?');
22+
}
23+
$('<a></a>', { href: newUrl })
24+
.appendTo('body')
25+
.trigger('click')
26+
.remove();
27+
}

js/src/modules/indexes.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import $ from 'jquery';
22
import { AJAX } from './ajax.js';
33
import { Functions } from './functions.js';
44
import { Navigation } from './navigation.js';
5-
import { CommonActions, CommonParams } from './common.js';
5+
import { CommonParams } from './common.js';
66
import highlightSql from './sql-highlight.js';
77
import { ajaxRemoveMessage, ajaxShowMessage } from './ajax-message.js';
88
import getJsConfirmCommonParam from './functions/getJsConfirmCommonParam.js';
9+
import refreshMainContent from './functions/refreshMainContent.js';
910

1011
/**
1112
* @fileoverview function used for index manipulation pages
@@ -698,7 +699,7 @@ Indexes.on = () => function () {
698699
highlightSql($('#page_content'));
699700
}
700701
Navigation.reload();
701-
CommonActions.refreshMain('index.php?route=/table/structure');
702+
refreshMainContent('index.php?route=/table/structure');
702703
} else {
703704
ajaxShowMessage(window.Messages.strErrorProcessingRequest + ' : ' + data.error, false);
704705
}
@@ -734,7 +735,7 @@ Indexes.on = () => function () {
734735
Functions.indexEditorDialog(url, title, function (data) {
735736
CommonParams.set('db', data.params.db);
736737
CommonParams.set('table', data.params.table);
737-
CommonActions.refreshMain('index.php?route=/table/structure');
738+
refreshMainContent('index.php?route=/table/structure');
738739
});
739740
});
740741

@@ -749,7 +750,7 @@ Indexes.on = () => function () {
749750
Functions.indexRenameDialog(url, title, function (data) {
750751
CommonParams.set('db', data.params.db);
751752
CommonParams.set('table', data.params.table);
752-
CommonActions.refreshMain('index.php?route=/table/structure');
753+
refreshMainContent('index.php?route=/table/structure');
753754
});
754755
});
755756

js/src/server/databases.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import $ from 'jquery';
22
import { AJAX } from '../modules/ajax.js';
33
import { Functions } from '../modules/functions.js';
44
import { Navigation } from '../modules/navigation.js';
5-
import { CommonActions, CommonParams } from '../modules/common.js';
5+
import { CommonParams } from '../modules/common.js';
66
import { ajaxShowMessage } from '../modules/ajax-message.js';
77
import getJsConfirmCommonParam from '../modules/functions/getJsConfirmCommonParam.js';
88
import { escapeHtml } from '../modules/functions/escape.js';
9+
import refreshMainContent from '../modules/functions/refreshMainContent.js';
910

1011
/**
1112
* @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
@@ -68,7 +69,7 @@ const DropDatabases = {
6869
$form.find('tbody').sortTable('.name');
6970
if ($form.find('tbody').find('tr').length === 0) {
7071
// user just dropped the last db on this page
71-
CommonActions.refreshMain();
72+
refreshMainContent();
7273
}
7374
Navigation.reload();
7475
} else {
@@ -132,7 +133,10 @@ function checkPrivilegesForDatabase () {
132133
var tableRows = $('.server_databases');
133134
$.each(tableRows, function () {
134135
$(this).on('click', function () {
135-
CommonActions.setDb($(this).attr('data'));
136+
const db = $(this).attr('data');
137+
if (db !== CommonParams.get('db')) {
138+
CommonParams.setAll({ 'db': db, 'table': '' });
139+
}
136140
});
137141
});
138142
}

js/src/sql.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import $ from 'jquery';
22
import { AJAX } from './modules/ajax.js';
33
import { Functions } from './modules/functions.js';
44
import { Navigation } from './modules/navigation.js';
5-
import { CommonActions, CommonParams } from './modules/common.js';
5+
import { CommonParams } from './modules/common.js';
66
import { Config } from './modules/config.js';
77
import highlightSql from './modules/sql-highlight.js';
88
import { ajaxRemoveMessage, ajaxShowMessage } from './modules/ajax-message.js';
99
import createProfilingChart from './modules/functions/createProfilingChart.js';
1010
import { escapeHtml } from './modules/functions/escape.js';
11+
import refreshMainContent from './modules/functions/refreshMainContent.js';
1112

1213
/**
1314
* @fileoverview functions used wherever an sql query form is used
@@ -823,14 +824,17 @@ AJAX.registerOnload('sql.js', function () {
823824
if (data.ajax_reload.reload) {
824825
if (data.ajax_reload.table_name) {
825826
CommonParams.set('table', data.ajax_reload.table_name);
826-
CommonActions.refreshMain();
827+
refreshMainContent();
827828
} else {
828829
Navigation.reload();
829830
}
830831
}
831832
} else if (typeof data.reload !== 'undefined') {
832833
// this happens if a USE or DROP command was typed
833-
CommonActions.setDb(data.db);
834+
if (data.db !== CommonParams.get('db')) {
835+
CommonParams.setAll({ 'db': data.db, 'table': '' });
836+
}
837+
834838
var url;
835839
if (data.db) {
836840
if (data.table) {
@@ -841,7 +845,7 @@ AJAX.registerOnload('sql.js', function () {
841845
} else {
842846
url = 'index.php?route=/server/sql';
843847
}
844-
CommonActions.refreshMain(url);
848+
refreshMainContent(url);
845849
AJAX.callback = () => {
846850
$('#sqlqueryresultsouter')
847851
.show()

js/src/table/operations.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import $ from 'jquery';
22
import { AJAX } from '../modules/ajax.js';
33
import { Functions } from '../modules/functions.js';
44
import { Navigation } from '../modules/navigation.js';
5-
import { CommonActions, CommonParams } from '../modules/common.js';
5+
import { CommonParams } from '../modules/common.js';
66
import highlightSql from '../modules/sql-highlight.js';
77
import { ajaxRemoveMessage, ajaxShowMessage } from '../modules/ajax-message.js';
88
import getJsConfirmCommonParam from '../modules/functions/getJsConfirmCommonParam.js';
99
import { escapeHtml } from '../modules/functions/escape.js';
10+
import refreshMainContent from '../modules/functions/refreshMainContent.js';
1011

1112
/**
1213
* Unbind all event handlers before tearing down a page
@@ -91,7 +92,7 @@ AJAX.registerOnload('table/operations.js', function () {
9192
'table',
9293
$form.find('input[name=\'new_name\']').val()
9394
);
94-
CommonActions.refreshMain(false);
95+
refreshMainContent(false);
9596
AJAX.callback = () => {
9697
ajaxShowMessage(data.message);
9798
};
@@ -118,7 +119,7 @@ AJAX.registerOnload('table/operations.js', function () {
118119
if (typeof data !== 'undefined' && data.success === true) {
119120
CommonParams.set('db', data.params.db);
120121
CommonParams.set('table', data.params.table);
121-
CommonActions.refreshMain('index.php?route=/table/sql');
122+
refreshMainContent('index.php?route=/table/sql');
122123
AJAX.callback = () => {
123124
ajaxShowMessage(data.message);
124125
};
@@ -168,7 +169,7 @@ AJAX.registerOnload('table/operations.js', function () {
168169
$.post($form.attr('action'), $form.serialize(), function (data) {
169170
if (typeof data !== 'undefined' && data.success === true) {
170171
CommonParams.set('table', data.params.table);
171-
CommonActions.refreshMain(false);
172+
refreshMainContent(false);
172173
AJAX.callback = () => {
173174
$('#page_content').html(data.message);
174175
highlightSql($('#page_content'));
@@ -293,7 +294,7 @@ AJAX.registerOnload('table/operations.js', function () {
293294
// Table deleted successfully, refresh both the frames
294295
Navigation.reload();
295296
CommonParams.set('table', '');
296-
CommonActions.refreshMain(CommonParams.get('opendb_url'));
297+
refreshMainContent(CommonParams.get('opendb_url'));
297298
AJAX.callback = () => {
298299
ajaxShowMessage(data.message);
299300
};
@@ -325,7 +326,7 @@ AJAX.registerOnload('table/operations.js', function () {
325326
// Table deleted successfully, refresh both the frames
326327
Navigation.reload();
327328
CommonParams.set('table', '');
328-
CommonActions.refreshMain(CommonParams.get('opendb_url'));
329+
refreshMainContent(CommonParams.get('opendb_url'));
329330
AJAX.callback = () => {
330331
ajaxShowMessage(data.message);
331332
};

0 commit comments

Comments
 (0)