;(function ($, window, document, undefined) {
'use strict';
function Paging(element, options) {
this.element = element;
this.options = {
nowPage: options.nowPage || 1, // 当前页码
pageNum: options.pageNum, // 总页码
canJump: options.canJump || 0, // 是否能跳转。0=不显示(默认),1=显示
showOne: options.showOne || 1,//只有一页时,是否显示。0=不显示,1=显示(默认)
buttonNum: (options.buttonNum>=5?options.buttonNum:5) || 7,// 页面显示页码数量
callback: options.callback // 回调函数
};
this.init();
}
Paging.prototype = {
constructor : Paging,
init : function() {
this.createHtml();
this.bindClickEvent();
this.disabled();
},
createHtml : function(){
var me = this;
var nowPage = this.options.nowPage;
var pageNum = this.options.pageNum;
var buttonNum = this.options.buttonNum;
var canJump = this.options.canJump;
var showOne = this.options.showOne;
var content = [];
//对nowPage进行判断
nowPage = nowPage > pageNum ? pageNum : nowPage;
nowPage = nowPage < 1 ? 1 : nowPage;
//如果只有一页并且设置为不显示,则不进行渲染
if(showOne && pageNum === 1){
//return '';
}
content.push("
");
me.element.html(content.join(''));
// DOM重新生成后每次调用是否禁用button
setTimeout(function () {
me.disabled();
}, 20);
},
bindClickEvent: function(){
var me = this;
me.element.off('click', 'li');
me.element.on('click', 'li', function () {
var cla = $(this).attr('class');
var num = parseInt($(this).html());
var nowPage = me.options.nowPage;
if( $(this).hasClass('xl-disabled') || cla === 'xl-jumpText'){
return '';
}
if (cla === 'xl-prevPage') {
if (nowPage !== 1) {
me.options.nowPage -= 1;
}
} else if (cla === 'xl-nextPage') {
if (nowPage !== me.options.pageNum) {
me.options.nowPage += 1;
}
}else if(cla === 'xl-jumpButton'){
me.options.nowPage = Number($('#xlJumpNum').val());
}else{
me.options.nowPage = num;
}
me.createHtml();
if (me.options.callback) {
me.options.callback(me.options.nowPage);
}
});
},
disabled: function () {
var me = this;
var nowPage = me.options.nowPage;
var pageNum = me.options.pageNum;
if (nowPage === 1) {
me.element.children().children('.xl-prevPage').addClass('xl-disabled');
} else if (nowPage === pageNum) {
me.element.children().children('.xl-nextPage').addClass('xl-disabled');
}
}
}
$.fn.paging = function (options) {
return new Paging($(this), options);
}
})(jQuery, window, document);