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