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

Source Code for Module library.pyjamas.ui.UIObject

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  from pyjamas import DOM 
 16  from pyjamas import Factory 
 17  from pyjamas import Window 
 18  from pyjamas.ui import Applier 
19 20 -def findStyleName(element, style):
21 22 oldStyle = DOM.getAttribute(element, "className") 23 if oldStyle is None: 24 return -1 25 idx = oldStyle.find(style) 26 27 # Calculate matching index 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
39 -def setStyleName(element, style, add):
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
63 -class UIObject(Applier):
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
76 - def _getProps(self):
77 return Applier._getProps() + self._props
78
79 - def __init__(self, **kwargs):
80 # do not initialise element, here, to None, whatever you do. 81 # there are circumstances where UIObject.__init__ is the last 82 # thing that is done in derived classes, where self.setElement 83 # will _already_ have been called. 84 Applier.__init__(self, **kwargs)
85
86 - def getAbsoluteLeft(self):
87 return DOM.getAbsoluteLeft(self.getElement())
88
89 - def getAbsoluteTop(self):
90 return DOM.getAbsoluteTop(self.getElement())
91
92 - def getClientHeight(self):
93 return DOM.getIntAttribute(self.getElement(), "clientHeight")
94
95 - def getClientWidth(self):
96 return DOM.getIntAttribute(self.getElement(), "clientWidth")
97
98 - def getElement(self):
99 """Get the DOM element associated with the UIObject, if any""" 100 return self.element
101
102 - def getOffsetHeight(self):
103 return DOM.getIntAttribute(self.element, "offsetHeight")
104
105 - def getOffsetWidth(self):
106 return DOM.getIntAttribute(self.element, "offsetWidth")
107
108 - def getStyleName(self):
109 return DOM.getAttribute(self.element, "className")
110
111 - def getStylePrimaryName(self):
112 """Return with the first className if there are multiples""" 113 fullClassName = self.getStyleName() 114 if fullClassName: return fullClassName.split()[0]
115
116 - def getStyleAttribute(self, attribute):
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 # if attribute is not a string, assume it is iterable, 124 # and return the multi-attribute form 125 el = self.getElement() 126 result = {} 127 for attr in attribute: 128 result[attr] = DOM.getStyleAttribute(el,attr) 129 return result
130
131 - def getTitle(self):
132 return DOM.getAttribute(self.element, "title")
133
134 - def setElement(self, element):
135 """Set the DOM element associated with the UIObject.""" 136 self.element = element
137
138 - def setHeight(self, height):
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
146 - def getHeight(self):
147 return DOM.getStyleAttribute(self.element, "height")
148
149 - def setPixelSize(self, width, height):
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
158 - def setSize(self, width, height):
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
166 - def addStyleName(self, style):
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
173 - def addStyleDependentName(self, styleSuffix):
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
181 - def removeStyleName(self, style):
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
186 - def removeStyleDependentName(self, styleSuffix):
187 """Remove a dependent style name by specifying the style name's suffix. 188 """ 189 self.removeStyleName(self.getStylePrimaryName()+"-"+styleSuffix)
190 191 # also callable as: setStyleName(self, style)
192 - def setStyleName(self, element, style=None, add=True):
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 # emulate setStyleName(self, style) 199 if style is not None: 200 setStyleName(element, style, add) 201 return 202 style = element 203 DOM.setAttribute(self.element, "className", style)
204
205 - def setStyleAttribute(self, attribute, value=None):
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: # assume single attr form 211 DOM.setStyleAttribute(self.getElement(), attribute, value) 212 return 213 # assume multi value form 214 el = self.getElement() 215 for attr, val in attribute.items(): 216 DOM.setStyleAttribute(el, attr, val)
217
218 - def setTitle(self, title):
219 DOM.setAttribute(self.element, "title", title)
220
221 - def setWidth(self, width):
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
229 - def getWidth(self):
230 return DOM.getStyleAttribute(self.element, "width")
231
232 - def sinkEvents(self, eventBitsToAdd):
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
243 - def setzIndex(self, index):
244 DOM.setIntStyleAttribute(self.element, "zIndex", index)
245
246 - def isVisible(self, element=None):
247 """ XXX DEPRECATED - use getVisible 248 """ 249 return self.getVisible(element)
250
251 - def getVisible(self, element=None):
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: # yuk! 258 return element.style.display != "none" 259 except AttributeError: # not been set (yet?) 260 return True
261 262 # also callable as: setVisible(visible)
263 - def setVisible(self, element, visible=None):
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
278 - def unsinkEvents(self, eventBitsToRemove):
279 """Reverse the operation of sinkEvents. See L{UIObject.sinkEvents}. 280 """ 281 DOM.sinkEvents(self.getElement(), 282 ~eventBitsToRemove & DOM.getEventsSunk(self.getElement()))
283 284 Factory.registerClass('pyjamas.ui.UIObject', 'UIObject', UIObject) 285