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
21 """
22 Has the DragEvent been canceled?
23 Coding this package was much easier with this function.
24 """
25 return not event.returnValue
26
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
44 clone = element.cloneNode(True)
45
46
47 return clone
48
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
56 if style:
57 return element_style[style]
58 return element_style
59
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
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
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
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
118
119
120
159
161 """
162 DOMStringList implementation
163 http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#DOMStringList
164 """
166 self._data = []
167 if iterable:
168 for item in iterable:
169 self._data.append(str(item))
170
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
182 return str(self._data)
183
185 """
186 text/uri-list implementation
187
188 see http://www.rfc-editor.org/rfc/rfc2483.txt
189 """
191 self._data = []
192 if data:
193
194 for item in data.split('\r\n'):
195 self._data.append(item.strip())
196
199
201 for item in self._data:
202 if item[0] != '#':
203 return item
204 return ''
205
207 if self._data:
208 return '\r\n'.join(self._data) + '\r\n'
209 return ''
210