Hi,
Please find updated patch with changes as per comments given by Joao.
@Joao,
Thank you for reviewing the patch.
--
Regards,
Murtuza Zabuawala
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
On Thu, May 11, 2017 at 8:19 PM, Joao Pedro De Almeida Pereira <
[email protected]> wrote:
> Hi Murtuza,
>
> Since you are adding a new function to the browser.js component, we would
> encourage you to extract the function out of the templated javascript file
> so that it can be tested. A good example of this would be the
> pgadmin/static/js/size_prettify.js.
>
> For this specific function we found one function that is used from
> browser.js, and you can Stub that out using
> createSpyObject(browser, 'get_preference');
>
> We think the object here is not 100% line coverage but 100% of the
> behavior should be covered.
>
> Also, suggest you change the variable name perf to preference. Looks like
> it is a typo.
>
> Regards,
> João & Matt
>
> On Thu, May 11, 2017 at 3:08 AM, Murtuza Zabuawala <
> [email protected]> wrote:
>
>> Hi,
>>
>> PFA patch to fix the issue where user hides the any node from Preference
>> dialog but the menu(both context/object) still appears in pgAdmin4.
>> RM#2225
>>
>> --
>> Regards,
>> Murtuza Zabuawala
>> EnterpriseDB: http://www.enterprisedb.com
>> The Enterprise PostgreSQL Company
>>
>>
>> --
>> Sent via pgadmin-hackers mailing list ([email protected])
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgadmin-hackers
>>
>>
>
diff --git a/web/pgadmin/browser/templates/browser/js/browser.js
b/web/pgadmin/browser/templates/browser/js/browser.js
index a663ae8..1f93f0b 100644
--- a/web/pgadmin/browser/templates/browser/js/browser.js
+++ b/web/pgadmin/browser/templates/browser/js/browser.js
@@ -1,6 +1,7 @@
define('pgadmin.browser',
['require', 'jquery', 'underscore', 'underscore.string', 'bootstrap',
- 'pgadmin', 'alertify', 'codemirror', 'codemirror/mode/sql/sql',
'wcdocker',
+ 'pgadmin', 'alertify', 'codemirror', 'sources/check_node_visibility',
+ 'codemirror/mode/sql/sql', 'wcdocker',
'jquery.contextmenu', 'jquery.aciplugin', 'jquery.acitree',
'pgadmin.alertifyjs', 'pgadmin.browser.messages',
'pgadmin.browser.menu', 'pgadmin.browser.panel',
@@ -8,7 +9,10 @@ define('pgadmin.browser',
'pgadmin.browser.node', 'pgadmin.browser.collection'
],
-function(require, $, _, S, Bootstrap, pgAdmin, Alertify, CodeMirror) {
+function(
+ require, $, _, S, Bootstrap, pgAdmin, Alertify,
+ CodeMirror, checkNodeVisibility
+) {
// Some scripts do export their object in the window only.
// Generally the one, which do no have AMD support.
@@ -593,10 +597,16 @@ function(require, $, _, S, Bootstrap, pgAdmin, Alertify,
CodeMirror) {
single: single
}
},
+
+ // This will hold preference data (Works as a cache object)
+ // Here node will be a key and it's preference data will be value
+ node_preference_data: {},
+
// Add menus of module/extension at appropriate menu
add_menus: function(menus) {
- var pgMenu = this.menus;
- var MenuItem = pgAdmin.Browser.MenuItem;
+ var self = this,
+ pgMenu = this.menus,
+ MenuItem = pgAdmin.Browser.MenuItem;
_.each(menus, function(m) {
_.each(m.applies, function(a) {
/* We do support menu type only from this list */
@@ -604,6 +614,19 @@ function(require, $, _, S, Bootstrap, pgAdmin, Alertify,
CodeMirror) {
'context', 'file', 'edit', 'object',
'management', 'tools', 'help']) >= 0) {
var menus;
+
+ // If current node is not visible in browser tree
+ // then return from here
+ if(!checkNodeVisibility(self, m.node)) {
+ return;
+ } else if(_.has(m, 'module') && !_.isUndefined(m.module)) {
+ // If module to which this menu applies is not visible in
+ // browser tree then also we do not display menu
+ if(!checkNodeVisibility(self, m.module.type)) {
+ return;
+ }
+ }
+
pgMenu[a] = pgMenu[a] || {};
if (_.isString(m.node)) {
menus = pgMenu[a][m.node] = pgMenu[a][m.node] || {};
diff --git a/web/pgadmin/static/js/check_node_visibility.js
b/web/pgadmin/static/js/check_node_visibility.js
new file mode 100644
index 0000000..987a395
--- /dev/null
+++ b/web/pgadmin/static/js/check_node_visibility.js
@@ -0,0 +1,53 @@
+//////////////////////////////////////////////////////////////////////////
+//
+// pgAdmin 4 - PostgreSQL Tools
+//
+// Copyright (C) 2013 - 2017, The pgAdmin Development Team
+// This software is released under the PostgreSQL Licence
+//
+//////////////////////////////////////////////////////////////////////////
+
+define(['jquery', 'underscore', 'underscore.string'],
+ function ($, _, S) {
+
+ var check_node_visibility = function (pgBrowser, node_type) {
+ if(_.isUndefined(node_type) || _.isNull(node_type)) {
+ return true;
+ }
+
+ // Target actual node instead of collection.
+ // If node is disabled then there is no meaning of
+ // adding collection node menu
+ if(S.startsWith(node_type, "coll-")) {
+ node_type = node_type.replace("coll-", "")
+ }
+
+ // Exclude non-applicable nodes
+ var nodes_not_supported = [
+ "server-group", "server", "catalog_object_column"
+ ];
+ if(_.indexOf(nodes_not_supported, node_type) >= 0) {
+ return true;
+ }
+
+ // If we have already fetched preference earlier then pick
+ // it from our cache object
+ if (_.has(pgBrowser.node_preference_data, node_type)) {
+ return pgBrowser.node_preference_data[node_type].value
+ }
+
+ var preference = pgBrowser.get_preference(
+ 'browser', 'show_node_' + node_type
+ );
+
+ // Save it for future use, kind of caching
+ if(!_.isUndefined(preference) && !_.isNull(preference)) {
+ pgBrowser.node_preference_data[node_type] = preference;
+ return preference.value;
+ } else {
+ return true;
+ }
+ }
+
+ return check_node_visibility;
+});
diff --git a/web/regression/javascript/check_node_visiblity_spec.js
b/web/regression/javascript/check_node_visiblity_spec.js
new file mode 100644
index 0000000..130b1c2
--- /dev/null
+++ b/web/regression/javascript/check_node_visiblity_spec.js
@@ -0,0 +1,32 @@
+//////////////////////////////////////////////////////////////////////////
+//
+// pgAdmin 4 - PostgreSQL Tools
+//
+// Copyright (C) 2013 - 2017, The pgAdmin Development Team
+// This software is released under the PostgreSQL Licence
+//
+//////////////////////////////////////////////////////////////////////////
+
+define(["sources/check_node_visibility"],
+function (checkNodeVisibility, pgBrowser) {
+ describe("checkNodeVisibility", function () {
+
+ var browser;
+
+ browser = jasmine.createSpyObj('browser', [
+ 'node_preference_data', 'get_preference']
+ );
+
+ describe("when node is server collection", function () {
+ it("returns true", function () {
+ expect(checkNodeVisibility(browser, 'coll-server')).toEqual(true);
+ });
+ });
+
+ describe("when node is server", function () {
+ it("returns true", function () {
+ expect(checkNodeVisibility(browser, 'server')).toEqual(true);
+ });
+ });
+ });
+});
diff --git a/web/regression/javascript/test-main.js
b/web/regression/javascript/test-main.js
index dfa4107..fe28a5f 100644
--- a/web/regression/javascript/test-main.js
+++ b/web/regression/javascript/test-main.js
@@ -32,6 +32,7 @@ require.config({
'jquery.ui': sourcesDir + 'vendor/jquery-ui/jquery-ui-1.11.3',
'jquery.event.drag': sourcesDir + 'vendor/jquery-ui/jquery.event.drag-2.2',
'underscore': sourcesDir + 'vendor/underscore/underscore',
+ 'underscore.string': sourcesDir + 'vendor/underscore/underscore.string',
'slickgrid': sourcesDir + 'vendor/slickgrid/slick.core',
'slickgrid/slick.grid': sourcesDir + 'vendor/slickgrid/slick.grid',
'slickgrid/slick.rowselectionmodel': sourcesDir +
'vendor/slickgrid/plugins/slick.rowselectionmodel',
--
Sent via pgadmin-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers