--- a/uilib.py Thu Jul 08 18:48:44 2010 +0200
+++ b/uilib.py Thu Jul 08 18:59:42 2010 +0200
@@ -31,6 +31,8 @@
from logilab.mtconverter import xml_escape, html_unescape
from logilab.common.date import ustrftime
+from cubicweb.utils import json_dumps
+
def rql_for_eid(eid):
"""return the rql query necessary to fetch entity with the given eid. This
@@ -228,6 +230,52 @@
# HTML generation helper functions ############################################
+class _JSId(object):
+ def __init__(self, id, parent=None):
+ self.id = id
+ self.parent = parent
+ def __str__(self):
+ if self.parent:
+ return '%s.%s' % (self.parent, self.id)
+ return '%s' % self.id
+ def __getattr__(self, attr):
+ return _JSId(attr, self)
+ def __call__(self, *args):
+ return _JSCallArgs(args, self)
+
+class _JSCallArgs(_JSId):
+ def __init__(self, args, parent=None):
+ assert isinstance(args, tuple)
+ self.args = args
+ self.parent = parent
+ def __str__(self):
+ args = ','.join(json_dumps(arg) for arg in self.args)
+ if self.parent:
+ return '%s(%s)' % (self.parent, args)
+ return args
+
+class _JS(object):
+ def __getattr__(self, attr):
+ return _JSId(attr)
+
+"""magic object to return strings suitable to call some javascript function with
+the given arguments (which should be correctly typed).
+
+>>> str(js.pouet(1, "2"))
+'pouet(1,"2")'
+>>> str(js.cw.pouet(1, "2"))
+'cw.pouet(1,"2")'
+>>> str(js.cw.pouet(1, "2").pouet(None))
+'cw.pouet(1,"2").pouet(null)')
+"""
+js = _JS()
+
+def domid(string):
+ """return a valid DOM id from a string (should also be usable in jQuery
+ search expression...)
+ """
+ return string.replace('.', '_').replace('-', '_')
+
HTML4_EMPTY_TAGS = frozenset(('base', 'meta', 'link', 'hr', 'br', 'param',
'img', 'area', 'input', 'col'))