All files / components/result/addons Pagination.jsx

62.96% Statements 34/54
48.39% Branches 30/62
26.67% Functions 4/15
64% Lines 32/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155          1x   5x 5x 5x     1x                           5x   5x           5x           5x       5x 5x   5x   5x 5x   5x     5x 5x 5x 5x           5x       5x 20x   20x         20x                     20x 20x       5x     5x 5x 5x             5x                                                                                           1x        
import VueTypes from 'vue-types';
import { helper } from '@appbaseio/reactivecore';
import Button, { pagination } from '../../../styles/Button';
import types from '../../../utils/vueTypes';
 
const { getClassName, handleA11yAction } = helper;
function getStartPage(totalPages, currentPage, showEndPage) {
	const midValue = parseInt(totalPages / 2, 10);
	const start = currentPage - (showEndPage ? Math.ceil(midValue / 2) - 1 : midValue);
	return start > 1 ? start : 2;
}
 
const Pagination = {
	name: 'Pagination',
	functional: true,
	props: {
		currentPage: types.number,
		innerClass: types.style,
		pages: types.number,
		setPage: types.func,
		totalPages: types.number,
		showEndPage: VueTypes.bool,
		prevLabel: types.string,
		nextLabel: types.string,
	},
	render(createElement, context) {
		const { props } = context;
 
		const onPrevPage = () => {
			if (props.currentPage) {
				props.setPage(props.currentPage - 1);
			}
		};
 
		const onNextPage = () => {
			if (props.currentPage < props.totalPages - 1) {
				props.setPage(props.currentPage + 1);
			}
		};
 
		Iif (!props.totalPages) {
			return null;
		}
 
		const innerClassName = getClassName(props.innerClass, 'button');
		const primary = props.currentPage === 0;
		const className
			= innerClassName || primary ? `${innerClassName} ${primary ? 'active' : ''}` : '';
 
		const buildPaginationDOM = position => {
			const { pages, currentPage, totalPages, setPage, showEndPage } = props;
			let start
				= position === 'start'
					? getStartPage(pages, currentPage, showEndPage)
					: Math.max(2, Math.ceil(totalPages - (pages - 1) / 2 + 1));
			const paginationButtons = [];
			Eif (start <= totalPages) {
				let totalPagesToShow = pages < totalPages ? start + (pages - 1) : totalPages + 1;
				Iif (showEndPage) {
					totalPagesToShow
						= position === 'start'
							? start + (Math.ceil(pages / 2) - (pages % 2))
							: totalPages + 1;
				}
				Iif (currentPage > totalPages - pages + 2) {
					start = Math.max(2, totalPages - pages + 2);
					totalPagesToShow = start + pages;
				}
				for (let i = start; i < Math.min(totalPages + 1, totalPagesToShow); i += 1) {
					const activeButton = currentPage === i - 1;
					const classNameBtn
						= innerClassName || activeButton
							? `${innerClassName} ${activeButton ? 'active' : ''}`
							: '';
 
					const pageBtn = (
						<Button
							class={classNameBtn}
							primary={activeButton}
							tabIndex="0"
							onKeyPress={event => handleA11yAction(event, () => setPage(i - 1))}
							alt={`page-${i}`}
							onClick={() => setPage(i - 1)}
						>
							{i}
						</Button>
					);
					Eif (i <= totalPages + 1) {
						paginationButtons.push(pageBtn);
					}
				}
			}
			return paginationButtons;
		};
 
		const buildIntermediatePaginationDom = () => {
			const { showEndPage, currentPage, totalPages, pages } = props;
			Eif (!showEndPage) return buildPaginationDOM('start');
			if (currentPage <= totalPages - pages + 2 || totalPages <= pages) {
				return buildPaginationDOM('start');
			}
			return null;
		};
 
		return (
			<div class={`${pagination} ${getClassName(props.innerClass, 'pagination')}`}>
				<Button
					class={getClassName(props.innerClass, 'button') || ''}
					disabled={props.currentPage === 0}
					onKeyPress={event => handleA11yAction(event, onPrevPage)}
					onClick={onPrevPage}
					tabIndex="0"
				>
					{props.prevLabel || 'Prev'}
				</Button>
				{
					<Button
						class={className}
						primary={primary}
						onKeyPress={event => handleA11yAction(event, () => props.setPage(0))}
						onClick={() => props.setPage(0)}
						tabIndex="0"
					>
						1
					</Button>
				}
				{props.showEndPage
				&& props.currentPage >= Math.floor(props.pages / 2) + !!(props.pages % 2) ? (
						<span>...</span>
					) : null}
				{buildIntermediatePaginationDom()}
				{props.showEndPage
				&& props.pages > 2
				&& props.currentPage <= props.totalPages - Math.ceil(props.pages * 0.75) ? (
						<span>...</span>
					) : null}
				{props.showEndPage && props.totalPages >= props.pages && buildPaginationDOM('end')}
				<Button
					class={getClassName(props.innerClass, 'button') || ''}
					disabled={props.currentPage >= props.totalPages - 1}
					onKeyPress={event => handleA11yAction(event, onNextPage)}
					onClick={onNextPage}
					tabIndex="0"
				>
					{props.nextLabel || 'Next'}
				</Button>
			</div>
		);
	},
};
Pagination.install = function(Vue) {
	Vue.component(Pagination.name, Pagination);
};
export default Pagination;