Ticket #1892: web_ui.py

File web_ui.py, 1.7 kB (added by anonymous, 5 months ago)
Line 
1 from trac.core import *
2 from trac.web.chrome import INavigationContributor, ITemplateProvider, add_script
3 from trac.web.main import _open_environment
4 from trac.util.html import html as tag
5
6 import os
7 import posixpath
8
9 class ProjectMenuModule(Component):
10    
11     implements(INavigationContributor, ITemplateProvider)
12    
13     # INavigationProvider methods
14     def get_navigation_items(self, req):
15         projects = []
16         search_path, this_project = os.path.split(self.env.path)
17         base_url, _ = posixpath.split(req.abs_href())
18        
19         for project in os.listdir(search_path):
20             if project != this_project:
21                 proj_env = _open_environment(os.path.join(search_path, project))
22                
23                 proj_elm = tag.OPTION(proj_env.project_name, value=posixpath.join(base_url, project))
24                
25                 projects.append((proj_elm, proj_env.project_name))
26         projects.sort(lambda a,b: cmp(a[1],b[1])) # Sort on the project names
27         projects.insert(0, (tag.OPTION(self.env.project_name, value=''), None))
28        
29         add_script(req, 'projectmenu/projectmenu.js')
30         yield 'metanav', 'projectmenu', tag.SELECT([e for e,_ in projects], name='projectmenu', id='projectmenu', onchange='return on_projectmenu_change();')
31        
32     def get_active_navigation_item(self, req):
33         return ''
34        
35     # ITemplateProvider methods
36     def get_htdocs_dirs(self):
37         from pkg_resources import resource_filename
38         return [('projectmenu', resource_filename(__name__, 'htdocs'))]
39        
40     def get_templates_dirs(self):
41         #from pkg_resources import resource_filename
42         #return [resource_filename(__name__, 'templates')]
43         return []