Ignore:
Timestamp:
Sep 1, 2010 2:32:08 PM (14 years ago)
Author:
chervol
Message:

refurbished the menu items activation algorithm

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.dropdownmenu/trunk/quintagroup/dropdownmenu/browser/viewlets.py

    r2769 r2772  
    4141            '/'.join(self.context.absolute_url().split('/')[:-1]) or \ 
    4242            self.context.absolute_url() 
    43  
     43             
     44        self.cat_sufix  = self.conf.nested_category_sufix or '' 
     45        self.cat_prefix = self.conf.nested_category_prefix or '' 
    4446        # fetch actions-based tabs? 
    4547        if conf.show_actions_tabs: 
     
    6668        if conf.actions_category not in tool.objectIds(): 
    6769            return [] 
    68  
    69         self.chain = []  #save not traversable chain here 
    70         self.tchain = []  #save the best traversable chain here 
    71         res, c = self._subactions(tool._getOb(conf.actions_category), context, 0, self.chain) 
    72         self.tchain.reverse() 
    73         self.chain.reverse() 
    74         self.chain = self.chain and self.chain or self.tchain 
    75         self._activate(res) 
    76         return res 
     70        self.tabs =[] 
     71        self.normalize_actions(tool._getOb(conf.actions_category), context, 0) 
     72        current_item = 0 
     73        delta = 1000 
     74        for info in self.tabs: 
     75            if  self.context_url.startswith(info['url']) and \ 
     76               len(self.context_url) - len(info['url']) < delta: 
     77               current_item = self.tabs.index(info) 
     78        self.id_chain = [id for id in self.mark_active(self.tabs[current_item]['id'])] 
     79         
     80        return self._subactions(tool._getOb(conf.actions_category), context, 0) 
    7781     
    78     def _activate(self, res, level=0): 
    79         """Mark selected chain in the tabs dictionary""" 
    80         if self.chain and len(self.chain) > level: 
    81             res[self.chain[level]]['currentItem'] = True 
    82             res[self.chain[level]]['currentParent'] = True 
    83             if level + 1 < len(self.chain): 
    84                 self._activate(res[self.chain[level]]['children'], level+1) 
    85  
     82    def mark_active(self, current_id): 
     83        for info in self.tabs: 
     84            if info['id'] == current_id: 
     85                info['currentItem'] = True 
     86                self.mark_active(info['parent']) 
     87                yield(info['id']) 
     88                     
     89    def normalize_actions(self, category, object, level): 
     90        for info in self._actionInfos(category, object): 
     91            children = [] 
     92            bottomLevel = self.conf.actions_tabs_level 
     93            if bottomLevel < 1 or level < bottomLevel: 
     94                # try to find out appropriate subcategory 
     95                subcat_id = self.cat_prefix + info['id'] + self.cat_sufix 
     96                if subcat_id != info['id'] and \ 
     97                   subcat_id in category.objectIds(): 
     98                    subcat = category._getOb(subcat_id) 
     99                    if IActionCategory.providedBy(subcat): 
     100                        children = self.normalize_actions(subcat, object, level+1) 
     101            tab = {'id' : info['id'], 
     102               'title': info['title'], 
     103               'url': info['url'], 
     104               'currentItem' : False, 
     105               'parent': category['id']} 
     106            self.tabs.append(tab) 
    86107 
    87108    def _subactions(self, category, object, level=0, lchain=[]): 
    88109        """Build tabs dictionary out of portal actions""" 
    89110        tabs = [] 
    90         currentParentId = -1 
    91         index = -1 
    92         local = False       
     111      
    93112        for info in self._actionInfos(category, object): 
    94             # prepare data for action 
    95             # TODO: implement current element functionality, maybe should be 
    96             #       done on a template level because of separate content and 
    97             #       actions tabs are rendered separately 
    98  
    99             index += 1 
    100113            icon = info['icon'] and '<img src="%s" />' % info['icon'] or '' 
    101114     
    102             if  self.context_url.startswith(info['url']) and info['url'] != self.site_url: 
    103                 if currentParentId > -1: 
    104                     if len(tabs[currentParentId]['getURL']) < len(info['url']):  
    105                         currentParentId = index 
    106                 else: 
    107                     currentParentId = index 
    108             if  self.context_url == info['url']: 
    109                 lchain.append(index) 
    110                 local = True 
    111                  
    112115            # look up children for a given action 
    113116            children = [] 
     
    124127                    subcat = category._getOb(subcat_id) 
    125128                    if IActionCategory.providedBy(subcat): 
    126                         children, local = self._subactions(subcat, object, level+1, lchain) 
    127                         if local: 
    128                             lchain.append(index) 
    129              
     129                        children = self._subactions(subcat, object, level+1) 
     130 
     131            active = False 
     132            if info['id'] in self.id_chain: 
     133                active = True 
    130134            # make up final tab dictionary 
    131135            tab = {'Title': info['title'], 
     
    134138                   'show_children': len(children) > 0, 
    135139                   'children': children, 
    136                    'currentItem': False,  
    137                    'currentParent': False, 
     140                   'currentItem': active,  
     141                   'currentParent': active, 
    138142                   'item_icon': {'html_tag': icon}, 
    139143                   'normalized_review_state': 'visible'} 
    140144            tabs.append(tab) 
    141  
    142         if currentParentId > -1: 
    143             self.tchain.append(currentParentId) 
    144         return tabs, local 
     145        return tabs 
    145146 
    146147    def _actionInfos(self, category, object, check_visibility=1, 
Note: See TracChangeset for help on using the changeset viewer.