Skip to content
Snippets Groups Projects
Commit 2a89c672 authored by Chris Beer's avatar Chris Beer
Browse files

Support facing-pages and non-paged viewing hints

parent 9e576d47
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
"sort-keys": ["error", "asc", { "sort-keys": ["error", "asc", {
"caseSensitive": false, "caseSensitive": false,
"natural": false "natural": false
}] }],
"no-underscore-dangle": "off",
} }
} }
...@@ -48,10 +48,21 @@ describe('CanvasGroupings', () => { ...@@ -48,10 +48,21 @@ describe('CanvasGroupings', () => {
beforeEach(() => { beforeEach(() => {
subject = new CanvasGroupings([0, 1, 2, 3], 'book'); subject = new CanvasGroupings([0, 1, 2, 3], 'book');
}); });
it('selects by index / 2', () => { it('selects by index', () => {
expect(subject.getCanvases(2)).toEqual([1, 2]); expect(subject.getCanvases(2)).toEqual([1, 2]);
}); });
}); });
describe('book with facing pages', () => {
let subject;
beforeEach(() => {
subject = new CanvasGroupings([0, 1, { getViewingHint: () => 'facing-pages', id: 2 }, 3], 'book');
});
it('selects by index', () => {
expect(subject.getCanvases(1)).toEqual([1]);
expect(subject.getCanvases(2)[0].id).toEqual(2);
expect(subject.getCanvases(3)).toEqual([3]);
});
});
describe('gallery', () => { describe('gallery', () => {
it('selects by index', () => { it('selects by index', () => {
const subject = new CanvasGroupings([0, 1, 2, 3]); const subject = new CanvasGroupings([0, 1, 2, 3]);
......
...@@ -7,15 +7,18 @@ export default class CanvasGroupings { ...@@ -7,15 +7,18 @@ export default class CanvasGroupings {
constructor(canvases, viewType = 'single') { constructor(canvases, viewType = 'single') {
this.canvases = canvases; this.canvases = canvases;
this.viewType = viewType; this.viewType = viewType;
this._groupings = null; // eslint-disable-line no-underscore-dangle this._groupings = null;
this._canvasGroupingMap = null;
} }
/** /**
*/ */
getCanvases(index) { getCanvases(index) {
switch (this.viewType) { switch (this.viewType) {
case 'scroll':
return this.groupings()[0];
case 'book': case 'book':
return this.groupings()[Math.ceil(index / 2)]; return this.groupings()[this._canvasGroupingMap[index]];
default: default:
return this.groupings()[index]; return this.groupings()[index];
} }
...@@ -26,27 +29,55 @@ export default class CanvasGroupings { ...@@ -26,27 +29,55 @@ export default class CanvasGroupings {
* of canvases, while book view creates pairs. * of canvases, while book view creates pairs.
*/ */
groupings() { groupings() {
if (this._groupings) { // eslint-disable-line no-underscore-dangle if (this._groupings) {
return this._groupings; // eslint-disable-line no-underscore-dangle return this._groupings;
} }
if (this.viewType !== 'book') { if (this.viewType !== 'book') {
return this.canvases.map(canvas => [canvas]); return this.canvases.map(canvas => [canvas]);
} }
const groupings = []; const groupings = [];
const canvasGroupingMap = [];
let groupIndex = 0;
this.canvases.forEach((canvas, i) => { this.canvases.forEach((canvas, i) => {
if (!groupings[groupIndex]) {
groupings[groupIndex] = [];
}
canvasGroupingMap[i] = groupIndex;
if (i === 0) { if (i === 0) {
groupings.push([canvas]); canvasGroupingMap[i] = groupIndex;
groupings[groupIndex].push(canvas);
groupIndex += 1;
return;
}
const hint = canvas && (
(canvas.getBehavior && canvas.getBehavior())
|| (canvas.getViewingHint && canvas.getViewingHint())
);
if (hint === 'facing-pages' || hint === 'non-paged') {
if (groupings[groupIndex].length > 0) {
groupIndex += 1;
canvasGroupingMap[i] = groupIndex;
}
groupings[groupIndex] = [canvas];
groupIndex += 1;
return; return;
} }
// Odd page
if (i % 2 !== 0) { if (groupings[groupIndex].length < 2) {
groupings.push([canvas]); groupings[groupIndex].push(canvas);
} else { }
// Even page
groupings[Math.ceil(i / 2)].push(canvas); if (groupings[groupIndex].length === 2) {
groupIndex += 1;
} }
}); });
this._groupings = groupings; // eslint-disable-line no-underscore-dangle this._groupings = groupings;
this._canvasGroupingMap = canvasGroupingMap;
return groupings; return groupings;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment