Extract parsing of debugging hash parameters into its own method in PDFViewerApplication

In order to move viewer related options from the global `PDFJS` object and into the initialization of the relevant components, we'll need to parse the hash parameters *before* calling `PDFViewerApplication._initializeViewerComponents`.
This commit is contained in:
Jonas Jenwald 2017-11-13 10:17:25 +01:00
parent f9a0515452
commit 25d6bc9de9

View File

@ -162,6 +162,8 @@ let PDFViewerApplication = {
this.appConfig = appConfig; this.appConfig = appConfig;
return this._readPreferences().then(() => { return this._readPreferences().then(() => {
return this._parseHashParameters();
}).then(() => {
return this._initializeL10n(); return this._initializeL10n();
}).then(() => { }).then(() => {
return this._initializeViewerComponents(); return this._initializeViewerComponents();
@ -268,19 +270,86 @@ let PDFViewerApplication = {
]).catch(function(reason) { }); ]).catch(function(reason) { });
}, },
_initializeL10n() { /**
* @private
*/
_parseHashParameters() {
let { appConfig, viewerPrefs, } = this;
let waitOn = [];
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION') ||
viewerPrefs['pdfBugEnabled']) {
// Special debugging flags in the hash section of the URL.
let hash = document.location.hash.substring(1);
let hashParams = parseQueryString(hash);
if ('disableworker' in hashParams) {
PDFJS.disableWorker = (hashParams['disableworker'] === 'true');
}
if ('disablerange' in hashParams) {
PDFJS.disableRange = (hashParams['disablerange'] === 'true');
}
if ('disablestream' in hashParams) {
PDFJS.disableStream = (hashParams['disablestream'] === 'true');
}
if ('disableautofetch' in hashParams) {
PDFJS.disableAutoFetch = (hashParams['disableautofetch'] === 'true');
}
if ('disablefontface' in hashParams) {
PDFJS.disableFontFace = (hashParams['disablefontface'] === 'true');
}
if ('disablehistory' in hashParams) {
PDFJS.disableHistory = (hashParams['disablehistory'] === 'true');
}
if ('webgl' in hashParams) {
PDFJS.disableWebGL = (hashParams['webgl'] !== 'true');
}
if ('useonlycsszoom' in hashParams) {
PDFJS.useOnlyCssZoom = (hashParams['useonlycsszoom'] === 'true');
}
if ('verbosity' in hashParams) {
PDFJS.verbosity = hashParams['verbosity'] | 0;
}
if ((typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) &&
hashParams['disablebcmaps'] === 'true') {
PDFJS.cMapUrl = '../external/cmaps/';
PDFJS.cMapPacked = false;
}
if ('textlayer' in hashParams) {
switch (hashParams['textlayer']) {
case 'off':
PDFJS.disableTextLayer = true;
break;
case 'visible':
case 'shadow':
case 'hover':
let viewer = appConfig.viewerContainer;
viewer.classList.add('textLayer-' + hashParams['textlayer']);
break;
}
}
if ('pdfbug' in hashParams) {
PDFJS.pdfBug = true;
let pdfBug = hashParams['pdfbug'];
let enabled = pdfBug.split(',');
waitOn.push(loadAndEnablePDFBug(enabled));
}
// Locale can be changed only when special debugging flags is present in // Locale can be changed only when special debugging flags is present in
// the hash section of the URL, or development version of viewer is used. // the hash section of the URL, or development version of viewer is used.
// It is not possible to change locale for Firefox extension builds. // It is not possible to change locale for Firefox extension builds.
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION') || if ((typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION') ||
(!PDFJSDev.test('FIREFOX || MOZCENTRAL') && !PDFJSDev.test('FIREFOX || MOZCENTRAL')) && 'locale' in hashParams) {
this.viewerPrefs['pdfBugEnabled'])) {
let hash = document.location.hash.substring(1);
let hashParams = parseQueryString(hash);
if ('locale' in hashParams) {
PDFJS.locale = hashParams['locale']; PDFJS.locale = hashParams['locale'];
} }
} }
return Promise.all(waitOn);
},
/**
* @private
*/
_initializeL10n() {
this.l10n = this.externalServices.createL10n(); this.l10n = this.externalServices.createL10n();
return this.l10n.getDirection().then((dir) => { return this.l10n.getDirection().then((dir) => {
document.getElementsByTagName('html')[0].dir = dir; document.getElementsByTagName('html')[0].dir = dir;
@ -291,7 +360,7 @@ let PDFViewerApplication = {
* @private * @private
*/ */
_initializeViewerComponents() { _initializeViewerComponents() {
let appConfig = this.appConfig; let { appConfig, viewerPrefs, } = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.overlayManager = new OverlayManager(); this.overlayManager = new OverlayManager();
@ -320,11 +389,11 @@ let PDFViewerApplication = {
renderingQueue: pdfRenderingQueue, renderingQueue: pdfRenderingQueue,
linkService: pdfLinkService, linkService: pdfLinkService,
downloadManager, downloadManager,
renderer: this.viewerPrefs['renderer'], renderer: viewerPrefs['renderer'],
l10n: this.l10n, l10n: this.l10n,
enhanceTextSelection: this.viewerPrefs['enhanceTextSelection'], enhanceTextSelection: viewerPrefs['enhanceTextSelection'],
renderInteractiveForms: this.viewerPrefs['renderInteractiveForms'], renderInteractiveForms: viewerPrefs['renderInteractiveForms'],
enablePrintAutoRotate: this.viewerPrefs['enablePrintAutoRotate'], enablePrintAutoRotate: viewerPrefs['enablePrintAutoRotate'],
}); });
pdfRenderingQueue.setViewer(this.pdfViewer); pdfRenderingQueue.setViewer(this.pdfViewer);
pdfLinkService.setViewer(this.pdfViewer); pdfLinkService.setViewer(this.pdfViewer);
@ -1461,7 +1530,6 @@ function webViewerInitialized() {
file = appConfig.defaultUrl; file = appConfig.defaultUrl;
} }
let waitForBeforeOpening = [];
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) { if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
let fileInput = document.createElement('input'); let fileInput = document.createElement('input');
fileInput.id = appConfig.openFileInputName; fileInput.id = appConfig.openFileInputName;
@ -1492,66 +1560,6 @@ function webViewerInitialized() {
appConfig.secondaryToolbar.openFileButton.setAttribute('hidden', 'true'); appConfig.secondaryToolbar.openFileButton.setAttribute('hidden', 'true');
} }
if ((typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) ||
PDFViewerApplication.viewerPrefs['pdfBugEnabled']) {
// Special debugging flags in the hash section of the URL.
let hash = document.location.hash.substring(1);
let hashParams = parseQueryString(hash);
if ('disableworker' in hashParams) {
PDFJS.disableWorker = (hashParams['disableworker'] === 'true');
}
if ('disablerange' in hashParams) {
PDFJS.disableRange = (hashParams['disablerange'] === 'true');
}
if ('disablestream' in hashParams) {
PDFJS.disableStream = (hashParams['disablestream'] === 'true');
}
if ('disableautofetch' in hashParams) {
PDFJS.disableAutoFetch = (hashParams['disableautofetch'] === 'true');
}
if ('disablefontface' in hashParams) {
PDFJS.disableFontFace = (hashParams['disablefontface'] === 'true');
}
if ('disablehistory' in hashParams) {
PDFJS.disableHistory = (hashParams['disablehistory'] === 'true');
}
if ('webgl' in hashParams) {
PDFJS.disableWebGL = (hashParams['webgl'] !== 'true');
}
if ('useonlycsszoom' in hashParams) {
PDFJS.useOnlyCssZoom = (hashParams['useonlycsszoom'] === 'true');
}
if ('verbosity' in hashParams) {
PDFJS.verbosity = hashParams['verbosity'] | 0;
}
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
if ('disablebcmaps' in hashParams && hashParams['disablebcmaps']) {
PDFJS.cMapUrl = '../external/cmaps/';
PDFJS.cMapPacked = false;
}
}
if ('textlayer' in hashParams) {
switch (hashParams['textlayer']) {
case 'off':
PDFJS.disableTextLayer = true;
break;
case 'visible':
case 'shadow':
case 'hover':
let viewer = appConfig.viewerContainer;
viewer.classList.add('textLayer-' + hashParams['textlayer']);
break;
}
}
if ('pdfbug' in hashParams) {
PDFJS.pdfBug = true;
let pdfBug = hashParams['pdfbug'];
let enabled = pdfBug.split(',');
waitForBeforeOpening.push(loadAndEnablePDFBug(enabled));
}
}
if (typeof PDFJSDev !== 'undefined' && if (typeof PDFJSDev !== 'undefined' &&
PDFJSDev.test('FIREFOX || MOZCENTRAL') && PDFJSDev.test('FIREFOX || MOZCENTRAL') &&
!PDFViewerApplication.supportsDocumentFonts) { !PDFViewerApplication.supportsDocumentFonts) {
@ -1587,11 +1595,11 @@ function webViewerInitialized() {
PDFViewerApplication.pdfSidebar.toggle(); PDFViewerApplication.pdfSidebar.toggle();
}); });
Promise.all(waitForBeforeOpening).then(function () { Promise.resolve().then(function() {
webViewerOpenFileViaURL(file); webViewerOpenFileViaURL(file);
}).catch(function(reason) { }).catch(function(reason) {
PDFViewerApplication.l10n.get('loading_error', null, PDFViewerApplication.l10n.get('loading_error', null,
'An error occurred while opening.').then((msg) => { 'An error occurred while loading the PDF.').then((msg) => {
PDFViewerApplication.error(msg, reason); PDFViewerApplication.error(msg, reason);
}); });
}); });
@ -1614,10 +1622,7 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
xhr.responseType = 'arraybuffer'; xhr.responseType = 'arraybuffer';
xhr.send(); xhr.send();
} catch (ex) { } catch (ex) {
PDFViewerApplication.l10n.get('loading_error', null, throw ex;
'An error occurred while loading the PDF.').then((msg) => {
PDFViewerApplication.error(msg, ex);
});
} }
return; return;
} }
@ -1632,9 +1637,9 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
PDFViewerApplication.initPassiveLoading(); PDFViewerApplication.initPassiveLoading();
}; };
} else { } else {
webViewerOpenFileViaURL = function webViewerOpenFileURL(file) { webViewerOpenFileViaURL = function webViewerOpenFileViaURL(file) {
if (file) { if (file) {
throw new Error('Not implemented: webViewerOpenFileURL'); throw new Error('Not implemented: webViewerOpenFileViaURL');
} }
}; };
} }