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

87.5% Statements 35/40
68.18% Branches 15/22
83.33% Functions 5/6
92.11% Lines 35/38

    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

    1×
    6×
    6×

    6×

    6×
    6×

    6×
    6×
    6×

    6×
    6×
    6×
    6×

    6×

    6×

    6×
    6×
    6×

    6×
    6×
    6×

    6×

    6×
    4×

    6×
    6×

    6×
    6×
    6×
    6×

    6×
    6×

    6×

    6×

    1×
     /**
     * Gets a special Query object that will paginate documents in the list
     *
     * Example:
     *     list.paginate({
     *         page: 1,
     *         perPage: 100,
     *         maxPages: 10
     *     }).exec(function(err, results) {
     *         // do something
     *     });
     *
     * @param {Object} options
     * @param {Function} callback (optional)
     */
    function paginate (options, callback) {
    	var list = this;
    	var model = this.model;

    	options = options || {};

    	var query = model.find(options.filters, options.optionalExpression);
    	var countQuery = model.find(options.filters);

    	query._original_exec = query.exec;
    	query._original_sort = query.sort;
    	query._original_select = query.select;

    	var currentPage = Number(options.page) || 1;
    	var resultsPerPage = Number(options.perPage) || 50;
    	var maxPages = Number(options.maxPages) || 10;
    	var skip = (currentPage - 1) * resultsPerPage;

    	list.pagination = { maxPages: maxPages };

    	// as of mongoose 3.7.x, we need to defer sorting and field selection
    	// until after the count has been executed

    	query.select = function () {
    		options.select = arguments[0];
    		return query;
    	};

    	query.sort = function () {
    		options.sort = arguments[0];
    		return query;
    	};

    	query.exec = function (callback) {
    		countQuery.count(function (err, count) {
    			Iif (err) callback(err);

    			query.find().limit(resultsPerPage).skip(skip);

    			// apply the select and sort options before calling exec
    			if (options.select) {
    				query._original_select(options.select);
    			}

    			Eif (options.sort) {
    				query._original_sort(options.sort);
    			}

    			query._original_exec(function (err, results) {
    				Iif (err) return callback(err);
    				var totalPages = Math.ceil(count / resultsPerPage);
    				var rtn = {
    					total: count,
    					results: results,
    					currentPage: currentPage,
    					totalPages: totalPages,
    					pages: [],
    					previous: (currentPage > 1) ? (currentPage - 1) : false,
    					next: (currentPage < totalPages) ? (currentPage + 1) : false,
    					first: skip + 1,
    					last: skip + results.length,
    				};
    				list.getPages(rtn, maxPages);
    				callback(err, rtn);
    			});
    		});

    	};

    	Iif (callback) {
    		return query(callback);
    	} else {
    		return query;
    	}
    }

    module.exports = paginate;