-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolbar.js
More file actions
106 lines (84 loc) · 2.58 KB
/
Toolbar.js
File metadata and controls
106 lines (84 loc) · 2.58 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
var _ = require('underscore'),
React = require('react'),
blacklist = require('blacklist');
var Toolbar = React.createClass({
displayName: 'Toolbar',
getInitialState: function() {
return {
position: 'relative',
width: 'auto',
height: 'auto',
top: 0
};
},
componentDidMount: function() {
// Bail in IE8 because React doesn't support the onScroll event in that browser
// Conveniently (!) IE8 doesn't have window.getComputedStyle which we also use here
if (!window.getComputedStyle) return;
var toolbar = this.refs.toolbar.getDOMNode();
this.windowSize = this.getWindowSize();
var toolbarStyle = window.getComputedStyle(toolbar);
this.toolbarSize = {
x: toolbar.offsetWidth,
y: toolbar.offsetHeight + parseInt(toolbarStyle.marginTop || '0')
};
window.addEventListener('scroll', this.recalcPosition, false);
window.addEventListener('resize', this.recalcPosition, false);
this.recalcPosition();
},
getWindowSize: function() {
return {
x: window.innerWidth,
y: window.innerHeight
};
},
recalcPosition: function() {
var wrapper = this.refs.wrapper.getDOMNode();
this.toolbarSize.x = wrapper.offsetWidth;
var offsetTop = 0;
var offsetEl = wrapper;
while (offsetEl) {
offsetTop += offsetEl.offsetTop;
offsetEl = offsetEl.offsetParent;
}
var maxY = offsetTop + this.toolbarSize.y;
var viewY = window.scrollY + window.innerHeight;
var newSize = this.getWindowSize();
var sizeChanged = (newSize.x !== this.windowSize.x || newSize.y !== this.windowSize.y);
this.windowSize = newSize;
var newState = {
width: this.toolbarSize.x,
height: this.toolbarSize.y
};
if (viewY > maxY && (sizeChanged || this.mode !== 'inline')) {
this.mode = 'inline';
newState.top = 0;
newState.position = 'absolute';
this.setState(newState);
} else if (viewY <= maxY && (sizeChanged || this.mode !== 'fixed')) {
this.mode = 'fixed';
newState.top = window.innerHeight - this.toolbarSize.y;
newState.position = 'fixed';
this.setState(newState);
}
},
render: function() {
var wrapperStyle = {
position: 'relative',
height: this.state.height
};
var toolbarProps = blacklist(this.props, 'children', 'style');
var toolbarStyle = _.extend(this.props.style || {}, {
position: this.state.position,
top: this.state.top,
width: this.state.width,
height: this.state.height
});
return (
<div ref="wrapper" style={wrapperStyle}>
<div ref="toolbar" style={toolbarStyle} {...toolbarProps}>{this.props.children}</div>
</div>
);
}
});
module.exports = Toolbar;