Only listen mousemove once

初始化时监听, 共享同一个 mousePosition
This commit is contained in:
afc163 2015-08-18 13:15:43 +08:00
parent f4964626d9
commit ee8335856c
3 changed files with 22 additions and 21 deletions

View File

@ -43,4 +43,3 @@ var Test = React.createClass({
React.render(<Test/> , document.getElementById('components-modal-demo-basic'));
````

View File

@ -0,0 +1,14 @@
function wrap(standard, fallback) {
return function (el, evtName, listener, useCapture) {
if (el[standard]) {
el[standard](evtName, listener, useCapture);
} else if (el[fallback]) {
el[fallback]('on' + evtName, listener);
}
};
}
export default {
add: wrap('addEventListener', 'attachEvent'),
remove: wrap('removeEventListener', 'detachEvent')
};

View File

@ -1,22 +1,18 @@
import React from 'react';
import Dialog from 'rc-dialog';
import eventListener from './eventListener';
function noop() {
}
function wrap(standard, fallback) {
return function (el, evtName, listener, useCapture) {
if (el[standard]) {
el[standard](evtName, listener, useCapture);
} else if (el[fallback]) {
el[fallback]('on' + evtName, listener);
}
let mousePosition;
function onDocumentMousemove(e) {
mousePosition = {
x: e.pageX,
y: e.pageY
};
}
let eventListener = {
add: wrap('addEventListener', 'attachEvent'),
remove: wrap('removeEventListener', 'detachEvent')
};
eventListener.add(document, 'mousemove', onDocumentMousemove);
export default React.createClass({
getDefaultProps() {
@ -41,14 +37,6 @@ export default React.createClass({
};
},
componentWillMount() {
eventListener.add(document, 'mousemove', this.onDocumentMousemove);
},
componentWillUnmount() {
eventListener.remove(document, 'mousemove', this.onDocumentMousemove);
},
handleCancel() {
this.props.onCancel();
this.setState({
@ -92,6 +80,6 @@ export default React.createClass({
let visible = this.state.visible;
return <Dialog transitionName="zoom" onClose={this.handleCancel}
maskAnimation="fade" width="500" footer={footer} {...props}
visible={visible} mousePosition={this.mousePosition} />;
visible={visible} mousePosition={mousePosition} />;
}
});