|
| 1 | +export const PER_PAGE_FIRST = 9; // No of posts to be shown on first page. |
| 2 | +export const PER_PAGE_REST = 12; // No of posts to be shown following page and after. |
| 3 | + |
| 4 | +export const getPageOffset = ( pageNo ) => { |
| 5 | + |
| 6 | + /** |
| 7 | + * Offset is how many posts are already shown ( meaning, after how many posts should we start qurying ). |
| 8 | + * @type {number} |
| 9 | + */ |
| 10 | + let offset = 0; |
| 11 | + pageNo = Number( pageNo ); |
| 12 | + if ( 1 === pageNo ) { |
| 13 | + offset = 0; |
| 14 | + } else if ( 2 === pageNo ) { |
| 15 | + offset = PER_PAGE_FIRST; |
| 16 | + } else { |
| 17 | + offset = PER_PAGE_FIRST + ( ( pageNo - 2 ) * PER_PAGE_REST ); |
| 18 | + } |
| 19 | + return offset; |
| 20 | +}; |
| 21 | + |
| 22 | +export const totalPagesCount = ( totalPostsCount ) => { |
| 23 | + return Math.ceil( ( totalPostsCount - PER_PAGE_FIRST ) / PER_PAGE_REST + 1 ); |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Create Pagination Links Array. |
| 28 | + * |
| 29 | + * Example: [1, "...", 521, 522, 523, 524, 525, "...", 529] |
| 30 | + * |
| 31 | + * @param {int} currentPage Current page no. |
| 32 | + * @param {int} totalPages Count of total no of pages. |
| 33 | + * |
| 34 | + * @return {Array} Array containing the indexes to be looped through to create pagination |
| 35 | + */ |
| 36 | +export const createPaginationLinks = ( currentPage, totalPages ) => { |
| 37 | + const paginationArray = []; |
| 38 | + let countOfDotItems = 0; |
| 39 | + |
| 40 | + // If there is only one page, return an empty array. |
| 41 | + if ( ! totalPages && 1 >= totalPages ) { |
| 42 | + return paginationArray; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Push the two index items before the current page. |
| 47 | + */ |
| 48 | + if ( 0 < currentPage - 2 ) { |
| 49 | + paginationArray.push( currentPage - 2 ); |
| 50 | + } |
| 51 | + |
| 52 | + if ( 0 < currentPage - 1 ) { |
| 53 | + paginationArray.push( currentPage - 1 ); |
| 54 | + } |
| 55 | + |
| 56 | + // Push the current page index item. |
| 57 | + paginationArray.push( currentPage ); |
| 58 | + |
| 59 | + /** |
| 60 | + * Push the two index items after the current page. |
| 61 | + */ |
| 62 | + if ( totalPages >= currentPage + 1 ) { |
| 63 | + paginationArray.push( currentPage + 1 ); |
| 64 | + } |
| 65 | + |
| 66 | + if ( totalPages >= currentPage + 2 ) { |
| 67 | + paginationArray.push( currentPage + 2 ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Push the '...' at the beginning of the array |
| 72 | + * only if the difference of between the 1st and 2nd index item is greater than 1. |
| 73 | + */ |
| 74 | + if ( 1 < paginationArray[0] - 1 ) { |
| 75 | + paginationArray.unshift( '...' ); |
| 76 | + countOfDotItems += 1; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Push the '...' at the end of the array. |
| 81 | + * only if the difference of between the last and 2nd last item is greater than 2. |
| 82 | + * We remove the count of dot items from the array to get the actual indexes, while checking the condition. |
| 83 | + */ |
| 84 | + if ( 2 < totalPages - paginationArray[paginationArray.length - ( 2 - countOfDotItems )] ) { |
| 85 | + paginationArray.push( '...' ); |
| 86 | + } |
| 87 | + |
| 88 | + // Push first index item in the array if it does not already exists. |
| 89 | + if ( -1 === paginationArray.indexOf( 1 ) ) { |
| 90 | + paginationArray.unshift( 1 ); |
| 91 | + } |
| 92 | + |
| 93 | + // Push last index item in the array if it does not already exists. |
| 94 | + if ( -1 === paginationArray.indexOf( totalPages ) ) { |
| 95 | + paginationArray.push( totalPages ); |
| 96 | + } |
| 97 | + |
| 98 | + return paginationArray; |
| 99 | +}; |
0 commit comments