-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdomFns.js
More file actions
256 lines (232 loc) · 7.56 KB
/
domFns.js
File metadata and controls
256 lines (232 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// @flow
import { findInArray, isFunction, int } from "./shims";
import browserPrefix, { browserPrefixToKey } from "./getPrefix";
import type {
ControlPosition,
PositionOffsetControlPosition,
MouseTouchEvent
} from "./types";
let matchesSelectorFunc = "";
export function matchesSelector(el: Node, selector: string): boolean {
if (!matchesSelectorFunc) {
matchesSelectorFunc = findInArray(
[
"matches",
"webkitMatchesSelector",
"mozMatchesSelector",
"msMatchesSelector",
"oMatchesSelector"
],
function(method) {
// $FlowIgnore: Doesn't think elements are indexable
return isFunction(el[method]);
}
);
}
// Might not be found entirely (not an Element?) - in that case, bail
// $FlowIgnore: Doesn't think elements are indexable
if (!isFunction(el[matchesSelectorFunc])) return false;
// $FlowIgnore: Doesn't think elements are indexable
return el[matchesSelectorFunc](selector);
}
// Works up the tree to the draggable itself attempting to match selector.
export function matchesSelectorAndParentsTo(
el: Node,
selector: string,
baseNode: Node
): boolean {
let node = el;
do {
if (matchesSelector(node, selector)) return true;
if (node === baseNode) return false;
node = node.parentNode;
} while (node);
return false;
}
export function addEvent(el: ?Node, event: string, handler: Function): void {
if (!el) {
return;
}
if (el.attachEvent) {
el.attachEvent("on" + event, handler);
} else if (el.addEventListener) {
el.addEventListener(event, handler, true);
} else {
// $FlowIgnore: Doesn't think elements are indexable
el["on" + event] = handler;
}
}
export function removeEvent(el: ?Node, event: string, handler: Function): void {
if (!el) {
return;
}
if (el.detachEvent) {
el.detachEvent("on" + event, handler);
} else if (el.removeEventListener) {
el.removeEventListener(event, handler, true);
} else {
// $FlowIgnore: Doesn't think elements are indexable
el["on" + event] = null;
}
}
export function outerHeight(node: HTMLElement): number {
// This is deliberately excluding margin for our calculations, since we are using
// offsetTop which is including margin. See getBoundPosition
let height = node.clientHeight;
const computedStyle = node.ownerDocument.defaultView.getComputedStyle(node);
height += int(computedStyle.borderTopWidth);
height += int(computedStyle.borderBottomWidth);
return height;
}
export function outerWidth(node: HTMLElement): number {
// This is deliberately excluding margin for our calculations, since we are using
// offsetLeft which is including margin. See getBoundPosition
let width = node.clientWidth;
const computedStyle = node.ownerDocument.defaultView.getComputedStyle(node);
width += int(computedStyle.borderLeftWidth);
width += int(computedStyle.borderRightWidth);
return width;
}
export function innerHeight(node: HTMLElement): number {
let height = node.clientHeight;
const computedStyle = node.ownerDocument.defaultView.getComputedStyle(node);
height -= int(computedStyle.paddingTop);
height -= int(computedStyle.paddingBottom);
return height;
}
export function innerWidth(node: HTMLElement): number {
let width = node.clientWidth;
const computedStyle = node.ownerDocument.defaultView.getComputedStyle(node);
width -= int(computedStyle.paddingLeft);
width -= int(computedStyle.paddingRight);
return width;
}
// Get from offsetParent
export function offsetXYFromParent(
evt: { clientX: number, clientY: number },
offsetParent: HTMLElement,
scale: number
): ControlPosition {
const isBody = offsetParent === offsetParent.ownerDocument.body;
const offsetParentRect = isBody
? { left: 0, top: 0 }
: offsetParent.getBoundingClientRect();
const x =
(evt.clientX + offsetParent.scrollLeft - offsetParentRect.left) / scale;
const y =
(evt.clientY + offsetParent.scrollTop - offsetParentRect.top) / scale;
return { x, y, r: 0 };
}
export function createCSSTransform(
controlPos: ControlPosition,
positionOffset: PositionOffsetControlPosition
): Object {
const translation = getTranslation(controlPos, positionOffset, "px");
return { [browserPrefixToKey("transform", browserPrefix)]: translation };
}
export function createSVGTransform(
controlPos: ControlPosition,
positionOffset: PositionOffsetControlPosition
): string {
const translation = getTranslation(controlPos, positionOffset, "");
return translation;
}
export function getTranslation(
{ x, y, r }: ControlPosition,
positionOffset: PositionOffsetControlPosition,
unitSuffix: string
): string {
let translation = `translate(${x}${unitSuffix},${y}${unitSuffix}) rotate(${r}deg)`;
if (positionOffset) {
const defaultX = `${
typeof positionOffset.x === "string"
? positionOffset.x
: positionOffset.x + unitSuffix
}`;
const defaultY = `${
typeof positionOffset.y === "string"
? positionOffset.y
: positionOffset.y + unitSuffix
}`;
translation = `translate(${defaultX}, ${defaultY})` + translation;
}
return translation;
}
export function getTouch(
e: MouseTouchEvent,
identifier: number
): ?{ clientX: number, clientY: number } {
return (
(e.targetTouches &&
findInArray(e.targetTouches, t => identifier === t.identifier)) ||
(e.changedTouches &&
findInArray(e.changedTouches, t => identifier === t.identifier))
);
}
export function getTouchIdentifier(e: MouseTouchEvent): ?number {
if (e.targetTouches && e.targetTouches[0])
return e.targetTouches[0].identifier;
if (e.changedTouches && e.changedTouches[0])
return e.changedTouches[0].identifier;
}
// User-select Hacks:
//
// Useful for preventing blue highlights all over everything when dragging.
// Note we're passing `document` b/c we could be iframed
export function addUserSelectStyles(doc: ?Document) {
if (!doc) return;
let styleEl = doc.getElementById("react-draggable-style-el");
if (!styleEl) {
styleEl = doc.createElement("style");
styleEl.type = "text/css";
styleEl.id = "react-draggable-style-el";
styleEl.innerHTML =
".react-draggable-transparent-selection *::-moz-selection {all: inherit;}\n";
styleEl.innerHTML +=
".react-draggable-transparent-selection *::selection {all: inherit;}\n";
doc.getElementsByTagName("head")[0].appendChild(styleEl);
}
if (doc.body) addClassName(doc.body, "react-draggable-transparent-selection");
}
export function removeUserSelectStyles(doc: ?Document) {
try {
if (doc && doc.body)
removeClassName(doc.body, "react-draggable-transparent-selection");
// $FlowIgnore: IE
if (doc.selection) {
// $FlowIgnore: IE
doc.selection.empty();
} else {
window.getSelection().removeAllRanges(); // remove selection caused by scroll
}
} catch (e) {
// probably IE
}
}
export function styleHacks(childStyle: Object = {}): Object {
// Workaround IE pointer events; see #51
// https://github.com/mzabriskie/react-draggable/issues/51#issuecomment-103488278
return {
touchAction: "none",
...childStyle
};
}
export function addClassName(el: HTMLElement, className: string) {
if (el.classList) {
el.classList.add(className);
} else {
if (!el.className.match(new RegExp(`(?:^|\\s)${className}(?!\\S)`))) {
el.className += ` ${className}`;
}
}
}
export function removeClassName(el: HTMLElement, className: string) {
if (el.classList) {
el.classList.remove(className);
} else {
el.className = el.className.replace(
new RegExp(`(?:^|\\s)${className}(?!\\S)`, "g"),
""
);
}
}