fix memory leaks for pdf viewers (#2316)

This commit is contained in:
Denys Vuika
2017-09-11 15:48:53 +01:00
committed by Eugenio Romano
parent b93cdc611c
commit c418e428f4
2 changed files with 35 additions and 24 deletions

View File

@@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { Component, HostListener, Input, OnChanges, ViewEncapsulation } from '@angular/core'; import { Component, HostListener, Input, OnChanges, OnDestroy, ViewEncapsulation } from '@angular/core';
import { LogService } from 'ng2-alfresco-core'; import { LogService } from 'ng2-alfresco-core';
import { RenderingQueueServices } from '../services/rendering-queue.services'; import { RenderingQueueServices } from '../services/rendering-queue.services';
@@ -32,7 +32,7 @@ declare let PDFJS: any;
host: { 'class': 'adf-pdf-viewer' }, host: { 'class': 'adf-pdf-viewer' },
encapsulation: ViewEncapsulation.None encapsulation: ViewEncapsulation.None
}) })
export class PdfViewerComponent implements OnChanges { export class PdfViewerComponent implements OnChanges, OnDestroy {
@Input() @Input()
urlFile: string; urlFile: string;
@@ -62,6 +62,8 @@ export class PdfViewerComponent implements OnChanges {
constructor(private renderingQueueServices: RenderingQueueServices, constructor(private renderingQueueServices: RenderingQueueServices,
private logService: LogService) { private logService: LogService) {
// needed to preserve "this" context when setting as a global document event listener
this.onDocumentScroll = this.onDocumentScroll.bind(this);
} }
ngOnChanges(changes) { ngOnChanges(changes) {
@@ -127,9 +129,7 @@ export class PdfViewerComponent implements OnChanges {
let documentContainer = document.getElementById('viewer-pdf-container'); let documentContainer = document.getElementById('viewer-pdf-container');
let viewer: any = document.getElementById('viewer-viewerPdf'); let viewer: any = document.getElementById('viewer-viewerPdf');
window.document.addEventListener('scroll', (event) => { window.document.addEventListener('scroll', this.onDocumentScroll, true);
this.watchScroll(event.target);
}, true);
this.pdfViewer = new PDFJS.PDFViewer({ this.pdfViewer = new PDFJS.PDFViewer({
container: documentContainer, container: documentContainer,
@@ -142,6 +142,10 @@ export class PdfViewerComponent implements OnChanges {
this.pdfViewer.setDocument(pdfDocument); this.pdfViewer.setDocument(pdfDocument);
} }
ngOnDestroy() {
window.document.removeEventListener('scroll', this.onDocumentScroll, true);
}
/** /**
* Method to scale the page current support implementation * Method to scale the page current support implementation
* *
@@ -397,4 +401,10 @@ export class PdfViewerComponent implements OnChanges {
} }
} }
onDocumentScroll(event: Event) {
if (event && event.target) {
this.watchScroll(event.target);
}
}
} }

View File

@@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { Component, HostListener, Input, OnInit, ViewEncapsulation } from '@angular/core'; import { Component, HostListener, Input, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
import { LogService } from 'ng2-alfresco-core'; import { LogService } from 'ng2-alfresco-core';
import { RenderingQueueServices } from '../../../services/rendering-queue.services'; import { RenderingQueueServices } from '../../../services/rendering-queue.services';
@@ -29,7 +29,7 @@ declare let PDFJS: any;
host: { 'class': 'adf-pdf-view' }, host: { 'class': 'adf-pdf-view' },
encapsulation: ViewEncapsulation.None encapsulation: ViewEncapsulation.None
}) })
export class PdfViewComponent implements OnInit { export class PdfViewComponent implements OnInit, OnDestroy {
@Input() @Input()
fileUrl: string; fileUrl: string;
@@ -53,6 +53,8 @@ export class PdfViewComponent implements OnInit {
constructor(private renderingQueueServices: RenderingQueueServices, constructor(private renderingQueueServices: RenderingQueueServices,
private logService: LogService) { private logService: LogService) {
// needed to preserve "this" context when setting as a global document event listener
this.onDocumentScroll = this.onDocumentScroll.bind(this);
} }
ngOnInit() { ngOnInit() {
@@ -61,6 +63,10 @@ export class PdfViewComponent implements OnInit {
} }
} }
ngOnDestroy() {
window.document.removeEventListener('scroll', this.onDocumentScroll, true);
}
private render(src) { private render(src) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.isLoading = true; this.isLoading = true;
@@ -113,9 +119,7 @@ export class PdfViewComponent implements OnInit {
let documentContainer = document.getElementById('viewer-pdf-container'); let documentContainer = document.getElementById('viewer-pdf-container');
let viewer: any = document.getElementById('viewer-viewerPdf'); let viewer: any = document.getElementById('viewer-viewerPdf');
window.document.addEventListener('scroll', (event) => { window.document.addEventListener('scroll', this.onDocumentScroll, true);
this.watchScroll(event.target);
}, true);
this.pdfViewer = new PDFJS.PDFViewer({ this.pdfViewer = new PDFJS.PDFViewer({
container: documentContainer, container: documentContainer,
@@ -128,6 +132,17 @@ export class PdfViewComponent implements OnInit {
this.pdfViewer.setDocument(pdfDocument); this.pdfViewer.setDocument(pdfDocument);
} }
private onDocumentScroll(event: Event) {
if (event && event.target) {
const outputPage = this.getVisibleElement(event.target);
if (outputPage) {
this.page = outputPage.id;
this.displayPage = this.page;
}
}
}
/** /**
* Method to scale the page current support implementation * Method to scale the page current support implementation
* *
@@ -324,20 +339,6 @@ export class PdfViewComponent implements OnInit {
} }
} }
/**
* Litener Scroll Event
*
* @param {any} target
*/
watchScroll(target) {
let outputPage = this.getVisibleElement(target);
if (outputPage) {
this.page = outputPage.id;
this.displayPage = this.page;
}
}
/** /**
* find out what elements are visible within a scroll pane * find out what elements are visible within a scroll pane
* *