forked from vkbansal/react-contextmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontextmenu-layer.js
More file actions
114 lines (100 loc) · 3.9 KB
/
Copy pathcontextmenu-layer.js
File metadata and controls
114 lines (100 loc) · 3.9 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
import React from "react";
import invariant from "invariant";
import _isObject from "lodash.isobject";
import store from "./redux/store";
export default function(identifier, configure) {
return function(Component) {
const displayName = Component.displayName
|| Component.name
|| "Component";
invariant(
identifier && (typeof identifier === "string"
|| typeof identifier === "symbol"
|| typeof identifier === "function"),
"Expected identifier to be string, symbol or function. See %s",
displayName
);
if (configure) {
invariant(
typeof configure === "function",
"Expected configure to be a function. See %s",
displayName
);
}
return React.createClass({
displayName: `${displayName}ContextMenuLayer`,
getDefaultProps() {
return {
renderTag: "div",
attributes: {}
};
},
mouseDown: false,
handleMouseDown(event) {
if (this.props.holdToDisplay >= 0 && event.button === 0) {
event.persist();
this.mouseDown = true;
setTimeout(() => {
if (this.mouseDown) this.handleContextClick(event);
}, this.props.holdToDisplay);
}
},
handleTouchstart(event) {
if (this.props.holdToDisplay >= 0) {
event.persist();
this.mouseDown = true;
setTimeout(() => {
if (this.mouseDown) this.handleContextClick(event);
}, this.props.holdToDisplay);
}
},
handleTouchEnd(event) {
event.preventDefault();
this.mouseDown = false;
},
handleMouseUp(event) {
if (event.button === 0) {
this.mouseDown = false;
}
},
handleContextClick(event) {
let currentItem = typeof configure === "function"
? configure(this.props)
: {};
invariant(
_isObject(currentItem),
"Expected configure to return an object. See %s",
displayName
);
event.preventDefault();
const xPos = event.clientX || (event.touches && event.touches[0].pageX),
yPos = event.clientY || (event.touches && event.touches[0].pageY);
store.dispatch({
type: "SET_PARAMS",
data: {
x: xPos,
y: yPos,
currentItem,
isVisible: typeof identifier === "function" ? identifier(this.props) : identifier
}
});
},
render() {
let { attributes: {className = "", ...attributes}, renderTag, ...props } = this.props;
attributes.className = `react-context-menu-wrapper ${className}`;
attributes.onContextMenu = this.handleContextClick;
attributes.onMouseDown = this.handleMouseDown;
attributes.onMouseUp = this.handleMouseUp;
attributes.onTouchStart = this.handleTouchstart;
attributes.onTouchEnd = this.handleTouchEnd;
attributes.onMouseOut = this.handleMouseUp;
attributes.ref = (c) => (this.target = c);
return React.createElement(
renderTag,
attributes,
React.createElement(Component, props)
);
}
});
};
}