From aea0d933903b3eb469e41a00f3d6c556f5bbcb57 Mon Sep 17 00:00:00 2001 From: Murtuza Zabuawala Date: Fri, 12 May 2017 12:10:46 +0100 Subject: [PATCH] Hide menu options for creating objects, if the object type is set to hidden. Includes Jasmine tests. Fixes #2225 --- .../browser/templates/browser/js/browser.js | 31 +++++++++-- .../static/js/check_node_visibility.js | 53 +++++++++++++++++++ .../javascript/check_node_visiblity_spec.js | 32 +++++++++++ web/regression/javascript/test-main.js | 1 + 4 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 web/pgadmin/static/js/check_node_visibility.js create mode 100644 web/regression/javascript/check_node_visiblity_spec.js diff --git a/web/pgadmin/browser/templates/browser/js/browser.js b/web/pgadmin/browser/templates/browser/js/browser.js index a663ae86a..1f93f0b98 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 000000000..987a395f7 --- /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 000000000..130b1c216 --- /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 dfa4107a8..fe28a5f87 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',