ngx-extended-pdf-viewer
Version:
Embedding PDF files in your Angular application. Highly configurable viewer including the toolbar, sidebar, and all the features you're used to.
1,398 lines (1,391 loc) • 254 kB
JavaScript
import * as deburr from 'lodash.deburr';
import { Component, ViewEncapsulation, Input, Output, EventEmitter, ViewChild, ChangeDetectionStrategy, HostListener, NgZone, Injectable, NgModule, defineInjectable } from '@angular/core';
import { CommonModule } from '@angular/common';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const _isIE11 = !!((/** @type {?} */ (window))).MSInputMethodContext && !!((/** @type {?} */ (document))).documentMode;
/** @type {?} */
const defaultOptions = {
cursorToolOnLoad: 0,
defaultUrl: '',
defaultZoomValue: '',
disableHistory: false,
disablePageLabels: false,
enablePrintAutoRotate: false,
enableWebGL: false,
eventBusDispatchToDOM: false,
externalLinkRel: 'noopener noreferrer nofollow',
externalLinkTarget: 0,
historyUpdateUrl: false,
imageResourcesPath: './images/',
maxCanvasPixels: 16777216,
pdfBugEnabled: false,
renderer: 'canvas',
renderInteractiveForms: false,
sidebarViewOnLoad: -1,
scrollModeOnLoad: -1,
spreadModeOnLoad: -1,
textLayerMode: 1,
useOnlyCssZoom: false,
viewOnLoad: 0,
cMapPacked: true,
cMapUrl: '../web/cmaps/',
disableAutoFetch: false,
disableCreateObjectURL: false,
disableFontFace: false,
disableRange: false,
disableStream: false,
isEvalSupported: true,
maxImageSize: -1,
pdfBug: false,
postMessageTransfers: true,
verbosity: 1,
workerPort: null,
workerSrc: _isIE11 ? './assets/pdf.worker-es5.js' : './assets/pdf.worker.js'
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @param {?} width
* @return {?}
*/
function resizeUpTo535px(width) {
/** @type {?} */
const tinyElement = document.getElementById('scaleSelectContainer');
/** @type {?} */
const tiny = (/** @type {?} */ (tinyElement));
if (width < 535) {
// not perfect, but good first approximation
tiny.classList.add('hidden');
}
else {
tiny.classList.remove('hidden');
}
}
/**
* @param {?} container
* @param {?} width
* @return {?}
*/
function resizeUpTo640px(container, width) {
/** @type {?} */
const smallElements = container.getElementsByClassName('hiddenSmallView');
for (let i = 0; i < smallElements.length; i++) {
/** @type {?} */
const elt = (/** @type {?} */ (smallElements[i]));
if (width < 640) {
elt.classList.add('hidden');
}
else {
elt.classList.remove('hidden');
}
}
/** @type {?} */
const visibleSmallViewElements = container.getElementsByClassName('visibleSmallView');
for (let i = 0; i < visibleSmallViewElements.length; i++) {
/** @type {?} */
const elt = (/** @type {?} */ (visibleSmallViewElements[i]));
if (width < 640) {
elt.classList.remove('hidden');
}
else {
elt.classList.add('hidden');
}
}
/** @type {?} */
const toolbarButtonSpacer = container.getElementsByClassName('toolbarButtonSpacer');
for (let i = 0; i < toolbarButtonSpacer.length; i++) {
/** @type {?} */
const elt = (/** @type {?} */ (toolbarButtonSpacer[i]));
if (width < 640) {
elt.classList.add('below640px');
}
else {
elt.classList.remove('below640px');
}
}
/** @type {?} */
const findbar = document.getElementsByClassName('findbar');
for (let i = 0; i < findbar.length; i++) {
/** @type {?} */
const elt = (/** @type {?} */ (findbar[i]));
if (width < 640) {
elt.classList.add('below640px');
}
else {
elt.classList.remove('below640px');
}
}
}
/**
* @param {?} container
* @param {?} width
* @return {?}
*/
function resizeUpTo700px(container, width) {
/** @type {?} */
const mediumElements = container.getElementsByClassName('hiddenMediumView');
for (let i = 0; i < mediumElements.length; i++) {
/** @type {?} */
const elt = (/** @type {?} */ (mediumElements[i]));
if (width < 700) {
elt.classList.add('hidden');
}
else {
elt.classList.remove('hidden');
}
}
// #103
/** @type {?} */
const visibleMediumElements = container.getElementsByClassName('visibleMediumView');
for (let i = 0; i < visibleMediumElements.length; i++) {
/** @type {?} */
const elt = (/** @type {?} */ (visibleMediumElements[i]));
if (width < 700) {
elt.classList.remove('hidden');
}
else {
elt.classList.add('hidden');
}
}
}
/**
* @param {?} width
* @return {?}
*/
function resizeUpTo900px(width) {
/** @type {?} */
const elt = document.getElementById('toolbarViewerMiddle');
if (elt) {
if (width < 900) {
elt.classList.add('toolbarViewerMiddleBelow900px');
}
else {
elt.classList.remove('toolbarViewerMiddleBelow900px');
}
}
}
/**
* @param {?} width
* @return {?}
*/
function resizeUpTo840px(width) {
/** @type {?} */
const elt = document.getElementsByClassName('zoom')[0];
if (elt) {
if (width < 840) {
elt.classList.add('below840px');
}
else {
elt.classList.remove('below840px');
}
}
}
/**
* @param {?} container
* @param {?} width
* @return {?}
*/
function resizeUpTo770px(container, width) {
/** @type {?} */
let elt = document.getElementsByClassName('zoom')[0];
if (elt) {
if (width < 770) {
elt.classList.add('below770px');
}
else {
elt.classList.remove('below770px');
}
}
/** @type {?} */
const hiddenLargeElements = container.getElementsByClassName('hiddenLargeView');
for (let i = 0; i < hiddenLargeElements.length; i++) {
elt = (/** @type {?} */ (hiddenLargeElements[i]));
if (width < 770) {
elt.classList.add('hidden');
}
else {
elt.classList.remove('hidden');
}
}
/** @type {?} */
const visibleLargeElements = container.getElementsByClassName('visibleLargeView');
for (let i = 0; i < visibleLargeElements.length; i++) {
elt = (/** @type {?} */ (visibleLargeElements[i]));
if (width < 770) {
elt.classList.remove('hidden');
}
else {
elt.classList.add('hidden');
}
}
}
/**
* @return {?}
*/
function removeDynamicCSS() {
/** @type {?} */
const classesToRemove = ['toolbarViewerMiddleBelow900px', 'below840px', 'below770px', 'below700px', 'below640px'];
/** @type {?} */
const elt = document.getElementsByClassName('zoom')[0];
if (elt) {
classesToRemove.forEach((/**
* @param {?} c
* @return {?}
*/
c => {
/** @type {?} */
const parents = elt.getElementsByClassName(c);
if (parents) {
for (let i = 0; i < parents.length; i++) {
/** @type {?} */
const element = parents.item(i);
if (element) {
element.classList.remove(c);
}
}
}
}));
/** @type {?} */
const tinyElement = document.getElementById('scaleSelectContainer');
if (tinyElement) {
tinyElement.classList.remove('hidden');
}
/** @type {?} */
const classesToRemoveHiddenFrom = [
'hiddenSmallView',
'visibleSmallView',
'hiddenMediumView',
'visibleMediumView',
'hiddenLargeView',
'visibleLargeView'
];
classesToRemoveHiddenFrom.forEach((/**
* @param {?} c
* @return {?}
*/
c => {
/** @type {?} */
const parents = elt.getElementsByClassName(c);
if (parents) {
for (let i = 0; i < parents.length; i++) {
/** @type {?} */
const element = parents.item(i);
if (element) {
element.classList.remove('hidden');
}
}
}
}));
}
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const ServiceWorkerOptions = {
showUnverifiedSignatures: false
};
((/** @type {?} */ (window))).ServiceWorkerOptions = ServiceWorkerOptions;
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @enum {number} */
const VerbosityLevel = {
ERRORS: 0,
WARNINGS: 1,
INFOS: 5,
};
VerbosityLevel[VerbosityLevel.ERRORS] = 'ERRORS';
VerbosityLevel[VerbosityLevel.WARNINGS] = 'WARNINGS';
VerbosityLevel[VerbosityLevel.INFOS] = 'INFOS';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @enum {number} */
const FindState = {
FOUND: 0,
NOT_FOUND: 1,
WRAPPED: 2,
PENDING: 3,
};
FindState[FindState.FOUND] = 'FOUND';
FindState[FindState.NOT_FOUND] = 'NOT_FOUND';
FindState[FindState.WRAPPED] = 'WRAPPED';
FindState[FindState.PENDING] = 'PENDING';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
((/** @type {?} */ (window))).deburr = deburr; // #177
// #177
class NgxExtendedPdfViewerComponent {
/**
* @param {?} ngZone
*/
constructor(ngZone) {
this.ngZone = ngZone;
this.srcChange = new EventEmitter();
this.resizeTimeout = null;
this.contextMenuAllowed = true;
this.afterPrint = new EventEmitter();
this.beforePrint = new EventEmitter();
this.currentZoomFactor = new EventEmitter();
/**
* Number of milliseconds to wait between initializing the PDF viewer and loading the PDF file.
* Most users can let this parameter safely at it's default value of zero.
* Set this to 1000 or higher if you run into timing problems (typically caused by loading the locale files
* after the PDF files, so they are not available when the PDF viewer is initialized).
*/
this.delayFirstView = 0;
this.ignoreResponsiveCSS = undefined;
/**
* How many log messages should be printed?
* Legal values: VerbosityLevel.INFOS (= 5), VerbosityLevel.WARNINGS (= 1), VerbosityLevel.ERRORS (= 0)
*/
this.logLevel = VerbosityLevel.WARNINGS;
this.primaryMenuVisible = true;
/**
* option to increase (or reduce) print resolution. Default is 150 (dpi). Sensible values
* are 300, 600, and 1200. Note the increase memory consumption, which may even result in a browser crash.
*/
this.printResolution = null;
this.rotationChange = new EventEmitter();
this.minHeight = undefined;
this._height = '100%';
/**
* If this flag is true, this components adds a link to the locale assets. The pdf viewer
* sees this link and uses it to load the locale files automatically.
* @param useBrowserLocale boolean
*/
this.useBrowserLocale = false;
this.backgroundColor = '#e8e8eb';
/**
* Allows the user to define the name of the file after clicking "download"
*/
this.filenameForDownload = 'document.pdf';
/**
* Allows the user to disable the keyboard bindings completely
*/
this.ignoreKeyboard = false;
/**
* Allows the user to disable a list of key bindings.
*/
this.ignoreKeys = [];
/**
* Allows the user to explicitely enable a list of key bindings. If this property is set, every other key binding is ignored.
*/
this.acceptKeys = [];
/**
* Allows the user to put the viewer's svg images into an arbitrary folder
*/
this.imageResourcesPath = './assets/images/';
/**
* Override the default locale. This must be the complete locale name, such as "es-ES". The string is allowed to be all lowercase.
*/
this.language = undefined;
/**
* By default, listening to the URL is deactivated because often the anchor tag is used for the Angular router
*/
this.listenToURL = false;
/**
* Navigate to a certain "named destination"
*/
this.nameddest = undefined;
/**
* allows you to pass a password to read password-protected files
*/
this.password = undefined;
this._showSidebarButton = true;
this.viewerPositionTop = '32px';
/**
* pdf.js can show signatures, but fails to verify them. So they are switched off by default.
* Set "[showUnverifiedSignatures]"="true" to display e-signatures nonetheless.
*/
this.showUnverifiedSignatures = false;
/**
* If [showSideBarButton]="true", do you want the sidebar to be shown by default ([showSidebarOnLoad])="true")
* or not? By default, this flag is undefined, telling the PDF viewer to use the last setting used with this particular
* document, or to hide the sidebar if the document is opened for the first time.
* @deprecated Use showSidebar instead; dreprecated since 1.8.0; to be removed with 2.0.0
*/
this.showSidebarOnLoad = undefined;
this.sidebarVisible = undefined;
this.sidebarVisibleChange = new EventEmitter();
this.showFindButton = undefined;
this.showPagingButtons = true;
this.showZoomButtons = true;
this.showPresentationModeButton = false;
this.showOpenFileButton = true;
this.showPrintButton = true;
this.showDownloadButton = true;
this.showBookmarkButton = true;
this.showSecondaryToolbarButton = true;
this.showRotateButton = true;
this.handTool = true;
this.handToolChange = new EventEmitter();
this.showHandToolButton = false;
this.showScrollingButton = true;
this.showSpreadButton = true;
this.showPropertiesButton = true;
this.spreadChange = new EventEmitter();
this.page = undefined;
this.pageChange = new EventEmitter();
this.pageLabel = undefined;
this.pageLabelChange = new EventEmitter();
this.pagesLoaded = new EventEmitter();
this.pageRendered = new EventEmitter();
this.pdfDownloaded = new EventEmitter();
this.pdfLoaded = new EventEmitter();
this.pdfLoadingFailed = new EventEmitter();
this.textLayer = undefined;
this.updateFindMatchesCount = new EventEmitter();
this.updateFindState = new EventEmitter();
/**
* Legal values: undefined, 'auto', 'page-actual', 'page_fit', 'page-width', or '50' (or any other percentage)
*/
this.zoom = undefined;
this.zoomChange = new EventEmitter();
/**
* This attributes allows you to increase the size of the UI elements so you can use them on small mobile devices.
* This attribute is a string with a percent character at the end (e.g. "150%").
*/
this._mobileFriendlyZoom = '100%';
this.mobileFriendlyZoomScale = 1;
this.toolbarWidth = '100%';
this.secondaryToolbarTop = undefined;
// dirty IE11 hack - temporary solution
this.findbarTop = undefined;
// dirty IE11 hack - temporary solution
this.findbarLeft = undefined;
this._top = undefined;
}
/**
* @param {?} url
* @return {?}
*/
set src(url) {
if (url instanceof Uint8Array) {
this._src = url.buffer;
}
else if (url instanceof Blob) {
this._src = URL.createObjectURL(url);
}
else if (typeof url === 'string') {
this._src = url;
if (url.length > 980) {
// minimal length of a base64 encoded PDF
if (url.length % 4 === 0) {
if (/^[a-zA-Z\d\/+]+={0,2}$/.test(url)) {
console.error('The URL looks like a base64 encoded string. If so, please use the attribute base64 instead of src');
}
}
}
}
else {
this._src = url;
}
}
/**
* @param {?} base64
* @return {?}
*/
set base64Src(base64) {
/** @type {?} */
const binary_string = window.atob(base64);
/** @type {?} */
const len = binary_string.length;
/** @type {?} */
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
this.src = bytes.buffer;
}
/**
* @param {?} h
* @return {?}
*/
set height(h) {
this.minHeight = undefined;
if (h) {
this._height = h;
}
else {
this.height = '100%';
}
setTimeout((/**
* @return {?}
*/
() => {
this.checkHeight();
}));
}
/**
* @return {?}
*/
get height() {
return this._height;
}
/**
* @return {?}
*/
get showSidebarButton() {
return this._showSidebarButton;
}
/**
* @param {?} show
* @return {?}
*/
set showSidebarButton(show) {
this._showSidebarButton = show;
/** @type {?} */
const isIE = /msie\s|trident\//i.test(window.navigator.userAgent);
/** @type {?} */
let factor = 1;
if (isIE) {
factor = Number((this._mobileFriendlyZoom || '100').replace('%', '')) / 100;
}
if (this._showSidebarButton) {
this.findbarLeft = (68 * factor).toString() + 'px';
}
else {
this.findbarLeft = '0px';
}
}
/**
* @return {?}
*/
get mobileFriendlyZoom() {
return this._mobileFriendlyZoom;
}
/**
* This attributes allows you to increase the size of the UI elements so you can use them on small mobile devices.
* This attribute is a string with a percent character at the end (e.g. "150%").
* @param {?} zoom
* @return {?}
*/
set mobileFriendlyZoom(zoom) {
// tslint:disable-next-line:triple-equals - the type conversion is intended
if (zoom == 'true') {
zoom = '150%';
// tslint:disable-next-line:triple-equals - the type conversion is intended
}
else if (zoom == 'false') {
zoom = '100%';
}
this._mobileFriendlyZoom = zoom;
/** @type {?} */
let factor = 1;
if (!String(zoom).includes('%')) {
zoom = 100 * Number(zoom) + '%';
}
factor = Number((zoom || '100').replace('%', '')) / 100;
this.mobileFriendlyZoomScale = factor;
this.toolbarWidth = (100 / factor).toString() + '%';
if (this.showSidebarButton) {
this.findbarLeft = (68 * factor).toString() + 'px';
}
else {
this.findbarLeft = '0px';
}
this.secondaryToolbarTop = (12 + 24 * factor).toString() + 'px';
this.findbarTop = (-6 + 42 * factor).toString() + 'px';
}
/**
* Deprecated. Please use [mobileFriendlyZoom] instead.
* This attributes allows you to increase the size of the UI elements so you can use them on small mobile devices.
* This attribute is a string with a percent character at the end (e.g. "150%").
* @param {?} mobileFriendlyZoom
* @return {?}
*/
set mobileZoom(mobileFriendlyZoom) {
this.mobileFriendlyZoom = mobileFriendlyZoom;
}
/**
* @return {?}
*/
get sidebarPositionTop() {
if (this._top) {
return this._top;
}
if (this.mobileFriendlyZoom) {
if (this.mobileFriendlyZoom.endsWith('%')) {
/** @type {?} */
const zoom = Number(this.mobileFriendlyZoom.substring(0, this.mobileFriendlyZoom.length - 1));
return (16 + 0.16 * zoom).toString() + 'px';
}
if (this.mobileFriendlyZoom.endsWith('px')) {
return this.mobileFriendlyZoom;
}
return (16 + 0.16 * Number(this.mobileFriendlyZoom)).toString() + 'px';
}
return '32px';
}
/**
* @return {?}
*/
calcViewerPositionTop() {
if (this._top) {
this.viewerPositionTop = this._top;
return;
}
if (this.mobileFriendlyZoom) {
if (this.mobileFriendlyZoom.endsWith('%')) {
/** @type {?} */
const zoom = Number(this.mobileFriendlyZoom.substring(0, this.mobileFriendlyZoom.length - 1));
if (!this.isPrimaryMenuVisible()) {
this.viewerPositionTop = '0';
}
else {
this.viewerPositionTop = (1 + 0.32 * zoom).toString() + 'px';
}
return;
}
if (this.mobileFriendlyZoom.endsWith('px')) {
this.viewerPositionTop = this.mobileFriendlyZoom;
return;
}
}
if (this.isPrimaryMenuVisible()) {
this.viewerPositionTop = '32px';
}
else {
this.viewerPositionTop = '0';
}
}
/**
* @return {?}
*/
emitZoomChange() {
/** @type {?} */
const selectedIndex = this.sizeSelector.nativeElement.selectedIndex;
if (selectedIndex || selectedIndex === 0) {
/** @type {?} */
const s = (/** @type {?} */ (this.sizeSelector.nativeElement.options[selectedIndex]));
/** @type {?} */
let value = s.label;
if (value.endsWith('%')) {
value = Number(value.replace('%', ''));
}
else {
value = s.value;
}
this.zoomChange.emit(value);
}
}
/**
* @return {?}
*/
ngOnInit() {
if (this.ignoreResponsiveCSS === undefined) {
/** @type {?} */
const pdfViewer = document.getElementsByClassName('html');
if (pdfViewer && pdfViewer.length > 0) {
/** @type {?} */
const toolbar = pdfViewer[0].getElementsByClassName('toolbar');
if (toolbar && toolbar.length > 0) {
/** @type {?} */
const width = toolbar[0].clientWidth;
if (window.innerWidth - width > 50) {
this.ignoreResponsiveCSS = true;
}
else {
this.ignoreResponsiveCSS = false;
}
}
}
}
/** @type {?} */
const langLinks = document.querySelectorAll('link[type="application/l10n"]');
/** @type {?} */
const langCount = langLinks.length;
if (langCount === 0) {
/** @type {?} */
const dict = document.querySelector('script[type="application/l10n"]');
if (!dict) {
if (!this.useBrowserLocale) {
console.error(
// tslint:disable-next-line:quotemark
"If you set the attribute 'useBrowserLocale' to false, you must provide the translations yourself in a script or link tag.");
console.error('The easiest way to do this is to add them to the index.html.');
}
}
else if (this.useBrowserLocale) {
console.error(
// tslint:disable-next-line:quotemark
"Please set the attribute 'useBrowserLocale' to false if you provide the translations yourself in a script or link tag.");
}
}
else if (this.useBrowserLocale) {
/** @type {?} */
const o = langLinks[0].attributes['origin'];
if (o && o.value !== 'ngx-extended-pdf-viewer') {
console.error(
// tslint:disable-next-line:quotemark
"Please set the attribute 'useBrowserLocale' to false if you provide the translations yourself in a script or link tag.");
}
}
/** @type {?} */
const callback = (/**
* @param {?} e
* @return {?}
*/
e => {
document.removeEventListener('localized', callback);
this.initTimeout = setTimeout((/**
* @return {?}
*/
() => {
this.openPDF();
}), this.delayFirstView);
});
window.addEventListener('afterprint', (/**
* @param {?} event
* @return {?}
*/
event => {
this.afterPrint.emit();
}));
window.addEventListener('beforeprint', (/**
* @param {?} event
* @return {?}
*/
event => {
this.beforePrint.emit();
}));
document.addEventListener('localized', callback);
if (NgxExtendedPdfViewerComponent.ngxExtendedPdfViewerInitialized) {
// tslint:disable-next-line:quotemark
console.error("You're trying to open two instances of the PDF viewer. Most likely, this will result in errors.");
}
/** @type {?} */
const onLoaded = (/**
* @return {?}
*/
() => {
this.overrideDefaultSettings();
document.removeEventListener('webviewerloaded', onLoaded);
});
document.addEventListener('webviewerloaded', onLoaded);
this.activateTextlayerIfNecessary(null);
setTimeout((/**
* @return {?}
*/
() => {
// This initializes the webviewer, the file may be passed in to it to initialize the viewer with a pdf directly
this.primaryMenuVisible = true;
if (!this.isSecondaryMenuVisible()) {
this.showSecondaryToolbarButton = false;
}
if (!this.showSecondaryToolbarButton) {
if (!this.isPrimaryMenuVisible()) {
this.primaryMenuVisible = false;
}
}
this.calcViewerPositionTop();
((/** @type {?} */ (window))).webViewerLoad();
((/** @type {?} */ (window))).PDFViewerApplication.appConfig.defaultUrl = ''; // IE bugfix
((/** @type {?} */ (window))).PDFViewerApplication.appConfig.filenameForDownload = this.filenameForDownload;
((/** @type {?} */ (window))).PDFViewerApplicationOptions.set('locale', this.language);
((/** @type {?} */ (window))).PDFViewerApplicationOptions.set('imageResourcesPath', this.imageResourcesPath);
((/** @type {?} */ (window))).PDFViewerApplication.isViewerEmbedded = true;
window.addEventListener('keydown', ((/** @type {?} */ (window))).PDFViewerApplication.printKeyDownListener, true);
/** @type {?} */
const pc = document.getElementById('printContainer');
if (pc) {
document.getElementsByTagName('body')[0].appendChild(pc);
}
}), 0);
}
/**
* @return {?}
*/
checkHeight() {
/** @type {?} */
const container = document.getElementsByClassName('zoom')[0];
if (container.clientHeight === 0 && this._height.includes('%')) {
/** @type {?} */
const available = window.innerHeight;
/** @type {?} */
const rect = container.getBoundingClientRect();
/** @type {?} */
const top = rect.top;
/** @type {?} */
let mh = available - top;
/** @type {?} */
const factor = Number(this._height.replace('%', ''));
mh = (mh * factor) / 100;
if (mh > 100) {
this.minHeight = mh + 'px';
}
else {
this.minHeight = '100px';
}
}
}
/**
* @param {?} newSpread
* @return {?}
*/
onSpreadChange(newSpread) {
this.spreadChange.emit(newSpread);
}
/**
* @private
* @param {?} options
* @return {?}
*/
activateTextlayerIfNecessary(options) {
if (this.textLayer === undefined) {
if (!this.handTool) {
if (options) {
options.set('textLayerMode', 1);
}
this.textLayer = true;
if (this.showFindButton === undefined) {
this.showFindButton = true;
}
}
else {
if (options) {
options.set('textLayerMode', this.showHandToolButton ? 1 : 0);
}
if (!this.showHandToolButton) {
if (this.showFindButton || this.showFindButton === undefined) {
this.showFindButton = false;
if (this.logLevel >= VerbosityLevel.WARNINGS) {
// tslint:disable-next-line:max-line-length
console.warn('Hiding the "find" button because the text layer of the PDF file is not rendered. Use [textLayer]="true" to enable the find button.');
}
}
if (this.showHandToolButton) {
if (this.logLevel >= VerbosityLevel.WARNINGS) {
// tslint:disable-next-line:max-line-length
console.warn('Hiding the "hand tool / selection mode" menu because the text layer of the PDF file is not rendered. Use [textLayer]="true" to enable the the menu items.');
this.showHandToolButton = false;
}
}
}
}
}
else {
if (this.textLayer) {
if (options) {
options.set('textLayerMode', 1);
}
this.textLayer = true;
if (this.showFindButton === undefined) {
this.showFindButton = true;
}
}
else {
if (options) {
options.set('textLayerMode', 0);
}
this.textLayer = false;
if (this.showFindButton) {
if (this.logLevel >= VerbosityLevel.WARNINGS) {
// tslint:disable-next-line:max-line-length
console.warn('Hiding the "find" button because the text layer of the PDF file is not rendered. Use [textLayer]="true" to enable the find button.');
this.showFindButton = false;
}
}
if (this.showHandToolButton) {
if (this.logLevel >= VerbosityLevel.WARNINGS) {
// tslint:disable-next-line:max-line-length
console.warn('Hiding the "hand tool / selection mode" menu because the text layer of the PDF file is not rendered. Use [textLayer]="true" to enable the the menu items.');
this.showHandToolButton = false;
}
}
}
}
}
/**
* @private
* @return {?}
*/
overrideDefaultSettings() {
/** @type {?} */
const options = ((/** @type {?} */ (window))).PDFViewerApplicationOptions;
// tslint:disable-next-line:forin
for (const key in defaultOptions) {
options.set(key, defaultOptions[key]);
}
options.set('disablePreferences', true);
this.setZoom();
options.set('ignoreKeyboard', this.ignoreKeyboard);
options.set('ignoreKeys', this.ignoreKeys);
options.set('acceptKeys', this.acceptKeys);
this.activateTextlayerIfNecessary(options);
/** @type {?} */
let sidebarVisible = this.sidebarVisible;
if (sidebarVisible === undefined) {
sidebarVisible = this.showSidebarOnLoad;
}
if (sidebarVisible !== undefined) {
((/** @type {?} */ (window))).PDFViewerApplication.sidebarViewOnLoad = sidebarVisible ? 1 : 0;
if (((/** @type {?} */ (window))).PDFViewerApplication.appConfig) {
((/** @type {?} */ (window))).PDFViewerApplication.appConfig.sidebarViewOnLoad = sidebarVisible ? 1 : 0;
}
options.set('sidebarViewOnLoad', this.sidebarVisible ? 1 : 0);
}
if (this.spread === 'even') {
options.set('spreadModeOnLoad', 2);
if (((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.spreadMode = 2;
}
this.onSpreadChange('even');
}
else if (this.spread === 'odd') {
options.set('spreadModeOnLoad', 1);
if (((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.spreadMode = 1;
}
this.onSpreadChange('odd');
}
else {
options.set('spreadModeOnLoad', 0);
if (((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.spreadMode = 0;
}
this.onSpreadChange('off');
}
if (this.printResolution) {
options.set('printResolution', this.printResolution);
}
}
/**
* @private
* @return {?}
*/
openPDF() {
ServiceWorkerOptions.showUnverifiedSignatures = this.showUnverifiedSignatures;
NgxExtendedPdfViewerComponent.ngxExtendedPdfViewerInitialized = true;
this.onResize();
if (!this.listenToURL) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfLinkService.setHash = (/**
* @return {?}
*/
function () { });
}
this.initTimeout = null;
this.selectCursorTool();
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('pagesloaded', (/**
* @param {?} x
* @return {?}
*/
(x) => {
this.pagesLoaded.emit(x);
if (this.rotation) {
/** @type {?} */
const r = Number(this.rotation);
if (r === 0 || r === 90 || r === 180 || r === 270) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.pagesRotation = r;
}
}
else {
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.pagesRotation = 0;
}
setTimeout((/**
* @return {?}
*/
() => {
if (this.nameddest) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfLinkService.navigateTo(this.nameddest);
}
else if (this.page) {
((/** @type {?} */ (window))).PDFViewerApplication.page = Number(this.page);
}
else if (this.pageLabel) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.currentPageLabel = this.pageLabel;
}
}));
this.setZoom();
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('pagerendered', (/**
* @param {?} x
* @return {?}
*/
(x) => {
this.ngZone.run((/**
* @return {?}
*/
() => {
this.pageRendered.emit(x);
}));
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('download', (/**
* @param {?} x
* @return {?}
*/
(x) => {
this.ngZone.run((/**
* @return {?}
*/
() => {
this.pdfDownloaded.emit(x);
}));
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('scalechanging', (/**
* @param {?} x
* @return {?}
*/
(x) => {
this.ngZone.run((/**
* @return {?}
*/
() => {
setTimeout((/**
* @return {?}
*/
() => {
this.ngZone.run((/**
* @return {?}
*/
() => {
this.currentZoomFactor.emit(x.scale);
this.emitZoomChange();
}));
}));
}));
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('rotationchanging', (/**
* @param {?} x
* @return {?}
*/
(x) => {
this.ngZone.run((/**
* @return {?}
*/
() => {
this.rotationChange.emit(x.pagesRotation);
}));
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('fileinputchange', (/**
* @param {?} x
* @return {?}
*/
(x) => {
this.ngZone.run((/**
* @return {?}
*/
() => {
/** @type {?} */
const path = x.fileInput.value.replace('C:\\fakepath\\', '');
this.srcChange.emit(path);
}));
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('cursortoolchanged', (/**
* @param {?} x
* @return {?}
*/
(x) => {
this.ngZone.run((/**
* @return {?}
*/
() => {
this.handToolChange.emit(x.tool === 1);
}));
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('sidebarviewchanged', (/**
* @param {?} x
* @return {?}
*/
(x) => {
this.ngZone.run((/**
* @return {?}
*/
() => {
this.sidebarVisibleChange.emit(x.view === 1);
}));
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('updatefindcontrolstate', (/**
* @param {?} x
* @return {?}
*/
(x) => {
if (this.updateFindMatchesCount) {
if (x.state === FindState.NOT_FOUND) {
this.updateFindMatchesCount.emit({ current: 0, total: 0 });
}
else if (x.matchesCount.total) {
this.updateFindMatchesCount.emit(x.matchesCount);
}
}
if (this.updateFindState) {
this.updateFindState.emit(x.state);
}
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('updatefindmatchescount', (/**
* @param {?} x
* @return {?}
*/
(x) => {
if (this.updateFindMatchesCount) {
if (x.matchesCount.total) {
this.updateFindMatchesCount.emit(x.matchesCount);
}
}
}));
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.on('pagechanging', (/**
* @param {?} x
* @return {?}
*/
(x) => {
this.ngZone.run((/**
* @return {?}
*/
() => {
/** @type {?} */
const currentPage = ((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.currentPageNumber;
/** @type {?} */
const currentPageLabel = ((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.currentPageLabel;
this.pageChange.emit(currentPage);
this.pageLabelChange.emit(currentPageLabel);
}));
}));
this.checkHeight();
// open a file in the viewer
if (!!this._src) {
/** @type {?} */
const options = {
password: this.password,
verbosity: this.logLevel
};
((/** @type {?} */ (window))).PDFViewerApplication.open(this._src, options).then((/**
* @return {?}
*/
() => this.pdfLoaded.emit({ pagesCount: ((/** @type {?} */ (window))).PDFViewerApplication.pagesCount })), (/**
* @param {?} error
* @return {?}
*/
(error) => this.pdfLoadingFailed.emit(error)));
}
setTimeout((/**
* @return {?}
*/
() => {
if (this.page) {
((/** @type {?} */ (window))).PDFViewerApplication.page = Number(this.page);
}
}), 100);
}
/**
* @private
* @return {?}
*/
selectCursorTool() {
((/** @type {?} */ (window))).PDFViewerApplication.eventBus.dispatch('switchcursortool', { tool: this.handTool ? 1 : 0 });
}
/**
* @return {?}
*/
ngOnDestroy() {
NgxExtendedPdfViewerComponent.ngxExtendedPdfViewerInitialized = false;
if (this.initTimeout) {
clearTimeout(this.initTimeout);
this.initTimeout = undefined;
}
/** @type {?} */
const app = ((/** @type {?} */ (window))).PDFViewerApplication;
if (app) {
app.cleanup();
app.close();
if (((/** @type {?} */ (window))).PDFViewerApplication.printKeyDownListener) {
((/** @type {?} */ (window))).removeEventListener('keydown', ((/** @type {?} */ (window))).PDFViewerApplication.printKeyDownListener, true);
}
if (app._boundEvents) {
app.unbindWindowEvents();
}
/** @type {?} */
const bus = app.eventBus;
if (bus) {
app.unbindEvents();
for (const key in bus._listeners) {
if (bus._listeners[key]) {
/** @type {?} */
const list = bus._listeners[key];
// not sure if the for loop is necessary - but
// it might improve garbage collection if the "listeners"
// array is stored somewhere else
for (let i = 0; i < list.length; i++) {
list[i] = undefined;
}
bus._listeners[key] = undefined;
}
}
}
app.eventBus = null;
}
/** @type {?} */
const body = document.getElementsByTagName('body');
if (body[0]) {
/** @type {?} */
const topLevelElements = body[0].children;
for (let i = topLevelElements.length - 1; i >= 0; i--) {
/** @type {?} */
const e = topLevelElements.item(i);
if (e && e.id === 'printContainer') {
body[0].removeChild(e);
}
else if (e && e.id === 'fileInput') {
body[0].removeChild(e);
}
}
}
}
/**
* @private
* @return {?}
*/
isSecondaryMenuVisible() {
/** @type {?} */
const visible = this.showHandToolButton ||
this.showPagingButtons ||
this.showPropertiesButton ||
this.showRotateButton ||
this.showScrollingButton ||
this.showRotateButton ||
this.showSpreadButton;
if (visible) {
return true;
}
return false;
}
/**
* @private
* @return {?}
*/
isPrimaryMenuVisible() {
/** @type {?} */
const visible = this.showBookmarkButton ||
this.showDownloadButton ||
this.showFindButton ||
this.showOpenFileButton ||
this.showPagingButtons ||
this.showPresentationModeButton ||
this.showPrintButton ||
this.showPropertiesButton ||
this.showSidebarButton ||
this.showSecondaryToolbarButton ||
this.showZoomButtons;
if (visible) {
return true;
}
return false;
}
/**
* @param {?} changes
* @return {?}
*/
ngOnChanges(changes) {
if (NgxExtendedPdfViewerComponent.ngxExtendedPdfViewerInitialized) {
if ('src' in changes || 'base64Src' in changes) {
if (!!this._src) {
this.overrideDefaultSettings();
((/** @type {?} */ (window))).PDFViewerApplication.open(this._src).then((/**
* @return {?}
*/
() => this.pdfLoaded.emit({ pagesCount: ((/** @type {?} */ (window))).PDFViewerApplication.pagesCount })), (/**
* @param {?} error
* @return {?}
*/
(error) => this.pdfLoadingFailed.emit(error)));
}
}
if ('zoom' in changes) {
this.setZoom();
}
if ('handTool' in changes) {
this.selectCursorTool();
}
if ('page' in changes) {
if (this.page) {
((/** @type {?} */ (window))).PDFViewerApplication.page = this.page;
}
}
if ('pageLabel' in changes) {
if (this.pageLabel) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.currentPageLabel = this.pageLabel;
}
}
if ('rotation' in changes) {
if (this.rotation) {
/** @type {?} */
const r = Number(this.rotation);
if (r === 0 || r === 90 || r === 180 || r === 270) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.pagesRotation = r;
}
}
else {
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.pagesRotation = 0;
}
}
if ('sidebarVisible' in changes) {
if (this.sidebarVisible) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfSidebar.open();
}
else {
((/** @type {?} */ (window))).PDFViewerApplication.pdfSidebar.close();
}
}
if ('filenameForDownload' in changes) {
((/** @type {?} */ (window))).PDFViewerApplication.appConfig.filenameForDownload = this.filenameForDownload;
}
if ('nameddest' in changes) {
if (this.nameddest) {
((/** @type {?} */ (window))).PDFViewerApplication.pdfLinkService.navigateTo(this.nameddest);
}
}
if ('spread' in changes) {
if (this.spread === 'even') {
((/** @type {?} */ (window))).PDFViewerApplication.spreadModeOnLoad = 2;
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.spreadMode = 2;
this.onSpreadChange('even');
}
else if (this.spread === 'odd') {
((/** @type {?} */ (window))).PDFViewerApplication.spreadModeOnLoad = 1;
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.spreadMode = 1;
this.onSpreadChange('odd');
}
else {
((/** @type {?} */ (window))).PDFViewerApplication.spreadModeOnLoad = 0;
((/** @type {?} */ (window))).PDFViewerApplication.pdfViewer.spreadMode = 0;
this.onSpreadChange('off');
}
}
this.primaryMenuVisible = true;
if (!this.isSecondaryMenuVisible()) {
this.showSecondaryToolbarButton = false;
}
if (!this.showSecondaryToolbarButton) {
if (!this.isPrimaryMenuVisible()) {
this.primaryMenuVisible = false;
}
}
this.calcViewerPositionTop();
}
if ('ignoreResponsiveCSS' in changes) {
if (this.ignoreResponsiveCSS) {
this.onResize();
}
else {
removeDynamicCSS();
}
}
if ('printResolution' in changes) {
/** @type {?} */
const options = ((/** @type {?} */ (window))).PDFViewerApplicationOptions;
if (options) {
options.set('printResolution', this.printResolution);
}
}