web/views/treeview.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 May 2010 20:50:00 +0200
changeset 5557 1a534c596bff
parent 5556 9ab2b4c74baf
child 5569 cb14af012a96
permissions -rw-r--r--
[entity] continue cleanup of Entity/AnyEntity namespace * prefix by cw_ or _cw all '2nd zone' attributes / methods, other most commonly used will be done later (some methods used by client are made private anyway when they are for internal purpose anyway) * kill set_eid, use direct affectation instead * kill the auto-transmutation feature (pre_add_hook), hence releasing 3.9 will require releasing file cube as well
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""Set of tree views / tree-building widgets, some based on jQuery treeview
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    19
plugin.
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    20
"""
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    21
__docformat__ = "restructuredtext en"
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    22
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    23
from warnings import warn
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    24
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2216
diff changeset
    25
from logilab.mtconverter import xml_escape
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    26
from logilab.common.decorators import cached
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    27
1828
e9e8beb06f01 [treeview] really fix #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1826
diff changeset
    28
from cubicweb.utils import make_uid
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    29
from cubicweb.selectors import implements, adaptable
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    30
from cubicweb.view import EntityView, EntityAdapter, implements_adapter_compat
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    31
from cubicweb.web import json
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
from cubicweb.interfaces import ITree
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    33
from cubicweb.web.views import baseviews
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    35
def treecookiename(treeid):
2391
59a2def75e93 [treeview] nicer cookie name
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2381
diff changeset
    36
    return str('%s-treestate' % treeid)
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    37
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    38
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    39
class ITreeAdapter(EntityAdapter):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    40
    """This adapter has to be overriden to be configured using the
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    41
    tree_relation, child_role and parent_role class attributes to
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    42
    benefit from this default implementation
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    43
    """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    44
    __regid__ = 'ITree'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    45
    __select__ = implements(ITree) # XXX for bw compat, else should be abstract
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    46
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    47
    tree_relation = None
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    48
    child_role = 'subject'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    49
    parent_role = 'object'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    50
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    51
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    52
    def children_rql(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    53
        """returns RQL to get children
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    54
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    55
        XXX should be removed from the public interface
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    56
        """
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
    57
        return self.entity.cw_related_rql(self.tree_relation, self.parent_role)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    58
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    59
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    60
    def different_type_children(self, entities=True):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    61
        """return children entities of different type as this entity.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    62
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    63
        according to the `entities` parameter, return entity objects or the
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    64
        equivalent result set
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    65
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    66
        res = self.entity.related(self.tree_relation, self.parent_role,
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    67
                                  entities=entities)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    68
        eschema = self.entity.e_schema
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    69
        if entities:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    70
            return [e for e in res if e.e_schema != eschema]
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    71
        return res.filtered_rset(lambda x: x.e_schema != eschema, self.entity.cw_col)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    72
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    73
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    74
    def same_type_children(self, entities=True):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    75
        """return children entities of the same type as this entity.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    76
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    77
        according to the `entities` parameter, return entity objects or the
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    78
        equivalent result set
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    79
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    80
        res = self.entity.related(self.tree_relation, self.parent_role,
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    81
                                  entities=entities)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    82
        eschema = self.entity.e_schema
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    83
        if entities:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    84
            return [e for e in res if e.e_schema == eschema]
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    85
        return res.filtered_rset(lambda x: x.e_schema is eschema, self.entity.cw_col)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    86
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    87
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    88
    def is_leaf(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    89
        """returns true if this node as no child"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    90
        return len(self.children()) == 0
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    91
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    92
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    93
    def is_root(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    94
        """returns true if this node has no parent"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    95
        return self.parent() is None
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    96
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    97
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    98
    def root(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    99
        """return the root object"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   100
        return self._cw.entity_from_eid(self.path()[0])
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   101
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   102
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   103
    def parent(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   104
        """return the parent entity if any, else None (e.g. if we are on the
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   105
        root)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   106
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   107
        try:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   108
            return self.entity.related(self.tree_relation, self.child_role,
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   109
                                       entities=True)[0]
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   110
        except (KeyError, IndexError):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   111
            return None
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   112
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   113
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   114
    def children(self, entities=True, sametype=False):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   115
        """return children entities
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   116
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   117
        according to the `entities` parameter, return entity objects or the
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   118
        equivalent result set
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   119
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   120
        if sametype:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   121
            return self.same_type_children(entities)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   122
        else:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   123
            return self.entity.related(self.tree_relation, self.parent_role,
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   124
                                       entities=entities)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   125
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   126
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   127
    def iterparents(self, strict=True):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   128
        def _uptoroot(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   129
            curr = self
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   130
            while True:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   131
                curr = curr.parent()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   132
                if curr is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   133
                    break
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   134
                yield curr
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   135
                curr = curr.cw_adapt_to('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   136
        if not strict:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   137
            return chain([self.entity], _uptoroot(self))
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   138
        return _uptoroot(self)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   139
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   140
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   141
    def iterchildren(self, _done=None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   142
        """iterates over the item's children"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   143
        if _done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   144
            _done = set()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   145
        for child in self.children():
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   146
            if child.eid in _done:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   147
                self.error('loop in %s tree', child.__regid__.lower())
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   148
                continue
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   149
            yield child
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   150
            _done.add(child.eid)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   151
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   152
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   153
    def prefixiter(self, _done=None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   154
        if _done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   155
            _done = set()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   156
        if self.entity.eid in _done:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   157
            return
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   158
        _done.add(self.entity.eid)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   159
        yield self.entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   160
        for child in self.same_type_children():
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   161
            for entity in child.cw_adapt_to('ITree').prefixiter(_done):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   162
                yield entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   163
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   164
    @cached
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   165
    @implements_adapter_compat('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   166
    def path(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   167
        """returns the list of eids from the root object to this object"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   168
        path = []
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   169
        adapter = self
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   170
        entity = adapter.entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   171
        while entity is not None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   172
            if entity.eid in path:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   173
                self.error('loop in %s tree', entity.__regid__.lower())
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   174
                break
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   175
            path.append(entity.eid)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   176
            try:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   177
                # check we are not jumping to another tree
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   178
                if (adapter.tree_relation != self.tree_relation or
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   179
                    adapter.child_role != self.child_role):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   180
                    break
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   181
                entity = adapter.parent()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   182
                adapter = entity.cw_adapt_to('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   183
            except AttributeError:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   184
                break
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   185
        path.reverse()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   186
        return path
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   187
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   188
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   189
def _done_init(done, view, row, col):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   190
    """handle an infinite recursion safety belt"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   191
    if done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   192
        done = set()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   193
    entity = view.cw_rset.get_entity(row, col)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   194
    if entity.eid in done:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   195
        msg = entity._cw._('loop in %(rel)s relation (%(eid)s)') % {
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   196
            'rel': entity.tree_attribute,
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   197
            'eid': entity.eid
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   198
            }
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   199
        return None, msg
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   200
    done.add(entity.eid)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   201
    return done, entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   202
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   203
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   204
class BaseTreeView(baseviews.ListView):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   205
    """base tree view"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   206
    __regid__ = 'tree'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   207
    __select__ = adaptable('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   208
    item_vid = 'treeitem'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   209
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   210
    def call(self, done=None, **kwargs):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   211
        if done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   212
            done = set()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   213
        super(TreeViewMixIn, self).call(done=done, **kwargs)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   214
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   215
    def cell_call(self, row, col=0, vid=None, done=None, **kwargs):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   216
        done, entity = _done_init(done, self, row, col)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   217
        if done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   218
            # entity is actually an error message
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   219
            self.w(u'<li class="badcontent">%s</li>' % entity)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   220
            return
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   221
        self.open_item(entity)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   222
        entity.view(vid or self.item_vid, w=self.w, **kwargs)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   223
        relatedrset = entity.cw_adapt_to('ITree').children(entities=False)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   224
        self.wview(self.__regid__, relatedrset, 'null', done=done, **kwargs)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   225
        self.close_item(entity)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   226
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   227
    def open_item(self, entity):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   228
        self.w(u'<li class="%s">\n' % entity.__regid__.lower())
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   229
    def close_item(self, entity):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   230
        self.w(u'</li>\n')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   231
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   232
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   233
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   234
class TreePathView(EntityView):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   235
    """a recursive path view"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   236
    __regid__ = 'path'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   237
    __select__ = adaptable('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   238
    item_vid = 'oneline'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   239
    separator = u'&#160;&gt;&#160;'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   240
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   241
    def call(self, **kwargs):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   242
        self.w(u'<div class="pathbar">')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   243
        super(TreePathMixIn, self).call(**kwargs)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   244
        self.w(u'</div>')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   245
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   246
    def cell_call(self, row, col=0, vid=None, done=None, **kwargs):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   247
        done, entity = _done_init(done, self, row, col)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   248
        if done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   249
            # entity is actually an error message
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   250
            self.w(u'<span class="badcontent">%s</span>' % entity)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   251
            return
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   252
        parent = entity.cw_adapt_to('ITree').parent_entity()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   253
        if parent:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   254
            parent.view(self.__regid__, w=self.w, done=done)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   255
            self.w(self.separator)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   256
        entity.view(vid or self.item_vid, w=self.w)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   257
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   258
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   259
# XXX rename regid to ajaxtree/foldabletree or something like that (same for
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   260
# treeitemview)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
class TreeView(EntityView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   262
    """ajax tree view, click to expand folder"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   263
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   264
    __regid__ = 'treeview'
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   265
    itemvid = 'treeitemview'
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   266
    subvid = 'oneline'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
   267
    css_classes = 'treeview widget'
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
   268
    title = _('tree view')
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   269
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   270
    def _init_params(self, subvid, treeid, initial_load, initial_thru_ajax, morekwargs):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   271
        form = self._cw.form
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
        if subvid is None:
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   273
            subvid = form.pop('treesubvid', self.subvid) # consume it
1828
e9e8beb06f01 [treeview] really fix #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1826
diff changeset
   274
        if treeid is None:
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   275
            treeid = form.pop('treeid', None)
1828
e9e8beb06f01 [treeview] really fix #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1826
diff changeset
   276
            if treeid is None:
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   277
                treeid = 'throw_away' + make_uid('uid')
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   278
        if 'morekwargs' in self._cw.form:
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   279
            ajaxargs = json.loads(form.pop('morekwargs'))
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   280
            # got unicode & python keywords must be strings
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   281
            morekwargs.update(dict((str(k), v)
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   282
                                   for k, v in ajaxargs.iteritems()))
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   283
        toplevel_thru_ajax = form.pop('treeview_top', False) or initial_thru_ajax
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   284
        toplevel = toplevel_thru_ajax or (initial_load and not form.get('fname'))
3299
cb543d6bd0e6 make it easier to subclass
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3222
diff changeset
   285
        return subvid, treeid, toplevel_thru_ajax, toplevel
cb543d6bd0e6 make it easier to subclass
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3222
diff changeset
   286
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   287
    def _init_headers(self, treeid, toplevel_thru_ajax):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   288
        self._cw.add_css('jquery.treeview.css')
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   289
        self._cw.add_js(('cubicweb.ajax.js', 'cubicweb.widgets.js', 'jquery.treeview.js'))
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   290
        self._cw.html_headers.add_onload(u"""
4851
e55bdd10421e remove deprecation warning introduced by add_onload api change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   291
jQuery("#tree-%s").treeview({toggle: toggleTree, prerendered: true});""" % treeid)
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   292
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   293
    def call(self, subvid=None, treeid=None,
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   294
             initial_load=True, initial_thru_ajax=False, **morekwargs):
3299
cb543d6bd0e6 make it easier to subclass
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3222
diff changeset
   295
        subvid, treeid, toplevel_thru_ajax, toplevel = self._init_params(
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   296
            subvid, treeid, initial_load, initial_thru_ajax, morekwargs)
3298
caef98aa4a98 backport from 3.5
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2391
diff changeset
   297
        ulid = ' '
caef98aa4a98 backport from 3.5
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2391
diff changeset
   298
        if toplevel:
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   299
            self._init_headers(treeid, toplevel_thru_ajax)
3298
caef98aa4a98 backport from 3.5
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2391
diff changeset
   300
            ulid = ' id="tree-%s"' % treeid
caef98aa4a98 backport from 3.5
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2391
diff changeset
   301
        self.w(u'<ul%s class="%s">' % (ulid, self.css_classes))
4552
79f461d7ec37 sort on sortvalue, not dc_title to allow customization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4459
diff changeset
   302
        # XXX force sorting on x.sortvalue() (which return dc_title by default)
79f461d7ec37 sort on sortvalue, not dc_title to allow customization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4459
diff changeset
   303
        # we need proper ITree & co specification to avoid this.
79f461d7ec37 sort on sortvalue, not dc_title to allow customization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4459
diff changeset
   304
        # (pb when type ambiguity at the other side of the tree relation,
79f461d7ec37 sort on sortvalue, not dc_title to allow customization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4459
diff changeset
   305
        # unability to provide generic implementation on eg Folder...)
4436
294e084f1263 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252 4411
diff changeset
   306
        for i, entity in enumerate(sorted(self.cw_rset.entities(),
4552
79f461d7ec37 sort on sortvalue, not dc_title to allow customization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4459
diff changeset
   307
                                          key=lambda x: x.sortvalue())):
4436
294e084f1263 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252 4411
diff changeset
   308
            if i+1 < len(self.cw_rset):
4407
e4f921a68703 [treeview] ensure items are ordered according to their dc_title, we can't rely on an ordered rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   309
                morekwargs['is_last'] = False
e4f921a68703 [treeview] ensure items are ordered according to their dc_title, we can't rely on an ordered rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   310
            else:
e4f921a68703 [treeview] ensure items are ordered according to their dc_title, we can't rely on an ordered rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   311
                morekwargs['is_last'] = True
4436
294e084f1263 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252 4411
diff changeset
   312
            entity.view(self.itemvid, vid=subvid, parentvid=self.__regid__,
4407
e4f921a68703 [treeview] ensure items are ordered according to their dc_title, we can't rely on an ordered rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   313
                        treeid=treeid, w=self.w, **morekwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
        self.w(u'</ul>')
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   315
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   316
    def cell_call(self, *args, **allargs):
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   317
        """ does not makes much sense until you have to invoke
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   318
        somentity.view('treeview') """
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   319
        allargs.pop('row')
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   320
        allargs.pop('col')
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   321
        self.call(*args, **allargs)
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   322
4407
e4f921a68703 [treeview] ensure items are ordered according to their dc_title, we can't rely on an ordered rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   323
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   324
class FileTreeView(TreeView):
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   325
    """specific version of the treeview to display file trees
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   326
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   327
    __regid__ = 'filetree'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
   328
    css_classes = 'treeview widget filetree'
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
   329
    title = _('file tree view')
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   330
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   331
    def call(self, subvid=None, treeid=None, initial_load=True, **kwargs):
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   332
        super(FileTreeView, self).call(treeid=treeid, subvid='filetree-oneline',
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   333
                                       initial_load=initial_load, **kwargs)
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   334
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
   335
class FileItemInnerView(EntityView):
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   336
    """inner view used by the TreeItemView instead of oneline view
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   337
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   338
    This view adds an enclosing <span> with some specific CSS classes
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   339
    around the oneline view. This is needed by the jquery treeview plugin.
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   340
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   341
    __regid__ = 'filetree-oneline'
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   342
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   343
    def cell_call(self, row, col):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   344
        entity = self.cw_rset.get_entity(row, col)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   345
        if entity.cw_adapt_to('ITree') and not entity.is_leaf():
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   346
            self.w(u'<div class="folder">%s</div>\n' % entity.view('oneline'))
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   347
        else:
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   348
            # XXX define specific CSS classes according to mime types
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   349
            self.w(u'<div class="file">%s</div>\n' % entity.view('oneline'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   350
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   351
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
class DefaultTreeViewItemView(EntityView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   353
    """default treeitem view for entities which don't adapt to ITree"""
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   354
    __regid__ = 'treeitemview'
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   355
4411
d0bde029f625 should take arbitrary arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4407
diff changeset
   356
    def cell_call(self, row, col, vid='oneline', treeid=None, **morekwargs):
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   357
        assert treeid is not None
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   358
        itemview = self._cw.view(vid, self.cw_rset, row=row, col=col)
4455
6af31772765f [views/treeview] fix non-ITree nodes last element computation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4411
diff changeset
   359
        last_class = morekwargs['is_last'] and ' class="last"' or ''
6af31772765f [views/treeview] fix non-ITree nodes last element computation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4411
diff changeset
   360
        self.w(u'<li%s>%s</li>' % (last_class, itemview))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   363
class TreeViewItemView(EntityView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   364
    """specific treeitem view for entities which adapt to ITree
916
968f00dd9a24 [treeview] (in progress) use less magic, having something that now allows prepopulating a part of the tree
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 472
diff changeset
   365
981
d86d1ee3b60e fix some imports, update treeview for 3.2
sylvain.thenault@logilab.fr
parents: 939
diff changeset
   366
    (each item should be expandable if it's not a tree leaf)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   367
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   368
    __regid__ = 'treeitemview'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   369
    __select__ = adaptable('ITree')
3298
caef98aa4a98 backport from 3.5
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2391
diff changeset
   370
    default_branch_state_is_open = False
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   371
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   372
    def open_state(self, eeid, treeid):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   373
        cookies = self._cw.get_cookie()
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   374
        treestate = cookies.get(treecookiename(treeid))
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   375
        if treestate:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   376
            return str(eeid) in treestate.value.split(';')
3298
caef98aa4a98 backport from 3.5
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2391
diff changeset
   377
        return self.default_branch_state_is_open
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   378
4407
e4f921a68703 [treeview] ensure items are ordered according to their dc_title, we can't rely on an ordered rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   379
    def cell_call(self, row, col, treeid, vid='oneline', parentvid='treeview',
e4f921a68703 [treeview] ensure items are ordered according to their dc_title, we can't rely on an ordered rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   380
                  is_last=False, **morekwargs):
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   381
        w = self.w
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   382
        entity = self.cw_rset.get_entity(row, col)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   383
        itree = entity.cw_adapt_to('ITree')
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   384
        liclasses = []
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   385
        is_open = self.open_state(entity.eid, treeid)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   386
        is_leaf = not hasattr(entity, 'is_leaf') or itree.is_leaf()
2216
b44b06520ddc [treeview] fix condition
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1977
diff changeset
   387
        if is_leaf:
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   388
            if is_last:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   389
                liclasses.append('last')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   390
            w(u'<li class="%s">' % u' '.join(liclasses))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   391
        else:
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   392
            rql = itree.children_rql() % {'x': entity.eid}
3460
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   393
            url = xml_escape(self._cw.build_url('json', rql=rql, vid=parentvid,
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   394
                                                pageid=self._cw.pageid,
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   395
                                                treeid=treeid,
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   396
                                                fname='view',
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   397
                                                treesubvid=vid,
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   398
                                                morekwargs=json.dumps(morekwargs)))
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   399
            divclasses = ['hitarea']
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   400
            if is_open:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   401
                liclasses.append('collapsable')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   402
                divclasses.append('collapsable-hitarea')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   403
            else:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   404
                liclasses.append('expandable')
1828
e9e8beb06f01 [treeview] really fix #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1826
diff changeset
   405
                divclasses.append('expandable-hitarea')
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   406
            if is_last:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   407
                if is_open:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   408
                    liclasses.append('lastCollapsable')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   409
                    divclasses.append('lastCollapsable-hitarea')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   410
                else:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   411
                    liclasses.append('lastExpandable')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   412
                    divclasses.append('lastExpandable-hitarea')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   413
            if is_open:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   414
                w(u'<li class="%s">' % u' '.join(liclasses))
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   415
            else:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   416
                w(u'<li cubicweb:loadurl="%s" class="%s">' % (url, u' '.join(liclasses)))
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   417
            if treeid.startswith('throw_away'):
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   418
                divtail = ''
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   419
            else:
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   420
                divtail = """ onclick="asyncRemoteExec('node_clicked', '%s', '%s')" """ % (
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   421
                    treeid, entity.eid)
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   422
            w(u'<div class="%s"%s></div>' % (u' '.join(divclasses), divtail))
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   423
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
            # add empty <ul> because jquery's treeview plugin checks for
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   425
            # sublists presence
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   426
            if not is_open:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   427
                w(u'<ul class="placeholder"><li>place holder</li></ul>')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   428
        # the local node info
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   429
        self.wview(vid, self.cw_rset, row=row, col=col, **morekwargs)
2216
b44b06520ddc [treeview] fix condition
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1977
diff changeset
   430
        if is_open and not is_leaf: #  => rql is defined
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   431
            self.wview(parentvid, itree.children(entities=False), subvid=vid,
3888
6f145783409d use ITree api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3596
diff changeset
   432
                       treeid=treeid, initial_load=False, **morekwargs)
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   433
        w(u'</li>')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   434