// // Copyright (C) Stanislaw Adaszewski, 2020 // Contact: s.adaszewski@gmail.com // Website: https://adared.ch/wba // License: GNU Affero General Public License, Version 3 // import { h, Component } from 'preact'; class WBPagination extends Component { renderVisiblePages(numPages, activePage, chunkSize, onPageChanged, getPageUrl) { let visible = {}; let begActChnk = activePage - Math.floor(chunkSize / 2); let endActChnk = activePage + Math.floor(chunkSize / 2) + 1; for (let i = Math.max(0, begActChnk); i < Math.min(numPages, endActChnk); i++) visible[i] = true; for (let i = 0; i < Math.min(numPages, chunkSize); i++) visible[i] = true; for (let i = Math.max(numPages - chunkSize, 0); i < numPages; i++) visible[i] = true; visible = Object.keys(visible).map(n => Number(n)); visible.sort((a, b) => (a - b)); let res = []; let prev = 0; res.push((
  • this.changePage(e, activePage - 1) }>Previous
  • )); for (let idx = 0; idx < visible.length; idx++) { let i = visible[idx]; let capturePrev = prev; if (i > prev + 1) res.push((
  • this.changePage(e, Math.round((i + capturePrev) / 2)) }>...
  • )); prev = i; res.push((
  • this.changePage(e, i) }>{ i + 1 }
  • )); } res.push((
  • = numPages - 1 ? "page-item disabled" : "page-item" }> this.changePage(e, activePage + 1) }>Next
  • )); return res; } changePage(e, pageIdx) { if (this.props.onPageChanged) { e.preventDefault(); this.props.onPageChanged(pageIdx); } } render({ numPages, activePage, chunkSize, onPageChanged, getPageUrl }) { return ( ); } } WBPagination.defaultProps = { 'chunkSize': 5, 'getPageUrl': () => ('#') }; export default WBPagination;