From 09da99b8a0fb908cde084c3359320cdea2ad6869 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 17 Feb 2018 13:22:26 +0100 Subject: [PATCH] Introduce a `AppOptions` abstraction in preparation for a complete refactoring of the way that viewer options are handled The way that various options are handled in the default viewer is currently a bit of a mess (to say the least). Some viewer options reside in the global `PDFJS` object, while others reside in `Preferences`. To make matters worse, some options even exist in both of the two. Since the goal, with PDF.js version `2.0`, is to reduce our usage of the global `PDFJS` object, we'll instead want pass in the options when initializing the viewer components and when calling API methods (such as `getDocument`). However given the current state of things in the default viewer, this wouldn't be exactly easy to implement. Hence this patch, which attempts to consolidate the way that viewer (and later API) options are handled by introducing a `AppOptions` singleton that provides *one* centralized way of interacting with the various options in the default viewer. --- web/app_options.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++ web/viewer.js | 8 ++++-- 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 web/app_options.js diff --git a/web/app_options.js b/web/app_options.js new file mode 100644 index 000000000..4325d22df --- /dev/null +++ b/web/app_options.js @@ -0,0 +1,70 @@ +/* Copyright 2018 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const OptionKind = { + VIEWER: 'viewer', + API: 'api', + WORKER: 'worker', +}; + +/** + * PLEASE NOTE: To avoid introducing unnecessary dependencies, we specify the + * values below *explicitly* rather than relying on imported types; + * compare with the format of `default_preferences.json`. + */ +const defaultOptions = { +}; + +const userOptions = Object.create(null); + +class AppOptions { + constructor() { + throw new Error('Cannot initialize AppOptions.'); + } + + static get(name) { + let defaultOption = defaultOptions[name], userOption = userOptions[name]; + if (userOption !== undefined) { + return userOption; + } + return (defaultOption !== undefined ? defaultOption.value : undefined); + } + + static getAll(kind = null) { + let options = Object.create(null); + for (let name in defaultOptions) { + let defaultOption = defaultOptions[name], userOption = userOptions[name]; + if (kind && defaultOption.kind !== kind) { + continue; + } + options[name] = (userOption !== undefined ? + userOption : defaultOption.value); + } + return options; + } + + static set(name, value) { + userOptions[name] = value; + } + + static remove(name) { + delete userOptions[name]; + } +} + +export { + AppOptions, + OptionKind, +}; diff --git a/web/viewer.js b/web/viewer.js index ba680ec65..ecd7bc6cf 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -35,9 +35,10 @@ if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME')) { })(); } -let pdfjsWebApp; +let pdfjsWebApp, pdfjsWebAppOptions; if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('PRODUCTION')) { pdfjsWebApp = require('./app.js'); + pdfjsWebAppOptions = require('./app_options.js'); } if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('FIREFOX || MOZCENTRAL')) { @@ -180,14 +181,17 @@ function webViewerLoad() { if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) { Promise.all([ SystemJS.import('pdfjs-web/app'), + SystemJS.import('pdfjs-web/app_options'), SystemJS.import('pdfjs-web/genericcom'), SystemJS.import('pdfjs-web/pdf_print_service'), - ]).then(function([app, ...otherModules]) { + ]).then(function([app, appOptions, ...otherModules]) { window.PDFViewerApplication = app.PDFViewerApplication; + window.PDFViewerApplicationOptions = appOptions.AppOptions; app.PDFViewerApplication.run(config); }); } else { window.PDFViewerApplication = pdfjsWebApp.PDFViewerApplication; + window.PDFViewerApplicationOptions = pdfjsWebAppOptions.AppOptions; pdfjsWebApp.PDFViewerApplication.run(config); } }