diff --git a/Readme.md b/Readme.md index 5ac137d..03a6fe7 100644 --- a/Readme.md +++ b/Readme.md @@ -1,13 +1,18 @@ - # pager Pager ui component. -  +  + +## How to use -## Installation +This is a component component. You can easily plug it into your site or web app. Check the example of usage in test/index.html. To get things working, follow these easy steps (assuming you already have Node.js and npm installed): - $ component install component/pager +* `npm install -g component` +* Clone this repository and navigate into the component folder +* Run `component install` to fetch dependencies +* Run `component build` +* Now you can open test/index.html and if everything is fine you should be able to see the component in action ## Events @@ -21,7 +26,11 @@ ### Pager#perpage(n) - Set the number of items per page to `n`. [5] + Set the number of items per page to `n`. [5]. Pager will calculate number of page links by dividing total amount of items on perpage value. + +### Pager#max_pages(n) + + Specify size of the links window. `n` means amount of page links to the left and right from the current page. `...` will be added to the start and end of the links window. ### Pager#pages() diff --git a/component.json b/component.json index 319e0e4..3bd80ff 100644 --- a/component.json +++ b/component.json @@ -2,11 +2,11 @@ "name": "pager", "repo": "component/pager", "description": "Pager ui component", - "version": "0.1.0", + "version": "0.5.0", "keywords": ["pager", "ui", "paginate", "pagination"], "dependencies": { "component/emitter": "1.0.1", - "component/dom": "0.8.0" + "component/dom": "*" }, "development": {}, "scripts": [ diff --git a/index.js b/index.js index 807bc96..e540044 100644 --- a/index.js +++ b/index.js @@ -45,6 +45,8 @@ Pager.prototype.onclick = function(e){ var el = dom(e.target.parentNode); if (el.hasClass('prev')) return this.prev(); if (el.hasClass('next')) return this.next(); + if (el.hasClass('first')) return this.first(); + if (el.hasClass('last')) return this.last(); this.show(el.text() - 1); }; @@ -79,6 +81,14 @@ Pager.prototype.next = function(){ this.show(Math.min(this.pages() - 1, this.current + 1)); }; +Pager.prototype.first = function(){ + this.show(0); +}; + +Pager.prototype.last = function(){ + this.show(this.pages()-1); +}; + /** * Select the page `n`. * @@ -133,6 +143,20 @@ Pager.prototype.total = function(n){ return this; }; +/** + * Set the max number of pages displayed by pager + * at once + * + * @param {Number} n + * @return {Pager} + * @api public + */ + +Pager.prototype.max_pages = function(n){ + this._max_pages = n; + return this; +}; + /** * Render the pager. * @@ -142,6 +166,7 @@ Pager.prototype.total = function(n){ Pager.prototype.render = function(){ var total = this._total; var curr = this.current; + var max_pages = this._max_pages; var per = this._perpage; var pages = this.pages(); var el = this.el; @@ -152,12 +177,67 @@ Pager.prototype.render = function(){ // remove old el.find('li.page').remove(); - // page links - for (var i = 0; i < pages; ++i) { - var n = i + 1; - links += curr == i - ? '