From a36cb5ae101051655213d64121e65e5703f6868a Mon Sep 17 00:00:00 2001 From: Dave Page Date: Fri, 20 Feb 2015 09:56:02 -0500 Subject: [PATCH] Make context menu items hookable. --- .../browser/nodes/server_groups/hooks.py | 11 ++++++++ .../templates/server_groups/server_groups.js | 22 ++++++++-------- .../browser/templates/browser/js/browser.js | 26 +++++++++---------- web/pgadmin/browser/views.py | 18 +++++++++++-- 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/web/pgadmin/browser/nodes/server_groups/hooks.py b/web/pgadmin/browser/nodes/server_groups/hooks.py index eda2ff169..0cb0d27af 100644 --- a/web/pgadmin/browser/nodes/server_groups/hooks.py +++ b/web/pgadmin/browser/nodes/server_groups/hooks.py @@ -24,17 +24,28 @@ def get_nodes(): value = value[:-1] return value + def get_file_menu_items(): """Return a (set) of dicts of file menu items, with name, priority and URL.""" return [ {'name': 'Add a server group...', 'priority': 10, 'url': '#', 'onclick': 'add_server_group()'} ] + + +def get_context_menu_items(): + """Return a (set) of dicts of content menu items with name, text, priority and JS""" + return [ + {'name': 'delete', 'label': 'Delete server group', 'priority': 100, 'onclick': 'delete_server_group(item);'}, + {'name': 'rename', 'label': 'Rename server group...', 'priority': 200, 'onclick': 'rename_server_group(item);'} + ] + def get_script_snippets(): """Return the script snippets needed to handle treeview node operations.""" return render_template('server_groups/server_groups.js') + def get_css_snippets(): """Return the CSS needed to display the treeview node image.""" css = ".icon-server-group {\n" diff --git a/web/pgadmin/browser/nodes/server_groups/templates/server_groups/server_groups.js b/web/pgadmin/browser/nodes/server_groups/templates/server_groups/server_groups.js index b8b5f3d6d..de40ee3c2 100644 --- a/web/pgadmin/browser/nodes/server_groups/templates/server_groups/server_groups.js +++ b/web/pgadmin/browser/nodes/server_groups/templates/server_groups/server_groups.js @@ -18,7 +18,7 @@ function add_server_group() { icon: 'icon-server-group' } - treeApi.append(null, { + tree.append(null, { itemData: item }); @@ -35,21 +35,21 @@ function add_server_group() { function delete_server_group(item) { alertify.confirm( 'Delete server group?', - 'Are you sure you wish to delete the server group "{0}"?'.replace('{0}', treeApi.getLabel(item)), + 'Are you sure you wish to delete the server group "{0}"?'.replace('{0}', tree.getLabel(item)), function() { - var id = treeApi.getId(item) + var id = tree.getId(item) $.post("{{ url_for('NODE-server-group.delete') }}", { id: id }) .done(function(data) { if (data.success == 0) { report_error(data.errormsg, data.info); } else { - var next = treeApi.next(item); - var prev = treeApi.prev(item); - treeApi.remove(item); + var next = tree.next(item); + var prev = tree.prev(item); + tree.remove(item); if (next.length) { - treeApi.select(next); + tree.select(next); } else if (prev.length) { - treeApi.select(prev); + tree.select(prev); } } } @@ -64,15 +64,15 @@ function rename_server_group(item) { alertify.prompt( 'Rename server group', 'Enter a new name for the server group', - treeApi.getLabel(item), + tree.getLabel(item), function(evt, value) { - var id = treeApi.getId(item) + var id = tree.getId(item) $.post("{{ url_for('NODE-server-group.rename') }}", { id: id, name: value }) .done(function(data) { if (data.success == 0) { report_error(data.errormsg, data.info); } else { - treeApi.setLabel(item, { label: value }); + tree.setLabel(item, { label: value }); } } ) diff --git a/web/pgadmin/browser/templates/browser/js/browser.js b/web/pgadmin/browser/templates/browser/js/browser.js index ba12f5fbd..62a247f1f 100644 --- a/web/pgadmin/browser/templates/browser/js/browser.js +++ b/web/pgadmin/browser/templates/browser/js/browser.js @@ -1,3 +1,7 @@ +// Page globals +var editor +var tree + // Store the main browser layout function storeLayout(pane, $pane, paneState, paneOptions) { state = layout.readState(); @@ -79,7 +83,7 @@ $(document).ready(function(){ }) // Syntax highlight the SQL Pane - var editor = CodeMirror.fromTextArea(document.getElementById("sql-textarea"), { + editor = CodeMirror.fromTextArea(document.getElementById("sql-textarea"), { lineNumbers: true, mode: "text/x-sql", readOnly: true, @@ -91,26 +95,22 @@ $(document).ready(function(){ url: '{{ url_for('browser.get_nodes') }}' }, }); - var treeApi = $('#tree').aciTree('api'); + tree = $('#tree').aciTree('api'); // Build the treeview context menu $('#tree').contextMenu({ selector: '.aciTreeLine', build: function(element) { - var item = treeApi.itemFrom(element); + var item = tree.itemFrom(element); var menu = { }; - - menu['rename'] = { - name: 'Rename server group', - callback: function() { rename_server_group(item); } + {% if context_items is defined %} + {% for context_item in context_items %} + menu['{{ context_item.name }}'] = { + name: '{{ context_item.label }}', + callback: function() { {{ context_item.onclick }} } }; - - menu['delete'] = { - name: 'Delete server group', - callback: function() { delete_server_group(item); } - }; - + {% endfor %}{% endif %} return { autoHide: true, items: menu, diff --git a/web/pgadmin/browser/views.py b/web/pgadmin/browser/views.py index a0f636a68..c3001e39b 100644 --- a/web/pgadmin/browser/views.py +++ b/web/pgadmin/browser/views.py @@ -82,7 +82,7 @@ def index(): # Get the help menu items if 'hooks' in dir(module) and 'get_help_menu_items' in dir(module.hooks): help_items.extend(module.hooks.get_help_menu_items()) - + # Get any stylesheets if 'hooks' in dir(module) and 'get_stylesheets' in dir(module.hooks): stylesheets += module.hooks.get_stylesheets() @@ -113,13 +113,26 @@ def browser_js(): modules_and_nodes = modules + nodes # Load the core browser code first + + # Get the context menu items + context_items = [ ] + modules_and_nodes = modules + nodes + + for module in modules_and_nodes: + if 'hooks' in dir(module) and 'get_context_menu_items' in dir(module.hooks): + context_items.extend(module.hooks.get_context_menu_items()) + + context_items = sorted(context_items, key=lambda k: k['priority']) + layout_settings = { } layout_settings['sql_size'] = get_setting('Browser/SQLPane/Size', default=250) layout_settings['sql_closed'] = get_setting('Browser/SQLPane/Closed', default="false") layout_settings['browser_size'] = get_setting('Browser/BrowserPane/Size', default=250) layout_settings['browser_closed'] = get_setting('Browser/BrowserPane/Closed', default="false") - snippets += render_template('browser/js/browser.js', layout_settings=layout_settings) + snippets += render_template('browser/js/browser.js', + layout_settings=layout_settings, + context_items=context_items) # Add module and node specific code for module in modules_and_nodes: @@ -149,6 +162,7 @@ def browser_css(): return resp + @blueprint.route("/root-nodes.json") @login_required def get_nodes():