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

Source Code for Module library.pyjamas.ui.PopupPanel

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009, 2010 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # Copyright (C) 2010 Serge Tarkovski <serge.tarkovski@gmail.com> 
  4  # 
  5  # Licensed under the Apache License, Version 2.0 (the "License"); 
  6  # you may not use this file except in compliance with the License. 
  7  # You may obtain a copy of the License at 
  8  # 
  9  #     http://www.apache.org/licenses/LICENSE-2.0 
 10  # 
 11  # Unless required by applicable law or agreed to in writing, software 
 12  # distributed under the License is distributed on an "AS IS" BASIS, 
 13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 14  # See the License for the specific language governing permissions and 
 15  # limitations under the License. 
 16  from pyjamas import DOM 
 17  from pyjamas import Window 
 18  from pyjamas import Factory 
 19  from __pyjamas__ import JS, doc 
 20  from pyjamas.ui.SimplePanel import SimplePanel 
 21  from pyjamas.ui.RootPanel import RootPanel 
 22  from pyjamas.ui import MouseListener 
 23  from pyjamas.ui import KeyboardListener 
24 25 26 -class PopupPanel(SimplePanel):
27 28 _props = [("modal", "Modal", "Modal", None), 29 ] 30
31 - def __init__(self, autoHide=False, modal=True, rootpanel=None, glass=False, 32 **kwargs):
33 34 self.popupListeners = [] 35 self.showing = False 36 self.autoHide = autoHide 37 kwargs['Modal'] = kwargs.get('Modal', modal) 38 39 if rootpanel is None: 40 rootpanel = RootPanel() 41 self.rootpanel = rootpanel 42 43 self.glass = glass 44 if self.glass: 45 self.glass = DOM.createDiv() 46 if not 'GlassStyleName' in kwargs: 47 kwargs['GlassStyleName'] = "gwt-PopupPanelGlass" 48 49 if kwargs.has_key('Element'): 50 element = kwargs.pop('Element') 51 else: 52 element = self.createElement() 53 DOM.setStyleAttribute(element, "position", "absolute") 54 55 SimplePanel.__init__(self, element, **kwargs) 56 57 if glass: 58 self.setGlassEnabled(True) 59 if 'GlassStyleName' in kwargs: 60 self.setGlassStyleName(kwargs.pop('GlassStyleName'))
61 62 63 64 @classmethod
65 - def _getProps(self):
66 return SimplePanel._getProps() + self._props
67
68 - def addPopupListener(self, listener):
69 self.popupListeners.append(listener)
70
71 - def getPopupLeft(self):
72 return DOM.getIntAttribute(self.getElement(), "offsetLeft")
73
74 - def getPopupTop(self):
75 return DOM.getIntAttribute(self.getElement(), "offsetTop")
76 77 # PopupImpl.createElement
78 - def createElement(self):
79 return DOM.createDiv()
80
81 - def hide(self, autoClosed=False):
82 if not self.showing: 83 return 84 self.showing = False 85 86 if self.glass: 87 self.hideGlass() 88 89 DOM.removeEventPreview(self) 90 91 self.rootpanel.remove(self) 92 self.onHideImpl(self.getElement()) 93 for listener in self.popupListeners: 94 if hasattr(listener, 'onPopupClosed'): 95 listener.onPopupClosed(self, autoClosed) 96 else: 97 listener(self, autoClosed)
98
99 - def setModal(self, modal):
100 self.modal = modal
101
102 - def getModal(self):
103 return self.isModal()
104
105 - def isModal(self):
106 """ deprecated - please use getModal 107 """ 108 return self.modal
109
110 - def _event_targets_popup(self, event):
111 target = DOM.eventGetTarget(event) 112 return target and DOM.isOrHasChild(self.getElement(), target)
113
114 - def onEventPreview(self, event):
115 etype = DOM.eventGetType(event) 116 if etype == "keydown": 117 return ( self.onKeyDownPreview( 118 DOM.eventGetKeyCode(event), 119 KeyboardListener.getKeyboardModifiers(event) 120 ) 121 and (not self.modal or self._event_targets_popup(event)) 122 ) 123 elif etype == "keyup": 124 return ( self.onKeyUpPreview( 125 DOM.eventGetKeyCode(event), 126 KeyboardListener.getKeyboardModifiers(event) 127 ) 128 and (not self.modal or self._event_targets_popup(event)) 129 ) 130 elif etype == "keypress": 131 return ( self.onKeyPressPreview( 132 DOM.eventGetKeyCode(event), 133 KeyboardListener.getKeyboardModifiers(event) 134 ) 135 and (not self.modal or self._event_targets_popup(event)) 136 ) 137 elif ( etype == "mousedown" 138 or etype == "blur" 139 ): 140 if DOM.getCaptureElement() is not None: 141 return True 142 if self.autoHide and not self._event_targets_popup(event): 143 self.hide(True) 144 return True 145 elif ( etype == "mouseup" 146 or etype == "click" 147 or etype == "mousemove" 148 or type == "dblclick" 149 ): 150 if DOM.getCaptureElement() is not None: 151 return True 152 return not self.modal or self._event_targets_popup(event)
153
154 - def onKeyDownPreview(self, key, modifiers):
155 return True
156
157 - def onKeyPressPreview(self, key, modifiers):
158 return True
159
160 - def onKeyUpPreview(self, key, modifiers):
161 return True
162 163 # PopupImpl.onHide
164 - def onHideImpl(self, popup):
165 pass
166 167 # PopupImpl.onShow
168 - def onShowImpl(self, popup):
169 pass
170
171 - def removePopupListener(self, listener):
172 self.popupListeners.remove(listener)
173
174 - def setPopupPosition(self, left, top):
175 if isinstance(left, basestring): 176 if left.endswith('%'): 177 left = int(left[:-1]) 178 left = int(left * Window.getClientWidth() / 100) 179 elif left.lower().endswith('px'): 180 left = int(left[:-2]) 181 if isinstance(top, basestring): 182 if top.lower().endswith('%'): 183 top = int(top[:-1]) 184 top = int(top * Window.getClientHeight() / 100) 185 elif top.endswith('px'): 186 top = int(top[:-2]) 187 188 left = max(left, 0) 189 top = max(top, 0) 190 191 # Account for the difference between absolute position and the 192 # body's positioning context. 193 left -= DOM.getBodyOffsetLeft() 194 top -= DOM.getBodyOffsetTop() 195 196 element = self.getElement() 197 DOM.setStyleAttribute(element, "left", "%dpx" % left) 198 DOM.setStyleAttribute(element, "top", "%dpx" % top)
199
200 - def isGlassEnabled(self):
201 return self.glass is not None
202
203 - def setGlassEnabled(self, enabled):
204 if enabled: 205 if self.glass is None: 206 self.glass = DOM.createDiv() 207 self.setGlassStyleName() 208 elif self.glass is not None: 209 self.hideGlass()
210
211 - def getGlassElement(self):
212 return self.glass
213
214 - def setGlassStyleName(self, style="gwt-PopupPanelGlass"):
215 if self.glass: 216 DOM.setAttribute(self.glass, "className", style)
217
218 - def getGlassStyleName(self):
219 if self.glass: 220 return DOM.setAttribute(self.glass, "className")
221
222 - def setGlassPosition(self):
223 top = Window.getScrollTop() 224 left = Window.getScrollLeft() 225 height = Window.getClientHeight() 226 width = Window.getClientWidth() 227 228 DOM.setStyleAttribute(self.glass, "position", "absolute") 229 DOM.setStyleAttribute(self.glass, "left", "%s" % \ 230 left if left == 0 else "%spx" % left) 231 DOM.setStyleAttribute(self.glass, "top", "%s" % \ 232 top if top == 0 else "%spx" % top) 233 DOM.setStyleAttribute(self.glass, "height", "%spx" % (top + height)) 234 DOM.setStyleAttribute(self.glass, "width", "%spx" % (left + width))
235
236 - def showGlass(self):
237 Window.enableScrolling(False) 238 self.setGlassPosition() 239 doc().body.appendChild(self.glass) 240 Window.addWindowResizeListener(self)
241
242 - def hideGlass(self):
243 Window.removeWindowResizeListener(self) 244 doc().body.removeChild(self.glass) 245 Window.enableScrolling(True)
246
247 - def onWindowResized(self, width, height):
248 self.setGlassPosition()
249
250 - def centerBox(self):
251 self_width = self.getOffsetWidth() 252 self_height = self.getOffsetHeight() 253 254 height = Window.getClientHeight() 255 width = Window.getClientWidth() 256 257 center_x = int(width) / 2 258 center_y = int(height) / 2 259 260 self_top = center_y - (int(self_height) / 2) 261 self_left = center_x - (int(self_width) / 2) 262 263 self.setPopupPosition(self_left, self_top)
264
265 - def center(self):
266 self.centerBox() 267 self.show()
268
269 - def add(self, widget):
270 self.setWidget(widget)
271
272 - def show(self):
273 if self.showing: 274 return 275 276 self.showing = True 277 278 if self.glass: 279 self.showGlass() 280 281 DOM.addEventPreview(self) 282 283 self.rootpanel.add(self) 284 self.onShowImpl(self.getElement())
285 286 Factory.registerClass('pyjamas.ui.PopupPanel', 'PopupPanel', PopupPanel) 287