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