1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 from pyjamas import DOM
16 from pyjamas import Factory
17 from pyjamas import Window
18 from pyjamas.ui import Applier
19
21
22 oldStyle = DOM.getAttribute(element, "className")
23 if oldStyle is None:
24 oldStyle = ""
25 idx = oldStyle.find(style)
26
27
28 lastPos = len(oldStyle)
29 while idx != -1:
30 if idx == 0 or (oldStyle[idx - 1] == " "):
31 last = idx + len(style)
32 if (last == lastPos) or ((last < lastPos) and (oldStyle[last] == " ")):
33 break
34 idx = oldStyle.find(style, idx + 1)
35
36 if add:
37 if idx == -1:
38 DOM.setAttribute(element, "className", oldStyle + " " + style)
39 else:
40 if idx != -1:
41 if idx == 0:
42 begin = ''
43 else:
44 begin = oldStyle[:idx-1]
45 end = oldStyle[idx + len(style):]
46 DOM.setAttribute(element, "className", begin + end)
47
49
56
59
62
64 """Get the DOM element associated with the UIObject, if any"""
65 return self.element
66
69
72
75
77 """Return with the first className if there are multiples"""
78 fullClassName = self.getStyleName()
79 if fullClassName: return fullClassName.split()[0]
80
83
85 """Set the DOM element associated with the UIObject."""
86 self.element = element
87
89 """Set the height of the element associated with this UIObject. The
90 value should be given as a CSS value, such as 100px, 30%, or 50pi"""
91 DOM.setStyleAttribute(self.element, "height", str(height))
92
95
97 """Set the width and height of the element associated with this UIObject
98 in pixels. Width and height should be numbers."""
99 if width >= 0:
100 self.setWidth("%dpx" % width)
101 if height >= 0:
102 self.setHeight("%dpx" % height)
103
105 """Set the width and height of the element associated with this UIObject. The
106 values should be given as a CSS value, such as 100px, 30%, or 50pi"""
107 self.setWidth(width)
108 self.setHeight(height)
109
111 """Append a style to the element associated with this UIObject. This is
112 a CSS class name. It will be added after any already-assigned CSS class for
113 the element."""
114 self.setStyleName(self.element, style, True)
115
117 """Adds a secondary or dependent style name to this element.
118 For example if the primary stylename is gwt-TextBox,
119 self.addStyleDependentName("readonly") will return gwt-TextBox-readonly."""
120 self.addStyleName(self.getStylePrimaryName()+"-"+styleSuffix)
121
123 """Remove a style from the element associated with this UIObject. This is
124 a CSS class name."""
125 self.setStyleName(self.element, style, False)
126
130
131
133 """When called with a single argument, this replaces all the CSS classes
134 associated with this UIObject's element with the given parameter. Otherwise,
135 this is assumed to be a worker function for addStyleName and removeStyleName."""
136
137 if style is None:
138 style = element
139 DOM.setAttribute(self.element, "className", style)
140 return
141 setStyleName(element, style, add)
142
145
147 """Set the width of the element associated with this UIObject. The
148 value should be given as a CSS value, such as 100px, 30%, or 50pi"""
149 DOM.setStyleAttribute(self.element, "width", str(width))
150
153
155 """Request that the given events be delivered to the event handler for this
156 element. The event bits passed are added (using inclusive OR) to the events
157 already "sunk" for the element associated with the UIObject. The event bits
158 are a combination of values from class L{Event}."""
159 if self.element:
160 DOM.sinkEvents(self.getElement(), eventBitsToAdd | DOM.getEventsSunk(self.getElement()))
161
164
166 """Determine whether this element is currently visible, by checking
167 the CSS property 'display'
168 """
169 if not element:
170 element = self.element
171 try:
172 return element.style.display != "none"
173 except AttributeError:
174 return True
175
176
178 """Set whether this element is visible or not. If a single parameter is
179 given, the self.element is used. This modifies the CSS property 'display',
180 which means that an invisible element not only is not drawn, but doesn't
181 occupy any space on the page."""
182 if visible is None:
183 visible = element
184 element = self.element
185
186 if visible:
187 DOM.setStyleAttribute(element, 'display', "")
188 else:
189 DOM.setStyleAttribute(element, 'display', "none")
190
194
195 Factory.registerClass('pyjamas.ui.UIObject', UIObject)
196