pagination-template
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -0,1 +1,2 @@ | ||
'use strict'; | ||
/** | ||
@@ -9,19 +10,20 @@ Module for create a pagination for use in a templates | ||
@param urlOrigin Set the url begin | ||
@param urlOrigin Set the url begin | ||
@param pageAct Actual page visit for the user | ||
@param totalResults Number of rows | ||
@param rowsPerPage Rows per page that we will show | ||
@param options Object whith another options | ||
@param options Object whith another options | ||
options: { | ||
maxPagination : 10, //Maximum number of indexes show | ||
//If for example we have a 20 pages and | ||
//If for example we have a 20 pages and | ||
//the maxPagination value is 10 the result is | ||
//and the actual pages is 7 the result is | ||
//4 5 6 7 8 9 10 11 12 13 | ||
showBeginingEnd: true, //Check if they have to show the scroll buttons start end | ||
domain: 'http://www.google.com', //If this options is passed then the url will | ||
//4 5 6 7 8 9 10 11 12 13 | ||
showBeginingEnd: true, //Check if they have to show the scroll buttons start end | ||
domain: 'http://www.google.com', //If this options is passed then the url will | ||
//be the concatenation of urlOrigin and the domain | ||
showPreviousNext: true //show next last buttons | ||
} | ||
**/ | ||
function Pagination(urlOrigin, pageAct, totalResults, rowsPerPage, options){ | ||
function Pagination(urlOrigin, pageAct, totalResults, rowsPerPage, options){ | ||
this.options = options || {}; | ||
@@ -38,4 +40,4 @@ this.pageAct = pageAct; | ||
actualpage: true //indicate if is the actual page | ||
index: 1 //if the number of page | ||
index: 1 //if the number of page | ||
} | ||
@@ -57,11 +59,10 @@ **/ | ||
if(maxPagination>0 && this.pageAct>(maxPagination/2)){ | ||
ini = this.pageAct - (maxPagination/2); | ||
ini = this.pageAct - (maxPagination/2); | ||
} | ||
//check the finish of pagination | ||
if(maxPagination>0 && (maxPagination+ini) < last){ | ||
end = (ini + maxPagination) -1; | ||
} | ||
console.log(ini, end, maxPagination); | ||
if(maxPagination > 0 && (last - ini) <maxPagination){ | ||
if(maxPagination>0 && (maxPagination+ini) <= last){ | ||
end = (ini + maxPagination) - 1; | ||
} | ||
if(maxPagination > 0 && (last - ini) <maxPagination){ | ||
ini = (end - maxPagination) + 1; | ||
@@ -73,23 +74,31 @@ //check ini is negative | ||
} | ||
console.log(ini, end, maxPagination); | ||
//check domain option | ||
var urlFinish = this.options.domain !== undefined ? (this.options.domain + this.urlOrigin) : (this.urlOrigin); | ||
//check go first button | ||
//check go first button | ||
if(this.options!==undefined && this.options.showBeginingEnd){ | ||
var pagina = {} | ||
pagina.url = urlFinish + '/' + 1 + '/' + this.rowsPerPage; | ||
pagina.actualPage = (i===1); | ||
var pagina = {} | ||
pagina.url = generateUrl(urlFinish, 1, this.rowsPerPage); | ||
pagina.actualPage = (this.pageAct===1); | ||
pagina.index = 1; | ||
pagina.specialButton = "first"; | ||
paginationResult.push(pagina); | ||
} | ||
//console.log(ini, last, end); | ||
if(this.options!==undefined && this.options.showPreviousNext){ | ||
var pagina = {}; | ||
var page = this.pageAct - (this.pageAct===1 ? 0 : 1); | ||
pagina.url = generateUrl(urlFinish, page, this.rowsPerPage); | ||
pagina.actualPage = (this.pageAct===page); | ||
pagina.index = page; | ||
pagina.specialButton = "previous"; | ||
paginationResult.push(pagina); | ||
} | ||
for(var i = ini; i<= end; ++i){ | ||
var pagina = {}; | ||
pagina.url = urlFinish + '/' + i + '/' + this.rowsPerPage; | ||
pagina.url = generateUrl(urlFinish, i, this.rowsPerPage); | ||
pagina.actualPage = (i===this.pageAct); | ||
pagina.index = i; | ||
pagina.index = i; | ||
@@ -100,12 +109,26 @@ paginationResult.push(pagina); | ||
}; | ||
//check go last button | ||
if(this.options!==undefined && this.options.showPreviousNext){ | ||
var pagina = {}; | ||
var page = this.pageAct + (this.pageAct===last ? 0 : 1); | ||
pagina.url = generateUrl(urlFinish, page, this.rowsPerPage); | ||
pagina.actualPage = (i===page); | ||
pagina.index = page; | ||
pagina.specialButton = "next"; | ||
paginationResult.push(pagina); | ||
} | ||
//check go last button | ||
if(this.options!==undefined && this.options.showBeginingEnd){ | ||
var pagina = {}; | ||
pagina.url = urlFinish + '/' + last + '/' + this.rowsPerPage; | ||
pagina.url = generateUrl(urlFinish, last, this.rowsPerPage); | ||
pagina.actualPage = (last===this.pageAct); | ||
pagina.index = last; | ||
pagina.index = last; | ||
pagina.specialButton = "last"; | ||
paginationResult.push(pagina); | ||
} | ||
} | ||
return paginationResult; | ||
} | ||
function generateUrl(url, index, rowsPerPage){ | ||
return url + '/' + index + '/' + rowsPerPage; | ||
} |
{ | ||
"name": "pagination-template", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Pagination of results for use in a template", | ||
@@ -15,3 +15,3 @@ "main": "index.js", | ||
"author": "amadormateof", | ||
"license": "BSD-2-Clause", | ||
"license": "MIT", | ||
"devDependencies": { | ||
@@ -18,0 +18,0 @@ "chai": "~1.9.2", |
@@ -17,3 +17,3 @@ pagination-template | ||
The value of variable result in this example is: | ||
[ { url: 'search/1/13', | ||
@@ -52,4 +52,4 @@ actualPage: false, | ||
index: 11 } ] | ||
##Use with Express | ||
@@ -70,7 +70,8 @@ | ||
Example: | ||
var parameters = { | ||
maxPagination : 10, | ||
showBeginingEnd: true, | ||
domain: 'http://www.google.com', | ||
maxPagination : 10, | ||
showBeginingEnd: true, | ||
domain: 'http://www.google.com', | ||
showPreviousNext: true | ||
}; | ||
@@ -87,1 +88,4 @@ var paginator = new pagination('search', 3, 141, 13, parameters); | ||
If this options is passed then the url will be the concatenation of domain and the urlOrigin | ||
###showPreviousNext | ||
Show next previous button |
@@ -0,0 +0,0 @@ /** |
var Pagination = require('../index.js'); | ||
describe('PAGINATION', function() { | ||
it('Check basic pagination', function(done){ | ||
var async = require('async'); | ||
describe('Check all the pages of pagination', function(){ | ||
var numTest = 22; | ||
it('Check 1 to ' + numTest + ' pageActual', function(done){ | ||
for(var i = 0; i<=numTest; ++i){ | ||
var pag = new Pagination('search', i, numTest * 10, 10, {maxPagination:10}); | ||
var result = pag.getPagination(); | ||
expect(result).to.be.a('array'); | ||
//check the length of results | ||
expect(result).to.have.length(10); | ||
} | ||
done(); | ||
}); | ||
}); | ||
describe('Check the options of pagination', function() { | ||
it('Check basic pagination', function(done){ | ||
var pag = new Pagination('search', 1, 100, 10); | ||
@@ -8,3 +24,3 @@ var result = pag.getPagination(); | ||
//check the length of results | ||
expect(result).to.have.length(10); | ||
expect(result).to.have.length(10); | ||
//check if the actual pages is marked | ||
@@ -16,6 +32,6 @@ expect(result[0]).to.have.property('actualPage').with.equal(true); | ||
var pag = new Pagination('search', 1, 0, 10); | ||
var result = pag.getPagination(); | ||
var result = pag.getPagination(); | ||
expect(result).to.be.a('array'); | ||
//check the length of results | ||
expect(result).to.have.length(0); | ||
expect(result).to.have.length(0); | ||
done(); | ||
@@ -25,5 +41,5 @@ }); | ||
var pag = new Pagination('search', 1, 150, 10, {maxPagination:10}); | ||
var result = pag.getPagination(); | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
expect(result).to.have.length(10); | ||
expect(result).to.have.length(10); | ||
done(); | ||
@@ -33,13 +49,13 @@ }); | ||
var pag = new Pagination('search', 3, 150, 10, {maxPagination:10}); | ||
var result = pag.getPagination(); | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
expect(result).to.have.length(10); | ||
expect(result).to.have.length(10); | ||
//check if the actual pages is marked | ||
expect(result[2]).to.have.property('actualPage').with.equal(true); | ||
expect(result[2]).to.have.property('actualPage').with.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it('Check extended option maxPagination', function(done){ | ||
var pag = new Pagination('search', 8, 150, 10, {maxPagination:10}); | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
expect(result).to.have.length(10); | ||
@@ -50,6 +66,6 @@ | ||
done(); | ||
}); | ||
}); | ||
it('Check extended option maxPagination, the finish is near', function(done){ | ||
var pag = new Pagination('search', 14, 150, 10, {maxPagination:10}); | ||
var result = pag.getPagination(); | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
@@ -60,6 +76,6 @@ expect(result).to.have.length(10); | ||
done(); | ||
}); | ||
}); | ||
it('Check extended option maxPagination, the finish is here', function(done){ | ||
var pag = new Pagination('search', 15, 150, 10, {maxPagination:10}); | ||
var result = pag.getPagination(); | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
@@ -70,28 +86,27 @@ expect(result).to.have.length(10); | ||
done(); | ||
}); | ||
}); | ||
it('Check decimals for maxPagination results', function(done){ | ||
var pag = new Pagination('search', 0, 4651, 10, {maxPagination:13, showBeginingEnd:true}); | ||
var result = pag.getPagination(); | ||
console.log(result); | ||
for(var i =0; i< result.length; ++i){ | ||
var index = result[i].index; | ||
var strIndex = index.toString(); | ||
expect(strIndex).not.include('.'); | ||
} | ||
expect(strIndex).not.include('.'); | ||
} | ||
done(); | ||
}); | ||
}); | ||
it('Check negative number for maxPagination index', function(done){ | ||
var pag = new Pagination('search', 3, 141, 13, {maxPagination:13}); | ||
var result = pag.getPagination(); | ||
var result = pag.getPagination(); | ||
if(result[0].index < 0){ | ||
done(new Error('Negative number for index')); | ||
done(new Error('Negative number for index')); | ||
} | ||
else{ | ||
done(); | ||
} | ||
} | ||
}); | ||
it('Check first last button', function(done){ | ||
var pag = new Pagination('search', 15, 150, 10, {showBeginingEnd:true}); | ||
var result = pag.getPagination(); | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
@@ -106,3 +121,3 @@ expect(result).to.have.length(17); | ||
var pag = new Pagination('search', 15, 150, 10, {maxPagination:10, showBeginingEnd:true}); | ||
var result = pag.getPagination(); | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
@@ -115,8 +130,19 @@ expect(result).to.have.length(12); | ||
}); | ||
it('Check next previous butotn', function(done){ | ||
var pag = new Pagination('search', 10, 150, 10, {maxPagination:10, showPreviousNext:true}); | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
expect(result).to.have.length(12); | ||
//check if the actual pages is marked | ||
expect(result[0]).to.have.property('specialButton').with.equal('previous'); | ||
expect(result[11]).to.have.property('specialButton').with.equal('next'); | ||
done(); | ||
}); | ||
it('Check domain option', function(done){ | ||
var domain = 'http://www.google.es/'; | ||
var pag = new Pagination('search', 15, 150, 10, {domain: domain}); | ||
var result = pag.getPagination(); | ||
var result = pag.getPagination(); | ||
//check the length of results | ||
expect(result).to.have.length(15); | ||
expect(result).to.have.length(15); | ||
//check if the actual pages is marked | ||
@@ -123,0 +149,0 @@ expect(result[0]).to.have.property('url').with.include(domain); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12010
259
88