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
94
97
99 """Get the DOM element associated with the UIObject, if any"""
100 return self.element
101
104
107
110
112 """Return with the first className if there are multiples"""
113 fullClassName = self.getStyleName()
114 if fullClassName: return fullClassName.split()[0]
115
117 """ can be called with two forms:
118 getStyleAttribute(self, attr) - returns value
119 getStyleAttribute(self, (attr1,attr2,...)) - returns dictionary of attr:value pairs
120 """
121 if isinstance(attribute, basestring):
122 return DOM.getStyleAttribute(self.getElement(), attribute)
123
124
125 el = self.getElement()
126 result = {}
127 for attr in attribute:
128 result[attr] = DOM.getStyleAttribute(el,attr)
129 return result
130
133
135 """Set the DOM element associated with the UIObject."""
136 self.element = element
137
139 """Set the height of the element associated with this UIObject. The
140 value should be given as a CSS value, such as 100px, 30%, or 50pi
141 """
142 if height is None:
143 height = ""
144 DOM.setStyleAttribute(self.element, "height", str(height))
145
148
150 """Set the width and height of the element associated with this UIObject
151 in pixels. Width and height should be numbers.
152 """
153 if width >= 0:
154 self.setWidth("%dpx" % width)
155 if height >= 0:
156 self.setHeight("%dpx" % height)
157
159 """Set the width and height of the element associated with this
160 UIObject. The values should be given as a CSS value,
161 such as 100px, 30%, or 50pi
162 """
163 self.setWidth(width)
164 self.setHeight(height)
165
167 """Append a style to the element associated with this UIObject.
168 This is a CSS class name. It will be added after any
169 already-assigned CSS class for the element.
170 """
171 self.setStyleName(self.element, style, True)
172
174 """Adds a secondary or dependent style name to this element.
175 For example if the primary stylename is gwt-TextBox,
176 self.addStyleDependentName("readonly") will return
177 gwt-TextBox-readonly.
178 """
179 self.addStyleName(self.getStylePrimaryName()+"-"+styleSuffix)
180
182 """Remove a style from the element associated with this UIObject. This is
183 a CSS class name."""
184 self.setStyleName(self.element, style, False)
185
190
191
193 """When called with a single argument, this replaces all the CSS
194 classes associated with this UIObject's element with the given
195 parameter. Otherwise, this is assumed to be a worker function
196 for addStyleName and removeStyleName.
197 """
198
199 if style is not None:
200 setStyleName(element, style, add)
201 return
202 style = element
203 DOM.setAttribute(self.element, "className", style)
204
206 """ can be called with two forms:
207 single attr: setStyleAttribute(self, attr, value)
208 multi attr: setStyleAttribute(self, {attr1:val1, attr2:val2, ...})
209 """
210 if value is not None:
211 DOM.setStyleAttribute(self.getElement(), attribute, value)
212 return
213
214 el = self.getElement()
215 for attr, val in attribute.items():
216 DOM.setStyleAttribute(el, attr, val)
217
220
222 """Set the width of the element associated with this UIObject. The
223 value should be given as a CSS value, such as 100px, 30%, or 50pi
224 """
225 if width is None:
226 width = ""
227 DOM.setStyleAttribute(self.element, "width", str(width))
228
231
233 """Request that the given events be delivered to the event handler
234 for this element. The event bits passed are added (using inclusive
235 OR) to the events already "sunk" for the element associated with
236 the UIObject. The event bits are a combination of values from
237 class L{Event}.
238 """
239 if self.element:
240 DOM.sinkEvents(self.getElement(),
241 eventBitsToAdd | DOM.getEventsSunk(self.getElement()))
242
245
247 """ XXX DEPRECATED - use getVisible
248 """
249 return self.getVisible(element)
250
252 """Determine whether this element is currently visible, by checking
253 the CSS property 'display'
254 """
255 if not element:
256 element = self.element
257 try:
258 return element.style.display != "none"
259 except AttributeError:
260 return True
261
262
264 """Set whether this element is visible or not. If a single parameter
265 is given, the self.element is used. This modifies the CSS
266 property 'display', which means that an invisible element not
267 only is not drawn, but doesn't occupy any space on the page.
268 """
269 if visible is None:
270 visible = element
271 element = self.element
272
273 if visible:
274 DOM.setStyleAttribute(element, 'display', "")
275 else:
276 DOM.setStyleAttribute(element, 'display', "none")
277
283
284 Factory.registerClass('pyjamas.ui.UIObject', 'UIObject', UIObject)
285