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

Source Code for Module 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 getElement(self):
93 """Get the DOM element associated with the UIObject, if any""" 94 return self.element
95
96 - def getOffsetHeight(self):
97 return DOM.getIntAttribute(self.element, "offsetHeight")
98
99 - def getOffsetWidth(self):
100 return DOM.getIntAttribute(self.element, "offsetWidth")
101
102 - def getStyleName(self):
103 return DOM.getAttribute(self.element, "className")
104
105 - def getStylePrimaryName(self):
106 """Return with the first className if there are multiples""" 107 fullClassName = self.getStyleName() 108 if fullClassName: return fullClassName.split()[0]
109
110 - def getStyleAttribute(self, attribute):
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 # if attribute is not a string, assume it is iterable, 118 # and return the multi-attribute form 119 el = self.getElement() 120 result = {} 121 for attr in attribute: 122 result[attr] = DOM.getStyleAttribute(el,attr) 123 return result
124
125 - def getTitle(self):
126 return DOM.getAttribute(self.element, "title")
127
128 - def setElement(self, element):
129 """Set the DOM element associated with the UIObject.""" 130 self.element = element
131
132 - def setHeight(self, height):
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
140 - def getHeight(self):
141 return DOM.getStyleAttribute(self.element, "height")
142
143 - def setPixelSize(self, width, height):
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
152 - def setSize(self, width, height):
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
160 - def addStyleName(self, style):
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
167 - def addStyleDependentName(self, styleSuffix):
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
175 - def removeStyleName(self, style):
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
180 - def removeStyleDependentName(self, styleSuffix):
181 """Remove a dependent style name by specifying the style name's suffix. 182 """ 183 self.removeStyleName(self.getStylePrimaryName()+"-"+styleSuffix)
184 185 # also callable as: setStyleName(self, style)
186 - def setStyleName(self, element, style=None, add=True):
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 # emulate setStyleName(self, style) 193 if style is not None: 194 setStyleName(element, style, add) 195 return 196 style = element 197 DOM.setAttribute(self.element, "className", style)
198
199 - def setStyleAttribute(self, attribute, value=None):
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: # assume single attr form 205 DOM.setStyleAttribute(self.getElement(), attribute, value) 206 return 207 # assume multi value form 208 el = self.getElement() 209 for attr, val in attribute.items(): 210 DOM.setStyleAttribute(el, attr, val)
211
212 - def setTitle(self, title):
213 DOM.setAttribute(self.element, "title", title)
214
215 - def setWidth(self, width):
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
223 - def getWidth(self):
224 return DOM.getStyleAttribute(self.element, "width")
225
226 - def sinkEvents(self, eventBitsToAdd):
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
237 - def setzIndex(self, index):
238 DOM.setIntStyleAttribute(self.element, "zIndex", index)
239
240 - def isVisible(self, element=None):
241 """ XXX DEPRECATED - use getVisible 242 """ 243 return self.getVisible(element)
244
245 - def getVisible(self, element=None):
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: # yuk! 252 return element.style.display != "none" 253 except AttributeError: # not been set (yet?) 254 return True
255 256 # also callable as: setVisible(visible)
257 - def setVisible(self, element, visible=None):
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
272 - def unsinkEvents(self, eventBitsToRemove):
273 """Reverse the operation of sinkEvents. See L{UIObject.sinkEvents}. 274 """ 275 DOM.sinkEvents(self.getElement(), 276 ~eventBitsToRemove & DOM.getEventsSunk(self.getElement()))
277 278 Factory.registerClass('pyjamas.ui.UIObject', 'UIObject', UIObject) 279