/
Link.py
121 lines (93 loc) · 3.69 KB
/
Link.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# CTK: Cherokee Toolkit
#
# Authors:
# Alvaro Lopez Ortega <alvaro@alobbs.com>
#
# Copyright (C) 2009 Alvaro Lopez Ortega
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
from Widget import Widget
from Container import Container
from util import formatter, props_to_str
LINK_HTML = '<a id="%(id)s" %(href)s %(props)s>%(content)s</a>'
LINK_ICON_HTML = '<div id="%(id)s"><span class="ui-icon ui-icon-%(icon)s"></span>%(link)s</div>'
class Link (Container):
"""
Widget for link HTML elements (<a> tag). All arguments are optional.
Arguments:
href: linked resource. Mind that an HTML <a> tag without an
href does not get selected when applying link styles.
content: CTK widget to show inside the <a>...</a> block.
props: dictionary with properties for the HTML element,
such as {'name': 'foo', 'id': 'bar', 'class': 'baz'}
Examples:
link1 = CTK.Link('http://example.com', CTK.RawHTML('Example'))
link2 = CTK.Link(content=CTK.Image({'src': '/var/www/static/image.png'})
"""
def __init__ (self, href=None, content=None, props={}):
Container.__init__ (self)
if href:
self.href = href[:]
else:
self.href = None
self.props = props.copy()
if 'id' in self.props:
self.id = self.props.pop('id')
if content:
self += content
def Render (self):
render = Container.Render (self)
if self.href:
href = 'href="%s"' %(self.href)
else:
href = ''
props = {'id': self.id,
'href': href,
'props': props_to_str(self.props),
'content': render.html}
render.html = formatter (LINK_HTML, props)
return render
class LinkWindow (Link):
"""
Widget for link HTML elements (<a> tag) to be opened on seprate
window. All arguments are optional.
Arguments:
href: linked resource. Mind that an HTML <a> tag without an
href does not get selected when applying link styles.
content: CTK widget to show inside the <a>...</a> block.
props: dictionary with properties for the HTML element,
such as {'name': 'foo', 'id': 'bar', 'class': 'baz'}
Examples:
link1 = CTK.LinkWindow ('http://example.com', CTK.RawHTML('Example'))
link2 = CTK.LinkWindow (content=CTK.Image({'src': '/var/www/static/image.png'})
"""
def __init__ (self, href, content=None, props={}):
self.props = props.copy()
props['target'] = '_blank'
Link.__init__ (self, href, content, props)
class LinkIcon (Link):
def __init__ (self, href="#", icon='newwin', content=None):
Link.__init__ (self, href)
self.icon = icon
if content:
self += content
def Render (self):
render = Link.Render (self)
props = {'id': self.id,
'icon': self.icon,
'link': render.html}
render.html = formatter (LINK_ICON_HTML, props)
return render