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

Source Code for Module pyjamas.ui.ClickListener

 1  """ 
 2      Copyright (C) 2008, 2009 - Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
 3   
 4  """ 
 5  from pyjamas import DOM 
 6  from pyjamas.ui import Event 
 7   
8 -class ClickHandler(object):
9
10 - def __init__(self, preventDefault=False):
11 self._clickListeners = [] 12 self._doubleclickListeners = [] 13 self._clickPreventDefault = preventDefault 14 15 self.sinkEvents(Event.ONCLICK) 16 self.sinkEvents(Event.ONDBLCLICK)
17
18 - def onClick(self, sender=None):
19 pass
20
21 - def onDoubleClick(self, sender=None):
22 pass
23
24 - def addDoubleClickListener(self, listener):
25 self._doubleclickListeners.append(listener)
26
27 - def addClickListener(self, listener):
28 self._clickListeners.append(listener)
29
30 - def onBrowserEvent(self, event):
31 """Listen to events raised by the browser and call the appropriate 32 method of the listener (widget, ..) object. 33 """ 34 type = DOM.eventGetType(event) 35 if type == "click": 36 if self._clickPreventDefault: 37 DOM.eventPreventDefault(event) 38 for listener in self._clickListeners: 39 if hasattr(listener, "onClick"): 40 listener.onClick(self) 41 else: 42 listener(self) 43 elif type == "dblclick": 44 if self._clickPreventDefault: 45 DOM.eventPreventDefault(event) 46 for listener in self._doubleclickListeners: 47 if hasattr(listener, "onDoubleClick"): 48 listener.onDoubleClick(self) 49 else: 50 listener(self)
51
52 - def removeClickListener(self, listener):
53 self._clickListeners.remove(listener)
54
55 - def removeDoubleClickListener(self, listener):
56 self._doubleclickListeners.remove(listener)
57
58 - def clearClickListener(self):
59 self._clickListeners[:] = []
60
62 self._doubleclickListeners.remove(listener)[:] = []
63