Make calcScrollToColRow function a lot nicer

pull/10616/head
ebb-tide 2018-03-27 10:55:09 -07:00
parent 4b08bdd42a
commit 99c1481332
1 changed files with 15 additions and 26 deletions

View File

@ -130,40 +130,29 @@ class TableGraph extends Component {
}
calcScrollToColRow = () => {
const {
data,
sortedTimeVals,
hoveredColumnIndex,
hoveredRowIndex,
} = this.state
const {data, sortedTimeVals, hoveredColumnIndex} = this.state
const {hoverTime, tableOptions} = this.props
if (_.isEmpty(data[0]) || hoverTime === NULL_HOVER_TIME) {
const hoveringThisTable = hoveredColumnIndex !== NULL_ARRAY_INDEX
const notHovering = hoverTime === NULL_HOVER_TIME
if (_.isEmpty(data[0]) || notHovering || hoveringThisTable) {
return {scrollToColumn: undefined, scrollToRow: undefined}
}
const hoveringThisTable = hoveredColumnIndex !== NULL_ARRAY_INDEX
if (hoveringThisTable) {
return {scrollToColumn: hoveredColumnIndex, scrollToRow: hoveredRowIndex}
}
const {verticalTimeAxis} = tableOptions
const hoverTimeIndex = sortedTimeVals.reduce(
(acc, currentTime, index, array) => {
const diff = Math.abs(hoverTime - currentTime)
if (diff === 0) {
return index
}
if (Math.abs(hoverTime - array[acc]) > diff) {
return index
const firstDiff = Math.abs(hoverTime - sortedTimeVals[1]) // sortedTimeVals[0] is "time"
const hoverTimeFound = sortedTimeVals.reduce(
(acc, currentTime, index) => {
const thisDiff = Math.abs(hoverTime - currentTime)
if (thisDiff < acc.diff) {
return {index, diff: thisDiff}
}
return acc
},
1
{index: 1, diff: firstDiff}
)
const scrollToColumn =
!hoveringThisTable && !verticalTimeAxis ? hoverTimeIndex : undefined
const scrollToRow =
!hoveringThisTable && verticalTimeAxis ? hoverTimeIndex : undefined
const {verticalTimeAxis} = tableOptions
const scrollToColumn = verticalTimeAxis ? undefined : hoverTimeFound.index
const scrollToRow = verticalTimeAxis ? hoverTimeFound.index : undefined
return {scrollToRow, scrollToColumn}
}