From 49b318c39e932b81472f2b186c3d242fedd41f2d Mon Sep 17 00:00:00 2001 From: Aditya Toshniwal Date: Thu, 18 Apr 2019 15:39:36 +0530 Subject: [PATCH] Ensure that confirmation dialog should be popped up before reload of query tool or debugger if it is opened in a new browser tab. Fixes #4101 --- docs/en_US/release_notes_4_6.rst | 1 + web/pgadmin/browser/static/js/browser.js | 9 +++++-- web/pgadmin/tools/datagrid/__init__.py | 2 +- .../tools/datagrid/static/js/datagrid.js | 2 +- .../datagrid/templates/datagrid/index.html | 22 +++++++++++++---- .../tools/debugger/static/js/debugger.js | 12 +++++----- .../debugger/templates/debugger/direct.html | 24 +++++++++++++++---- 7 files changed, 52 insertions(+), 20 deletions(-) diff --git a/docs/en_US/release_notes_4_6.rst b/docs/en_US/release_notes_4_6.rst index fc6b06075..39c1775ca 100644 --- a/docs/en_US/release_notes_4_6.rst +++ b/docs/en_US/release_notes_4_6.rst @@ -19,6 +19,7 @@ Bug fixes | `Bug #3582 `_ - Ensure that JSON strings as comments should be added properly for all the objects. | `Bug #3605 `_ - Fix an issue where Deleting N number of rows makes first N number of rows disable. | `Bug #3938 `_ - Added support for Default Partition. +| `Bug #4101 `_ - Ensure that confirmation dialog should be popped up before reload of query tool or debugger if it is opened in a new browser tab. | `Bug #4104 `_ - Ensure that record should be add/edited for root partition table with primary keys. | `Bug #4121 `_ - Fixed alignment issue of columns in definition section of Index node. | `Bug #4134 `_ - Fixed 'Location cannot be empty' error when open Tablespace properties. diff --git a/web/pgadmin/browser/static/js/browser.js b/web/pgadmin/browser/static/js/browser.js index 3ba7a3180..62610bec2 100644 --- a/web/pgadmin/browser/static/js/browser.js +++ b/web/pgadmin/browser/static/js/browser.js @@ -1945,12 +1945,17 @@ define('pgadmin.browser', [ } $(window).on('beforeunload', function(e) { + /* Can open you in new tab */ + let openerBrowser = window.opener ? + window.opener.pgAdmin.Browser : window.top.pgAdmin.Browser; + let tree_save_interval = pgBrowser.get_preference('browser', 'browser_tree_state_save_interval'), - confirm_on_refresh_close = pgBrowser.get_preference('browser', 'confirm_on_refresh_close'); + confirm_on_refresh_close = openerBrowser.get_preference('browser', 'confirm_on_refresh_close'); + if (!_.isUndefined(tree_save_interval) && tree_save_interval.value !== -1) pgAdmin.Browser.browserTreeState.save_state(); - if(confirm_on_refresh_close.value) { + if(!_.isUndefined(confirm_on_refresh_close) && confirm_on_refresh_close.value) { /* This message will not be displayed in Chrome, Firefox, Safari as they have disabled it*/ let msg = S(gettext('Are you sure you want to close the %s browser?')).sprintf(pgBrowser.utils.app_name).value(); e.originalEvent.returnValue = msg; diff --git a/web/pgadmin/tools/datagrid/__init__.py b/web/pgadmin/tools/datagrid/__init__.py index 05e723ea7..c0f61e9f0 100644 --- a/web/pgadmin/tools/datagrid/__init__.py +++ b/web/pgadmin/tools/datagrid/__init__.py @@ -411,7 +411,7 @@ def initialize_query_tool(sgid, sid, did=None): ) -@blueprint.route('/close/', methods=["GET"], endpoint='close') +@blueprint.route('/close/', methods=["DELETE"], endpoint='close') def close(trans_id): """ This method is used to close the asynchronous connection diff --git a/web/pgadmin/tools/datagrid/static/js/datagrid.js b/web/pgadmin/tools/datagrid/static/js/datagrid.js index b22034e02..3e2e1440c 100644 --- a/web/pgadmin/tools/datagrid/static/js/datagrid.js +++ b/web/pgadmin/tools/datagrid/static/js/datagrid.js @@ -505,7 +505,7 @@ define('pgadmin.datagrid', [ queryToolPanel.on(wcDocker.EVENT.CLOSED, function() { $.ajax({ url: url_for('datagrid.close', {'trans_id': trans_obj.gridTransId}), - method: 'GET', + method: 'DELETE', }); }); diff --git a/web/pgadmin/tools/datagrid/templates/datagrid/index.html b/web/pgadmin/tools/datagrid/templates/datagrid/index.html index 594608310..f138b9a42 100644 --- a/web/pgadmin/tools/datagrid/templates/datagrid/index.html +++ b/web/pgadmin/tools/datagrid/templates/datagrid/index.html @@ -368,12 +368,24 @@ msgDiv = loadingDiv.find('.sql-editor-busy-text'); // Register unload event on window close. - window.onbeforeunload = function(ev) { - $.ajax({ - url: "{{ url_for('datagrid.index') }}" + "close/" + {{ uniqueId }}, - method: 'GET' + /* If opened in new tab, close the connection only on tab/window close and + * not on refresh attempt because the user may cancel the reload + */ + if(window.opener) { + $(window).on('unload', function(ev) { + $.ajax({ + method: 'DELETE', + url: "{{ url_for('datagrid.index') }}" + "close/" + {{ uniqueId }} + }); }); - }; + } else { + $(window).on('beforeunload', function(ev) { + $.ajax({ + method: 'DELETE', + url: "{{ url_for('datagrid.index') }}" + "close/" + {{ uniqueId }} + }); + }); + } // Get the controller object from pgAdmin.SqlEditor var sqlEditorController = pgAdmin.SqlEditor.create(editorPanel); diff --git a/web/pgadmin/tools/debugger/static/js/debugger.js b/web/pgadmin/tools/debugger/static/js/debugger.js index ed0f6b796..2ee1d55c2 100644 --- a/web/pgadmin/tools/debugger/static/js/debugger.js +++ b/web/pgadmin/tools/debugger/static/js/debugger.js @@ -196,14 +196,14 @@ define([ let self = this; let cacheIntervalId = setInterval(function() { - try { - self.preferences = window.top.pgAdmin.Browser; + if(pgBrowser.preference_version() > 0) { + self.preferences = pgBrowser.get_preferences_for_module('debugger'); clearInterval(cacheIntervalId); } - catch(err) { - clearInterval(cacheIntervalId); - throw err; - } + },0); + + pgBrowser.onPreferencesChange('debugger', function() { + self.preferences = pgBrowser.get_preferences_for_module('debugger'); }); }, // It will check weather the function is actually debuggable or not with pre-required condition. diff --git a/web/pgadmin/tools/debugger/templates/debugger/direct.html b/web/pgadmin/tools/debugger/templates/debugger/direct.html index d5a97138b..f4965c4f4 100644 --- a/web/pgadmin/tools/debugger/templates/debugger/direct.html +++ b/web/pgadmin/tools/debugger/templates/debugger/direct.html @@ -10,12 +10,26 @@ try { var $ = pgDirectDebug.jquery; pgDirectDebug.load({{ uniqueId }}, {{ debug_type }}, '{{ function_name_with_arguments }}', '{{layout|safe}}'); - window.onbeforeunload = function(ev) { - $.ajax({ - url: "{{ url_for('debugger.index') }}close/{{ uniqueId }}", - method: 'DELETE' + + // Register unload event on window close. + /* If opened in new tab, close the connection only on tab/window close and + * not on refresh attempt because the user may cancel the reload + */ + if(window.opener) { + $(window).on('unload', function(ev) { + $.ajax({ + method: 'DELETE', + url: "{{ url_for('debugger.index') }}close/{{ uniqueId }}" + }); }); - }; + } else { + $(window).on('beforeunload', function(ev) { + $.ajax({ + method: 'DELETE', + url: "{{ url_for('debugger.index') }}close/{{ uniqueId }}" + }); + }); + } }, function() { console.log(arguments);