Blame view

apps/guang/helpers/pager.js 6.13 KB
刘传洋 authored
1 2 3 4 5 6 7 8
/**
 * guang
 * @author: lcy<chuanyang.liu@yoho.cn>
 * @date: 2016/09/01
 */

'use strict';
刘传洋 authored
9 10
let _ = require('lodash');
let Handlebars = require('handlebars');
刘传洋 authored
11
刘传洋 authored
12 13 14 15 16 17
let ALL_TYPES = {
    stand: 'p-page-n',
    mini: 'p-n',
    full: 'f-p-page-n-l-info-input-gobtn',
    fullellipsis: 'f-p-pe-n-l-info-input-gobtn',
    ellipsis: 'p-pe-n'
刘传洋 authored
18 19
};
刘传洋 authored
20 21
exports.pager = function() {
刘传洋 authored
22
    let options = arguments[arguments.length - 1];
刘传洋 authored
23
刘传洋 authored
24 25 26 27 28
    let baseUrl = (arguments.length > 1 ? arguments[0] : null) || options.hash.baseUrl || '',
        page = (options.hash.page || 1) * 1,
        showNum = (options.hash.showNum || 7) * 1,
        pageSize = options.hash.pageSize || 20,
        totalPages = options.hash.totalPages,
刘传洋 authored
29 30 31 32
        totalRecords = options.hash.totalRecords,
        theme = options.hash.theme || 'pager',
        currentClass = options.hash.currentClass || 'cur',
刘传洋 authored
33 34
        // min full stand ellipsis  or: f-首页, p-上一页, page-页码,n-下一页,l-最后一页,info-displayMsg,inout输入框,gobtn-goto Btn
        type = options.hash.type || 'stand',
刘传洋 authored
35 36 37 38 39 40 41 42 43 44 45 46
        pageVar = options.hash.pageVar || 'page',
        pageSizeVar = options.hash.pageSizeVar || 'pageSize';

    // 清除原来page(page=1&) 重新定义page
    let clearPageReg = new RegExp(pageVar + '=[^&]*(&|$)'),
        clearSizeReg = new RegExp(pageSizeVar + '=[^&]*(&|$)'),
        base = baseUrl.replace(clearPageReg, '');

    if (options.hash.pageSize) {
        base = base.replace(clearSizeReg, '');
    }
47
    base += (base.indexOf('?') < 0 ? '?' : (/(\?|&)$/.test(base) ? '' : '&')) +
刘传洋 authored
48 49
            (options.hash.pageSize ? (pageSizeVar + '=' + pageSize + '&') : '') +
            pageVar + '=';
刘传洋 authored
50
刘传洋 authored
51
    function getPageNums(ntype) {
刘传洋 authored
52 53
        var pageNums = [];
        var num = showNum;
刘传洋 authored
54
刘传洋 authored
55
        if (ntype === 'e') {
刘传洋 authored
56 57 58 59 60
            num = num - 2;
            num = num > 2 ? num : 2;
        }

        /** 分页展示页码个数begin 规则:展示最靠近当前页的指定个数 **/
刘传洋 authored
61 62
        let pageShowMax = num % 2 === 0 ? page - 1 : page;
        let pageShowMin = page;
刘传洋 authored
63
刘传洋 authored
64
        for (let i = 0; i < Math.floor(num / 2); i++) {
刘传洋 authored
65 66
            pageShowMax++;
            pageShowMin--;
刘传洋 authored
67
            if (pageShowMax > totalPages) {
刘传洋 authored
68
                pageShowMax = totalPages;
刘传洋 authored
69
                if (pageShowMin > 1) {
刘传洋 authored
70 71 72
                    pageShowMin--;
                }
            }
刘传洋 authored
73
            if (pageShowMin < 1) {
刘传洋 authored
74
                pageShowMin = 1;
刘传洋 authored
75
                if (pageShowMax < totalPages) {
刘传洋 authored
76 77 78 79 80
                    pageShowMax++;
                }
            }
        }
刘传洋 authored
81
        for (let n = pageShowMin; n <= pageShowMax; n++) {
刘传洋 authored
82 83 84
            pageNums.push(n);
        }
刘传洋 authored
85 86
        if (ntype === 'e') {
            if (pageShowMin > 3) {
刘传洋 authored
87
                pageNums.unshift(1, '.');
刘传洋 authored
88
            } else if (pageShowMin === 2) {
刘传洋 authored
89
                pageNums.unshift(1);
刘传洋 authored
90
            } else if (pageShowMin === 3) {
刘传洋 authored
91 92 93
                pageNums.unshift(1, 2);
            }
刘传洋 authored
94
            if (pageShowMax < totalPages - 2) {
刘传洋 authored
95 96
                pageNums.push('.', totalPages);
            } else {
刘传洋 authored
97
                for (let x = pageShowMax + 1; x <= totalPages; x++) {
刘传洋 authored
98 99 100 101 102 103 104 105 106 107
                    pageNums.push(x);
                }
            }
        }

        return pageNums;
    }

    function renderItem(arr) {
        /** 分页展示页码个数end **/
刘传洋 authored
108
        let ret = '';
刘传洋 authored
109
刘传洋 authored
110
        if (_.isArray(arr) && arr.length) {
刘传洋 authored
111
刘传洋 authored
112 113
            arr.forEach((val) => {
                if (val === '.') {
刘传洋 authored
114 115
                    ret += '<a>...</a>';
                } else {
刘传洋 authored
116 117 118
                    ret += `<a href="${base}${val}" ` +
                            (page === val ? `class="${currentClass}"` : '') +
                            ` title="第${val}页">${val}</a>`;
刘传洋 authored
119 120 121 122 123 124 125
                }
            });
        }

        return ret;
    }
刘传洋 authored
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    function createStandItems() {
        return renderItem(getPageNums());
    }

    function createEllipsisItems() {
        return renderItem(getPageNums('e'));
    }

    if (!totalPages) {
        if (!totalRecords || !pageSize) {
            return new Handlebars.SafeString('');
        } else {
            totalPages = Math.ceil(totalRecords / pageSize);
        }
    }

    let items = ALL_TYPES[type] ? ALL_TYPES[type] : type;
刘传洋 authored
143
刘传洋 authored
144 145 146 147
    items = _.isArray(items) ? items : (items || ALL_TYPES.stand).split('-');

    let hasPage = false; // 配置中如果配置了多次 page/pe 则将忽略,只第一次有效
    let ret = `<div class="pager ${theme}">`;
刘传洋 authored
148
刘传洋 authored
149 150 151 152 153 154 155 156 157 158
    items.forEach(function(val) {

        switch (val) {
            case 'f' :
                if (page > 1) {
                    ret += `<a href="${base}1" title="首页">首页</a>`;
                }
                break;
            case 'p' :
                if (page > 1) {
刘传洋 authored
159 160
                    ret += `<a href="${base}` + (page - 1) +
                            '" title="上一页"><span class="iconfont">&#xe60e;</span>上一页</a>';
刘传洋 authored
161 162 163 164
                }
                break;
            case 'n' :
                if (page < totalPages) {
刘传洋 authored
165 166
                    ret += `<a href="${base}` + (page + 1) +
                            '" title="下一页">下一页<span class="iconfont">&#xe60c;</span></a>';
刘传洋 authored
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
                }
                break;
            case 'l' :
                if (page < totalPages) {
                    ret += `<a href="${base}${totalPages}" title="尾页">尾页</a>`;
                }
                break;
            case 'info' :
                ret += '共{totalRecords}条/{totalPages}页';
                break;
            case 'input' :
                // input
                // ret += '<span class="{cls}"><input value="{value}" type="text"></span>';
                break;
            case 'gobtn' :
                // goto btn
                break;
            case 'page' :
                if (!hasPage) {
                    ret += createStandItems();
                }
                hasPage = true;
                break;
            case 'pe' :
                if (!hasPage) {
                    ret += createEllipsisItems();
                }
                hasPage = true;
                break;
        }
    });

    if (options.fn) {
刘传洋 authored
200 201 202 203
        ret += options.fn(options.context);
    }
    ret += '</div>';
    return new Handlebars.SafeString(ret);
刘传洋 authored
204
};