Skip to content

Commit

Permalink
Separate utility methods from core
Browse files Browse the repository at this point in the history
The EventBus creation shouldn't be a responsibility of the pdf-viewer, so
it was moved to a utility file.
When pdfjs exports their methods contained in 'pdf.js/web/dom_events.js'
these utility classes can be removed in favor of them.
  • Loading branch information
AntonioDA committed Oct 12, 2018
1 parent f70dc84 commit 7faff03
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 118 deletions.
122 changes: 4 additions & 118 deletions src/app/pdf-viewer/pdf-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
} from '@angular/core';
import {PDFDocumentProxy, PDFViewerParams, PDFPageProxy, PDFSource, PDFProgressData, PDFPromise} from 'pdfjs-dist';

import {createEventBus} from '../utils/event-bus-utils';

let PDFJS: any;
let PDFJSViewer: any;

Expand Down Expand Up @@ -294,11 +296,8 @@ export class PdfViewerComponent implements OnChanges, OnInit, OnDestroy {

this.pdfMultiPageLinkService = new PDFJSViewer.PDFLinkService();

const globalEventBus = new PDFJSViewer.EventBus(true);
this.attachDOMEventsToEventBus(globalEventBus);

const pdfOptions: PDFViewerParams | any = {
eventBus: globalEventBus,
eventBus: createEventBus(PDFJSViewer),
container: this.element.nativeElement.querySelector('div'),
removePageBorders: true,
linkService: this.pdfMultiPageLinkService,
Expand All @@ -318,11 +317,8 @@ export class PdfViewerComponent implements OnChanges, OnInit, OnDestroy {

this.pdfSinglePageLinkService = new PDFJSViewer.PDFLinkService();

const globalEventBus = new PDFJSViewer.EventBus(true);
this.attachDOMEventsToEventBus(globalEventBus);

const pdfOptions: PDFViewerParams | any = {
eventBus: globalEventBus,
eventBus: createEventBus(PDFJSViewer),
container: this.element.nativeElement.querySelector('div'),
removePageBorders: true,
linkService: this.pdfSinglePageLinkService,
Expand Down Expand Up @@ -471,114 +467,4 @@ export class PdfViewerComponent implements OnChanges, OnInit, OnDestroy {

}

private attachDOMEventsToEventBus(eventBus: any) {
eventBus.on('documentload', function () {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('documentload', true, true, {});
window.dispatchEvent(event);
});
eventBus.on('pagerendered', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('pagerendered', true, true, {
pageNumber: evt.pageNumber,
cssTransform: evt.cssTransform,
});
evt.source.div.dispatchEvent(event);
});
eventBus.on('textlayerrendered', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('textlayerrendered', true, true, {
pageNumber: evt.pageNumber,
});
evt.source.textLayerDiv.dispatchEvent(event);
});
eventBus.on('pagechange', function (evt) {
const event = document.createEvent('UIEvents');
event.initUIEvent('pagechange', true, true, window, 0);
event['pageNumber'] = evt.pageNumber;
evt.source.container.dispatchEvent(event);
});
eventBus.on('pagesinit', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('pagesinit', true, true, null);
evt.source.container.dispatchEvent(event);
});
eventBus.on('pagesloaded', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('pagesloaded', true, true, {
pagesCount: evt.pagesCount,
});
evt.source.container.dispatchEvent(event);
});
eventBus.on('scalechange', function (evt) {
const event = document.createEvent('UIEvents');
event.initUIEvent('scalechange', true, true, window, 0);
event['scale'] = evt.scale;
event['presetValue'] = evt.presetValue;
evt.source.container.dispatchEvent(event);
});
eventBus.on('updateviewarea', function (evt) {
const event = document.createEvent('UIEvents');
event.initUIEvent('updateviewarea', true, true, window, 0);
event['location'] = evt.location;
evt.source.container.dispatchEvent(event);
});
eventBus.on('find', function (evt) {
if (evt.source === window) {
return; // event comes from FirefoxCom, no need to replicate
}
const event = document.createEvent('CustomEvent');
event.initCustomEvent('find' + evt.type, true, true, {
query: evt.query,
phraseSearch: evt.phraseSearch,
caseSensitive: evt.caseSensitive,
highlightAll: evt.highlightAll,
findPrevious: evt.findPrevious,
});
window.dispatchEvent(event);
});
eventBus.on('attachmentsloaded', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('attachmentsloaded', true, true, {
attachmentsCount: evt.attachmentsCount,
});
evt.source.container.dispatchEvent(event);
});
eventBus.on('sidebarviewchanged', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('sidebarviewchanged', true, true, {
view: evt.view,
});
evt.source.outerContainer.dispatchEvent(event);
});
eventBus.on('pagemode', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('pagemode', true, true, {
mode: evt.mode,
});
evt.source.pdfViewer.container.dispatchEvent(event);
});
eventBus.on('namedaction', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('namedaction', true, true, {
action: evt.action,
});
evt.source.pdfViewer.container.dispatchEvent(event);
});
eventBus.on('presentationmodechanged', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('presentationmodechanged', true, true, {
active: evt.active,
switchInProgress: evt.switchInProgress,
});
window.dispatchEvent(event);
});
eventBus.on('outlineloaded', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('outlineloaded', true, true, {
outlineCount: evt.outlineCount,
});
evt.source.container.dispatchEvent(event);
});
}
}
121 changes: 121 additions & 0 deletions src/app/utils/event-bus-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
function _createEventBus(pdfJsViewer: any): any {

const globalEventBus = new pdfJsViewer.EventBus(true);
attachDOMEventsToEventBus(globalEventBus);

return globalEventBus;
}


function attachDOMEventsToEventBus(eventBus: any) {
eventBus.on('documentload', function () {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('documentload', true, true, {});
window.dispatchEvent(event);
});
eventBus.on('pagerendered', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('pagerendered', true, true, {
pageNumber: evt.pageNumber,
cssTransform: evt.cssTransform,
});
evt.source.div.dispatchEvent(event);
});
eventBus.on('textlayerrendered', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('textlayerrendered', true, true, {
pageNumber: evt.pageNumber,
});
evt.source.textLayerDiv.dispatchEvent(event);
});
eventBus.on('pagechange', function (evt) {
const event = document.createEvent('UIEvents');
event.initUIEvent('pagechange', true, true, window, 0);
event['pageNumber'] = evt.pageNumber;
evt.source.container.dispatchEvent(event);
});
eventBus.on('pagesinit', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('pagesinit', true, true, null);
evt.source.container.dispatchEvent(event);
});
eventBus.on('pagesloaded', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('pagesloaded', true, true, {
pagesCount: evt.pagesCount,
});
evt.source.container.dispatchEvent(event);
});
eventBus.on('scalechange', function (evt) {
const event = document.createEvent('UIEvents');
event.initUIEvent('scalechange', true, true, window, 0);
event['scale'] = evt.scale;
event['presetValue'] = evt.presetValue;
evt.source.container.dispatchEvent(event);
});
eventBus.on('updateviewarea', function (evt) {
const event = document.createEvent('UIEvents');
event.initUIEvent('updateviewarea', true, true, window, 0);
event['location'] = evt.location;
evt.source.container.dispatchEvent(event);
});
eventBus.on('find', function (evt) {
if (evt.source === window) {
return; // event comes from FirefoxCom, no need to replicate
}
const event = document.createEvent('CustomEvent');
event.initCustomEvent('find' + evt.type, true, true, {
query: evt.query,
phraseSearch: evt.phraseSearch,
caseSensitive: evt.caseSensitive,
highlightAll: evt.highlightAll,
findPrevious: evt.findPrevious,
});
window.dispatchEvent(event);
});
eventBus.on('attachmentsloaded', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('attachmentsloaded', true, true, {
attachmentsCount: evt.attachmentsCount,
});
evt.source.container.dispatchEvent(event);
});
eventBus.on('sidebarviewchanged', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('sidebarviewchanged', true, true, {
view: evt.view,
});
evt.source.outerContainer.dispatchEvent(event);
});
eventBus.on('pagemode', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('pagemode', true, true, {
mode: evt.mode,
});
evt.source.pdfViewer.container.dispatchEvent(event);
});
eventBus.on('namedaction', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('namedaction', true, true, {
action: evt.action,
});
evt.source.pdfViewer.container.dispatchEvent(event);
});
eventBus.on('presentationmodechanged', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('presentationmodechanged', true, true, {
active: evt.active,
switchInProgress: evt.switchInProgress,
});
window.dispatchEvent(event);
});
eventBus.on('outlineloaded', function (evt) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('outlineloaded', true, true, {
outlineCount: evt.outlineCount,
});
evt.source.container.dispatchEvent(event);
});
}

export const createEventBus = _createEventBus;

0 comments on commit 7faff03

Please sign in to comment.