cubicweb/web/views/treeview.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Mon, 30 Mar 2020 15:17:10 +0200
changeset 12958 3667f6df1ec3
parent 12503 b01dd0ef43aa
permissions -rw-r--r--
[server] extract "no pooler" CnxSet class to a _BaseCnxSet class So we get rid of "if self._queue is None" in each method of _CnxSetPool Also add helper get_cnxset(source, size) to instantiate the correct pooler class.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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
     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
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    22
10666
7f6b5f023884 [py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
    23
from cubicweb import _
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
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
5940
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5912
diff changeset
    27
from cubicweb.utils import make_uid, json
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8128
diff changeset
    28
from cubicweb.predicates import adaptable
981
d86d1ee3b60e fix some imports, update treeview for 3.2
sylvain.thenault@logilab.fr
parents: 939
diff changeset
    29
from cubicweb.view import EntityView
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    30
from cubicweb.web.views import baseviews
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
    31
from cubicweb.web.views.ajaxcontroller import ajaxfunc
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
    33
def treecookiename(treeid):
2391
59a2def75e93 [treeview] nicer cookie name
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2381
diff changeset
    34
    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
    35
9256
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    36
def _done_init(done, view, row, col):
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    37
    """handle an infinite recursion safety belt"""
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    38
    if done is None:
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    39
        done = set()
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    40
    entity = view.cw_rset.get_entity(row, col)
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    41
    if entity.eid in done:
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    42
        msg = entity._cw._('loop in %(rel)s relation (%(eid)s)') % {
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    43
            'rel': entity.cw_adapt_to('ITree').tree_relation,
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    44
            'eid': entity.eid
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    45
            }
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    46
        return None, msg
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    47
    done.add(entity.eid)
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    48
    return done, entity
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8900
diff changeset
    49
5556
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
class BaseTreeView(baseviews.ListView):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    52
    """base tree view"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    53
    __regid__ = 'tree'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    54
    __select__ = adaptable('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    55
    item_vid = 'treeitem'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    56
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    57
    def call(self, done=None, **kwargs):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    58
        if done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    59
            done = set()
5597
180229f07c12 [adapters] fix NameError introduced by adapters refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5569
diff changeset
    60
        super(BaseTreeView, self).call(done=done, **kwargs)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    61
10167
de62952550d9 [web] Allow propagating 'klass' parameter on tree views recursively
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10100
diff changeset
    62
    def cell_call(self, row, col=0, vid=None, done=None, maxlevel=None, klass=None, **kwargs):
5718
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
    63
        assert maxlevel is None or maxlevel > 0
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    64
        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
    65
        if done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    66
            # entity is actually an error message
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    67
            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
    68
            return
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    69
        self.open_item(entity)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    70
        entity.view(vid or self.item_vid, w=self.w, **kwargs)
5718
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
    71
        if maxlevel is not None:
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
    72
            maxlevel -= 1
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
    73
            if maxlevel == 0:
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
    74
                self.close_item(entity)
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
    75
                return
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    76
        relatedrset = entity.cw_adapt_to('ITree').children(entities=False)
5718
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
    77
        self.wview(self.__regid__, relatedrset, 'null', done=done,
10167
de62952550d9 [web] Allow propagating 'klass' parameter on tree views recursively
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10100
diff changeset
    78
                   maxlevel=maxlevel, klass=klass, **kwargs)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    79
        self.close_item(entity)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    80
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    81
    def open_item(self, entity):
8900
010a59e12d89 use cw_etype instead of __regid__
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8728
diff changeset
    82
        self.w(u'<li class="%s">\n' % entity.cw_etype.lower())
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    83
    def close_item(self, entity):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    84
        self.w(u'</li>\n')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    85
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
class TreePathView(EntityView):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    88
    """a recursive path view"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    89
    __regid__ = 'path'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    90
    __select__ = adaptable('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    91
    item_vid = 'oneline'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    92
    separator = u'&#160;&gt;&#160;'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    93
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    94
    def call(self, **kwargs):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    95
        self.w(u'<div class="pathbar">')
5597
180229f07c12 [adapters] fix NameError introduced by adapters refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5569
diff changeset
    96
        super(TreePathView, self).call(**kwargs)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    97
        self.w(u'</div>')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    98
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    99
    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
   100
        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
   101
        if done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   102
            # entity is actually an error message
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   103
            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
   104
            return
5871
b21eb3e90a10 [ITree] small fixes: parent_entity is a IBreadCrumbs method, we should use .parent on ITree
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5762
diff changeset
   105
        parent = entity.cw_adapt_to('ITree').parent()
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   106
        if parent:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   107
            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
   108
            self.w(self.separator)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   109
        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
   110
6006
54cdc0164193 [massmailing] need edition.js for the freeezeFormButtons function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5979
diff changeset
   111
5912
ff916bf2eba0 [itree] backport default combobox view for entities implementing ITree (from folder/zone cube)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5871
diff changeset
   112
class TreeComboBoxView(TreePathView):
ff916bf2eba0 [itree] backport default combobox view for entities implementing ITree (from folder/zone cube)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5871
diff changeset
   113
    """display folder in edition's combobox"""
ff916bf2eba0 [itree] backport default combobox view for entities implementing ITree (from folder/zone cube)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5871
diff changeset
   114
    __regid__ = 'combobox'
ff916bf2eba0 [itree] backport default combobox view for entities implementing ITree (from folder/zone cube)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5871
diff changeset
   115
    item_vid = 'text'
ff916bf2eba0 [itree] backport default combobox view for entities implementing ITree (from folder/zone cube)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5871
diff changeset
   116
    separator = u' > '
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   117
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   118
# 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
   119
# treeitemview)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
class TreeView(EntityView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   121
    """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
   122
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   123
    __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
   124
    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
   125
    subvid = 'oneline'
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
   126
    cssclass = 'treeview widget'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
   127
    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
   128
11803
9f7949b63ab2 [views] simplify and fix the TreeView (closes #16055814)
David Douard <david.douard@logilab.fr>
parents: 11767
diff changeset
   129
    def _init_params(self, subvid, treeid, initial_load, morekwargs):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   130
        form = self._cw.form
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
        if subvid is None:
11803
9f7949b63ab2 [views] simplify and fix the TreeView (closes #16055814)
David Douard <david.douard@logilab.fr>
parents: 11767
diff changeset
   132
            subvid = form.pop('treesubvid', self.subvid)  # consume it
1828
e9e8beb06f01 [treeview] really fix #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1826
diff changeset
   133
        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
   134
            treeid = form.pop('treeid', None)
1828
e9e8beb06f01 [treeview] really fix #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1826
diff changeset
   135
            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
   136
                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
   137
        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
   138
            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
   139
            # 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
   140
            morekwargs.update(dict((str(k), v)
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10299
diff changeset
   141
                                   for k, v in ajaxargs.items()))
11803
9f7949b63ab2 [views] simplify and fix the TreeView (closes #16055814)
David Douard <david.douard@logilab.fr>
parents: 11767
diff changeset
   142
        return subvid, treeid
3299
cb543d6bd0e6 make it easier to subclass
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3222
diff changeset
   143
10082
aa63a8e8c213 [web] remove unused argument from treeview._init_headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9256
diff changeset
   144
    def _init_headers(self, treeid):
10100
6718c03f8938 [web] Update jquery-treeview to the latest version
Julien Cristau <julien.cristau@logilab.fr>
parents: 10099
diff changeset
   145
        self._cw.add_css(('jquery-treeview/jquery.treeview.css', 'cubicweb.treeview.css'))
6718c03f8938 [web] Update jquery-treeview to the latest version
Julien Cristau <julien.cristau@logilab.fr>
parents: 10099
diff changeset
   146
        self._cw.add_js(('cubicweb.ajax.js', 'cubicweb.widgets.js', 'jquery-treeview/jquery.treeview.js'))
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   147
        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
   148
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
   149
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   150
    def call(self, subvid=None, treeid=None,
12503
b01dd0ef43aa Drop most deprecated code in cubicweb.web
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11803
diff changeset
   151
             initial_load=True, **morekwargs):
11803
9f7949b63ab2 [views] simplify and fix the TreeView (closes #16055814)
David Douard <david.douard@logilab.fr>
parents: 11767
diff changeset
   152
        subvid, treeid = self._init_params(subvid, treeid,
9f7949b63ab2 [views] simplify and fix the TreeView (closes #16055814)
David Douard <david.douard@logilab.fr>
parents: 11767
diff changeset
   153
                                           initial_load, morekwargs)
3298
caef98aa4a98 backport from 3.5
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2391
diff changeset
   154
        ulid = ' '
11803
9f7949b63ab2 [views] simplify and fix the TreeView (closes #16055814)
David Douard <david.douard@logilab.fr>
parents: 11767
diff changeset
   155
        self._init_headers(treeid)
9f7949b63ab2 [views] simplify and fix the TreeView (closes #16055814)
David Douard <david.douard@logilab.fr>
parents: 11767
diff changeset
   156
        ulid = ' id="tree-%s"' % treeid
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
   157
        self.w(u'<ul%s class="%s">' % (ulid, self.cssclass))
4552
79f461d7ec37 sort on sortvalue, not dc_title to allow customization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4459
diff changeset
   158
        # 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
   159
        # 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
   160
        # (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
   161
        # unability to provide generic implementation on eg Folder...)
4436
294e084f1263 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252 4411
diff changeset
   162
        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
   163
                                          key=lambda x: x.sortvalue())):
4436
294e084f1263 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252 4411
diff changeset
   164
            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
   165
                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
   166
            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
   167
                morekwargs['is_last'] = True
4436
294e084f1263 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252 4411
diff changeset
   168
            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
   169
                        treeid=treeid, w=self.w, **morekwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
        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
   171
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   172
    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
   173
        """ 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
   174
        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
   175
        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
   176
        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
   177
        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
   178
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
   179
150
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   180
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
   181
    """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
   182
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   183
    __regid__ = 'filetree'
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6006
diff changeset
   184
    cssclass = 'treeview widget filetree'
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
   185
    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
   186
3320
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   187
    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
   188
        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
   189
                                       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
   190
443
3c0e9c1d645d cleanup
sylvain.thenault@logilab.fr
parents: 431
diff changeset
   191
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
   192
    """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
   193
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   194
    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
   195
    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
   196
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   197
    __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
   198
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   199
    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
   200
        entity = self.cw_rset.get_entity(row, col)
5720
f0e521487903 [ITree] more fixes, fix fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5718
diff changeset
   201
        itree = entity.cw_adapt_to('ITree')
f0e521487903 [ITree] more fixes, fix fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5718
diff changeset
   202
        if itree and not itree.is_leaf():
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   203
            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
   204
        else:
1190261a1f13 provide a specific version of treeview to display files and directories
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
   205
            # 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
   206
            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
   207
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
class DefaultTreeViewItemView(EntityView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   210
    """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
   211
    __regid__ = 'treeitemview'
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   212
4411
d0bde029f625 should take arbitrary arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4407
diff changeset
   213
    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
   214
        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
   215
        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
   216
        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
   217
        self.w(u'<li%s>%s</li>' % (last_class, itemview))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
928
57614f333dc6 [treeview] (in progress) treeid has to be produced from outside
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 918
diff changeset
   220
class TreeViewItemView(EntityView):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   221
    """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
   222
981
d86d1ee3b60e fix some imports, update treeview for 3.2
sylvain.thenault@logilab.fr
parents: 939
diff changeset
   223
    (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
   224
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   225
    __regid__ = 'treeitemview'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   226
    __select__ = adaptable('ITree')
3298
caef98aa4a98 backport from 3.5
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2391
diff changeset
   227
    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
   228
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   229
    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
   230
        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
   231
        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
   232
        if treestate:
5758
37968502da70 imported patch cookie handling: do not use semicolon as it has some tech meaning (web2 -> web)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5693
diff changeset
   233
            return str(eeid) in treestate.value.split(':')
3298
caef98aa4a98 backport from 3.5
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2391
diff changeset
   234
        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
   235
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
   236
    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
   237
                  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
   238
        w = self.w
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   239
        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
   240
        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
   241
        liclasses = []
10299
4f3b6dad5084 [web/view/treeview] add a 'selected' class on <li> if the entity's url is the currently displayed url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10167
diff changeset
   242
        if self._cw.url(includeparams=False) == entity.absolute_url():
4f3b6dad5084 [web/view/treeview] add a 'selected' class on <li> if the entity's url is the currently displayed url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10167
diff changeset
   243
            liclasses.append(u'selected')
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   244
        is_open = self.open_state(entity.eid, treeid)
5720
f0e521487903 [ITree] more fixes, fix fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5718
diff changeset
   245
        is_leaf = itree is None or itree.is_leaf()
2216
b44b06520ddc [treeview] fix condition
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1977
diff changeset
   246
        if is_leaf:
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   247
            if is_last:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   248
                liclasses.append('last')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   249
            w(u'<li class="%s">' % u' '.join(liclasses))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
        else:
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   251
            rql = itree.children_rql() % {'x': entity.eid}
8728
75be9de9d68e [web] Use the new '/ajax' URL path to access the AjaxController (closes #2758254)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8190
diff changeset
   252
            url = xml_escape(self._cw.build_url('ajax', rql=rql, vid=parentvid,
3460
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   253
                                                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
   254
                                                treeid=treeid,
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   255
                                                fname='view',
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   256
                                                treesubvid=vid,
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   257
                                                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
   258
            divclasses = ['hitarea']
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   259
            if is_open:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   260
                liclasses.append('collapsable')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   261
                divclasses.append('collapsable-hitarea')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   262
            else:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   263
                liclasses.append('expandable')
1828
e9e8beb06f01 [treeview] really fix #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1826
diff changeset
   264
                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
   265
            if is_last:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   266
                if is_open:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   267
                    liclasses.append('lastCollapsable')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   268
                    divclasses.append('lastCollapsable-hitarea')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   269
                else:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   270
                    liclasses.append('lastExpandable')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   271
                    divclasses.append('lastExpandable-hitarea')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   272
            if is_open:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   273
                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
   274
            else:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   275
                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
   276
            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
   277
                divtail = ''
0dc8b2d46a53 more in the way of subclassability & args passing all the way down
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3311
diff changeset
   278
            else:
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   279
                divtail = """ onclick="asyncRemoteExec('node_clicked', '%s', '%s')" """ % (
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   280
                    treeid, entity.eid)
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   281
            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
   282
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
            # add empty <ul> because jquery's treeview plugin checks for
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
            # sublists presence
1826
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   285
            if not is_open:
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   286
                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
   287
        # 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
   288
        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
   289
        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
   290
            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
   291
                       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
   292
        w(u'</li>')
afc563537d8f [treeview] restore functionnality lost in some previous merge #343232
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1802
diff changeset
   293
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   294
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   295
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   296
@ajaxfunc
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   297
def node_clicked(self, treeid, nodeeid):
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   298
    """add/remove eid in treestate cookie"""
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   299
    cookies = self._cw.get_cookie()
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   300
    statename = treecookiename(treeid)
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   301
    treestate = cookies.get(statename)
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   302
    if treestate is None:
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   303
        self._cw.set_cookie(statename, nodeeid)
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   304
    else:
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   305
        marked = set(filter(None, treestate.value.split(':')))
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   306
        if nodeeid in marked:
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   307
            marked.remove(nodeeid)
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   308
        else:
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   309
            marked.add(nodeeid)
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7879
diff changeset
   310
        self._cw.set_cookie(statename, ':'.join(marked))