1 #!/usr/bin/env python 2 3 """ 4 DOM events support. 5 6 Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Lesser General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 Lesser General Public License for more details. 17 18 You should have received a copy of the GNU Lesser General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 21 """ 22 23 class EventException(Exception): 24 25 UNSPECIFIED_EVENT_TYPE_ERR = 0 26 DISPATCH_REQUEST_ERR = 1 27 28 class Event: 29 30 CAPTURING_PHASE = 1 31 AT_TARGET = 2 32 BUBBLING_PHASE = 3 33 34 def __init__(self): 35 type 36 target 37 currentTarget 38 eventPhase 39 bubbles 40 cancelable 41 timeStamp 42 namespaceURI 43 defaultPrevented 44 45 def initEvent(self, eventTypeArg, canBubbleArg, cancelableArg): 46 pass 47 48 def initEvent(self, eventTypeArg, canBubbleArg, cancelableArg): 49 pass 50 51 def initEventNS(self, namespaceURIArg, eventTypeArg, canBubbleArg, cancelableArg): 52 pass 53 54 def preventDefault(self): 55 pass 56 57 def stopPropagation(self): 58 pass 59 60 def stopImmediatePropagation(self): 61 pass 62 63 class EventTarget: 64 65 "An event target class." 66 67 def __init__(self): 68 self.listeners = {} 69 70 def addEventListener(self, type, listener, useCapture): 71 self.addEventListenerNS(None, type, listener, useCapture) 72 73 def addEventListenerNS(self, namespaceURI, type, listener, useCapture): 74 if not self.listeners.has_key((namespaceURI, type)): 75 self.listeners[(namespaceURI, type)] = [] 76 self.listeners[(namespaceURI, type)].append((listener, useCapture)) 77 78 def dispatchEvent(self, evt): 79 if not evt.type: 80 raise EventException(EventException.UNSPECIFIED_EVENT_TYPE_ERR) 81 # NOTE: Dispatch on namespaceURI, type... 82 83 def removeEventListener(self, type, listener, useCapture): 84 self.removeEventListenerNS(None, type, listener, useCapture) 85 86 def removeEventListenerNS(self, namespaceURI, type, listener, useCapture): 87 if self.listeners.has_key((namespaceURI, type)): 88 listeners = self.listeners[(namespaceURI, type)] 89 try: 90 listeners.remove((listener, useCapture)) 91 except ValueError: 92 pass 93 94 # vim: tabstop=4 expandtab shiftwidth=4