# all files / keystone/lib/list/ getPages.js

76.47% Statements 13/17
50% Branches 4/8
100% Functions 1/1
76.47% Lines 13/17

    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

    1×
    6×
    6×
    6×
    6×
    6×
    6×
    6×
    6×
    20×

    6×

    6×

    1×
     /**
     * Generate page array for pagination
     *
     * @param {Number} the maximum number pages to display in the pagination
     * @param {Object} page options
     */
    function getPages (options, maxPages) {
    	var surround = Math.floor(maxPages / 2);
    	var firstPage = maxPages ? Math.max(1, options.currentPage - surround) : 1;
    	var padRight = Math.max(((options.currentPage - surround) - 1) * -1, 0);
    	var lastPage = maxPages ? Math.min(options.totalPages, options.currentPage + surround + padRight) : options.totalPages;
    	var padLeft = Math.max(((options.currentPage + surround) - lastPage), 0);
    	options.pages = [];
    	firstPage = Math.max(Math.min(firstPage, firstPage - padLeft), 1);
    	for (var i = firstPage; i <= lastPage; i++) {
    		options.pages.push(i);
    	}
    	Iif (firstPage !== 1) {
    		options.pages.shift();
    		options.pages.unshift('...');
    	}
    	Iif (lastPage !== Number(options.totalPages)) {
    		options.pages.pop();
    		options.pages.push('...');
    	}
    }

    module.exports = getPages;