Package library :: Package pyjamas :: Package dnd :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.dnd.utils

  1  from __pyjamas__ import wnd, doc 
  2  from pyjamas.ui.Widget import Widget 
  3  from pyjamas import DOM 
  4   
5 -def isIn(element, x, y):
6 """ 7 Given an element and an absolute x and y, return True if the 8 (x,y) coordinate is within the element. Otherwise, return False. 9 """ 10 top = DOM.getAbsoluteTop(element) 11 height = DOM.getIntAttribute(element, 'offsetHeight') 12 if y >= top and y <= top + height: 13 left = DOM.getAbsoluteLeft(element) 14 width = DOM.getIntAttribute(element, 'offsetWidth') 15 if x >= left and x <= left + width: 16 return True 17 return False
18 19
20 -def isCanceled(event):
21 """ 22 Has the DragEvent been canceled? 23 Coding this package was much easier with this function. 24 """ 25 return not event.returnValue
26
27 -def findDraggable(element, x, y):
28 """ 29 Find the element within a widget's main element that is 30 draggable and under the mouse cursor. We need this for event delegation. 31 32 """ 33 for elt in DOM.IterWalkChildren(element): 34 try: 35 draggable = elt.draggable 36 except: 37 draggable = False 38 if draggable: 39 if isIn(elt, x, y): 40 return elt 41 return None
42
43 -def cloneElement(element):
44 clone = element.cloneNode(True) 45 # if not element.tagName.lower() == 'canvas': 46 # copyStyles(element, clone) 47 return clone
48
49 -def getComputedStyle(element, style=None):
50 """ 51 Get computed style of an element, like in 52 http://efreedom.com/Question/1-1848445/Duplicating-Element-Style-JavaScript 53 """ 54 element_style = doc().defaultView.getComputedStyle(element, None) 55 #element_style = doc().defaultView.getComputedStyle(element, None) 56 if style: 57 return element_style[style] 58 return element_style
59
60 -def copyStyles(elem1, elem2):
61 """ 62 Copy styles from one element to another, like in 63 http://efreedom.com/Question/1-1848445/Duplicating-Element-Style-JavaScript 64 """ 65 element_style = getComputedStyle(elem1) 66 if element_style: 67 element_style = dict(element_style) 68 else: 69 return 70 for style in element_style: 71 try: 72 value = element_style[style] 73 if isinstance(value, str): 74 if not style == 'cssText': 75 DOM.setStyleAttribute(elem2, style, value) 76 if value == 'font': 77 DOM.setStyleAttribute(elem2, 'fontSize', 78 DOM.getStyleAttribute(elem1, 'fontSize')) 79 except: 80 pass
81
82 -def getTargetInChildren(element, x, y):
83 """ 84 x and y are absolute coordinates within the document. 85 Return the last child of element that contains (x,y). 86 Return None if not found. 87 """ 88 return_elt = None 89 if DOM.getChildCount(element) > 0: 90 for elt in DOM.IterWalkChildren(element): 91 hit = isIn(elt, x, y) 92 if hit: 93 return_elt = elt 94 return return_elt
95
96 -def getElementUnderMouse(widget, x,y):
97 """ 98 Return the most specific element in widget that contains (x,y). 99 """ 100 element = widget.getElement() 101 hit = isIn(element, x, y) 102 if hit: 103 child_elem = getTargetInChildren(element, x, y) 104 if child_elem: 105 return child_elem 106 return element 107 return None
108
109 -def eventCoordinates(event):
110 """ Get the absolute coordinates of a mouse event. 111 http://www.quirksmode.org/js/events_properties.html#position 112 """ 113 return event.pageX, event.pageY
114
115 -def getScrollOffsets():
116 # this is only needed in ie 117 pass
118 119 120
121 -class DraggingWidget(Widget):
122 """ 123 A widget for holding the dragging feedback elements. 124 """
125 - def __init__(self, element=None):
126 Widget.__init__(self, Element=DOM.createElement('div')) 127 self.children = [] 128 if element is not None: 129 clone = cloneElement(element) 130 self.addChild(clone) 131 self.setStyleAttribute('position', 'absolute')
132
133 - def addChild(self, element):
134 DOM.appendChild(self.getElement(),element) 135 self.children.append(element)
136
137 - def setImage(self,element):
138 container = self.getElement() 139 clone = cloneElement(element) 140 while self.children: 141 child = self.children.pop() 142 DOM.removeChild(container, child) 143 self.addChild(clone)
144
145 - def addElement(self, element):
146 clone = cloneElement(element) 147 self.addChild(clone)
148
149 - def updateCursor(self, operation):
150 # TODO: change cursors. Probably means making custom cursors. 151 if operation == 'copy': 152 pass 153 elif operation == 'link': 154 pass 155 elif operation == 'move': 156 pass 157 else: 158 self.setStyleAttribute('cursor', 'auto')
159
160 -class DOMStringList(object):
161 """ 162 DOMStringList implementation 163 http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#DOMStringList 164 """
165 - def __init__(self, iterable=None):
166 self._data = [] 167 if iterable: 168 for item in iterable: 169 self._data.append(str(item))
170
171 - def _getLength(self):
172 return len(self._data)
173 174 length = property(_getLength) 175
176 - def item(self, index):
177 try: 178 return self._data[index] 179 except: 180 return None
181 - def __str__(self):
182 return str(self._data)
183
184 -class Uri_list(object):
185 """ 186 text/uri-list implementation 187 188 see http://www.rfc-editor.org/rfc/rfc2483.txt 189 """
190 - def __init__(self, data=None):
191 self._data = [] 192 if data: 193 # presume crlf-delimited data 194 for item in data.split('\r\n'): 195 self._data.append(item.strip())
196
197 - def add_url(self, url):
198 self._data.append(url)
199
200 - def get_first_url(self):
201 for item in self._data: 202 if item[0] != '#': 203 return item 204 return ''
205
206 - def __str__(self):
207 if self._data: 208 return '\r\n'.join(self._data) + '\r\n' 209 return ''
210