-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathDockMonitor.js
89 lines (77 loc) · 2.35 KB
/
DockMonitor.js
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
import React, { cloneElement, Component, PropTypes } from 'react';
import Dock from 'react-dock';
import { POSITIONS } from './constants';
import { toggleVisibility, changePosition, changeSize } from './actions';
import reducer from './reducers';
export default class DockMonitor extends Component {
static reducer = reducer;
static propTypes = {
defaultPosition: PropTypes.oneOf(POSITIONS).isRequired,
defaultIsVisible: PropTypes.bool.isRequired,
defaultSize: PropTypes.number.isRequired,
toggleVisibilityKey: PropTypes.string.isRequired,
changePositionKey: PropTypes.string.isRequired,
children: PropTypes.element,
dispatch: PropTypes.func,
monitorState: PropTypes.shape({
position: PropTypes.oneOf(POSITIONS).isRequired,
size: PropTypes.number.isRequired,
isVisible: PropTypes.bool.isRequired,
childMonitorState: PropTypes.any
})
};
static defaultProps = {
defaultIsVisible: true,
defaultPosition: 'right',
defaultSize: 0.3
};
constructor(props) {
super(props);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleSizeChange = this.handleSizeChange.bind(this);
}
componentDidMount() {
window.addEventListener('keydown', this.handleKeyDown);
}
componentWillUnmount() {
window.removeEventListener('keydown', this.handleKeyDown);
}
handleKeyDown(e) {
if (!e.ctrlKey) {
return;
}
e.preventDefault();
const key = e.keyCode || e.which;
const char = String.fromCharCode(key);
switch (char.toUpperCase()) {
case this.props.toggleVisibilityKey.toUpperCase():
this.props.dispatch(toggleVisibility());
break;
case this.props.changePositionKey.toUpperCase():
this.props.dispatch(changePosition());
break;
default:
break;
}
}
handleSizeChange(requestedSize) {
this.props.dispatch(changeSize(requestedSize));
}
render() {
const { monitorState, children, ...rest } = this.props;
const { position, isVisible, size } = monitorState;
const childProps = {
...rest,
monitorState: monitorState.childMonitorState
};
return (
<Dock position={position}
isVisible={isVisible}
size={size}
onSizeChange={this.handleSizeChange}
dimMode='none'>
{cloneElement(children, childProps)}
</Dock>
);
}
}