Commit 714ae51f authored by Jim Fulton's avatar Jim Fulton

Added id, single, url, and assume_children options.

parent 0f533572
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
# rights reserved. # rights reserved.
# #
############################################################################ ############################################################################
__rcs_id__='$Id: TreeTag.py,v 1.23 1998/04/10 15:58:50 brian Exp $' __rcs_id__='$Id: TreeTag.py,v 1.24 1998/07/23 14:03:08 jim Exp $'
__version__='$Revision: 1.23 $'[11:-2] __version__='$Revision: 1.24 $'[11:-2]
from DocumentTemplate.DT_Util import * from DocumentTemplate.DT_Util import *
from DocumentTemplate.DT_String import String from DocumentTemplate.DT_String import String
...@@ -35,7 +35,12 @@ class Tree: ...@@ -35,7 +35,12 @@ class Tree:
expand=None, leaves=None, expand=None, leaves=None,
header=None, footer=None, header=None, footer=None,
branches=None, branches_expr=None, branches=None, branches_expr=None,
sort=None, skip_unauthorized=1) sort=None, skip_unauthorized=1,
id=None, single=1, url=None,
# opened_decoration=None,
# closed_decoration=None,
# childless_decoration=None,
assume_children=1)
has_key=args.has_key has_key=args.has_key
if has_key('name'): name=args['name'] if has_key('name'): name=args['name']
...@@ -50,6 +55,11 @@ class Tree: ...@@ -50,6 +55,11 @@ class Tree:
args['branches_expr'], expr_globals).eval args['branches_expr'], expr_globals).eval
elif not has_key('branches'): args['branches']='tpValues' elif not has_key('branches'): args['branches']='tpValues'
if not has_key('id'): args['id']='tpId'
if not has_key('url'): args['url']='tpURL'
if not has_key('childless_decoration'):
args['childless_decoration']=''
self.__name__ = name self.__name__ = name
self.section=section.blocks self.section=section.blocks
self.args=args self.args=args
...@@ -74,7 +84,8 @@ String.commands['tree']=Tree ...@@ -74,7 +84,8 @@ String.commands['tree']=Tree
pyid=id # Copy builtin pyid=id # Copy builtin
def tpRender(self, md, section, args): def tpRender(self, md, section, args,
simple_type={type(''):0, type(1):0, type(1.0):0}.has_key):
"""Render data organized as a tree. """Render data organized as a tree.
We keep track of open nodes using a cookie. The cookie stored the We keep track of open nodes using a cookie. The cookie stored the
...@@ -97,7 +108,10 @@ def tpRender(self, md, section, args): ...@@ -97,7 +108,10 @@ def tpRender(self, md, section, args):
data=[] data=[]
if hasattr(self, 'tpId'): id=self.tpId() idattr=args['id']
if hasattr(self, idattr):
id=getattr(self, idattr)
if not simple_type(type(id)): id=id()
elif hasattr(self, '_p_oid'): id=self._p_oid elif hasattr(self, '_p_oid'): id=self._p_oid
else: id=pyid(self) else: id=pyid(self)
...@@ -118,7 +132,7 @@ def tpRender(self, md, section, args): ...@@ -118,7 +132,7 @@ def tpRender(self, md, section, args):
if md.has_key('collapse_all'): if md.has_key('collapse_all'):
state=[id,[]], state=[id,[]],
elif md.has_key('expand_all'): elif md.has_key('expand_all'):
state=[id, tpValuesIds(self, args['branches'])], state=[id, tpValuesIds(self, args['branches'], args)],
else: else:
if md.has_key('tree-s'): if md.has_key('tree-s'):
state=md['tree-s'] state=md['tree-s']
...@@ -156,7 +170,7 @@ def tpRender(self, md, section, args): ...@@ -156,7 +170,7 @@ def tpRender(self, md, section, args):
section,md,treeData, level, args) section,md,treeData, level, args)
finally: md._pop(2) finally: md._pop(2)
if state is substate: if state is substate and not (args.has_key('single') and args['single']):
state=state or ([id],) state=state or ([id],)
state=encode_seq(state) state=encode_seq(state)
md['RESPONSE'].setCookie('tree-s',state) md['RESPONSE'].setCookie('tree-s',state)
...@@ -164,37 +178,57 @@ def tpRender(self, md, section, args): ...@@ -164,37 +178,57 @@ def tpRender(self, md, section, args):
return join(data,'') return join(data,'')
def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
colspan, section, md, treeData, level=0, args=None): colspan, section, md, treeData, level=0, args=None,
simple_type={type(''):0, type(1):0, type(1.0):0}.has_key,
):
"Render a tree as a table" "Render a tree as a table"
have_arg=args.has_key have_arg=args.has_key
exp=0
if level >= 0: if level >= 0:
tpUrl=self.tpURL() urlattr=args['url']
if urlattr and hasattr(self, urlattr):
tpUrl=getattr(self, urlattr)
if not simple_type(type(tpUrl)): tpUrl=tpUrl()
url = (url and ('%s/%s' % (url, tpUrl))) or tpUrl url = (url and ('%s/%s' % (url, tpUrl))) or tpUrl
root_url = root_url or tpUrl root_url = root_url or tpUrl
treeData['tree-item-url']=url treeData['tree-item-url']=url
treeData['tree-level']=level treeData['tree-level']=level
treeData['tree-item-expanded']=0 treeData['tree-item-expanded']=0
idattr=args['id']
exp=0
sub=None
output=data.append output=data.append
script=md['SCRIPT_NAME']
items=None
if (have_arg('assume_children') and args['assume_children']
and substate is not state):
# We should not compute children unless we have to.
# See if we've been asked to expand our children.
for i in range(len(substate)):
sub=substate[i]
if sub[0]==id:
exp=i+1
break
if not exp: items=1
if items is None:
validate=md.validate validate=md.validate
if have_arg('branches') and hasattr(self, args['branches']): if have_arg('branches') and hasattr(self, args['branches']):
if validate is None or not hasattr(self, 'aq_acquire'): if validate is None or not hasattr(self, 'aq_acquire'):
items=getattr(self, args['branches'])() items=getattr(self, args['branches'])
else: else:
items=self.aq_acquire(args['branches'],validate,md)() items=self.aq_acquire(args['branches'],validate,md)
items=items()
elif have_arg('branches_expr'): elif have_arg('branches_expr'):
items=args['branches_expr'](md) items=args['branches_expr'](md)
else:
items=None
if items is not None and validate is not None: if not items and have_arg('leaves'): items=1
if items and items != 1:
if validate is not None:
unauth=[] unauth=[]
index=0 index=0
for i in items: for i in items:
...@@ -211,9 +245,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -211,9 +245,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
else: else:
raise ValidationError, unauth raise ValidationError, unauth
if not items and have_arg('leaves'): items=1 if have_arg('sort'):
if (args.has_key('sort')) and (items is not None) and (items != 1):
# Faster/less mem in-place sort # Faster/less mem in-place sort
if type(items)==type(()): if type(items)==type(()):
items=list(items) items=list(items)
...@@ -231,6 +263,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -231,6 +263,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
diff.append(id) diff.append(id)
sub=None
if substate is state: if substate is state:
output('<TABLE CELLSPACING="0">\n') output('<TABLE CELLSPACING="0">\n')
sub=substate[0] sub=substate[0]
...@@ -263,6 +296,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -263,6 +296,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
s=translate(s, tplus) s=translate(s, tplus)
#################################### ####################################
script=md['SCRIPT_NAME']
if exp: if exp:
treeData['tree-item-expanded']=1 treeData['tree-item-expanded']=1
output('<A NAME="%s">' output('<A NAME="%s">'
...@@ -284,7 +318,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -284,7 +318,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
# add item text # add item text
dataspan=colspan-level dataspan=colspan-level
output('<TD%s%s VALIGN="TOP">' % output('<TD%s%s VALIGN="TOP" ALLIGN="LEFT">' %
((dataspan > 1 and (' COLSPAN="%s"' % dataspan) or ''), ((dataspan > 1 and (' COLSPAN="%s"' % dataspan) or ''),
(have_arg('nowrap') and args['nowrap'] and ' NOWRAP' or '')) (have_arg('nowrap') and args['nowrap'] and ' NOWRAP' or ''))
) )
...@@ -318,6 +352,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -318,6 +352,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
if items==1: if items==1:
# leaves # leaves
if have_arg['leaves']:
doc=args['leaves'] doc=args['leaves']
if md.has_key(doc): doc=md.getitem(doc,0) if md.has_key(doc): doc=md.getitem(doc,0)
else: doc=None else: doc=None
...@@ -359,7 +394,9 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -359,7 +394,9 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
__traceback_info__=sub, args, state, substate __traceback_info__=sub, args, state, substate
ids={} ids={}
for item in items: for item in items:
if hasattr(item, 'tpId'): id=item.tpId() if hasattr(item, idattr):
id=getattr(item, idattr)
if not simple_type(type(id)): id=id()
elif hasattr(item, '_p_oid'): id=item._p_oid elif hasattr(item, '_p_oid'): id=item._p_oid
else: id=pyid(item) else: id=pyid(item)
if len(sub)==1: sub.append([]) if len(sub)==1: sub.append([])
...@@ -503,11 +540,14 @@ def tpStateLevel(state, level=0): ...@@ -503,11 +540,14 @@ def tpStateLevel(state, level=0):
else: level=max(level,1) else: level=max(level,1)
return level return level
def tpValuesIds(self, branches): def tpValuesIds(self, branches, args,
simple_type={type(''):0, type(1):0, type(1.0):0}.has_key,
):
# This should build the ids of subitems which are # This should build the ids of subitems which are
# expandable (non-empty). Leaves should never be # expandable (non-empty). Leaves should never be
# in the state - it will screw the colspan counting. # in the state - it will screw the colspan counting.
r=[] r=[]
idattr=args['id']
try: try:
try: items=getattr(self, branches)() try: items=getattr(self, branches)()
except AttributeError: items=() except AttributeError: items=()
...@@ -515,7 +555,9 @@ def tpValuesIds(self, branches): ...@@ -515,7 +555,9 @@ def tpValuesIds(self, branches):
try: try:
if getattr(item, branches)(): if getattr(item, branches)():
if hasattr(item, 'tpId'): id=item.tpId() if hasattr(self, idattr):
id=getattr(self, idattr)
if not simple_type(type(id)): id=id()
elif hasattr(item, '_p_oid'): id=item._p_oid elif hasattr(item, '_p_oid'): id=item._p_oid
else: id=pyid(item) else: id=pyid(item)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment