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

Source Code for Module library.pyjamas.ui.DragWidget

  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 Factory 
 16  from pyjamas import DOM 
 17  from pyjamas.ui.Widget import Widget 
 18  from pyjamas.ui.MouseListener import MouseHandler 
 19  from pyjamas.ui.DragHandler import DragHandler 
 20  from pyjamas.dnd import makeDraggable, DNDHelper 
 21  import pyjd 
 22   
23 -class DragWidget(object):
24 """ 25 Mix-in class for a draggable widget. 26 Override DragHandler on*** methods to enable drag behavior. 27 28 At runtime, we change the implementation based on html5 29 dra-and-drop capabilities of the engine. 30 """ 31 pass
32 33
34 -class DragContainer(object):
35 """ 36 mixin providing drag handlers for contained draggables 37 events bubble up to here. event.target will be the actual draggable 38 39 This class is the same as DragWidget, but does not make itself draggable. 40 41 At runtime, we change the implementation based on html5 42 drag-and-drop capabilities of the engine. 43 """ 44 pass
45 46
47 -class Draggable(Widget):
48 - def makeDraggable(self):
49 makeDraggable(self)
50 51
52 -class Html5DragContainer(Widget, DragHandler):
53 - def __init__(self, **kw):
54 if (not hasattr(self, 'attached')) or kw: 55 Widget.__init__(self, **kw) 56 DragHandler.__init__(self) 57 self.addDragListener(self)
58 59
60 -class MouseDragContainer(Widget, MouseHandler, DragHandler):
61 - def __init__(self, **kw):
62 if (not hasattr(self, 'attached')) or kw: 63 Widget.__init__(self, **kw) 64 MouseHandler.__init__(self) 65 self.addMouseListener(DNDHelper.dndHelper) 66 DragHandler.__init__(self) 67 self.addDragListener(self)
68 69
70 -class Html5DragWidget(Html5DragContainer, Draggable):
71 - def __init__(self, **kw):
72 Html5DragContainer.__init__(self, **kw) 73 self.makeDraggable()
74 75
76 -class MouseDragWidget(MouseDragContainer, Draggable):
77 - def __init__(self, **kw):
78 MouseDragContainer.__init__(self, **kw) 79 self.makeDraggable()
80 81
82 -def init(is_native=None):
83 global DragWidget, DragContainer 84 if is_native is None: 85 html5_dnd = hasattr(DOM.createElement('span'), 'draggable') 86 else: 87 html5_dnd = is_native 88 if html5_dnd: 89 DragContainer = Html5DragContainer 90 DragWidget = Html5DragWidget 91 else: 92 DragContainer = MouseDragContainer 93 DragWidget = MouseDragWidget
94 95 if pyjd.is_desktop: 96 init(pyjd.native_dnd) 97 else: 98 init(None) 99 100 Factory.registerClass('pyjamas.ui.DragWidget', 'DragWidget', DragWidget) 101 Factory.registerClass('pyjamas.ui.DragWidget', 'DragContainer', DragContainer) 102