|
|
@ -1,4 +1,3 @@
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import dimensions from 'Styles/Variables/dimensions';
|
|
|
|
import dimensions from 'Styles/Variables/dimensions';
|
|
|
@ -18,7 +17,7 @@ class PageJumpBar extends Component {
|
|
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
this.state = {
|
|
|
|
height: 0,
|
|
|
|
height: 0,
|
|
|
|
visibleItems: props.items
|
|
|
|
visibleItems: props.items.order
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -52,29 +51,47 @@ class PageJumpBar extends Component {
|
|
|
|
minimumItems
|
|
|
|
minimumItems
|
|
|
|
} = this.props;
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!items) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
|
|
|
characters,
|
|
|
|
|
|
|
|
order
|
|
|
|
|
|
|
|
} = items;
|
|
|
|
|
|
|
|
|
|
|
|
const height = this.state.height;
|
|
|
|
const height = this.state.height;
|
|
|
|
const maximumItems = Math.floor(height / ITEM_HEIGHT);
|
|
|
|
const maximumItems = Math.floor(height / ITEM_HEIGHT);
|
|
|
|
const diff = items.length - maximumItems;
|
|
|
|
const diff = order.length - maximumItems;
|
|
|
|
|
|
|
|
|
|
|
|
if (diff < 0) {
|
|
|
|
if (diff < 0) {
|
|
|
|
this.setState({ visibleItems: items });
|
|
|
|
this.setState({ visibleItems: order });
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (items.length < minimumItems) {
|
|
|
|
if (order.length < minimumItems) {
|
|
|
|
this.setState({ visibleItems: items });
|
|
|
|
this.setState({ visibleItems: order });
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const removeDiff = Math.ceil(items.length / maximumItems);
|
|
|
|
// get first, last, and most common in between to make up numbers
|
|
|
|
|
|
|
|
const visibleItems = [order[0]];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sorted = order.slice(1, -1).map((x) => characters[x]).sort((a, b) => b - a);
|
|
|
|
|
|
|
|
const minCount = sorted[maximumItems - 3];
|
|
|
|
|
|
|
|
const greater = sorted.reduce((acc, value) => acc + (value > minCount ? 1 : 0), 0);
|
|
|
|
|
|
|
|
let minAllowed = maximumItems - 2 - greater;
|
|
|
|
|
|
|
|
|
|
|
|
const visibleItems = _.reduce(items, (acc, item, index) => {
|
|
|
|
for (let i = 1; i < order.length - 1; i++) {
|
|
|
|
if (index % removeDiff === 0) {
|
|
|
|
if (characters[order[i]] > minCount) {
|
|
|
|
acc.push(item);
|
|
|
|
visibleItems.push(order[i]);
|
|
|
|
|
|
|
|
} else if (characters[order[i]] === minCount && minAllowed > 0) {
|
|
|
|
|
|
|
|
visibleItems.push(order[i]);
|
|
|
|
|
|
|
|
minAllowed--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
visibleItems.push(order[order.length - 1]);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.setState({ visibleItems });
|
|
|
|
this.setState({ visibleItems });
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -129,7 +146,7 @@ class PageJumpBar extends Component {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PageJumpBar.propTypes = {
|
|
|
|
PageJumpBar.propTypes = {
|
|
|
|
items: PropTypes.arrayOf(PropTypes.string).isRequired,
|
|
|
|
items: PropTypes.object.isRequired,
|
|
|
|
minimumItems: PropTypes.number.isRequired,
|
|
|
|
minimumItems: PropTypes.number.isRequired,
|
|
|
|
onItemPress: PropTypes.func.isRequired
|
|
|
|
onItemPress: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
};
|
|
|
|