Skip to content

Commit 9f50643

Browse files
author
IA Jim
committed
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
1 parent 7c7357e commit 9f50643

File tree

4 files changed

+73
-17
lines changed

4 files changed

+73
-17
lines changed

src/BookReader/Navbar/Navbar.js

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,36 @@ export class Navbar {
142142
if (this.br.options.bookType !== 'linerNotes') {
143143
if (this.br.refs.$brContainer.prop('clientWidth') < 640) {
144144
this.showMinimumNavbarControls();
145+
this.showMinimumNavPageNum();
145146
} else {
146147
this.showMaximumNavbarControls();
148+
this.showMaximumNavPageNum();
147149
}
148150
}
149151
}
150152

153+
/**
154+
* Switch Book Nav page number display to minimum/mobile
155+
*/
156+
showMinimumNavPageNum() {
157+
const minelement = document.querySelector(`.BRcurrentpage.BRmin`);
158+
const maxelement = document.querySelector(`.BRcurrentpage.BRmax`);
159+
160+
if (minelement) minelement.classList.remove('hide');
161+
if (maxelement) maxelement.classList.add('hide');
162+
}
163+
164+
/**
165+
* Switch Book Nav page number display to maximum/desktop
166+
*/
167+
showMaximumNavPageNum() {
168+
const minelement = document.querySelector(`.BRcurrentpage.BRmin`);
169+
const maxelement = document.querySelector(`.BRcurrentpage.BRmax`);
170+
171+
if (minelement) minelement.classList.add('hide');
172+
if (maxelement) maxelement.classList.remove('hide');
173+
}
174+
151175
/**
152176
* Switch Book Navbar controls to minimised
153177
* NOTE: only `this.minimumControls` and `this.maximumControls` switch on resize
@@ -203,7 +227,10 @@ export class Navbar {
203227
<div class="BRpager"></div>
204228
<div class="BRnavline"></div>
205229
</div>
206-
<p><span class='BRcurrentpage'></span></p>
230+
<p>
231+
<span class="BRcurrentpage BRmax"></span>
232+
<span class="BRcurrentpage BRmin"></span>
233+
</p>
207234
</li>
208235
${this._renderControls()}
209236
</ul>
@@ -246,9 +273,10 @@ export class Navbar {
246273
/**
247274
* Returns the textual representation of the current page for the navbar
248275
* @param {number} index
276+
* @param {"max"|"min"} size
249277
* @return {string}
250278
*/
251-
getNavPageNumString(index) {
279+
getNavPageNumString(index, size) {
252280
const { br } = this;
253281
// Accessible index starts at 0 (alas) so we add 1 to make human
254282
const pageNum = br.book.getPageNum(index);
@@ -268,15 +296,17 @@ export class Navbar {
268296
this.maxPageNum = maxPageNum;
269297
}
270298

271-
return getNavPageNumHtml(index, numLeafs, pageNum, pageType, this.maxPageNum);
299+
return getNavPageNumHtml(index, numLeafs, pageNum, pageType, this.maxPageNum, size);
300+
272301
}
273302

274303
/**
275304
* Renders the navbar string to the DOM
276305
* @param {number} index
277306
*/
278307
updateNavPageNum(index) {
279-
this.$root.find('.BRcurrentpage').html(this.getNavPageNumString(index));
308+
this.$root.find('.BRcurrentpage.BRmax').html(this.getNavPageNumString(index,'max'));
309+
this.$root.find('.BRcurrentpage.BRmin').html(this.getNavPageNumString(index,'min'));
280310
}
281311

282312
/**
@@ -298,16 +328,28 @@ export class Navbar {
298328
* @param {number|string} pageNum
299329
* @param {*} pageType - Deprecated
300330
* @param {number} maxPageNum
331+
* @param {"max"|"min"} size
301332
* @return {string}
302333
*/
303-
export function getNavPageNumHtml(index, numLeafs, pageNum, pageType, maxPageNum) {
334+
export function getNavPageNumHtml(index, numLeafs, pageNum, pageType, maxPageNum, size) {
304335
const pageIsAsserted = pageNum[0] != 'n';
336+
const pageIndex = index + 1;
305337

306338
if (!pageIsAsserted) {
307-
const pageIndex = index + 1;
308-
return `(${pageIndex} of ${numLeafs})`; // Page (8 of 10)
339+
pageNum = '—';
340+
}
341+
342+
if (size === "max") {
343+
return `Page ${pageNum} (${pageIndex}/${numLeafs})`;
309344
}
310345

311346
const bookLengthLabel = (maxPageNum && parseFloat(pageNum)) ? ` of ${maxPageNum}` : '';
312-
return `${pageNum}${bookLengthLabel}`;
347+
348+
if (size === "min" && !pageIsAsserted) {
349+
return `(${pageIndex} of ${numLeafs})`;
350+
}
351+
352+
if (size === "min" && pageIsAsserted) {
353+
return `${pageNum}${bookLengthLabel}`;
354+
}
313355
}

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: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@ import BookReader from '@/src/BookReader.js';
44

55
describe('getNavPageNumHtml', () => {
66
const f = getNavPageNumHtml;
7-
test('handle n-prefixed page numbers', () => {
8-
expect(f(3, 40, 'n3', '', 40)).toBe('(4 of 40)');
7+
8+
test('handle n-prefixed page numbers-min format', () => {
9+
expect(f(3, 40, 'n3', '', 40, 'min')).toBe('(4 of 40)');
10+
});
11+
12+
test('handle regular page numbers-min format', () => {
13+
expect(f(3, 40, '14', '', 40, 'min')).toBe('14 of 40');
14+
});
15+
16+
test('handle no max page-min format', () => {
17+
expect(f(3, 40, '14', '', null, 'min')).toBe('14');
18+
});
19+
20+
test('handle n-prefixed page numbers-max format', () => {
21+
expect(f(3, 40, 'n3', '', 40, 'max')).toBe('Page — (4/40)');
922
});
1023

11-
test('handle regular page numbers', () => {
12-
expect(f(3, 40, '14', '', 40)).toBe('14 of 40');
24+
test('handle regular page numbers-max format', () => {
25+
expect(f(3, 40, '14', '', 40, 'max')).toBe('Page 14 (4/40)');
1326
});
1427

15-
test('handle no max page', () => {
16-
expect(f(3, 40, '14', '', null)).toBe('14');
28+
test('handle no max page-max format', () => {
29+
expect(f(3, 40, '14', '', null, 'max')).toBe('Page 14 (4/40)');
1730
});
1831
});
1932

0 commit comments

Comments
 (0)