ampersand-paginated-subcollection
Advanced tools
Comparing version 0.9.0 to 0.10.0
118
index.js
@@ -5,19 +5,7 @@ var State = require('ampersand-state'); | ||
var Pagination = State.extend({ | ||
session: { | ||
pageSize: { | ||
type: 'number', | ||
required: true, | ||
default: 50 | ||
}, | ||
page: { | ||
type: 'number', | ||
required: true, | ||
default: 1 | ||
}, | ||
parent: 'state', | ||
size: { | ||
type: 'number', | ||
required: true, | ||
default: 0 | ||
} | ||
props: { | ||
pageSize: ['number', true, 50], | ||
page: ['number', true, 1], | ||
parent: 'collection', | ||
size: ['number', true, 0] | ||
}, | ||
@@ -42,56 +30,98 @@ derived: { | ||
initialize: function (opts) { | ||
if (this.parent && this.parent.filtered) { | ||
this.size = this.parent.filtered.length || 0; | ||
this.listenTo(this.parent, 'add remove sync reset', function () { | ||
this.size = this.parent.filtered.length; | ||
}); | ||
if (this.parent) { | ||
this.size = this._collectionLength() || 0; | ||
this.listenTo(this.parent, 'add remove sync reset', this._updateSize); | ||
} | ||
}, | ||
_collectionLength: function () { | ||
if (this.parent && this.parent.filtered) return this.parent.filtered.length; | ||
return this.parent.length; | ||
}, | ||
_updateSize: function () { | ||
this.size = this._collectionLength(); | ||
} | ||
}); | ||
function Paginator(collection, config) { | ||
SubCollection.call(this, collection); | ||
function _Paginator(collection, config) { | ||
config = config || {}; | ||
SubCollection.call(this, collection, {}); | ||
config.parent = this; | ||
this.initPagination(config); | ||
this.configure(this.pagination.bounds); | ||
var bounds = this.pagination.bounds; | ||
config.offset = bounds.offset; | ||
config.limit = bounds.limit; | ||
this.configure(config); | ||
} | ||
module.exports = SubCollection.extend({ | ||
constructor: Paginator, | ||
var Paginator = module.exports = SubCollection.extend({ | ||
constructor: _Paginator, | ||
_pagination: Pagination, | ||
initPagination: function (config) { | ||
this.pagination = new this._pagination(config); | ||
this.listenTo(this.pagination, 'change:bounds', this._changePage); | ||
return this; | ||
}, | ||
_changePage: function (pagination, bounds) { | ||
var page = pagination.page; | ||
var event; | ||
this.configure(bounds); | ||
if (page === 1) { | ||
event = 'page:first'; | ||
} else if (page === pagination.pages) { | ||
event = 'page:last'; | ||
} else { | ||
event = 'page:change'; | ||
} | ||
this.trigger(event, this, page); | ||
}, | ||
getFirstPage: function () { | ||
this.pagination.page = 1; | ||
this.configure(this.pagination.bounds); | ||
this.trigger('page:first', this, 1); | ||
return (this.page = 1); | ||
}, | ||
getLastPage: function () { | ||
if (this.pagination.page === this.pagination.pages) return; | ||
var page = this.pagination.page = this.pagination.pages; | ||
this.configure(this.pagination.bounds); | ||
this.trigger('page:last', this, page); | ||
if (this.page === this.pages) return this.page; | ||
return (this.page = this.pages); | ||
}, | ||
getNextPage: function () { | ||
if (!this.hasNextPage()) return this.getLastPage(); | ||
this.pagination.page++; | ||
this.configure(this.pagination.bounds); | ||
return ++this.page; | ||
}, | ||
getPreviousPage: function () { | ||
if (this.pagination.page > 2) { | ||
this.pagination.page--; | ||
this.configure(this.pagination.bounds); | ||
} else { | ||
this.getFirstPage(); | ||
} | ||
if (this.page < 2) return this.getFirstPage(); | ||
return --this.page; | ||
}, | ||
hasNextPage: function () { | ||
return this.pagination.page < this.pagination.pages; | ||
return this.page < this.pages; | ||
}, | ||
hasPreviousPage: function () { | ||
return (this.pagination.page - 1) > 0; | ||
return (this.page - 1) > 0; | ||
} | ||
}); | ||
Object.defineProperties(Paginator.prototype, { | ||
page: { | ||
get: function () { | ||
return this.pagination.page; | ||
}, | ||
set: function (newPage) { | ||
if (typeof newPage === 'number') { | ||
if (newPage < 1) { | ||
throw new RangeError('Cannot set page < 1.'); | ||
} | ||
if (newPage > this.pagination.pages) { | ||
throw new RangeError('Cannot set page > ' + this.pagination.pages + '.'); | ||
} | ||
} | ||
this.pagination.page = newPage; | ||
} | ||
}, | ||
pages: { | ||
get: function () { | ||
return this.pagination.pages; | ||
} | ||
}, | ||
first: { get: Paginator.prototype.getFirstPage }, | ||
last: { get: Paginator.prototype.getLastPage }, | ||
next: { get: Paginator.prototype.getNextPage }, | ||
previous: { get: Paginator.prototype.getPreviousPage } | ||
}); |
{ | ||
"name": "ampersand-paginated-subcollection", | ||
"description": "ampersand-subcollection with built-in pagination", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"author": "Aaron McCall <aaron@andyet.net>", | ||
@@ -23,3 +23,3 @@ "bugs": { | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node test/main.js | tap-spec" | ||
}, | ||
@@ -29,3 +29,8 @@ "dependencies": { | ||
"ampersand-subcollection": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"ampersand-collection": "^1.3.17", | ||
"tap-spec": "^2.1.2", | ||
"tape": "^3.0.3" | ||
} | ||
} |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
7679
6
159
1
3