f625e0de by Adam Heath

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

do pagination itself anymore.
1 parent a0fe18f0
1 { 1 {
2 "name": "solr-frontend", 2 "name": "solr-frontend",
3 "version": "2016.06.24-0", 3 "version": "2016.07.26-0",
4 "authors": [ 4 "authors": [
5 "Adam Heath <adam@brainfood.com>" 5 "Adam Heath <adam@brainfood.com>"
6 ], 6 ],
......
1 { 1 {
2 "name": "solr-frontend", 2 "name": "solr-frontend",
3 "license": "UNLICENSED", 3 "license": "UNLICENSED",
4 "version": "2016.06.24-0", 4 "version": "2016.07.26-0",
5 "description": "Solr Frontend", 5 "description": "Solr Frontend",
6 "repository": { 6 "repository": {
7 "type": "git", 7 "type": "git",
......
1 define(function(require) {
2 'use strict';
3 var Backbone = require('backbone');
4 var Util = require('./Util');
5
6 var Pagination = Backbone.Model.extend({
7 defaults: function defaults() {
8 return {
9 currentPage: 1,
10 hasNext: false,
11 hasNextJump: false,
12 hasPrevious: false,
13 hasPreviousJump: false,
14 nextJumpPage: undefined,
15 nextPage: undefined,
16 pages: [],
17 pageJump: 5,
18 pageSize: 10,
19 previousJumpPage: undefined,
20 previousPage: undefined,
21 totalCount: 0,
22 totalPages: 0,
23 };
24 },
25 initialize: function(data, options) {
26 var buildPages = function buildPages() {
27 var currentPage = parseInt(this.get('currentPage'));
28 var pageSize = parseInt(this.get('pageSize'));
29 var totalCount = parseInt(this.get('totalCount'));
30 if (!totalCount) {
31 return;
32 }
33 var pages = [];
34 var totalPages = Math.floor((totalCount + pageSize - 1) / pageSize);
35 if (currentPage < 0) {
36 this.set('currentPage', 1);
37 return;
38 } else if (currentPage > totalPages) {
39 this.set('currentPage', totalPages);
40 return;
41 }
42 function addPage(self, i) {
43 pages.push({
44 current: i === currentPage,
45 jump: function() {
46 self.set('currentPage', i);
47 return true;
48 },
49 number: i,
50 });
51 }
52 var startAt = currentPage - 4, endAt = currentPage + 5;
53 if (startAt < 1) {
54 endAt += (1 - startAt);
55 }
56 if (endAt > totalPages) {
57 startAt -= endAt - totalPages - 1;
58 endAt = totalPages + 1;
59 }
60 if (startAt < 1) {
61 startAt = 1;
62 }
63 if (endAt - startAt < 9) {
64 ///* global console:false */
65 //console.log('foo');
66 }
67
68 for (var i = startAt; i < endAt; i++) {
69 addPage(this, i);
70 }
71 var hasPrevious = currentPage > 1;
72 var hasNext = currentPage < totalPages;
73 var pageJump = this.get('pageJump');
74 var nextJumpPage, previousJumpPage, hasNextJump, hasPreviousJump;
75 if (pageJump) {
76 nextJumpPage = currentPage + pageJump;
77 previousJumpPage = currentPage - pageJump;
78 hasNextJump = nextJumpPage < totalPages;
79 hasPreviousJump = previousJumpPage > 0;
80 } else {
81 hasNextJump = false;
82 hasPreviousJump = false;
83 }
84 this.set({
85 hasNext: hasNext,
86 hasNextJump: hasNextJump,
87 hasPrevious: hasPrevious,
88 hasPreviousJump: hasPreviousJump,
89 nextJumpPage: hasNextJump ? nextJumpPage : undefined,
90 nextPage: hasNext ? currentPage + 1 : undefined,
91 pages: pages,
92 previousJumpPage: hasNextJump ? previousJumpPage : undefined,
93 previousPage: hasPrevious ? currentPage - 1 : undefined,
94 totalPages: totalPages,
95 });
96 };
97 this.on('change:pageSize change:currentPage change:totalCount', buildPages, this);
98 var installActions = _.bind(function installActions(eventName, nextName, hasNextName, previousName, hasPreviousName, pageCount) {
99 this.on(eventName, function() {
100 var hasNext = this.get(hasNextName);
101 var hasPrevious = this.get(hasPreviousName);
102 var next, previous;
103 if (hasNext) {
104 next = _.bind(function(e) {
105 Util.preventDefault(e);
106 this.set('currentPage', this.get('currentPage') + pageCount);
107 return false;
108 }, this);
109 } else {
110 next = Util.stepFalse;
111 }
112 if (hasPrevious) {
113 previous = _.bind(function(e) {
114 Util.preventDefault(e);
115 this.set('currentPage', this.get('currentPage') - pageCount);
116 return false;
117 }, this);
118 } else {
119 previous = Util.stepFalse;
120 }
121 this.set(nextName, next);
122 this.set(previousName, previous);
123 }, this);
124 this[nextName] = _.bind(function() {
125 return this.get(nextName)();
126 }, this);
127 this[previousName] = _.bind(function() {
128 return this.get(previousName)();
129 }, this);
130 }, this);
131 this.on('change:pageJump', function() {
132 var pageJump = this.get('pageJump');
133 installActions('change:hasNextJump change:hasPreviousJump', 'nextJump', 'hasNextJump', 'previousJump', 'hasPreviousJump', pageJump);
134 }, this);
135 installActions('change:hasNext change:hasPrevious', 'next', 'hasNext', 'previous', 'hasPrevious', 1);
136 buildPages.apply(this);
137 this.trigger('change:pageJump');
138 return Pagination.__super__.initialize.apply(this, arguments);
139 }
140 });
141 return Pagination;
142 });
1 define(function(require) {
2 'use strict';
3 var _ = require('underscore');
4 var Backbone = require('backbone');
5 var Pagination = require('solr/model/Pagination');
6
7 describe('Pagination', function() {
8 it('loads', function() {
9 expect(Pagination).toBeDefined();
10 });
11 describe(':pages', function() {
12 it('default value is not shared globally', function() {
13 var p1 = new Pagination(), p2 = new Pagination();
14 expect(p1.get('pages')).not.toBe(p2.get('pages'));
15 });
16 });
17 describe('singleton', function() {
18 var p;
19
20 function checkHas(hasNext, hasNextJump, hasPrevious, hasPreviousJump) {
21 var wanted = {
22 hasNext: hasNext,
23 hasNextJump: hasNextJump,
24 hasPrevious: hasPrevious,
25 hasPreviousJump: hasPreviousJump,
26 };
27 it('has', function() {
28 var got = {
29 hasNext: p.get('hasNext'),
30 hasNextJump: p.get('hasNextJump'),
31 hasPrevious: p.get('hasPrevious'),
32 hasPreviousJump: p.get('hasPreviousJump'),
33 };
34 expect(got).toEqual(wanted);
35 });
36 }
37 function checkPages(wantedLength, offset, currentPage) {
38 var pages;
39 beforeEach(function() {
40 pages = p.get('pages');
41 });
42 //console.log(JSON.stringify(pages));
43 var wanted = { length: wantedLength, pages: [] };
44 for (var i = 0; i < wantedLength; i++) {
45 wanted.pages[i] = { current: i + 1 === currentPage, number: i + 1 + offset };
46 }
47 it('pages', function() {
48 pages = p.get('pages');
49 var got = { length: pages.length, pages: [] };
50 for (var i = 0; i < pages.length; i++) {
51 var page = pages[i];
52 got.pages[i] = { current: page.current, number: page.number };
53 }
54 expect(got).toEqual(wanted);
55 });
56 }
57
58 beforeEach(function() {
59 p = new Pagination();
60 });
61 describe('default settings', function() {
62 checkHas(false, undefined, false, undefined);
63 checkPages(0, 0, 1);
64 });
65 describe('35 items', function() {
66 beforeEach(function() {
67 p.set({totalCount:35});
68 });
69 describe('initial settings', function() {
70 checkHas(true, false, false, false);
71 checkPages(4, 0, 1);
72 });
73 describe('clamping', function() {
74 describe('set currentPage=5', function() {
75 beforeEach(function() {
76 p.set({currentPage: 5});
77 });
78 checkHas(false, false, true, false);
79 checkPages(4, 0, 4);
80 });
81 describe('set currentPage=-1', function() {
82 beforeEach(function() {
83 p.set({currentPage: -1});
84 });
85 checkHas(true, false, false, false);
86 checkPages(4, 0, 1);
87 });
88 });
89 describe('next[1]', function() {
90 beforeEach(function() {
91 p.next();
92 });
93 checkPages(4, 0, 2);
94 describe('previous', function() {
95 beforeEach(function() {
96 p.previous();
97 });
98 checkHas(true, false, false, false);
99 checkPages(4, 0, 1);
100 });
101 describe('next[2]', function() {
102 beforeEach(function() {
103 p.next();
104 p.next();
105 });
106 checkHas(false, false, true, false);
107 checkPages(4, 0, 4);
108 describe('next[1] - clamp', function() {
109 beforeEach(function() {
110 p.next();
111 });
112 checkHas(false, false, true, false);
113 checkPages(4, 0, 4);
114 });
115 describe('page[0].jump', function() {
116 beforeEach(function() {
117 p.get('pages')[0].jump();
118 });
119 checkHas(true, false, false, false);
120 checkPages(4, 0, 1);
121 });
122 describe('page[3].jump', function() {
123 beforeEach(function() {
124 p.get('pages')[3].jump();
125 });
126 checkHas(false, false, true, false);
127 checkPages(4, 0, 4);
128 });
129 describe('decrease to 25 items', function() {
130 beforeEach(function() {
131 p.set({totalCount:25});
132 });
133 checkHas(false, false, true, false);
134 checkPages(3, 0, 3);
135 describe('increase to 150 items', function() {
136 beforeEach(function() {
137 p.set({totalCount:150});
138 });
139 checkHas(true, true, true, false);
140 checkPages(9, 0, 3);
141 });
142 });
143 });
144 });
145 describe('previous[1]', function() {
146 beforeEach(function() {
147 p.previous();
148 });
149 checkHas(true, false, false, false);
150 checkPages(4, 0, 1);
151 });
152 });
153 describe('150 items', function() {
154 beforeEach(function() {
155 p.set({totalCount:150});
156 });
157 checkHas(true, true, false, false);
158 checkPages(9, 0, 1);
159 describe('page[7].jump', function() {
160 beforeEach(function() {
161 p.get('pages')[7].jump();
162 });
163 checkHas(true, true, true, true);
164 checkPages(9, 3, 5);
165 describe('previousJump', function() {
166 beforeEach(function() {
167 p.previousJump();
168 });
169 checkHas(true, true, true, false);
170 checkPages(9, 0, 3);
171 });
172 });
173 });
174 describe('no page jump', function() {
175 beforeEach(function() {
176 p.set({pageJump: false});
177 p.set({totalCount:150});
178 });
179 checkHas(true, false, false, false);
180 checkPages(9, 0, 1);
181 });
182 });
183 });
184 });
...@@ -5,7 +5,6 @@ define(function(require) { ...@@ -5,7 +5,6 @@ define(function(require) {
5 5
6 var Facets = require('solr/model/Facets'); 6 var Facets = require('solr/model/Facets');
7 var Ordering = require('solr/model/Ordering'); 7 var Ordering = require('solr/model/Ordering');
8 var Pagination = require('solr/model/Pagination');
9 var QueryTextField = require('solr/model/QueryTextField'); 8 var QueryTextField = require('solr/model/QueryTextField');
10 var Util = require('solr/model/Util'); 9 var Util = require('solr/model/Util');
11 10
...@@ -18,7 +17,7 @@ define(function(require) { ...@@ -18,7 +17,7 @@ define(function(require) {
18 _.invoke(this.values(), 'resetParameters', options); 17 _.invoke(this.values(), 'resetParameters', options);
19 }, 18 },
20 }); 19 });
21 var Solr = Pagination.extend({ 20 var Solr = Backbone.Model.extend({
22 url: function url() { 21 url: function url() {
23 return this.constructor.selectUrl; 22 return this.constructor.selectUrl;
24 }, 23 },
...@@ -41,7 +40,10 @@ define(function(require) { ...@@ -41,7 +40,10 @@ define(function(require) {
41 } 40 }
42 queryFields.set(queryName, qtf); 41 queryFields.set(queryName, qtf);
43 }, this); 42 }, this);
44 return _.extend(_.result(Solr.__super__, 'defaults'), { 43 return {
44 currentPage: 1,
45 pageSize: 10,
46 totalCount: 0,
45 initializing: true, 47 initializing: true,
46 initialized: false, 48 initialized: false,
47 query: '', 49 query: '',
...@@ -50,7 +52,7 @@ define(function(require) { ...@@ -50,7 +52,7 @@ define(function(require) {
50 ordering: new Ordering({items: constructor.orderingItems}, {parse: true}), 52 ordering: new Ordering({items: constructor.orderingItems}, {parse: true}),
51 facets: facets, 53 facets: facets,
52 queryFields: queryFields, 54 queryFields: queryFields,
53 }); 55 };
54 }, 56 },
55 applyQueryParameters: function() { 57 applyQueryParameters: function() {
56 var skipOptions = {skipSearch: true}; 58 var skipOptions = {skipSearch: true};
...@@ -405,6 +407,5 @@ define(function(require) { ...@@ -405,6 +407,5 @@ define(function(require) {
405 }, 407 },
406 }); 408 });
407 Solr.Facets = Facets; 409 Solr.Facets = Facets;
408 Solr.Pagination = Pagination;
409 return Solr; 410 return Solr;
410 }); 411 });
......
...@@ -14,17 +14,7 @@ define(function(require) { ...@@ -14,17 +14,7 @@ define(function(require) {
14 return result; 14 return result;
15 } 15 }
16 16
17 function preventDefault(e) {
18 if (e) {
19 e.preventDefault();
20 }
21 }
22 return { 17 return {
23 preventDefault: preventDefault,
24 stepFalse: function stepFalse(e) {
25 preventDefault(e);
26 return false;
27 },
28 getField: function getField(obj, key) { 18 getField: function getField(obj, key) {
29 return obj[key]; 19 return obj[key];
30 }, 20 },
......
...@@ -33,8 +33,6 @@ define(function(require) { ...@@ -33,8 +33,6 @@ define(function(require) {
33 keys[key] = true; 33 keys[key] = true;
34 }); 34 });
35 expect(keys).toEqual({ 35 expect(keys).toEqual({
36 preventDefault: true,
37 stepFalse: true,
38 getField: true, 36 getField: true,
39 setField: true, 37 setField: true,
40 getItemKeyAccessor: true, 38 getItemKeyAccessor: true,
...@@ -43,42 +41,11 @@ define(function(require) { ...@@ -43,42 +41,11 @@ define(function(require) {
43 }); 41 });
44 }); 42 });
45 describe('methods', function() { 43 describe('methods', function() {
46 var e, pojo, bb, emptyBB; 44 var pojo, bb, emptyBB;
47 beforeEach(function() { 45 beforeEach(function() {
48 pojo = {a: 1, b: 2, key: 'TheKey'}; 46 pojo = {a: 1, b: 2, key: 'TheKey'};
49 bb = new BaseModel(pojo); 47 bb = new BaseModel(pojo);
50 emptyBB = new SubModel(); 48 emptyBB = new SubModel();
51 e = {preventDefault: jasmine.createSpy('preventDefault')};
52 });
53 it('preventDefault(null)', function() {
54 var r = Util.preventDefault(null);
55 expect(r).toBe(undefined);
56 expect(e.preventDefault).not.toHaveBeenCalled();
57 });
58 it('preventDefault(undefined)', function() {
59 var r = Util.preventDefault(undefined);
60 expect(r).toBe(undefined);
61 expect(e.preventDefault).not.toHaveBeenCalled();
62 });
63 it('preventDefault(e)', function() {
64 var r = Util.preventDefault(e);
65 expect(r).toBe(undefined);
66 expect(e.preventDefault).toHaveBeenCalled();
67 });
68 it('stepFalse(null)', function() {
69 var r = Util.stepFalse(null);
70 expect(r).toBe(false);
71 expect(e.preventDefault).not.toHaveBeenCalled();
72 });
73 it('stepFalse(undefined)', function() {
74 var r = Util.stepFalse(undefined);
75 expect(r).toBe(false);
76 expect(e.preventDefault).not.toHaveBeenCalled();
77 });
78 it('preventDefault(e)', function() {
79 var r = Util.stepFalse(e);
80 expect(r).toBe(false);
81 expect(e.preventDefault).toHaveBeenCalled();
82 }); 49 });
83 it('setField only modifies given', function() { 50 it('setField only modifies given', function() {
84 var r = Util.setField(pojo, 'a', 100); 51 var r = Util.setField(pojo, 'a', 100);
......