forked from iviasensio/PLSmartPivot
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from qlik-oss/feature/QPE-474
[QPE-474] replace jquery scroll linking
- Loading branch information
Showing
8 changed files
with
312 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as LinkedScrollWrapper } from './linked-scroll-wrapper.jsx'; | ||
export { default as LinkedScrollSection } from './linked-scroll-section.jsx'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { LinkedScrollContext } from './linked-scroll-wrapper.jsx'; | ||
|
||
class LinkedScrollSection extends React.PureComponent { | ||
static contextType = LinkedScrollContext; | ||
|
||
componentDidMount () { | ||
const { link } = this.context; | ||
link(this); | ||
} | ||
|
||
componentWillUnmount () { | ||
const { unlink } = this.context; | ||
unlink(this); | ||
} | ||
|
||
render () { | ||
const { children } = this.props; | ||
|
||
return children; | ||
} | ||
} | ||
|
||
LinkedScrollSection.propTypes = { | ||
children: PropTypes.any | ||
}; | ||
|
||
export default LinkedScrollSection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export const LinkedScrollContext = React.createContext(); | ||
|
||
class LinkedScrollWrapper extends React.PureComponent { | ||
constructor (props) { | ||
super(props); | ||
|
||
this.linkComponent = this.linkComponent.bind(this); | ||
this.unlinkComponent = this.unlinkComponent.bind(this); | ||
this.handleScroll = this.handleScroll.bind(this); | ||
this.scrollElements = []; | ||
|
||
this.linkActions = { | ||
link: this.linkComponent, | ||
unlink: this.unlinkComponent | ||
}; | ||
} | ||
|
||
linkComponent (component) { | ||
// eslint-disable-next-line react/no-find-dom-node | ||
const node = ReactDOM.findDOMNode(component); | ||
const element = { | ||
component, | ||
node | ||
}; | ||
this.scrollElements.push(element); | ||
node.onscroll = this.handleScroll.bind(this, element); | ||
} | ||
|
||
unlinkComponent (component) { | ||
const componentIndex = this.scrollElements.map(element => element.component).indexOf(component); | ||
if (componentIndex !== -1) { | ||
this.scrollElements.removeAt(componentIndex); | ||
// eslint-disable-next-line react/no-find-dom-node | ||
const node = ReactDOM.findDOMNode(component); | ||
node.onscroll = null; | ||
} | ||
} | ||
|
||
handleScroll (element) { | ||
window.requestAnimationFrame(() => { | ||
this.sync(element); | ||
}); | ||
} | ||
|
||
sync (scrollElement) { | ||
this.scrollElements.forEach(element => { | ||
if (scrollElement === element) { | ||
return; | ||
} | ||
element.node.onscroll = null; | ||
if (element.component.props.linkHorizontal) { | ||
element.node.scrollLeft = scrollElement.node.scrollLeft; | ||
} | ||
|
||
if (element.component.props.linkVertical) { | ||
element.node.scrollTop = scrollElement.node.scrollTop; | ||
} | ||
window.requestAnimationFrame(() => { | ||
element.node.onscroll = this.handleScroll.bind(this, element); | ||
}); | ||
}); | ||
} | ||
|
||
render () { | ||
const { children } = this.props; | ||
return ( | ||
<LinkedScrollContext.Provider value={this.linkActions}> | ||
{children} | ||
</LinkedScrollContext.Provider> | ||
); | ||
} | ||
} | ||
|
||
LinkedScrollWrapper.propTypes = { | ||
children: PropTypes.any | ||
}; | ||
|
||
export default LinkedScrollWrapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.