Skip to content

Commit

Permalink
Add a BasePreferences.getAll method and use it to fetch all Prefere…
Browse files Browse the repository at this point in the history
…nces at once in `PDFViewerApplication._readPreferences`

Given that *all* Preferences are already fetched in `PDFViewerApplication._readPreferences`, the amount of boilerplate/duplication can be considerably reduced with the addition of a `BasePreferences.getAll` method.
  • Loading branch information
Snuffleupagus committed Jul 25, 2018
1 parent c3433ae commit 34957ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 78 deletions.
96 changes: 18 additions & 78 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,90 +171,30 @@ let PDFViewerApplication = {
* @private
*/
_readPreferences() {
let { preferences, } = this;

return Promise.all([
preferences.get('enableWebGL').then(function resolved(value) {
AppOptions.set('enableWebGL', value);
}),
preferences.get('sidebarViewOnLoad').then(function resolved(value) {
AppOptions.set('sidebarViewOnLoad', value);
}),
preferences.get('cursorToolOnLoad').then(function resolved(value) {
AppOptions.set('cursorToolOnLoad', value);
}),
preferences.get('pdfBugEnabled').then(function resolved(value) {
AppOptions.set('pdfBugEnabled', value);
}),
preferences.get('showPreviousViewOnLoad').then(function resolved(value) {
AppOptions.set('showPreviousViewOnLoad', value);
}),
preferences.get('defaultZoomValue').then(function resolved(value) {
AppOptions.set('defaultZoomValue', value);
}),
preferences.get('textLayerMode').then(function resolved(value) {
if (AppOptions.get('textLayerMode') === TextLayerMode.DISABLE) {
return;
}
AppOptions.set('textLayerMode', value);
}),
preferences.get('disableRange').then(function resolved(value) {
if (AppOptions.get('disableRange') === true) {
return;
}
AppOptions.set('disableRange', value);
}),
preferences.get('disableStream').then(function resolved(value) {
if (AppOptions.get('disableStream') === true) {
return;
}
AppOptions.set('disableStream', value);
}),
preferences.get('disableAutoFetch').then(function resolved(value) {
AppOptions.set('disableAutoFetch', value);
}),
preferences.get('disableFontFace').then(function resolved(value) {
if (AppOptions.get('disableFontFace') === true) {
return;
// A subset of the Preferences that `AppOptions`, for compatibility reasons,
// is allowed to override if the `AppOptions` values matches the ones below.
const OVERRIDES = {
disableFontFace: true,
disableRange: true,
disableStream: true,
textLayerMode: TextLayerMode.DISABLE,
};

return this.preferences.getAll().then(function(prefs) {
for (let name in prefs) {
if ((name in OVERRIDES) && AppOptions.get(name) === OVERRIDES[name]) {
continue;
}
AppOptions.set('disableFontFace', value);
}),
preferences.get('useOnlyCssZoom').then(function resolved(value) {
AppOptions.set('useOnlyCssZoom', value);
}),
preferences.get('externalLinkTarget').then(function resolved(value) {
AppOptions.set('externalLinkTarget', value);
}),
preferences.get('renderer').then(function resolved(value) {
AppOptions.set('renderer', value);
}),
preferences.get('renderInteractiveForms').then(function resolved(value) {
AppOptions.set('renderInteractiveForms', value);
}),
preferences.get('disablePageMode').then(function resolved(value) {
AppOptions.set('disablePageMode', value);
}),
preferences.get('disablePageLabels').then(function resolved(value) {
AppOptions.set('disablePageLabels', value);
}),
preferences.get('enablePrintAutoRotate').then(function resolved(value) {
AppOptions.set('enablePrintAutoRotate', value);
}),
preferences.get('scrollModeOnLoad').then(function resolved(value) {
AppOptions.set('scrollModeOnLoad', value);
}),
preferences.get('spreadModeOnLoad').then(function resolved(value) {
AppOptions.set('spreadModeOnLoad', value);
}),
]).catch(function(reason) { });
AppOptions.set(name, prefs[name]);
}
}, function(reason) { });
},

/**
* @private
*/
_parseHashParameters() {
let { appConfig, } = this;
let waitOn = [];
const waitOn = [];

if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION') ||
AppOptions.get('pdfBugEnabled')) {
Expand Down Expand Up @@ -307,7 +247,7 @@ let PDFViewerApplication = {
case 'visible':
case 'shadow':
case 'hover':
let viewer = appConfig.viewerContainer;
let viewer = this.appConfig.viewerContainer;
viewer.classList.add('textLayer-' + hashParams['textlayer']);
break;
}
Expand Down
11 changes: 11 additions & 0 deletions web/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ class BasePreferences {
return defaultValue;
});
}

/**
* Get the values of all preferences.
* @return {Promise} A promise that is resolved with an {Object} containing
* the values of all preferences.
*/
getAll() {
return this._initializedPromise.then(() => {
return Object.assign(Object.create(null), this.defaults, this.prefs);
});
}
}

export {
Expand Down

0 comments on commit 34957ec

Please sign in to comment.