Changeset 2210

Show
Ignore:
Timestamp:
04/28/07 15:04:15 (2 years ago)
Author:
coderanger
Message:

CtxtnavAddPlugin:

More changes from a long time ago.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ctxtnavaddplugin/0.11/ctxtnavadd/htdocs/js/ctxtnavadd.js

    r806 r2210  
    1 function find_old_last() { 
    2     var ctxtnav = document.getElementById('ctxtnav'); 
    3     for (var i in ctxtnav.childNodes) { 
    4         if(ctxtnav.childNodes[i].tagName == 'UL') { 
    5             var ul_node = ctxtnav.childNodes[i]; 
    6             for (var j in ul_node.childNodes) { 
    7                 if(ul_node.childNodes[j].tagName == 'LI') { 
    8                     var li_node = ul_node.childNodes[j]; 
    9                     if(li_node.getAttribute('class') == 'last') { 
    10                         return(li_node); 
    11                     } 
    12                 } 
    13             } 
    14         } 
    15     } 
     1function add_ctxtnav(html) { 
     2    var ctxtnav_bar = $('#ctxtnav'); 
     3    ctxtnav_bar.find('li:last-child').removeClass('last').after('<li class="last">'+html+'</li>'); 
    164} 
    17  
    18 function add_ctxtnav(txt) { 
    19     // Make a new <li> and find the old <li class="last"> 
    20     var new_li = document.createElement('LI'); 
    21     var old_li = find_old_last(); 
    22  
    23     // Insert content 
    24     new_li.innerHTML = txt; 
    25      
    26     // Set the class on the new <li> and clear the old one 
    27     new_li.setAttribute('class','last'); 
    28     old_li.removeAttribute('class'); 
    29  
    30     // Insert the new node 
    31     old_li.parentNode.appendChild(new_li); 
    32 } 
    33  
    34  
    35 function add_ctxtnav_link(href, txt) { 
    36     var html = '<a href="' + href + '">' + txt + '</a>'; 
    37     add_ctxtnav(html); 
    38 } 
  • ctxtnavaddplugin/0.11/ctxtnavadd/web_ui.py

    r902 r2210  
    11from trac.core import * 
    2 from trac.web.chrome import INavigationContributor, ITemplateProvider 
    3 from trac.util import Markup 
    4  
    5 try: 
    6     from trac.web.chrome import add_javascript 
    7     have_aj = True 
    8 except ImportError: 
    9     have_aj = False 
     2from trac.web.chrome import ITemplateProvider, add_script 
     3from trac.web.api import IRequestHandler, IRequestFilter 
     4from genshi.core import Markup 
     5from genshi.builder import tag 
    106 
    117from ctxtnavadd.api import ICtxtnavAdder 
     
    139inc_script = """<script type="text/javascript" src="%s"></script>"""  
    1410 
     11class ReqProxy(object): 
     12    """A proxy to intercept req.path_info.""" 
     13     
     14    def __init__(self, req, path_info): 
     15        object.__setattr__(self, '_req', req) 
     16        object.__setattr__(self, 'path_info', path_info) 
     17         
     18    def __getattr__(self, key): 
     19        return getattr(self._req, key) 
     20         
     21    def __setattr__(self, key, value): 
     22        setattr(self._req, key, value) 
     23 
    1524class CtxtnavAddModule(Component): 
    1625    """An evil module that adds buttons to the ctxtnav bar of other plugins.""" 
    1726  
    18     implements(INavigationContributor, ITemplateProvider) 
     27    implements(ITemplateProvider, IRequestFilter, IRequestHandler) 
    1928     
    2029    ctxtnav_adders = ExtensionPoint(ICtxtnavAdder) 
     
    2635    def get_navigation_items(self, req): 
    2736        evil_js = '/'.join(['ctxtnavadd','js','ctxtnavadd.js']) 
    28         if have_aj: 
    29             add_javascript(req, evil_js) 
    30         else: 
    31             self._add_js_inc(req, req.href.chrome(evil_js)) 
    32         self._add_js(req,self._make_js(req)) 
     37        #add_script(req, evil_js) 
     38        #self._add_js(req,self._make_js(req)) 
    3339         
    3440        return [] # This returns no buttons 
     
    3642    # ITemplateProvider methods     
    3743    def get_templates_dirs(self): 
    38         """ 
    39         Return the absolute path of the directory containing the provided 
    40         ClearSilver templates. 
    41         """ 
    4244        from pkg_resources import resource_filename 
    43         #return [resource_filename(__name__, 'templates')] 
    44         return [] 
     45        return [resource_filename(__name__, 'templates')] 
    4546 
    4647    def get_htdocs_dirs(self): 
    47         """ 
    48         Return a list of directories with static resources (such as style 
    49         sheets, images, etc.) 
    50  
    51         Each item in the list must be a `(prefix, abspath)` tuple. The 
    52         `prefix` part defines the path in the URL that requests to these 
    53         resources are prefixed with. 
    54          
    55         The `abspath` is the absolute path to the directory containing the 
    56         resources on the local file system. 
    57         """ 
    5848        from pkg_resources import resource_filename 
    5949        return [('ctxtnavadd', resource_filename(__name__, 'htdocs'))] 
    6050 
     51    # IRequestHandler methods 
     52    def match_request(self, req): 
     53        return req.path_info.startswith('/ctxtnavadd') 
     54             
     55    def process_request(self, req): 
     56        data = {}         
     57        real_path = req.path_info[11:] 
     58        fake_req = ReqProxy(req, real_path) 
     59        data['adds'] = self._get_adds(fake_req) 
     60        return 'ctxtnavadd.js', data, 'text/plain' 
     61             
     62    # IRequestFilter methods 
     63    def pre_process_request(self, req, handler): 
     64        return handler 
     65         
     66    def post_process_request(self, req, template, data, content_type): 
     67        add_script(req, 'ctxtnavadd/js/ctxtnavadd.js') 
     68        add_script(req, '/ctxtnavadd'+req.path_info) 
     69        #self._add_js(req, data, self._make_js(req)) 
     70        return template, data, content_type         
     71 
    6172    # Internal methods 
    62     def _add_js(self, req, data): 
    63         """Add javascript to a page via hdf['project.footer']""" 
    64         footer = req.hdf['project.footer'] 
    65         footer += data 
    66         req.hdf['project.footer'] = Markup(footer) 
    67      
    68     def _add_js_inc(self, req, file): 
    69         """Add a javascript include via hdf['project.footer']""" 
    70         self._add_js(req, inc_script%file) 
    71  
    72     def _make_js(self, req): 
    73         """Generate the needed Javascript.""" 
    74         adds = [] 
     73    def _get_adds(self, req): 
     74        """Find all links to add.""" 
    7575        for adder in self.ctxtnav_adders: 
    7676            if adder.match_ctxtnav_add(req): 
    7777                for add in adder.get_ctxtnav_adds(req): 
    7878                    if isinstance(add, Markup): 
    79                         adds.append(Markup(add.replace("'","\\'"))) 
     79                        yield Markup(add.replace("'","\\'")) 
    8080                    else: 
    81                         href, text = add 
    82                         adds.append(Markup('<a href="%s">%s</a>'%(href,Markup.escape(text,False)))) 
    83         js = "" 
    84         for add in adds: 
    85             js += "add_ctxtnav('%s');\n"%add 
    86         return """<script type="text/javascript">%s</script>"""%js 
     81                        yield tag.a(add[1], href=Markup(add[0])) 
     82 
    8783     
     84 
  • ctxtnavaddplugin/0.11/setup.py

    r901 r2210  
    66setup( 
    77    name = 'TracCtxtnavAdd', 
    8     version = '1.1-r1', 
     8    version = '2.0', 
    99    packages = ['ctxtnavadd'], 
    1010    package_data={ 'ctxtnavadd' : [ 'templates/*.cs' , 'htdocs/js/*.js' ] }, 
     
    1616    keywords = "trac plugin ctxtnav", 
    1717    url = "http://trac-hacks.org/wiki/CtxtnavAddPlugin", 
    18  
     18    classifiers = [ 
     19        'Framework :: Trac', 
     20    ], 
     21     
    1922    entry_points = { 
    2023        'trac.plugins': [ 
     
    2225        ], 
    2326    }, 
    24  
    25     install_requires = [  ], 
    2627)