Skip to content

Commit

Permalink
#274 reset page properly when out of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimDez committed Feb 10, 2018
1 parent afb07e6 commit 9be555e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
37 changes: 36 additions & 1 deletion src/app/pdf-viewer/pdf-viewer.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestBed, async } from '@angular/core/testing';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { Component } from '@angular/core';

import { PdfViewerComponent } from './pdf-viewer.component';
Expand All @@ -10,6 +10,9 @@ import { PdfViewerModule } from './pdf-viewer.module';
class TestComponent {}

describe('AppComponent', () => {
let pdfViewerFixture: ComponentFixture<PdfViewerComponent>;
let pdfViewer: PdfViewerComponent;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
Expand All @@ -23,7 +26,39 @@ describe('AppComponent', () => {

it('should create test component', async(() => {
const fixture = TestBed.createComponent(TestComponent);
pdfViewerFixture = TestBed.createComponent(PdfViewerComponent);
const app = fixture.debugElement.componentInstance;
pdfViewer = pdfViewerFixture.debugElement.componentInstance;
expect(app).toBeTruthy();
expect(pdfViewer).toBeTruthy();
}));

describe('getValidPageNumber', () => {
function setPdf(numPages: number) {
(pdfViewer as any)._pdf = {
numPages
};
}

it('should return page if between first and last pages', () => {
setPdf(10);

[1, 3, 7, 10].forEach((page: number) => {
expect((pdfViewer as any).getValidPageNumber(page)).toBe(page, `page: ${ page }`);
});
});

it('should return last page', function () {
const pages = 100;
setPdf(pages);
expect((pdfViewer as any).getValidPageNumber(pages + 1)).toBe(pages);
expect((pdfViewer as any).getValidPageNumber(pages + 2)).toBe(pages);
});

it('should return first page when page is less then 1', function () {
setPdf(10);
expect((pdfViewer as any).getValidPageNumber(0)).toBe(1);
expect((pdfViewer as any).getValidPageNumber(-1)).toBe(1);
});
});
});
25 changes: 15 additions & 10 deletions src/app/pdf-viewer/pdf-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class PdfViewerComponent implements OnChanges, OnInit {
@Output('after-load-complete') afterLoadComplete = new EventEmitter<PDFDocumentProxy>();
@Output('error') onError = new EventEmitter<any>();
@Output('on-progress') onProgress = new EventEmitter<PDFProgressData>();
@Output() pageChange: EventEmitter<number> = new EventEmitter<number>(true);

constructor(private element: ElementRef) {
if (!isSSR() && typeof PDFJS.workerSrc !== 'string') {
Expand Down Expand Up @@ -95,18 +96,16 @@ export class PdfViewerComponent implements OnChanges, OnInit {

@Input('page')
set page(_page) {
_page = parseInt(_page, 10);
_page = parseInt(_page, 10) || 1;

if (this._pdf && !this.isValidPageNumber(_page)) {
_page = 1;
if (this._pdf) {
_page = this.getValidPageNumber(_page);
}

this._page = _page;
this.pageChange.emit(_page);
}

@Output() pageChange: EventEmitter<number> = new EventEmitter<number>(true);

@Input('render-text')
set renderText(renderText: boolean) {
this._renderText = renderText;
Expand Down Expand Up @@ -203,8 +202,16 @@ export class PdfViewerComponent implements OnChanges, OnInit {
});
}

private isValidPageNumber(page: number): boolean {
return this._pdf.numPages >= page && page >= 1;
private getValidPageNumber(page: number): number {
if (page < 1) {
return 1;
}

if (page > this._pdf.numPages) {
return this._pdf.numPages;
}

return page;
}

static getLinkTarget(type: string) {
Expand Down Expand Up @@ -289,9 +296,7 @@ export class PdfViewerComponent implements OnChanges, OnInit {
}

private renderMultiplePages() {
if (!this.isValidPageNumber(this._page)) {
this._page = 1;
}
this._page = this.getValidPageNumber(this._page);

if (this._rotation !== 0 || this.pdfViewer.pagesRotation !== this._rotation) {
setTimeout(() => {
Expand Down

0 comments on commit 9be555e

Please sign in to comment.