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

Source Code for Module pyjamas.ui.DropHandler

  1  # Copyright (C) 2010 Jim Washington 
  2  # 
  3  # Licensed under the Apache License, Version 2.0 (the "License"); 
  4  # you may not use this file except in compliance with the License. 
  5  # You may obtain a copy of the License at 
  6  # 
  7  #     http://www.apache.org/licenses/LICENSE-2.0 
  8  # 
  9  # Unless required by applicable law or agreed to in writing, software 
 10  # distributed under the License is distributed on an "AS IS" BASIS, 
 11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 12  # See the License for the specific language governing permissions and 
 13  # limitations under the License. 
 14   
 15  from pyjamas import DOM 
 16  from pyjamas.ui import Event 
 17   
 18  DROP_EVENTS = [ "dragenter", "dragover", "dragleave", "drop"] 
 19   
20 -def fireDropEvent(listeners, event):
21 etype = DOM.eventGetType(event) 22 if etype == "dragenter": 23 for listener in listeners: 24 listener.onDragEnter(event) 25 return True 26 elif etype == "dragover": 27 for listener in listeners: 28 listener.onDragOver(event) 29 return True 30 elif etype == "dragleave": 31 for listener in listeners: 32 listener.onDragLeave(event) 33 return True 34 elif etype == "drop": 35 for listener in listeners: 36 listener.onDrop(event) 37 return True 38 return False
39 40
41 -class DropHandler(object):
42
43 - def __init__(self):
44 self._dropListeners = [] 45 self.sinkEvents(Event.DROPEVENTS)
46
47 - def onBrowserEvent(self, event):
48 event_type = DOM.eventGetType(event) 49 if event_type in DROP_EVENTS: 50 return fireDropEvent(self._dropListeners, event) 51 return False
52
53 - def addDropListener(self, listener):
54 self._dropListeners.append(listener)
55
56 - def removeDropListener(self, listener):
57 self._dropListeners.remove(listener)
58
59 - def onDragEnter(self,event):
60 """ 61 Decide whether to accept the drop. 62 63 You may inspect the event's dataTransfer member. 64 65 You may get the types using pyjamas.dnd.getTypes(event). 66 67 This event is used to determine whether the drop target may 68 accept the drop. If the drop is to be accepted, then this event has 69 to be canceled using DOM.eventPreventDefault(event). 70 """ 71 pass
72
73 - def onDragOver(self,event):
74 """ 75 This event determines what feedback is to be shown to the user. If 76 the event is canceled, then the feedback (typically the cursor) is 77 updated based on the dropEffect attribute's value, as set by the event 78 handler; otherwise, the default behavior (typically to do nothing) 79 is used instead. 80 81 Setting event.dataTransfer.dropEffect may affect dropping behavior. 82 83 Cancel this event with DOM.eventPreventDefault(event) if you want the 84 drop to succeed. 85 86 """ 87 pass
88
89 - def onDragLeave(self,event):
90 """ 91 This event happens when the mouse leaves the target element. 92 """ 93 pass
94
95 - def onDrop(self,event):
96 """allows the actual drop to be performed. This event also needs to be 97 canceled, so that the dropEffect attribute's value can be used by the 98 source (otherwise it's reset). 99 """ 100 pass
101