...
|
...
|
@@ -6,149 +6,86 @@ |
|
|
|
|
|
'use strict';
|
|
|
|
|
|
let fs = require('fs'),
|
|
|
let // fs = require('fs'),
|
|
|
_ = require('lodash'),
|
|
|
exphbs = require('express-handlebars').create(),
|
|
|
Handlebars = exphbs.handlebars;/*,
|
|
|
Handlebars = exphbs.handlebars;/* ,
|
|
|
Pagination = require('../common/pagination');
|
|
|
*/
|
|
|
|
|
|
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'
|
|
|
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'
|
|
|
};
|
|
|
|
|
|
Handlebars.registerHelper("page", function(){
|
|
|
var options = arguments[arguments.length - 1];
|
|
|
var 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,
|
|
|
Handlebars.registerHelper('page', function() {
|
|
|
let options = arguments[arguments.length - 1];
|
|
|
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,
|
|
|
totalRecords = options.hash.totalRecords,
|
|
|
theme = options.hash.theme || 'pager',
|
|
|
currentClass = options.hash.currentClass || 'cur',
|
|
|
type = options.hash.type || 'stand', //min full stand ellipsis or: f-首页, p-上一页, page-页码,n-下一页,l-最后一页,info-displayMsg,inout输入框,gobtn-goto Btn
|
|
|
pageVar = options.pageVar || 'page',
|
|
|
pageSizeVar = options.pageSizeVar || "pageSize",
|
|
|
base = baseUrl + (baseUrl.indexOf('?') < 0 ? '?' : '&')
|
|
|
+ (options.hash.pageSize ? (pageSizeVar + '=' + pageSize + '&') : '')
|
|
|
+ pageVar + '=';
|
|
|
|
|
|
if(!totalPages) {
|
|
|
if(!totalRecords || !pageSize) {
|
|
|
return new Handlebars.SafeString("");
|
|
|
} else {
|
|
|
totalPages = Math.ceil(totalRecords / pageSize);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
var items = ALL_TYPES[type] ? ALL_TYPES[type] : type;
|
|
|
items = _.isArray(items) ? items : (items || ALL_TYPES.stand).split('-');
|
|
|
|
|
|
var hasPage = false; //配置中如果配置了多次 page/pe 则将忽略,只第一次有效
|
|
|
var ret = `<div class="pager ${theme}">`;
|
|
|
items.forEach(function(val, index, arr){
|
|
|
|
|
|
switch(val) {
|
|
|
case 'f' :
|
|
|
if(page > 1) {
|
|
|
ret += `<a href="${base}1" title="首页">首页</a>`;
|
|
|
}
|
|
|
break;
|
|
|
case 'p' :
|
|
|
if(page > 1) {
|
|
|
ret += `<a href="${base}` + (page - 1) + `" title="上一页">上一页</a>`;
|
|
|
}
|
|
|
break;
|
|
|
case 'n' :
|
|
|
if(page < totalPages) {
|
|
|
ret += `<a href="${base}` + (page + 1) + `" title="下一页">下一页</a>`;
|
|
|
}
|
|
|
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;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
function createStandItems(){
|
|
|
return renderItem(getPageNums());
|
|
|
}
|
|
|
|
|
|
function createEllipsisItems(){
|
|
|
return renderItem(getPageNums('e'));
|
|
|
}
|
|
|
// min full stand ellipsis or: f-首页, p-上一页, page-页码,n-下一页,l-最后一页,info-displayMsg,inout输入框,gobtn-goto Btn
|
|
|
type = options.hash.type || 'stand',
|
|
|
pageVar = options.pageVar || 'page',
|
|
|
pageSizeVar = options.pageSizeVar || 'pageSize',
|
|
|
base = baseUrl + (baseUrl.indexOf('?') < 0 ? '?' : '&') +
|
|
|
(options.hash.pageSize ? (pageSizeVar + '=' + pageSize + '&') : '') +
|
|
|
pageVar + '=';
|
|
|
|
|
|
function getPageNums(type){
|
|
|
function getPageNums(ntype) {
|
|
|
var pageNums = [];
|
|
|
var num = showNum;
|
|
|
if(type == 'e') {
|
|
|
if (ntype === 'e') {
|
|
|
num = num - 2;
|
|
|
num = num > 2 ? num : 2;
|
|
|
}
|
|
|
|
|
|
/** 分页展示页码个数begin 规则:展示最靠近当前页的指定个数 **/
|
|
|
var pageShowMax = num % 2 == 0 ? page - 1 : page;
|
|
|
var pageShowMin = page;
|
|
|
let pageShowMax = num % 2 === 0 ? page - 1 : page;
|
|
|
let pageShowMin = page;
|
|
|
|
|
|
for(var i = 0; i < Math.floor(num/2); i++) {
|
|
|
for (let i = 0; i < Math.floor(num / 2); i++) {
|
|
|
pageShowMax++;
|
|
|
pageShowMin--;
|
|
|
if(pageShowMax > totalPages) {
|
|
|
if (pageShowMax > totalPages) {
|
|
|
pageShowMax = totalPages;
|
|
|
if(pageShowMin > 1) {
|
|
|
if (pageShowMin > 1) {
|
|
|
pageShowMin--;
|
|
|
}
|
|
|
}
|
|
|
if(pageShowMin < 1) {
|
|
|
if (pageShowMin < 1) {
|
|
|
pageShowMin = 1;
|
|
|
if(pageShowMax < totalPages) {
|
|
|
if (pageShowMax < totalPages) {
|
|
|
pageShowMax++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for(var n = pageShowMin; n <= pageShowMax; n++) {
|
|
|
for (let n = pageShowMin; n <= pageShowMax; n++) {
|
|
|
pageNums.push(n);
|
|
|
}
|
|
|
|
|
|
if(type == 'e') {
|
|
|
if(pageShowMin > 3) {
|
|
|
if (ntype === 'e') {
|
|
|
if (pageShowMin > 3) {
|
|
|
pageNums.unshift(1, '.');
|
|
|
} else if(pageShowMin == 2){
|
|
|
} else if (pageShowMin === 2) {
|
|
|
pageNums.unshift(1);
|
|
|
} else if(pageShowMin == 3) {
|
|
|
} else if (pageShowMin === 3) {
|
|
|
pageNums.unshift(1, 2);
|
|
|
}
|
|
|
|
|
|
if(pageShowMax < totalPages - 2) {
|
|
|
if (pageShowMax < totalPages - 2) {
|
|
|
pageNums.push('.', totalPages);
|
|
|
} else {
|
|
|
for(var x = pageShowMax + 1; x <= totalPages; x++) {
|
|
|
for (let x = pageShowMax + 1; x <= totalPages; x++) {
|
|
|
pageNums.push(x);
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -159,14 +96,16 @@ Handlebars.registerHelper("page", function(){ |
|
|
|
|
|
function renderItem(arr) {
|
|
|
/** 分页展示页码个数end **/
|
|
|
var ret = '';
|
|
|
if(_.isArray(arr) && arr.length) {
|
|
|
let ret = '';
|
|
|
if (_.isArray(arr) && arr.length) {
|
|
|
|
|
|
arr.forEach((val, index, arrs) => {
|
|
|
if('.' == val) {
|
|
|
arr.forEach((val) => {
|
|
|
if (val === '.') {
|
|
|
ret += '<a>...</a>';
|
|
|
} else {
|
|
|
ret += `<a href="${base}${val}" ` + (page === val ? `class="${currentClass}"` : '') + ` title="第${val}页">${val}</a>`;
|
|
|
ret += `<a href="${base}${val}" ` +
|
|
|
(page === val ? `class="${currentClass}"` : '') +
|
|
|
` title="第${val}页">${val}</a>`;
|
|
|
}
|
|
|
});
|
|
|
}
|
...
|
...
|
@@ -174,7 +113,76 @@ Handlebars.registerHelper("page", function(){ |
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
if(options.fn) {
|
|
|
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;
|
|
|
items = _.isArray(items) ? items : (items || ALL_TYPES.stand).split('-');
|
|
|
|
|
|
let hasPage = false; // 配置中如果配置了多次 page/pe 则将忽略,只第一次有效
|
|
|
let ret = `<div class="pager ${theme}">`;
|
|
|
items.forEach(function(val) {
|
|
|
|
|
|
switch (val) {
|
|
|
case 'f' :
|
|
|
if (page > 1) {
|
|
|
ret += `<a href="${base}1" title="首页">首页</a>`;
|
|
|
}
|
|
|
break;
|
|
|
case 'p' :
|
|
|
if (page > 1) {
|
|
|
ret += `<a href="${base}` + (page - 1) + '" title="上一页">上一页</a>';
|
|
|
}
|
|
|
break;
|
|
|
case 'n' :
|
|
|
if (page < totalPages) {
|
|
|
ret += `<a href="${base}` + (page + 1) + '" title="下一页">下一页</a>';
|
|
|
}
|
|
|
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) {
|
|
|
ret += options.fn(options.context);
|
|
|
}
|
|
|
ret += '</div>';
|
...
|
...
|
|