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
21
22 oldStyle = DOM.getAttribute(element, "className")
23 if oldStyle is None:
24 return -1
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 \
33 (oldStyle[last] == " ")):
34 break
35 idx = oldStyle.find(style, idx + 1)
36
37 return idx
38
40
41 oldStyle = DOM.getAttribute(element, "className")
42 if oldStyle is None:
43 oldStyle = ""
44
45 idx = findStyleName(element, style)
46
47 if add:
48 if idx == -1:
49 DOM.setAttribute(element, "className", oldStyle + " " + style)
50 return
51
52 if idx == -1:
53 return
54
55 if idx == 0:
56 begin = ''
57 else:
58 begin = oldStyle[:idx-1]
59
60 end = oldStyle[idx + len(style):]
61 DOM.setAttribute(element, "className", begin + end)
62
64
65 _props = [ ("visible", "Visibility", "Visible", None),
66 ("element", "Element", "Element", None),
67 ("stylename", "Style name", "StyleName", None),
68 ("width", "Width", "Width", None),
69 ("height", "Height", "Height", None),
70 ("size", "Size", "Size", None),
71 ("title", "Title", "Title", None),
72 ("zindex", "Z Index", "zIndex", None),
73 ]
74
75 @classmethod
78
85
88
91
93 """Get the DOM element associated with the UIObject, if any"""
94 return self.element
95
98
101
104
106 """Return with the first className if there are multiples"""
107 fullClassName = self.getStyleName()
108 if fullClassName: return fullClassName.split()[0]
109
111 """ can be called with two forms:
112 getStyleAttribute(self, attr) - returns value
113 getStyleAttribute(self, (attr1,attr2,...)) - returns dictionary of attr:value pairs
114 """
115 if isinstance(attribute, basestring):
116 return DOM.getStyleAttribute(self.getElement(), attribute)
117
118
119 el = self.getElement()
120 result = {}
121 for attr in attribute:
122 result[attr] = DOM.getStyleAttribute(el,attr)
123 return result
124
127
129 """Set the DOM element associated with the UIObject."""
130 self.element = element
131
133 """Set the height of the element associated with this UIObject. The
134 value should be given as a CSS value, such as 100px, 30%, or 50pi
135 """
136 if height is None:
137 height = ""
138 DOM.setStyleAttribute(self.element, "height", str(height))
139
142
144 """Set the width and height of the element associated with this UIObject
145 in pixels. Width and height should be numbers.
146 """
147 if width >= 0:
148 self.setWidth("%dpx" % width)
149 if height >= 0:
150 self.setHeight("%dpx" % height)
151
153 """Set the width and height of the element associated with this
154 UIObject. The values should be given as a CSS value,
155 such as 100px, 30%, or 50pi
156 """
157 self.setWidth(width)
158 self.setHeight(height)
159
161 """Append a style to the element associated with this UIObject.
162 This is a CSS class name. It will be added after any
163 already-assigned CSS class for the element.
164 """
165 self.setStyleName(self.element, style, True)
166
168 """Adds a secondary or dependent style name to this element.
169 For example if the primary stylename is gwt-TextBox,
170 self.addStyleDependentName("readonly") will return
171 gwt-TextBox-readonly.
172 """
173 self.addStyleName(self.getStylePrimaryName()+"-"+styleSuffix)
174
176 """Remove a style from the element associated with this UIObject. This is
177 a CSS class name."""
178 self.setStyleName(self.element, style, False)
179
184
185
187 """When called with a single argument, this replaces all the CSS
188 classes associated with this UIObject's element with the given
189 parameter. Otherwise, this is assumed to be a worker function
190 for addStyleName and removeStyleName.
191 """
192
193 if style is not None:
194 setStyleName(element, style, add)
195 return
196 style = element
197 DOM.setAttribute(self.element, "className", style)
198
200 """ can be called with two forms:
201 single attr: setStyleAttribute(self, attr, value)
202 multi attr: setStyleAttribute(self, {attr1:val1, attr2:val2, ...})
203 """
204 if value is not None:
205 DOM.setStyleAttribute(self.getElement(), attribute, value)
206 return
207
208 el = self.getElement()
209 for attr, val in attribute.items():
210 DOM.setStyleAttribute(el, attr, val)
211
214
216 """Set the width of the element associated with this UIObject. The
217 value should be given as a CSS value, such as 100px, 30%, or 50pi
218 """
219 if width is None:
220 width = ""
221 DOM.setStyleAttribute(self.element, "width", str(width))
222
225
227 """Request that the given events be delivered to the event handler
228 for this element. The event bits passed are added (using inclusive
229 OR) to the events already "sunk" for the element associated with
230 the UIObject. The event bits are a combination of values from
231 class L{Event}.
232 """
233 if self.element:
234 DOM.sinkEvents(self.getElement(),
235 eventBitsToAdd | DOM.getEventsSunk(self.getElement()))
236
239
241 """ XXX DEPRECATED - use getVisible
242 """
243 return self.getVisible(element)
244
246 """Determine whether this element is currently visible, by checking
247 the CSS property 'display'
248 """
249 if not element:
250 element = self.element
251 try:
252 return element.style.display != "none"
253 except AttributeError:
254 return True
255
256
258 """Set whether this element is visible or not. If a single parameter
259 is given, the self.element is used. This modifies the CSS
260 property 'display', which means that an invisible element not
261 only is not drawn, but doesn't occupy any space on the page.
262 """
263 if visible is None:
264 visible = element
265 element = self.element
266
267 if visible:
268 DOM.setStyleAttribute(element, 'display', "")
269 else:
270 DOM.setStyleAttribute(element, 'display', "none")
271
277
278 Factory.registerClass('pyjamas.ui.UIObject', 'UIObject', UIObject)
279