From bbe2e33757997026a97d031b411327839b7c7708 Mon Sep 17 00:00:00 2001 From: ianb Date: Thu, 21 Jul 2005 21:41:22 +0000 Subject: Fixed some js_repr usage; added class for buttons --- paste/url.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 5 deletions(-) (limited to 'paste') diff --git a/paste/url.py b/paste/url.py index 3e92186..110cdcc 100644 --- a/paste/url.py +++ b/paste/url.py @@ -4,7 +4,7 @@ This module implements a class for handling URLs. import urllib import cgi -__all__ = ["URL"] +__all__ = ["URL", "Image"] def html_quote(v): if v is None: @@ -117,6 +117,11 @@ class URLResource(object): return u __div__ = addpath + + def become(self, OtherClass): + return OtherClass(self.url, vars=self.vars, + attrs=self.attrs, + params=self.original_params) def href__get(self): s = self.url @@ -192,7 +197,7 @@ class URL(URLResource): >>> u['//foo'].param(content='view').html 'view' >>> u.param(confirm='Really?', content='goto').html - 'goto' + 'goto' >>> u(title='See "it"', content='goto').html 'goto' >>> u('another', var='fuggetaboutit', content='goto').html @@ -232,14 +237,19 @@ class URL(URLResource): attrs = self.attrs.items() attrs.insert(0, ('href', self.href)) if self.params.get('confirm'): - attrs.append(('onclick', 'return prompt(%r)' + attrs.append(('onclick', 'return prompt(%s)' % js_repr(self.params['confirm']))) return attrs def onclick_goto__get(self): - return 'location.href=%r; return false' % js_repr(self.href) - + return 'location.href=%s; return false' % js_repr(self.href) + onclick_goto = property(onclick_goto__get) + + def button__get(self): + return self.become(Button) + + button = property(button__get) class Image(URLResource): @@ -272,6 +282,50 @@ class Image(URLResource): attrs = self.attrs.items() attrs.insert(0, ('src', self.href)) return attrs + +class Button(URLResource): + + r""" + >>> u = URL('/') + >>> u = u / 'delete' + >>> b = u.button['confirm=Sure?'](id=5, content='del') + >>> str(b) + '' + """ + + default_params = {'tag': 'button'} + + def __str__(self): + return self.html + + def _get_content(self): + if self.params.get('content'): + return self.params['content'] + if self.attrs.get('value'): + return self.attrs['content'] + # @@: Error? + return None + + def _add_vars(self, vars): + button = self + if 'confirm' in vars: + button = button.param(confirm=vars.pop('confirm')) + if 'content' in vars: + button = button.param(content=vars.pop('content')) + return button.var(**vars) + + def _add_positional(self, args): + return self.addpath(*args) + + def _html_attrs(self): + attrs = self.attrs.items() + onclick = 'location.href=%s' % js_repr(self.href) + if self.params.get('confirm'): + onclick = 'if (prompt(%s)) {%s}' % ( + js_repr(self.params['confirm']), onclick) + onclick += '; return false' + attrs.insert(0, ('onclick', onclick)) + return attrs if __name__ == '__main__': import doctest -- cgit v1.2.1