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

Source Code for Module library.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 stylename = self.getStyleName() 29 if stylename: 30 self.addStyleName("%s-clickable" % stylename.split()[0]) 31 self._clickListeners.append(listener)
32
33 - def onBrowserEvent(self, event):
34 """Listen to events raised by the browser and call the appropriate 35 method of the listener (widget, ..) object. 36 """ 37 type = DOM.eventGetType(event) 38 if type == "click": 39 if self._clickPreventDefault: 40 DOM.eventPreventDefault(event) 41 for listener in self._clickListeners: 42 if hasattr(listener, "onClick"): 43 listener.onClick(self) 44 else: 45 listener(self) 46 elif type == "dblclick": 47 if self._clickPreventDefault: 48 DOM.eventPreventDefault(event) 49 for listener in self._doubleclickListeners: 50 if hasattr(listener, "onDoubleClick"): 51 listener.onDoubleClick(self) 52 else: 53 listener(self)
54
55 - def removeClickListener(self, listener):
56 self._clickListeners.remove(listener)
57
58 - def removeDoubleClickListener(self, listener):
59 self._doubleclickListeners.remove(listener)
60
61 - def clearClickListener(self):
62 self._clickListeners[:] = []
63
65 self._doubleclickListeners.remove(listener)[:] = []
66