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

Source Code for Module pyjamas.ui.PopupPanel

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 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 SimplePanel import SimplePanel 
 21  from RootPanel import RootPanel 
 22  from pyjamas.ui import MouseListener 
 23  from pyjamas.ui import KeyboardListener 
 24   
25 -class PopupPanel(SimplePanel):
26 - def __init__(self, autoHide=False, modal=True, rootpanel=None, glass=False, **kwargs):
27 28 self.popupListeners = [] 29 self.showing = False 30 self.autoHide = autoHide 31 self.modal = modal 32 33 self.glass = glass 34 if self.glass: 35 self.glass = DOM.createDiv() 36 if not 'GlassStyleName' in kwargs: 37 kwargs['GlassStyleName'] = "gwt-PopupPanelGlass" 38 39 if rootpanel is None: 40 rootpanel = RootPanel() 41 42 self.rootpanel = rootpanel 43 44 if kwargs.has_key('Element'): 45 element = kwargs.pop('Element') 46 else: 47 element = self.createElement() 48 DOM.setStyleAttribute(element, "position", "absolute") 49 50 SimplePanel.__init__(self, element, **kwargs)
51
52 - def addPopupListener(self, listener):
53 self.popupListeners.append(listener)
54
55 - def getPopupLeft(self):
56 return DOM.getIntAttribute(self.getElement(), "offsetLeft")
57
58 - def getPopupTop(self):
59 return DOM.getIntAttribute(self.getElement(), "offsetTop")
60 61 # PopupImpl.createElement
62 - def createElement(self):
63 return DOM.createDiv()
64
65 - def hide(self, autoClosed=False):
66 if not self.showing: 67 return 68 self.showing = False 69 70 if self.glass: 71 self.hideGlass() 72 73 DOM.removeEventPreview(self) 74 75 self.rootpanel.remove(self) 76 self.onHideImpl(self.getElement()) 77 for listener in self.popupListeners: 78 if hasattr(listener, 'onPopupClosed'): listener.onPopupClosed(self, autoClosed) 79 else: listener(self, autoClosed)
80
81 - def isModal(self):
82 return self.modal
83
84 - def _event_targets_popup(self, event):
85 target = DOM.eventGetTarget(event) 86 return target and DOM.isOrHasChild(self.getElement(), target)
87
88 - def onEventPreview(self, event):
89 type = DOM.eventGetType(event) 90 if type == "keydown": 91 return ( self.onKeyDownPreview( 92 DOM.eventGetKeyCode(event), 93 KeyboardListener.getKeyboardModifiers(event) 94 ) 95 and (not self.modal or self._event_targets_popup(event)) 96 ) 97 elif type == "keyup": 98 return ( self.onKeyUpPreview( 99 DOM.eventGetKeyCode(event), 100 KeyboardListener.getKeyboardModifiers(event) 101 ) 102 and (not self.modal or self._event_targets_popup(event)) 103 ) 104 elif type == "keypress": 105 return ( self.onKeyPressPreview( 106 DOM.eventGetKeyCode(event), 107 KeyboardListener.getKeyboardModifiers(event) 108 ) 109 and (not self.modal or self._event_targets_popup(event)) 110 ) 111 elif ( type == "mousedown" 112 or type == "blur" 113 ): 114 if DOM.getCaptureElement() is not None: 115 return True 116 if self.autoHide and not self._event_targets_popup(event): 117 self.hide(True) 118 return True 119 elif ( type == "mouseup" 120 or type == "click" 121 or type == "mousemove" 122 or type == "dblclick" 123 ): 124 if DOM.getCaptureElement() is not None: 125 return True 126 return not self.modal or self._event_targets_popup(event)
127
128 - def onKeyDownPreview(self, key, modifiers):
129 return True
130
131 - def onKeyPressPreview(self, key, modifiers):
132 return True
133
134 - def onKeyUpPreview(self, key, modifiers):
135 return True
136 137 # PopupImpl.onHide
138 - def onHideImpl(self, popup):
139 pass
140 141 # PopupImpl.onShow
142 - def onShowImpl(self, popup):
143 pass
144
145 - def removePopupListener(self, listener):
146 self.popupListeners.remove(listener)
147
148 - def setPopupPosition(self, left, top):
149 if left < 0: 150 left = 0 151 if top < 0: 152 top = 0 153 154 # Account for the difference between absolute position and the 155 # body's positioning context. 156 left -= DOM.getBodyOffsetLeft() 157 top -= DOM.getBodyOffsetTop() 158 159 element = self.getElement() 160 DOM.setStyleAttribute(element, "left", "%dpx" % left) 161 DOM.setStyleAttribute(element, "top", "%dpx" % top)
162
163 - def setGlassStyleName(self, style="gwt-PopupPanelGlass"):
164 if self.glass: 165 DOM.setAttribute(self.glass, "className", style)
166
167 - def setGlassPosition(self):
168 top = Window.getScrollTop() 169 left = Window.getScrollLeft() 170 height = Window.getClientHeight() 171 width = Window.getClientWidth() 172 173 DOM.setStyleAttribute(self.glass, "position", "absolute") 174 DOM.setStyleAttribute(self.glass, "left", "%s" % left if left == 0 else "%spx" % left) 175 DOM.setStyleAttribute(self.glass, "top", "%s" % top if top == 0 else "%spx" % top) 176 DOM.setStyleAttribute(self.glass, "height", "%spx" % (top + height)) 177 DOM.setStyleAttribute(self.glass, "width", "%spx" % (left + width))
178
179 - def showGlass(self):
180 Window.enableScrolling(False) 181 self.setGlassPosition() 182 doc().body.appendChild(self.glass) 183 Window.addWindowResizeListener(self)
184
185 - def hideGlass(self):
186 Window.removeWindowResizeListener(self) 187 doc().body.removeChild(self.glass) 188 Window.enableScrolling(True)
189
190 - def onWindowResized(self, width, height):
191 self.setGlassPosition()
192
193 - def show(self):
194 if self.showing: 195 return 196 197 self.showing = True 198 199 if self.glass: 200 self.showGlass() 201 202 DOM.addEventPreview(self) 203 204 self.rootpanel.add(self) 205 self.onShowImpl(self.getElement())
206 207 Factory.registerClass('pyjamas.ui.PopupPanel', PopupPanel) 208