From c099dc895f10ed142366fde3aa59d8c12300e2fe Mon Sep 17 00:00:00 2001 From: Jeremy Goecks Date: Fri, 18 Jun 2010 15:14:27 -0400 Subject: [PATCH] Add 'recently used tools' section to tools menu. Recently used tools is a list of the five most recent tools that a user has run. This menu is hidden by default but can be shown via the tools menu options. A user must be logged in to use this option. --- lib/galaxy/web/controllers/root.py | 19 +++++++- lib/galaxy/web/controllers/user.py | 26 +++++++++++ templates/root/index.mako | 64 ++++++++++++++++++-------- templates/root/tool_menu.mako | 74 +++++++++++++++++++++++++++++- templates/tool_executed.mako | 9 +++- 5 files changed, 169 insertions(+), 23 deletions(-) diff --git a/lib/galaxy/web/controllers/root.py b/lib/galaxy/web/controllers/root.py index dc2af7357b3..14b8f65a935 100644 --- a/lib/galaxy/web/controllers/root.py +++ b/lib/galaxy/web/controllers/root.py @@ -6,6 +6,7 @@ from cgi import escape, FieldStorage from galaxy import util, datatypes, jobs, web, util from galaxy.web.base.controller import * from galaxy.util.sanitize_html import sanitize_html +from galaxy.util.odict import odict from galaxy.model.orm import * log = logging.getLogger( __name__ ) @@ -30,7 +31,23 @@ class RootController( BaseController, UsesHistory, UsesAnnotations ): if trans.app.config.require_login and not trans.user: return trans.fill_template( '/no_access.mako', message = 'Please log in to access Galaxy tools.' ) else: - return trans.fill_template('/root/tool_menu.mako', toolbox=self.get_toolbox() ) + ## Get most recently used tools. + toolbox = self.get_toolbox() + recent_tool_ids = odict() + if trans.user: + for row in trans.sa_session.query( self.app.model.Job.tool_id ).join( self.app.model.History ). \ + filter( self.app.model.GalaxySession.user==trans.user ). \ + order_by( self.app.model.Job.create_time.desc() ): + tool_id = row[0] + recent_tool_ids[tool_id] = tool_id + ## TODO: make number of recently used tools a user preference. + if len ( recent_tool_ids ) == 5: + break + + # Create list of recently used tools. + recent_tools = [ toolbox.tools_by_id[ tool_id ] for tool_id in recent_tool_ids.keys() ] + + return trans.fill_template('/root/tool_menu.mako', toolbox=toolbox, recent_tools=recent_tools ) @web.json def tool_search( self, trans, query ): diff --git a/lib/galaxy/web/controllers/user.py b/lib/galaxy/web/controllers/user.py index c218a8e9027..8ec1b86e2b5 100644 --- a/lib/galaxy/web/controllers/user.py +++ b/lib/galaxy/web/controllers/user.py @@ -8,6 +8,7 @@ import logging, os, string, re from random import choice from galaxy.web.form_builder import * from galaxy.util.json import from_json_string, to_json_string +from galaxy.web.framework.helpers import iff log = logging.getLogger( __name__ ) @@ -650,6 +651,31 @@ class User( BaseController ): else: # User not logged in, history group must be only public return trans.show_error_message( "You must be logged in to change your default permitted actions." ) + + @web.expose + @web.require_login( "to get most recently used tool" ) + @web.json_pretty + def get_most_recently_used_tool_async( self, trans ): + """ Returns information about the most recently used tool. """ + + # Get most recently used tool. + query = trans.sa_session.query( self.app.model.Job.tool_id ).join( self.app.model.History ). \ + filter( self.app.model.History.user==trans.user ). \ + order_by( self.app.model.Job.create_time.desc() ).limit(1) + tool_id = query[0][0] # Get first element in first row of query. + tool = self.get_toolbox().tools_by_id[ tool_id ] + + # Return tool info. + tool_info = { + "id" : tool.id, + "link" : url_for( controller='tool_runner', tool_id=tool.id ), + "target" : tool.target, + "name" : tool.name, ## TODO: translate this using _() + "minsizehint" : tool.uihints.get( 'minwidth', -1 ), + "description" : tool.description + } + return tool_info + @web.expose def manage_addresses(self, trans, **kwd): if trans.user: diff --git a/templates/root/index.mako b/templates/root/index.mako index 02c01fc351f..a525b84dfc7 100644 --- a/templates/root/index.mako +++ b/templates/root/index.mako @@ -53,12 +53,12 @@ }); // Init tool options. - %if trans.app.toolbox_search.enabled: - make_popupmenu( $("#tools-options-button"), { - "Search Tools": function() { + make_popupmenu( $("#tools-options-button"), { + ## Search tools menu item. + %if trans.app.toolbox_search.enabled: + "Search Tools": function() { // Show/hide menu and update user preference for menu visibility. - var tool_menu_frame = $("#galaxy_tools").contents(); - var menu = tool_menu_frame.find('#tool-search'); + var menu = $("#galaxy_tools").contents().find('#tool-search'); if (menu.is(":visible")) { // Hide menu. @@ -78,24 +78,48 @@ } // Update menu option. - $("#tools-options-button-menu").find("li").first().text(menu_option_text); + $("#tools-options-button-menu").find("li").eq(0).text(menu_option_text); // TODO: Set user preference. - /* - TODO: uncomment this when this method is working. - $.ajax({ - url: "${h.url_for( controller='/user', action='set_user_pref_async' )}", - data: { "pref_name" : "show_tool_search", - "pref_value" : pref_value - }, - error: function() { - alert( "Failed to set user preference." ); - }, - }); - */ + }, + %endif + ## Recently used tools menu. + %if trans.user: + <% + if 'show_recently_used_menu' in trans.user.preferences and trans.user.preferences['show_recently_used_menu'] == 'True': + action = "Hide" + else: + action = "Show" + %> + "${action} Recently Used": function() { + // Show/hide menu. + var ru_menu = $('#galaxy_tools').contents().find('#recently_used_wrapper'); + var ru_menu_body = ru_menu.find(".toolSectionBody"); + var pref_value = null; + var menu_option_text = null; + if (ru_menu.is(":visible")) + { + // Hide menu. + ru_menu_body.slideUp(); + ru_menu.slideUp(); + pref_value = "False"; + menu_option_text = "Show Recently Used"; + } + else + { + // Show menu. + ru_menu.slideDown(); + pref_value = "True"; + menu_option_text = "Hide Recently Used"; + } + + // Update menu option. + $("#tools-options-button-menu").find("li").eq(1).text(menu_option_text); + + // TODO: Set user preference. } - }); - %endif + %endif + }); }); diff --git a/templates/root/tool_menu.mako b/templates/root/tool_menu.mako index c537b98b726..fecf2da8e5f 100644 --- a/templates/root/tool_menu.mako +++ b/templates/root/tool_menu.mako @@ -149,7 +149,56 @@ } this.lastValue = this.value; }); - }); + }); + + // Update recently used tools menu. Function inserts a new item and removes the last item. + function update_recently_used() + { + $.ajax({ + url: "${h.url_for( controller='/user', action='get_most_recently_used_tool_async' )}", + dataType: 'json', + error: function() { + console.log( "Failed to update recently used list." ); + }, + success: function(new_tool_info) { + var recently_used_elts = $("#recently_used").find(".toolTitle"); + var first_elt = $(recently_used_elts.first()); + var found_in_list = false; + + // Look for new tool in current list. If found, rearrange list to move tool to top. + recently_used_elts.each( function(index) { + var anchor = $(this).find("a"); + var tool_id = anchor.attr("id").split("-")[1]; + if (tool_id == new_tool_info.id) + { + found_in_list = true; + + // If tool is first, do nothing. + if (index == 0) + return; + else + { + // Tool not first; reorder. + $(this).remove(); + first_elt.before($(this)); + } + } + }); + + // If tool not in list, create new element, remove last element, and put new element first in list. + if (!found_in_list) + { + new_tool_elt = $("
\ + " + + new_tool_info.name + " " + new_tool_info.description + " \ +
"); + recently_used_elts.last().remove(); + recently_used_elts.first().before(new_tool_elt); + } + } + }); + } @@ -162,6 +211,29 @@ + ## Recently used tools. + %if trans.user: + <% + if 'show_recently_used_menu' in trans.user.preferences and trans.user.preferences['show_recently_used_menu'] == 'True': + display = "block" + else: + display = "none" + %> +
+
+ Recently Used +
+
+
+ %for tool in recent_tools: + ${render_tool( tool, True )} + %endfor +
+
+
+
+ %endif + ## Tools. %for key, val in toolbox.tool_panel.items():
diff --git a/templates/tool_executed.mako b/templates/tool_executed.mako index df1ea4829ee..8fcd2bc8b87 100644 --- a/templates/tool_executed.mako +++ b/templates/tool_executed.mako @@ -12,6 +12,13 @@ parent.frames.galaxy_history.location.href="${h.url_for( controller='root', action='history' )}"; inside_galaxy_frameset = true; } + + %if trans.user: + if (inside_galaxy_frameset) + { + parent.frames.galaxy_tools.update_recently_used(); + } + %endif if ( parent.handle_minwidth_hint ) { parent.handle_minwidth_hint( -1 ); @@ -29,7 +36,7 @@ function refresh() { top.location.href = '${h.url_for( "/" )}'; - } + }