Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'develop'
  • Loading branch information
Eugene Simakin committed Sep 26, 2016
2 parents 4a2f5ff + 8749dc1 commit 2eb283c
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -23,4 +23,4 @@ For development use grunt build to make minified file.
To use grunt install packages by using: npm install

## Demo and Docs
For more information see [docs on github pages](http://esimakin.github.io/twbs-pagination/)
For more information see [docs on github pages](http://esimakin.github.io/twbs-pagination/) (out of date since 26 september)
6 changes: 3 additions & 3 deletions bower.json
@@ -1,6 +1,6 @@
{
"name": "twbs-pagination",
"version": "1.3.1",
"version": "1.4.0",
"homepage": "https://github.com/esimakin/twbs-pagination",
"authors": [
"Eugene Simakin <eugenesimakin@mail.ru>"
Expand All @@ -11,15 +11,15 @@
"globals"
],
"dependencies": {
"jquery": ">=1.7.0"
"jquery": ">=1.7"
},
"keywords": [
"pagination",
"jQuery",
"jQuery-plugin",
"bootstrap"
],
"license": "Apache 2.0",
"license": "Apache-2.0",
"ignore": [
"**/.*",
"node_modules",
Expand Down
31 changes: 31 additions & 0 deletions examples/bootstrap-v4.html
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Pagination plugin</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" integrity="sha384-2hfp1SzUoho7/TsGGGDaFdsuuDL0LX2hnUp6VkX3CUQ2K4K+xjboZdsXyp4oUHZj" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js" integrity="sha384-THPy051/pYDQGanwU6poAc/hOdQxjnOEXzbT+OuUAFqNqFjL+4IGLBgCJC3ZOShY" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.2.0/js/tether.min.js" integrity="sha384-Plbmg8JY28KFelvJVai01l8WyZzrYWG825m+cZ0eDDS1f7d/js6ikvy1+X+guPIB" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/js/bootstrap.min.js" integrity="sha384-VjEeINv9OSwtWFLAtmc4JCtEJXXBub00gtSnszmspDLCtC0I4z4nqz7rEFbIZLLU" crossorigin="anonymous"></script>
<script src="../jquery.twbsPagination.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<nav aria-label="Page navigation">
<ul class="pagination" id="pagination"></ul>
</nav>
</div>
<script type="text/javascript">
$(function () {
var obj = $('#pagination').twbsPagination({
totalPages: 35,
visiblePages: 10,
onPageClick: function (event, page) {
console.info(page);
}
});
console.info(obj.data());
});
</script>
</body>
</html>
31 changes: 31 additions & 0 deletions examples/index.html
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Pagination plugin</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="../jquery.twbsPagination.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<nav aria-label="Page navigation">
<ul class="pagination" id="pagination"></ul>
</nav>
</div>
<script type="text/javascript">
$(function () {
var obj = $('#pagination').twbsPagination({
totalPages: 35,
visiblePages: 10,
onPageClick: function (event, page) {
console.info(page + ' (from options)');
}
}).on('page', function (event, page) {
console.info(page + ' (from event listening)');
});
});
</script>
</body>
</html>
139 changes: 79 additions & 60 deletions jquery.twbsPagination.js
@@ -1,8 +1,8 @@
/*
* jQuery Bootstrap Pagination v1.3.1
* https://github.com/esimakin/twbs-pagination
/*!
* jQuery pagination plugin v1.4
* http://esimakin.github.io/twbs-pagination/
*
* Copyright 2014-2015 Eugene Simakin <eugenesimakin@mail.ru>
* Copyright 2014-2016, Eugene Simakin
* Released under Apache 2.0 license
* http://apache.org/licenses/LICENSE-2.0.html
*/
Expand Down Expand Up @@ -32,6 +32,11 @@
throw new Error('Visible pages option is not correct!');
}

// hide if only one page exists
if (this.options.totalPages == 1) {
return this;
}

if (this.options.totalPages < this.options.visiblePages) {
this.options.visiblePages = this.options.totalPages;
}
Expand All @@ -41,10 +46,9 @@
}

if (this.options.href) {
var match, regexp = this.options.href.replace(/[-\/\\^$*+?.|[\]]/g, '\\$&');
regexp = regexp.replace(this.options.hrefVariable, '(\\d+)');
if ((match = new RegExp(regexp, 'i').exec(window.location.href)) != null) {
this.options.startPage = parseInt(match[1], 10);
this.options.startPage = this.getPageFromQueryString();
if (!this.options.startPage) {
this.options.startPage = 1;
}
}

Expand All @@ -63,11 +67,11 @@
this.$element.append(this.$listContainer);
}

this.render(this.getPages(this.options.startPage));
this.setupEvents();

if (this.options.initiateStartPageClick) {
this.$element.trigger('page', this.options.startPage);
this.show(this.options.startPage);
} else {
this.render(this.getPages(this.options.startPage));
this.setupEvents();
}

return this;
Expand All @@ -89,6 +93,7 @@
if (page < 1 || page > this.options.totalPages) {
throw new Error('Page is incorrect.');
}
this.currentPage = page;

this.render(this.getPages(page));
this.setupEvents();
Expand Down Expand Up @@ -129,36 +134,12 @@
buildItem: function (type, page) {
var $itemContainer = $('<li></li>'),
$itemContent = $('<a></a>'),
itemText = null;

switch (type) {
case 'page':
itemText = page;
$itemContainer.addClass(this.options.pageClass);
break;
case 'first':
itemText = this.options.first;
$itemContainer.addClass(this.options.firstClass);
break;
case 'prev':
itemText = this.options.prev;
$itemContainer.addClass(this.options.prevClass);
break;
case 'next':
itemText = this.options.next;
$itemContainer.addClass(this.options.nextClass);
break;
case 'last':
itemText = this.options.last;
$itemContainer.addClass(this.options.lastClass);
break;
default:
break;
}
itemText = this.options[type] ? this.makeText(this.options[type], page) : page;

$itemContainer.addClass(this.options[type + 'Class']);
$itemContainer.data('page', page);
$itemContainer.data('page-type', type);
$itemContainer.append($itemContent.attr('href', this.makeHref(page)).html(itemText));
$itemContainer.append($itemContent.attr('href', this.makeHref(page)).addClass(this.options.anchorClass).html(itemText));

return $itemContainer;
},
Expand Down Expand Up @@ -192,7 +173,10 @@
render: function (pages) {
var _this = this;
this.$listContainer.children().remove();
this.$listContainer.append(this.buildListItems(pages));
var items = this.buildListItems(pages);
jQuery.each(items, function(key, item){
_this.$listContainer.append(item);
});

this.$listContainer.children().each(function () {
var $this = $(this),
Expand Down Expand Up @@ -226,23 +210,53 @@

setupEvents: function () {
var _this = this;
this.$listContainer.find('li').each(function () {
this.$listContainer.off('click').on('click', 'li', function (evt) {
var $this = $(this);
$this.off();
if ($this.hasClass(_this.options.disabledClass) || $this.hasClass(_this.options.activeClass)) {
$this.on('click', false);
return;
return false;
}
$this.click(function (evt) {
// Prevent click event if href is not set.
!_this.options.href && evt.preventDefault();
_this.show(parseInt($this.data('page')));
});
// Prevent click event if href is not set.
!_this.options.href && evt.preventDefault();
_this.show(parseInt($this.data('page')));
});
},

makeHref: function (c) {
return this.options.href ? this.options.href.replace(this.options.hrefVariable, c) : "#";
makeHref: function (page) {
return this.options.href ? this.generateQueryString(page) : "#";
},

makeText: function (text, page) {
return text.replace(this.options.pageVariable, page)
.replace(this.options.totalPagesVariable, this.options.totalPages)
},
getPageFromQueryString: function (searchStr) {
var search = this.getSearchString(searchStr),
regex = new RegExp(this.options.pageVariable + '(=([^&#]*)|&|#|$)'),
page = regex.exec(search);
if (!page || !page[2]) {
return null;
}
page = decodeURIComponent(page[2]);
page = parseInt(page);
if (isNaN(page)) {
return null;
}
return page;
},
generateQueryString: function (pageNumber, searchStr) {
var search = this.getSearchString(searchStr),
regex = new RegExp(this.options.pageVariable + '=*[^&#]*');
if (!search) return '';
return '?' + search.replace(regex, this.options.pageVariable + '=' + pageNumber);

},
getSearchString: function (searchStr) {
var search = searchStr || window.location.search;
if (search === '') {
return null;
}
if (search.indexOf('?') === 0) search = search.substr(1);
return search;
}

};
Expand All @@ -255,7 +269,7 @@

var $this = $(this);
var data = $this.data('twbs-pagination');
var options = typeof option === 'object' && option;
var options = typeof option === 'object' ? option : {};

if (!data) $this.data('twbs-pagination', (data = new TwbsPagination(this, options) ));
if (typeof option === 'string') methodReturn = data[ option ].apply(data, args);
Expand All @@ -264,26 +278,29 @@
};

$.fn.twbsPagination.defaults = {
totalPages: 0,
totalPages: 1,
startPage: 1,
visiblePages: 5,
initiateStartPageClick: true,
href: false,
hrefVariable: '{{number}}',
pageVariable: '{{page}}',
totalPagesVariable: '{{total_pages}}',
page: null,
first: 'First',
prev: 'Previous',
next: 'Next',
last: 'Last',
loop: false,
onPageClick: null,
paginationClass: 'pagination',
nextClass: 'next',
prevClass: 'prev',
lastClass: 'last',
firstClass: 'first',
pageClass: 'page',
nextClass: 'page-item next',
prevClass: 'page-item prev',
lastClass: 'page-item last',
firstClass: 'page-item first',
pageClass: 'page-item',
activeClass: 'active',
disabledClass: 'disabled'
disabledClass: 'disabled',
anchorClass: 'page-link'
};

$.fn.twbsPagination.Constructor = TwbsPagination;
Expand All @@ -293,4 +310,6 @@
return this;
};

$.fn.twbsPagination.version = "1.4";

})(window.jQuery, window, document);

0 comments on commit 2eb283c

Please sign in to comment.