Package library :: Package pyjamas :: Package ui :: Module Anchor
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.ui.Anchor

 1  """ 
 2  Anchor Widget, use this to create the equivalent of the <a></a> tag. 
 3   
 4  Copyright(C) 2010, Martin Hellwig 
 5  Copyright(C) 2010, Luke Leighton <lkcl@lkcl.net> 
 6   
 7  License: Apache Software Foundation v2 
 8   
 9  Here is an example for using it with an image:: 
10   
11      if __name__ == '__main__': 
12          from pyjamas.ui.RootPanel import RootPanel 
13          from pyjamas.ui.Image import Image 
14          root = RootPanel() 
15          image = Image('http://pyjs.org/img/pyjamas.128x128.png') 
16          anchor = Anchor(Widget=image, Href='http://pyjs.org') 
17          root.add(anchor) 
18  """ 
19   
20  from pyjamas import DOM 
21  from pyjamas.ui.Widget import Widget 
22  from pyjamas.ui.ClickListener import ClickHandler 
23   
24 -class _Attribute(object):
25 "Attribute definition class with method set and remove"
26 - def __init__(self, element, attribute, 27 attribute_type=None, type_restriction=None):
28 self.element = element 29 self.attribute = attribute 30 self._type = attribute_type 31 self._restriction = type_restriction
32
33 - def get(self):
34 "Get the value" 35 return DOM.getAttribute(self.element, self.attribute)
36
37 - def set(self, value):
38 "Set the value" 39 DOM.setAttribute(self.element, self.attribute, value)
40
41 - def remove(self):
42 "Remove the attribute from the element" 43 DOM.removeAttribute(self.element, self.attribute)
44
45 -class _Attributes(object):
46 "Attribute container class"
47 - def __init__(self, element):
48 self.name = _Attribute(element, 'name', 'cdata', 'cs') 49 self.href = _Attribute(element, 'href', 'uri', 'ct') 50 self.hreflang = _Attribute(element, 'hreflang', 'langcode', 'ci') 51 self.type = _Attribute(element, 'type', 'content-type', 'ci') 52 self.rel = _Attribute(element, 'rel', 'link-types' ,'ci') 53 self.rev = _Attribute(element, 'rev', 'link-types', 'ci') 54 self.charset = _Attribute(element, 'charset', 'charset', 'ci') 55 self.target = _Attribute(element, 'target', 'target', 'ci')
56
57 -class Anchor(Widget, ClickHandler, _Attributes):
58 """Anchor attribute, use this to create the equivalent of the <a></a> tag. 59 The attributes: name, href. hreflang, type, rel, rev, charset are in the 60 namespace of the Anchor instance. 61 These attributes themselves have the functions 'set' and 'remove' 62 For example: 63 anchor = Anchor() 64 anchor.href.set('http://www.dcuktec.com') 65 """
66 - def __init__(self, **kwargs):
67 element = kwargs.pop('Element', None) or DOM.createAnchor() 68 kwargs['StyleName'] = kwargs.pop('StyleName', 'gwt-Anchor') 69 _Attributes.__init__(self, element) 70 self.setElement(element) 71 self.widget = None 72 Widget.__init__(self, **kwargs) 73 ClickHandler.__init__(self)
74
75 - def setWidget(self, widget):
76 """ Add child widget 77 """ 78 widget.removeFromParent() 79 widget.setParent(self) 80 self.widget = widget 81 DOM.appendChild(self.getElement(), widget.getElement())
82
83 - def removeWidget(self):
84 """ remove child widget 85 """ 86 self.widget.removeFromParent() 87 DOM.removeChild(self.getElement(), self.widget.getElement()) 88 self.widget = None
89
90 - def setHref(self, url):
91 self.href.set(url)
92