diff --git a/CHANGES.txt b/CHANGES.txt index 36fc8a2..7d8329e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,4 +5,7 @@ CHANGES Version 0.5.0 (unreleased) ------------------------- +- implemented ITreeItems adapter which is responsible for list all items listed + in tree + - Initial Release diff --git a/src/z3c/jsontree/base.py b/src/z3c/jsontree/base.py index 5dbb6a8..e398a6d 100644 --- a/src/z3c/jsontree/base.py +++ b/src/z3c/jsontree/base.py @@ -20,12 +20,9 @@ import zope.interface import zope.component from zope.traversing import api -from zope.security.interfaces import Unauthorized -from zope.security.interfaces import Forbidden from zope.traversing.browser import absoluteURL from zope.traversing.namespace import getResource from zope.contentprovider.interfaces import IContentProvider -from zope.app.container.interfaces import IReadContainer from zope.app.component import hooks from z3c.template.template import getPageTemplate @@ -42,10 +39,54 @@ from z3c.jsontree.interfaces import STATE_EXPANDED from z3c.jsontree.interfaces import STATE_COLLAPSED from z3c.jsontree.interfaces import STATE_STATIC +from z3c.jsontree import subitem from z3c.jsontree import util -class TreeBase(object): +class IdGenerator(object): + """This mixin class generates Object Ids based on the the objects path. + + Note: The objects must be traversable by it's path. You can implement a + a custom path traverse concept in the getObjectByPath it you need to use + another traverse concept. + + This ids must conform the w3c recommendation described in: + http://www.w3.org/TR/1999/REC-html401-19991224/types.html#type-name + """ + + def getId(self, item): + """Returns the DOM id for a given object. + + Note: we encode the upper case letters because the Dom element id are + not case sensitive in HTML. We prefix each upper case letter with ':'. + """ + path = api.getPath(item) + newPath = u'' + for letter in path: + if letter in string.uppercase: + newPath += ':' + letter + else: + newPath += letter + + # we use a dot as a root representation, this avoids to get the same id + # for the ul and the first li tag + if newPath == '/': + newPath = '.' + # add additinal dot which separates the tree id and the path, is used + # for get the tree id out of the string in the javascript using + # ids = id.split("."); treeId = ids[0]; + id = self.z3cJSONTreeId +'.'+ newPath + # convert '/' path separator to marker '::', because the path '/'' is + # not allowed as DOM id. See also: + # http://www.w3.org/TR/1999/REC-html401-19991224/types.html#type-name + return id.replace('/', '::') + + def id(self): + """Returns the DOM id for a given context.""" + return self.getId(self.context) + + +class TreeBase(subitem.SubItemMixin): """Tree iterator base implementation.""" root = None @@ -78,20 +119,6 @@ def __init__(self, context, request): self.context = context self.request = request - def getSubItems(self, item): - items = [] - append = items.append - if IReadContainer.providedBy(item): - keys = list(item.keys()) - else: - keys = [] - for name in keys: - # Only include items we can traverse to - subItem = api.traverse(item, name, None) - if subItem is not None: - append(subItem) - return items - def getIconURL(self, item, request, name='icon'): return util.getIconURL(item, request, name=name) @@ -99,16 +126,6 @@ def getParents(self): root = self.getRoot() return util.getParentsFromContextToObject(self.context, root) - def hasSubItems(self, item): - res = False - if IReadContainer.providedBy(item): - try: - if len(item) > 0: - res = True - except(Unauthorized, Forbidden): - pass - return res - def update(self): """Returns HTML code for representing a