f625e0de by Adam Heath

Remove pagination logic; this is now in a separate repo. Solr does *not*

do pagination itself anymore.
1 parent a0fe18f0
{
"name": "solr-frontend",
"version": "2016.06.24-0",
"version": "2016.07.26-0",
"authors": [
"Adam Heath <adam@brainfood.com>"
],
......
{
"name": "solr-frontend",
"license": "UNLICENSED",
"version": "2016.06.24-0",
"version": "2016.07.26-0",
"description": "Solr Frontend",
"repository": {
"type": "git",
......
define(function(require) {
'use strict';
var Backbone = require('backbone');
var Util = require('./Util');
var Pagination = Backbone.Model.extend({
defaults: function defaults() {
return {
currentPage: 1,
hasNext: false,
hasNextJump: false,
hasPrevious: false,
hasPreviousJump: false,
nextJumpPage: undefined,
nextPage: undefined,
pages: [],
pageJump: 5,
pageSize: 10,
previousJumpPage: undefined,
previousPage: undefined,
totalCount: 0,
totalPages: 0,
};
},
initialize: function(data, options) {
var buildPages = function buildPages() {
var currentPage = parseInt(this.get('currentPage'));
var pageSize = parseInt(this.get('pageSize'));
var totalCount = parseInt(this.get('totalCount'));
if (!totalCount) {
return;
}
var pages = [];
var totalPages = Math.floor((totalCount + pageSize - 1) / pageSize);
if (currentPage < 0) {
this.set('currentPage', 1);
return;
} else if (currentPage > totalPages) {
this.set('currentPage', totalPages);
return;
}
function addPage(self, i) {
pages.push({
current: i === currentPage,
jump: function() {
self.set('currentPage', i);
return true;
},
number: i,
});
}
var startAt = currentPage - 4, endAt = currentPage + 5;
if (startAt < 1) {
endAt += (1 - startAt);
}
if (endAt > totalPages) {
startAt -= endAt - totalPages - 1;
endAt = totalPages + 1;
}
if (startAt < 1) {
startAt = 1;
}
if (endAt - startAt < 9) {
///* global console:false */
//console.log('foo');
}
for (var i = startAt; i < endAt; i++) {
addPage(this, i);
}
var hasPrevious = currentPage > 1;
var hasNext = currentPage < totalPages;
var pageJump = this.get('pageJump');
var nextJumpPage, previousJumpPage, hasNextJump, hasPreviousJump;
if (pageJump) {
nextJumpPage = currentPage + pageJump;
previousJumpPage = currentPage - pageJump;
hasNextJump = nextJumpPage < totalPages;
hasPreviousJump = previousJumpPage > 0;
} else {
hasNextJump = false;
hasPreviousJump = false;
}
this.set({
hasNext: hasNext,
hasNextJump: hasNextJump,
hasPrevious: hasPrevious,
hasPreviousJump: hasPreviousJump,
nextJumpPage: hasNextJump ? nextJumpPage : undefined,
nextPage: hasNext ? currentPage + 1 : undefined,
pages: pages,
previousJumpPage: hasNextJump ? previousJumpPage : undefined,
previousPage: hasPrevious ? currentPage - 1 : undefined,
totalPages: totalPages,
});
};
this.on('change:pageSize change:currentPage change:totalCount', buildPages, this);
var installActions = _.bind(function installActions(eventName, nextName, hasNextName, previousName, hasPreviousName, pageCount) {
this.on(eventName, function() {
var hasNext = this.get(hasNextName);
var hasPrevious = this.get(hasPreviousName);
var next, previous;
if (hasNext) {
next = _.bind(function(e) {
Util.preventDefault(e);
this.set('currentPage', this.get('currentPage') + pageCount);
return false;
}, this);
} else {
next = Util.stepFalse;
}
if (hasPrevious) {
previous = _.bind(function(e) {
Util.preventDefault(e);
this.set('currentPage', this.get('currentPage') - pageCount);
return false;
}, this);
} else {
previous = Util.stepFalse;
}
this.set(nextName, next);
this.set(previousName, previous);
}, this);
this[nextName] = _.bind(function() {
return this.get(nextName)();
}, this);
this[previousName] = _.bind(function() {
return this.get(previousName)();
}, this);
}, this);
this.on('change:pageJump', function() {
var pageJump = this.get('pageJump');
installActions('change:hasNextJump change:hasPreviousJump', 'nextJump', 'hasNextJump', 'previousJump', 'hasPreviousJump', pageJump);
}, this);
installActions('change:hasNext change:hasPrevious', 'next', 'hasNext', 'previous', 'hasPrevious', 1);
buildPages.apply(this);
this.trigger('change:pageJump');
return Pagination.__super__.initialize.apply(this, arguments);
}
});
return Pagination;
});
define(function(require) {
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var Pagination = require('solr/model/Pagination');
describe('Pagination', function() {
it('loads', function() {
expect(Pagination).toBeDefined();
});
describe(':pages', function() {
it('default value is not shared globally', function() {
var p1 = new Pagination(), p2 = new Pagination();
expect(p1.get('pages')).not.toBe(p2.get('pages'));
});
});
describe('singleton', function() {
var p;
function checkHas(hasNext, hasNextJump, hasPrevious, hasPreviousJump) {
var wanted = {
hasNext: hasNext,
hasNextJump: hasNextJump,
hasPrevious: hasPrevious,
hasPreviousJump: hasPreviousJump,
};
it('has', function() {
var got = {
hasNext: p.get('hasNext'),
hasNextJump: p.get('hasNextJump'),
hasPrevious: p.get('hasPrevious'),
hasPreviousJump: p.get('hasPreviousJump'),
};
expect(got).toEqual(wanted);
});
}
function checkPages(wantedLength, offset, currentPage) {
var pages;
beforeEach(function() {
pages = p.get('pages');
});
//console.log(JSON.stringify(pages));
var wanted = { length: wantedLength, pages: [] };
for (var i = 0; i < wantedLength; i++) {
wanted.pages[i] = { current: i + 1 === currentPage, number: i + 1 + offset };
}
it('pages', function() {
pages = p.get('pages');
var got = { length: pages.length, pages: [] };
for (var i = 0; i < pages.length; i++) {
var page = pages[i];
got.pages[i] = { current: page.current, number: page.number };
}
expect(got).toEqual(wanted);
});
}
beforeEach(function() {
p = new Pagination();
});
describe('default settings', function() {
checkHas(false, undefined, false, undefined);
checkPages(0, 0, 1);
});
describe('35 items', function() {
beforeEach(function() {
p.set({totalCount:35});
});
describe('initial settings', function() {
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
describe('clamping', function() {
describe('set currentPage=5', function() {
beforeEach(function() {
p.set({currentPage: 5});
});
checkHas(false, false, true, false);
checkPages(4, 0, 4);
});
describe('set currentPage=-1', function() {
beforeEach(function() {
p.set({currentPage: -1});
});
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
});
describe('next[1]', function() {
beforeEach(function() {
p.next();
});
checkPages(4, 0, 2);
describe('previous', function() {
beforeEach(function() {
p.previous();
});
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
describe('next[2]', function() {
beforeEach(function() {
p.next();
p.next();
});
checkHas(false, false, true, false);
checkPages(4, 0, 4);
describe('next[1] - clamp', function() {
beforeEach(function() {
p.next();
});
checkHas(false, false, true, false);
checkPages(4, 0, 4);
});
describe('page[0].jump', function() {
beforeEach(function() {
p.get('pages')[0].jump();
});
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
describe('page[3].jump', function() {
beforeEach(function() {
p.get('pages')[3].jump();
});
checkHas(false, false, true, false);
checkPages(4, 0, 4);
});
describe('decrease to 25 items', function() {
beforeEach(function() {
p.set({totalCount:25});
});
checkHas(false, false, true, false);
checkPages(3, 0, 3);
describe('increase to 150 items', function() {
beforeEach(function() {
p.set({totalCount:150});
});
checkHas(true, true, true, false);
checkPages(9, 0, 3);
});
});
});
});
describe('previous[1]', function() {
beforeEach(function() {
p.previous();
});
checkHas(true, false, false, false);
checkPages(4, 0, 1);
});
});
describe('150 items', function() {
beforeEach(function() {
p.set({totalCount:150});
});
checkHas(true, true, false, false);
checkPages(9, 0, 1);
describe('page[7].jump', function() {
beforeEach(function() {
p.get('pages')[7].jump();
});
checkHas(true, true, true, true);
checkPages(9, 3, 5);
describe('previousJump', function() {
beforeEach(function() {
p.previousJump();
});
checkHas(true, true, true, false);
checkPages(9, 0, 3);
});
});
});
describe('no page jump', function() {
beforeEach(function() {
p.set({pageJump: false});
p.set({totalCount:150});
});
checkHas(true, false, false, false);
checkPages(9, 0, 1);
});
});
});
});
......@@ -5,7 +5,6 @@ define(function(require) {
var Facets = require('solr/model/Facets');
var Ordering = require('solr/model/Ordering');
var Pagination = require('solr/model/Pagination');
var QueryTextField = require('solr/model/QueryTextField');
var Util = require('solr/model/Util');
......@@ -18,7 +17,7 @@ define(function(require) {
_.invoke(this.values(), 'resetParameters', options);
},
});
var Solr = Pagination.extend({
var Solr = Backbone.Model.extend({
url: function url() {
return this.constructor.selectUrl;
},
......@@ -41,7 +40,10 @@ define(function(require) {
}
queryFields.set(queryName, qtf);
}, this);
return _.extend(_.result(Solr.__super__, 'defaults'), {
return {
currentPage: 1,
pageSize: 10,
totalCount: 0,
initializing: true,
initialized: false,
query: '',
......@@ -50,7 +52,7 @@ define(function(require) {
ordering: new Ordering({items: constructor.orderingItems}, {parse: true}),
facets: facets,
queryFields: queryFields,
});
};
},
applyQueryParameters: function() {
var skipOptions = {skipSearch: true};
......@@ -405,6 +407,5 @@ define(function(require) {
},
});
Solr.Facets = Facets;
Solr.Pagination = Pagination;
return Solr;
});
......
......@@ -14,17 +14,7 @@ define(function(require) {
return result;
}
function preventDefault(e) {
if (e) {
e.preventDefault();
}
}
return {
preventDefault: preventDefault,
stepFalse: function stepFalse(e) {
preventDefault(e);
return false;
},
getField: function getField(obj, key) {
return obj[key];
},
......
......@@ -33,8 +33,6 @@ define(function(require) {
keys[key] = true;
});
expect(keys).toEqual({
preventDefault: true,
stepFalse: true,
getField: true,
setField: true,
getItemKeyAccessor: true,
......@@ -43,42 +41,11 @@ define(function(require) {
});
});
describe('methods', function() {
var e, pojo, bb, emptyBB;
var pojo, bb, emptyBB;
beforeEach(function() {
pojo = {a: 1, b: 2, key: 'TheKey'};
bb = new BaseModel(pojo);
emptyBB = new SubModel();
e = {preventDefault: jasmine.createSpy('preventDefault')};
});
it('preventDefault(null)', function() {
var r = Util.preventDefault(null);
expect(r).toBe(undefined);
expect(e.preventDefault).not.toHaveBeenCalled();
});
it('preventDefault(undefined)', function() {
var r = Util.preventDefault(undefined);
expect(r).toBe(undefined);
expect(e.preventDefault).not.toHaveBeenCalled();
});
it('preventDefault(e)', function() {
var r = Util.preventDefault(e);
expect(r).toBe(undefined);
expect(e.preventDefault).toHaveBeenCalled();
});
it('stepFalse(null)', function() {
var r = Util.stepFalse(null);
expect(r).toBe(false);
expect(e.preventDefault).not.toHaveBeenCalled();
});
it('stepFalse(undefined)', function() {
var r = Util.stepFalse(undefined);
expect(r).toBe(false);
expect(e.preventDefault).not.toHaveBeenCalled();
});
it('preventDefault(e)', function() {
var r = Util.stepFalse(e);
expect(r).toBe(false);
expect(e.preventDefault).toHaveBeenCalled();
});
it('setField only modifies given', function() {
var r = Util.setField(pojo, 'a', 100);
......