Skip to content

Commit 5c9f6d8

Browse files
pezviIA Jim
andauthored
Switch to new page number display format (#1366)
* Switch to new page number display format - Split desktop and mobile page number displays - CSS: Move 'hide' to .controls - Add/split tests for desktop and mobile page number displays * default to false for useMaxFormat everywhere * Fix for liner notes --------- Co-authored-by: IA Jim <jshelton@archive.org>
1 parent 3548feb commit 5c9f6d8

File tree

4 files changed

+73
-17
lines changed

4 files changed

+73
-17
lines changed

src/BookReader/Navbar/Navbar.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,43 @@ export class Navbar {
138138
* Switch navbar controls on mobile and desktop
139139
*/
140140
switchNavbarControls() {
141-
// we don't want navbar controls switching with liner-notes
142-
if (this.br.options.bookType !== 'linerNotes') {
143-
if (this.br.refs.$brContainer.prop('clientWidth') < 640) {
141+
if (this.br.refs.$brContainer.prop('clientWidth') < 640) {
142+
this.showMinimumNavPageNum();
143+
// we don't want navbar controls switching with liner-notes
144+
if (this.br.options.bookType !== 'linerNotes') {
144145
this.showMinimumNavbarControls();
145-
} else {
146+
}
147+
} else {
148+
this.showMaximumNavPageNum();
149+
// we don't want navbar controls switching with liner-notes
150+
if (this.br.options.bookType !== 'linerNotes') {
146151
this.showMaximumNavbarControls();
147152
}
148153
}
149154
}
150155

156+
/**
157+
* Switch Book Nav page number display to minimum/mobile
158+
*/
159+
showMinimumNavPageNum() {
160+
const minElement = document.querySelector('.BRcurrentpage.BRmin');
161+
const maxElement = document.querySelector('.BRcurrentpage.BRmax');
162+
163+
if (minElement) minElement.classList.remove('hide');
164+
if (maxElement) maxElement.classList.add('hide');
165+
}
166+
167+
/**
168+
* Switch Book Nav page number display to maximum/desktop
169+
*/
170+
showMaximumNavPageNum() {
171+
const minElement = document.querySelector('.BRcurrentpage.BRmin');
172+
const maxElement = document.querySelector('.BRcurrentpage.BRmax');
173+
174+
if (minElement) minElement.classList.add('hide');
175+
if (maxElement) maxElement.classList.remove('hide');
176+
}
177+
151178
/**
152179
* Switch Book Navbar controls to minimised
153180
* NOTE: only `this.minimumControls` and `this.maximumControls` switch on resize
@@ -203,7 +230,10 @@ export class Navbar {
203230
<div class="BRpager"></div>
204231
<div class="BRnavline"></div>
205232
</div>
206-
<p><span class='BRcurrentpage'></span></p>
233+
<p>
234+
<span class="BRcurrentpage BRmax"></span>
235+
<span class="BRcurrentpage BRmin"></span>
236+
</p>
207237
</li>
208238
${this._renderControls()}
209239
</ul>
@@ -246,9 +276,10 @@ export class Navbar {
246276
/**
247277
* Returns the textual representation of the current page for the navbar
248278
* @param {number} index
279+
* @param {boolean} [useMaxFormat = false]
249280
* @return {string}
250281
*/
251-
getNavPageNumString(index) {
282+
getNavPageNumString(index, useMaxFormat = false) {
252283
const { br } = this;
253284
// Accessible index starts at 0 (alas) so we add 1 to make human
254285
const pageNum = br.book.getPageNum(index);
@@ -268,15 +299,17 @@ export class Navbar {
268299
this.maxPageNum = maxPageNum;
269300
}
270301

271-
return getNavPageNumHtml(index, numLeafs, pageNum, pageType, this.maxPageNum);
302+
return getNavPageNumHtml(index, numLeafs, pageNum, pageType, this.maxPageNum, useMaxFormat);
303+
272304
}
273305

274306
/**
275307
* Renders the navbar string to the DOM
276308
* @param {number} index
277309
*/
278310
updateNavPageNum(index) {
279-
this.$root.find('.BRcurrentpage').html(this.getNavPageNumString(index));
311+
this.$root.find('.BRcurrentpage.BRmax').html(this.getNavPageNumString(index, true));
312+
this.$root.find('.BRcurrentpage.BRmin').html(this.getNavPageNumString(index));
280313
}
281314

282315
/**
@@ -298,14 +331,23 @@ export class Navbar {
298331
* @param {number|string} pageNum
299332
* @param {*} pageType - Deprecated
300333
* @param {number} maxPageNum
334+
* @param {boolean} [useMaxFormat = false]
301335
* @return {string}
302336
*/
303-
export function getNavPageNumHtml(index, numLeafs, pageNum, pageType, maxPageNum) {
337+
export function getNavPageNumHtml(index, numLeafs, pageNum, pageType, maxPageNum, useMaxFormat = false) {
304338
const pageIsAsserted = pageNum[0] != 'n';
339+
const pageIndex = index + 1;
340+
341+
if (!pageIsAsserted) {
342+
pageNum = '—';
343+
}
344+
345+
if (useMaxFormat === true) {
346+
return `Page ${pageNum} (${pageIndex}/${numLeafs})`;
347+
}
305348

306349
if (!pageIsAsserted) {
307-
const pageIndex = index + 1;
308-
return `(${pageIndex} of ${numLeafs})`; // Page (8 of 10)
350+
return `(${pageIndex} of ${numLeafs})`;
309351
}
310352

311353
const bookLengthLabel = (maxPageNum && parseFloat(pageNum)) ? ` of ${maxPageNum}` : '';

src/css/_BRnav.scss

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@
110110
display: block;
111111
}
112112
}
113-
&.hide {
114-
display: none;
115-
}
116113
}
117114
}
118115

src/css/_controls.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
height: 30px;
8282
}
8383

84+
.controls .hide {
85+
display: none;
86+
}
87+
8488
@keyframes slideUp {
8589
from {
8690
opacity: 0;

tests/jest/BookReader/Navbar/Navbar.test.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@ import BookReader from '@/src/BookReader.js';
44

55
describe('getNavPageNumHtml', () => {
66
const f = getNavPageNumHtml;
7-
test('handle n-prefixed page numbers', () => {
7+
8+
test('handle n-prefixed page numbers-min format', () => {
89
expect(f(3, 40, 'n3', '', 40)).toBe('(4 of 40)');
910
});
1011

11-
test('handle regular page numbers', () => {
12+
test('handle regular page numbers-min format', () => {
1213
expect(f(3, 40, '14', '', 40)).toBe('14 of 40');
1314
});
1415

15-
test('handle no max page', () => {
16+
test('handle no max page-min format', () => {
1617
expect(f(3, 40, '14', '', null)).toBe('14');
1718
});
19+
20+
test('handle n-prefixed page numbers-max format', () => {
21+
expect(f(3, 40, 'n3', '', 40, true)).toBe('Page — (4/40)');
22+
});
23+
24+
test('handle regular page numbers-max format', () => {
25+
expect(f(3, 40, '14', '', 40, true)).toBe('Page 14 (4/40)');
26+
});
27+
28+
test('handle no max page-max format', () => {
29+
expect(f(3, 40, '14', '', null, true)).toBe('Page 14 (4/40)');
30+
});
1831
});
1932

2033
/** @type {BookReader} */

0 commit comments

Comments
 (0)