Merge pull request #9995 from Snuffleupagus/async-web
Refactor code in the `web/` folder to use `async`/`await`
This commit is contained in:
commit
510f1c84d2
109
web/app.js
109
web/app.js
@ -133,23 +133,22 @@ let PDFViewerApplication = {
|
|||||||
contentDispositionFilename: null,
|
contentDispositionFilename: null,
|
||||||
|
|
||||||
// Called once when the document is loaded.
|
// Called once when the document is loaded.
|
||||||
initialize(appConfig) {
|
async initialize(appConfig) {
|
||||||
this.preferences = this.externalServices.createPreferences();
|
this.preferences = this.externalServices.createPreferences();
|
||||||
this.appConfig = appConfig;
|
this.appConfig = appConfig;
|
||||||
|
|
||||||
return this._readPreferences().then(() => {
|
await this._readPreferences();
|
||||||
return this._parseHashParameters();
|
await this._parseHashParameters();
|
||||||
}).then(() => {
|
await this._initializeL10n();
|
||||||
return this._initializeL10n();
|
|
||||||
}).then(() => {
|
|
||||||
if (this.isViewerEmbedded &&
|
if (this.isViewerEmbedded &&
|
||||||
AppOptions.get('externalLinkTarget') === LinkTarget.NONE) {
|
AppOptions.get('externalLinkTarget') === LinkTarget.NONE) {
|
||||||
// Prevent external links from "replacing" the viewer,
|
// Prevent external links from "replacing" the viewer,
|
||||||
// when it's embedded in e.g. an <iframe> or an <object>.
|
// when it's embedded in e.g. an <iframe> or an <object>.
|
||||||
AppOptions.set('externalLinkTarget', LinkTarget.TOP);
|
AppOptions.set('externalLinkTarget', LinkTarget.TOP);
|
||||||
}
|
}
|
||||||
return this._initializeViewerComponents();
|
await this._initializeViewerComponents();
|
||||||
}).then(() => {
|
|
||||||
// Bind the various event handlers *after* the viewer has been
|
// Bind the various event handlers *after* the viewer has been
|
||||||
// initialized, to prevent errors if an event arrives too soon.
|
// initialized, to prevent errors if an event arrives too soon.
|
||||||
this.bindEvents();
|
this.bindEvents();
|
||||||
@ -164,13 +163,12 @@ let PDFViewerApplication = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_readPreferences() {
|
async _readPreferences() {
|
||||||
// A subset of the Preferences that `AppOptions`, for compatibility reasons,
|
// A subset of the Preferences that `AppOptions`, for compatibility reasons,
|
||||||
// is allowed to override if the `AppOptions` values matches the ones below.
|
// is allowed to override if the `AppOptions` values matches the ones below.
|
||||||
const OVERRIDES = {
|
const OVERRIDES = {
|
||||||
@ -180,24 +178,27 @@ let PDFViewerApplication = {
|
|||||||
textLayerMode: TextLayerMode.DISABLE,
|
textLayerMode: TextLayerMode.DISABLE,
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.preferences.getAll().then(function(prefs) {
|
try {
|
||||||
|
const prefs = await this.preferences.getAll();
|
||||||
for (let name in prefs) {
|
for (let name in prefs) {
|
||||||
if ((name in OVERRIDES) && AppOptions.get(name) === OVERRIDES[name]) {
|
if ((name in OVERRIDES) && AppOptions.get(name) === OVERRIDES[name]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
AppOptions.set(name, prefs[name]);
|
AppOptions.set(name, prefs[name]);
|
||||||
}
|
}
|
||||||
}, function(reason) { });
|
} catch (reason) { }
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_parseHashParameters() {
|
async _parseHashParameters() {
|
||||||
|
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('PRODUCTION') &&
|
||||||
|
!AppOptions.get('pdfBugEnabled')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const waitOn = [];
|
const waitOn = [];
|
||||||
|
|
||||||
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION') ||
|
|
||||||
AppOptions.get('pdfBugEnabled')) {
|
|
||||||
// Special debugging flags in the hash section of the URL.
|
// Special debugging flags in the hash section of the URL.
|
||||||
let hash = document.location.hash.substring(1);
|
let hash = document.location.hash.substring(1);
|
||||||
let hashParams = parseQueryString(hash);
|
let hashParams = parseQueryString(hash);
|
||||||
@ -221,15 +222,13 @@ let PDFViewerApplication = {
|
|||||||
hashParams['disablefontface'] === 'true');
|
hashParams['disablefontface'] === 'true');
|
||||||
}
|
}
|
||||||
if ('disablehistory' in hashParams) {
|
if ('disablehistory' in hashParams) {
|
||||||
AppOptions.set('disableHistory',
|
AppOptions.set('disableHistory', hashParams['disablehistory'] === 'true');
|
||||||
hashParams['disablehistory'] === 'true');
|
|
||||||
}
|
}
|
||||||
if ('webgl' in hashParams) {
|
if ('webgl' in hashParams) {
|
||||||
AppOptions.set('enableWebGL', hashParams['webgl'] === 'true');
|
AppOptions.set('enableWebGL', hashParams['webgl'] === 'true');
|
||||||
}
|
}
|
||||||
if ('useonlycsszoom' in hashParams) {
|
if ('useonlycsszoom' in hashParams) {
|
||||||
AppOptions.set('useOnlyCssZoom',
|
AppOptions.set('useOnlyCssZoom', hashParams['useonlycsszoom'] === 'true');
|
||||||
hashParams['useonlycsszoom'] === 'true');
|
|
||||||
}
|
}
|
||||||
if ('verbosity' in hashParams) {
|
if ('verbosity' in hashParams) {
|
||||||
AppOptions.set('verbosity', hashParams['verbosity'] | 0);
|
AppOptions.set('verbosity', hashParams['verbosity'] | 0);
|
||||||
@ -262,7 +261,6 @@ let PDFViewerApplication = {
|
|||||||
PDFJSDev.test('!PRODUCTION || GENERIC')) && 'locale' in hashParams) {
|
PDFJSDev.test('!PRODUCTION || GENERIC')) && 'locale' in hashParams) {
|
||||||
AppOptions.set('locale', hashParams['locale']);
|
AppOptions.set('locale', hashParams['locale']);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.all(waitOn);
|
return Promise.all(waitOn);
|
||||||
},
|
},
|
||||||
@ -270,22 +268,20 @@ let PDFViewerApplication = {
|
|||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_initializeL10n() {
|
async _initializeL10n() {
|
||||||
this.l10n = this.externalServices.createL10n({
|
this.l10n = this.externalServices.createL10n({
|
||||||
locale: AppOptions.get('locale'),
|
locale: AppOptions.get('locale'),
|
||||||
});
|
});
|
||||||
return this.l10n.getDirection().then((dir) => {
|
const dir = await this.l10n.getDirection();
|
||||||
document.getElementsByTagName('html')[0].dir = dir;
|
document.getElementsByTagName('html')[0].dir = dir;
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_initializeViewerComponents() {
|
async _initializeViewerComponents() {
|
||||||
let { appConfig, } = this;
|
const appConfig = this.appConfig;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
this.overlayManager = new OverlayManager();
|
this.overlayManager = new OverlayManager();
|
||||||
|
|
||||||
const dispatchToDOM = AppOptions.get('eventBusDispatchToDOM');
|
const dispatchToDOM = AppOptions.get('eventBusDispatchToDOM');
|
||||||
@ -426,8 +422,6 @@ let PDFViewerApplication = {
|
|||||||
|
|
||||||
this.pdfSidebarResizer = new PDFSidebarResizer(appConfig.sidebarResizer,
|
this.pdfSidebarResizer = new PDFSidebarResizer(appConfig.sidebarResizer,
|
||||||
eventBus, this.l10n);
|
eventBus, this.l10n);
|
||||||
resolve(undefined);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
run(config) {
|
run(config) {
|
||||||
@ -582,12 +576,12 @@ let PDFViewerApplication = {
|
|||||||
* @returns {Promise} - Returns the promise, which is resolved when all
|
* @returns {Promise} - Returns the promise, which is resolved when all
|
||||||
* destruction is completed.
|
* destruction is completed.
|
||||||
*/
|
*/
|
||||||
close() {
|
async close() {
|
||||||
let errorWrapper = this.appConfig.errorWrapper.container;
|
let errorWrapper = this.appConfig.errorWrapper.container;
|
||||||
errorWrapper.setAttribute('hidden', 'true');
|
errorWrapper.setAttribute('hidden', 'true');
|
||||||
|
|
||||||
if (!this.pdfLoadingTask) {
|
if (!this.pdfLoadingTask) {
|
||||||
return Promise.resolve();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let promise = this.pdfLoadingTask.destroy();
|
let promise = this.pdfLoadingTask.destroy();
|
||||||
@ -632,13 +626,10 @@ let PDFViewerApplication = {
|
|||||||
* @returns {Promise} - Returns the promise, which is resolved when document
|
* @returns {Promise} - Returns the promise, which is resolved when document
|
||||||
* is opened.
|
* is opened.
|
||||||
*/
|
*/
|
||||||
open(file, args) {
|
async open(file, args) {
|
||||||
if (this.pdfLoadingTask) {
|
if (this.pdfLoadingTask) {
|
||||||
// We need to destroy already opened document.
|
// We need to destroy already opened document.
|
||||||
return this.close().then(() => {
|
await this.close();
|
||||||
// ... and repeat the open() call.
|
|
||||||
return this.open(file, args);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
// Set the necessary global worker parameters, using the available options.
|
// Set the necessary global worker parameters, using the available options.
|
||||||
const workerParameters = AppOptions.getAll('worker');
|
const workerParameters = AppOptions.getAll('worker');
|
||||||
@ -951,10 +942,6 @@ let PDFViewerApplication = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let initialParams = {
|
|
||||||
bookmark: null,
|
|
||||||
hash: null,
|
|
||||||
};
|
|
||||||
let storePromise = store.getMultiple({
|
let storePromise = store.getMultiple({
|
||||||
page: null,
|
page: null,
|
||||||
zoom: DEFAULT_SCALE_VALUE,
|
zoom: DEFAULT_SCALE_VALUE,
|
||||||
@ -966,8 +953,10 @@ let PDFViewerApplication = {
|
|||||||
spreadMode: null,
|
spreadMode: null,
|
||||||
}).catch(() => { /* Unable to read from storage; ignoring errors. */ });
|
}).catch(() => { /* Unable to read from storage; ignoring errors. */ });
|
||||||
|
|
||||||
Promise.all([storePromise, pageModePromise]).then(
|
Promise.all([
|
||||||
([values = {}, pageMode]) => {
|
storePromise, pageModePromise,
|
||||||
|
]).then(async ([values = {}, pageMode]) => {
|
||||||
|
const initialBookmark = this.initialBookmark;
|
||||||
// Initialize the default values, from user preferences.
|
// Initialize the default values, from user preferences.
|
||||||
const zoom = AppOptions.get('defaultZoomValue');
|
const zoom = AppOptions.get('defaultZoomValue');
|
||||||
let hash = zoom ? `zoom=${zoom}` : null;
|
let hash = zoom ? `zoom=${zoom}` : null;
|
||||||
@ -990,52 +979,40 @@ let PDFViewerApplication = {
|
|||||||
// Always let the user preference/history take precedence.
|
// Always let the user preference/history take precedence.
|
||||||
sidebarView = sidebarView || apiPageModeToSidebarView(pageMode);
|
sidebarView = sidebarView || apiPageModeToSidebarView(pageMode);
|
||||||
}
|
}
|
||||||
return {
|
|
||||||
hash,
|
|
||||||
rotation,
|
|
||||||
sidebarView,
|
|
||||||
scrollMode,
|
|
||||||
spreadMode,
|
|
||||||
};
|
|
||||||
}).then(({ hash, rotation, sidebarView, scrollMode, spreadMode, }) => {
|
|
||||||
initialParams.bookmark = this.initialBookmark;
|
|
||||||
initialParams.hash = hash;
|
|
||||||
|
|
||||||
this.setInitialView(hash, {
|
this.setInitialView(hash, {
|
||||||
rotation, sidebarView, scrollMode, spreadMode,
|
rotation, sidebarView, scrollMode, spreadMode,
|
||||||
});
|
});
|
||||||
this.eventBus.dispatch('documentinit', { source: this, });
|
this.eventBus.dispatch('documentinit', { source: this, });
|
||||||
|
|
||||||
// Make all navigation keys work on document load,
|
// Make all navigation keys work on document load,
|
||||||
// unless the viewer is embedded in a web page.
|
// unless the viewer is embedded in a web page.
|
||||||
if (!this.isViewerEmbedded) {
|
if (!this.isViewerEmbedded) {
|
||||||
pdfViewer.focus();
|
pdfViewer.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.race([
|
// For documents with different page sizes, once all pages are resolved,
|
||||||
|
// ensure that the correct location becomes visible on load.
|
||||||
|
// (To reduce the risk, in very large and/or slow loading documents,
|
||||||
|
// that the location changes *after* the user has started interacting
|
||||||
|
// with the viewer, wait for either `pagesPromise` or a timeout.)
|
||||||
|
await Promise.race([
|
||||||
pagesPromise,
|
pagesPromise,
|
||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
setTimeout(resolve, FORCE_PAGES_LOADED_TIMEOUT);
|
setTimeout(resolve, FORCE_PAGES_LOADED_TIMEOUT);
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
}).then(() => {
|
if (!initialBookmark && !hash) {
|
||||||
// For documents with different page sizes, once all pages are resolved,
|
|
||||||
// ensure that the correct location becomes visible on load.
|
|
||||||
// To reduce the risk, in very large and/or slow loading documents,
|
|
||||||
// that the location changes *after* the user has started interacting
|
|
||||||
// with the viewer, wait for either `pagesPromise` or a timeout above.
|
|
||||||
|
|
||||||
if (!initialParams.bookmark && !initialParams.hash) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (pdfViewer.hasEqualPageSizes) {
|
if (pdfViewer.hasEqualPageSizes) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.initialBookmark = initialParams.bookmark;
|
this.initialBookmark = initialBookmark;
|
||||||
|
|
||||||
// eslint-disable-next-line no-self-assign
|
// eslint-disable-next-line no-self-assign
|
||||||
pdfViewer.currentScaleValue = pdfViewer.currentScaleValue;
|
pdfViewer.currentScaleValue = pdfViewer.currentScaleValue;
|
||||||
this.setInitialView(initialParams.hash);
|
// Re-apply the initial document location.
|
||||||
|
this.setInitialView(hash);
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
// At this point, rendering of the initial page(s) should always have
|
// At this point, rendering of the initial page(s) should always have
|
||||||
// started (and may even have completed).
|
// started (and may even have completed).
|
||||||
@ -1629,14 +1606,14 @@ function webViewerInitialized() {
|
|||||||
PDFViewerApplication.pdfSidebar.toggle();
|
PDFViewerApplication.pdfSidebar.toggle();
|
||||||
});
|
});
|
||||||
|
|
||||||
Promise.resolve().then(function() {
|
try {
|
||||||
webViewerOpenFileViaURL(file);
|
webViewerOpenFileViaURL(file);
|
||||||
}).catch(function(reason) {
|
} catch (reason) {
|
||||||
PDFViewerApplication.l10n.get('loading_error', null,
|
PDFViewerApplication.l10n.get('loading_error', null,
|
||||||
'An error occurred while loading the PDF.').then((msg) => {
|
'An error occurred while loading the PDF.').then((msg) => {
|
||||||
PDFViewerApplication.error(msg, reason);
|
PDFViewerApplication.error(msg, reason);
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let webViewerOpenFileViaURL;
|
let webViewerOpenFileViaURL;
|
||||||
|
@ -300,7 +300,7 @@ function setReferer(url, callback) {
|
|||||||
let storageArea = chrome.storage.sync || chrome.storage.local;
|
let storageArea = chrome.storage.sync || chrome.storage.local;
|
||||||
|
|
||||||
class ChromePreferences extends BasePreferences {
|
class ChromePreferences extends BasePreferences {
|
||||||
_writeToStorage(prefObj) {
|
async _writeToStorage(prefObj) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
if (prefObj === this.defaults) {
|
if (prefObj === this.defaults) {
|
||||||
let keysToRemove = Object.keys(this.defaults);
|
let keysToRemove = Object.keys(this.defaults);
|
||||||
@ -317,7 +317,7 @@ class ChromePreferences extends BasePreferences {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_readFromStorage(prefObj) {
|
async _readFromStorage(prefObj) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
let getPreferences = (defaultPrefs) => {
|
let getPreferences = (defaultPrefs) => {
|
||||||
if (chrome.runtime.lastError) {
|
if (chrome.runtime.lastError) {
|
||||||
|
@ -124,13 +124,13 @@ class DownloadManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class FirefoxPreferences extends BasePreferences {
|
class FirefoxPreferences extends BasePreferences {
|
||||||
_writeToStorage(prefObj) {
|
async _writeToStorage(prefObj) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
FirefoxCom.request('setPreferences', prefObj, resolve);
|
FirefoxCom.request('setPreferences', prefObj, resolve);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_readFromStorage(prefObj) {
|
async _readFromStorage(prefObj) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
FirefoxCom.request('getPreferences', prefObj, function(prefStr) {
|
FirefoxCom.request('getPreferences', prefObj, function(prefStr) {
|
||||||
let readPrefs = JSON.parse(prefStr);
|
let readPrefs = JSON.parse(prefStr);
|
||||||
@ -145,21 +145,20 @@ class MozL10n {
|
|||||||
this.mozL10n = mozL10n;
|
this.mozL10n = mozL10n;
|
||||||
}
|
}
|
||||||
|
|
||||||
getLanguage() {
|
async getLanguage() {
|
||||||
return Promise.resolve(this.mozL10n.getLanguage());
|
return this.mozL10n.getLanguage();
|
||||||
}
|
}
|
||||||
|
|
||||||
getDirection() {
|
async getDirection() {
|
||||||
return Promise.resolve(this.mozL10n.getDirection());
|
return this.mozL10n.getDirection();
|
||||||
}
|
}
|
||||||
|
|
||||||
get(property, args, fallback) {
|
async get(property, args, fallback) {
|
||||||
return Promise.resolve(this.mozL10n.get(property, args, fallback));
|
return this.mozL10n.get(property, args, fallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
translate(element) {
|
async translate(element) {
|
||||||
this.mozL10n.translate(element);
|
this.mozL10n.translate(element);
|
||||||
return Promise.resolve();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,18 +26,12 @@ if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('GENERIC')) {
|
|||||||
let GenericCom = {};
|
let GenericCom = {};
|
||||||
|
|
||||||
class GenericPreferences extends BasePreferences {
|
class GenericPreferences extends BasePreferences {
|
||||||
_writeToStorage(prefObj) {
|
async _writeToStorage(prefObj) {
|
||||||
return new Promise(function(resolve) {
|
|
||||||
localStorage.setItem('pdfjs.preferences', JSON.stringify(prefObj));
|
localStorage.setItem('pdfjs.preferences', JSON.stringify(prefObj));
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_readFromStorage(prefObj) {
|
async _readFromStorage(prefObj) {
|
||||||
return new Promise(function(resolve) {
|
return JSON.parse(localStorage.getItem('pdfjs.preferences'));
|
||||||
let readPrefs = JSON.parse(localStorage.getItem('pdfjs.preferences'));
|
|
||||||
resolve(readPrefs);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,28 +27,24 @@ class GenericL10n {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getLanguage() {
|
async getLanguage() {
|
||||||
return this._ready.then((l10n) => {
|
const l10n = await this._ready;
|
||||||
return l10n.getLanguage();
|
return l10n.getLanguage();
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getDirection() {
|
async getDirection() {
|
||||||
return this._ready.then((l10n) => {
|
const l10n = await this._ready;
|
||||||
return l10n.getDirection();
|
return l10n.getDirection();
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get(property, args, fallback) {
|
async get(property, args, fallback) {
|
||||||
return this._ready.then((l10n) => {
|
const l10n = await this._ready;
|
||||||
return l10n.get(property, args, fallback);
|
return l10n.get(property, args, fallback);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
translate(element) {
|
async translate(element) {
|
||||||
return this._ready.then((l10n) => {
|
const l10n = await this._ready;
|
||||||
return l10n.translate(element);
|
return l10n.translate(element);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,12 +163,12 @@ class IL10n {
|
|||||||
/**
|
/**
|
||||||
* @returns {Promise<string>} - Resolves to the current locale.
|
* @returns {Promise<string>} - Resolves to the current locale.
|
||||||
*/
|
*/
|
||||||
getLanguage() {}
|
async getLanguage() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {Promise<string>} - Resolves to 'rtl' or 'ltr'.
|
* @returns {Promise<string>} - Resolves to 'rtl' or 'ltr'.
|
||||||
*/
|
*/
|
||||||
getDirection() {}
|
async getDirection() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Translates text identified by the key and adds/formats data using the args
|
* Translates text identified by the key and adds/formats data using the args
|
||||||
@ -179,12 +179,12 @@ class IL10n {
|
|||||||
* @param {string} fallback
|
* @param {string} fallback
|
||||||
* @returns {Promise<string>}
|
* @returns {Promise<string>}
|
||||||
*/
|
*/
|
||||||
get(key, args, fallback) { }
|
async get(key, args, fallback) { }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Translates HTML element.
|
* Translates HTML element.
|
||||||
* @param {HTMLElement} element
|
* @param {HTMLElement} element
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
translate(element) { }
|
async translate(element) { }
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,8 @@ class OverlayManager {
|
|||||||
* @returns {Promise} A promise that is resolved when the overlay has been
|
* @returns {Promise} A promise that is resolved when the overlay has been
|
||||||
* registered.
|
* registered.
|
||||||
*/
|
*/
|
||||||
register(name, element, callerCloseMethod = null, canForceClose = false) {
|
async register(name, element, callerCloseMethod = null,
|
||||||
return new Promise((resolve) => {
|
canForceClose = false) {
|
||||||
let container;
|
let container;
|
||||||
if (!name || !element || !(container = element.parentNode)) {
|
if (!name || !element || !(container = element.parentNode)) {
|
||||||
throw new Error('Not enough parameters.');
|
throw new Error('Not enough parameters.');
|
||||||
@ -51,8 +51,6 @@ class OverlayManager {
|
|||||||
callerCloseMethod,
|
callerCloseMethod,
|
||||||
canForceClose,
|
canForceClose,
|
||||||
};
|
};
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,16 +58,13 @@ class OverlayManager {
|
|||||||
* @returns {Promise} A promise that is resolved when the overlay has been
|
* @returns {Promise} A promise that is resolved when the overlay has been
|
||||||
* unregistered.
|
* unregistered.
|
||||||
*/
|
*/
|
||||||
unregister(name) {
|
async unregister(name) {
|
||||||
return new Promise((resolve) => {
|
|
||||||
if (!this._overlays[name]) {
|
if (!this._overlays[name]) {
|
||||||
throw new Error('The overlay does not exist.');
|
throw new Error('The overlay does not exist.');
|
||||||
} else if (this._active === name) {
|
} else if (this._active === name) {
|
||||||
throw new Error('The overlay cannot be removed while it is active.');
|
throw new Error('The overlay cannot be removed while it is active.');
|
||||||
}
|
}
|
||||||
delete this._overlays[name];
|
delete this._overlays[name];
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,8 +72,7 @@ class OverlayManager {
|
|||||||
* @returns {Promise} A promise that is resolved when the overlay has been
|
* @returns {Promise} A promise that is resolved when the overlay has been
|
||||||
* opened.
|
* opened.
|
||||||
*/
|
*/
|
||||||
open(name) {
|
async open(name) {
|
||||||
return new Promise((resolve) => {
|
|
||||||
if (!this._overlays[name]) {
|
if (!this._overlays[name]) {
|
||||||
throw new Error('The overlay does not exist.');
|
throw new Error('The overlay does not exist.');
|
||||||
} else if (this._active) {
|
} else if (this._active) {
|
||||||
@ -95,8 +89,6 @@ class OverlayManager {
|
|||||||
this._overlays[this._active].container.classList.remove('hidden');
|
this._overlays[this._active].container.classList.remove('hidden');
|
||||||
|
|
||||||
window.addEventListener('keydown', this._keyDownBound);
|
window.addEventListener('keydown', this._keyDownBound);
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -104,8 +96,7 @@ class OverlayManager {
|
|||||||
* @returns {Promise} A promise that is resolved when the overlay has been
|
* @returns {Promise} A promise that is resolved when the overlay has been
|
||||||
* closed.
|
* closed.
|
||||||
*/
|
*/
|
||||||
close(name) {
|
async close(name) {
|
||||||
return new Promise((resolve) => {
|
|
||||||
if (!this._overlays[name]) {
|
if (!this._overlays[name]) {
|
||||||
throw new Error('The overlay does not exist.');
|
throw new Error('The overlay does not exist.');
|
||||||
} else if (!this._active) {
|
} else if (!this._active) {
|
||||||
@ -118,8 +109,6 @@ class OverlayManager {
|
|||||||
this._active = null;
|
this._active = null;
|
||||||
|
|
||||||
window.removeEventListener('keydown', this._keyDownBound);
|
window.removeEventListener('keydown', this._keyDownBound);
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -83,8 +83,8 @@ class BasePreferences {
|
|||||||
* @return {Promise} A promise that is resolved when the preference values
|
* @return {Promise} A promise that is resolved when the preference values
|
||||||
* have been written.
|
* have been written.
|
||||||
*/
|
*/
|
||||||
_writeToStorage(prefObj) {
|
async _writeToStorage(prefObj) {
|
||||||
return Promise.reject(new Error('Not implemented: _writeToStorage'));
|
throw new Error('Not implemented: _writeToStorage');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -93,8 +93,8 @@ class BasePreferences {
|
|||||||
* @return {Promise} A promise that is resolved with an {Object} containing
|
* @return {Promise} A promise that is resolved with an {Object} containing
|
||||||
* the preferences that have been read.
|
* the preferences that have been read.
|
||||||
*/
|
*/
|
||||||
_readFromStorage(prefObj) {
|
async _readFromStorage(prefObj) {
|
||||||
return Promise.reject(new Error('Not implemented: _readFromStorage'));
|
throw new Error('Not implemented: _readFromStorage');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,11 +102,10 @@ class BasePreferences {
|
|||||||
* @return {Promise} A promise that is resolved when the preference values
|
* @return {Promise} A promise that is resolved when the preference values
|
||||||
* have been reset.
|
* have been reset.
|
||||||
*/
|
*/
|
||||||
reset() {
|
async reset() {
|
||||||
return this._initializedPromise.then(() => {
|
await this._initializedPromise;
|
||||||
this.prefs = Object.assign(Object.create(null), this.defaults);
|
this.prefs = Object.assign(Object.create(null), this.defaults);
|
||||||
return this._writeToStorage(this.defaults);
|
return this._writeToStorage(this.defaults);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -116,15 +115,17 @@ class BasePreferences {
|
|||||||
* @return {Promise} A promise that is resolved when the value has been set,
|
* @return {Promise} A promise that is resolved when the value has been set,
|
||||||
* provided that the preference exists and the types match.
|
* provided that the preference exists and the types match.
|
||||||
*/
|
*/
|
||||||
set(name, value) {
|
async set(name, value) {
|
||||||
return this._initializedPromise.then(() => {
|
await this._initializedPromise;
|
||||||
if (this.defaults[name] === undefined) {
|
let defaultValue = this.defaults[name];
|
||||||
|
|
||||||
|
if (defaultValue === undefined) {
|
||||||
throw new Error(`Set preference: "${name}" is undefined.`);
|
throw new Error(`Set preference: "${name}" is undefined.`);
|
||||||
} else if (value === undefined) {
|
} else if (value === undefined) {
|
||||||
throw new Error('Set preference: no value is specified.');
|
throw new Error('Set preference: no value is specified.');
|
||||||
}
|
}
|
||||||
let valueType = typeof value;
|
let valueType = typeof value;
|
||||||
let defaultType = typeof this.defaults[name];
|
let defaultType = typeof defaultValue;
|
||||||
|
|
||||||
if (valueType !== defaultType) {
|
if (valueType !== defaultType) {
|
||||||
if (valueType === 'number' && defaultType === 'string') {
|
if (valueType === 'number' && defaultType === 'string') {
|
||||||
@ -140,7 +141,6 @@ class BasePreferences {
|
|||||||
}
|
}
|
||||||
this.prefs[name] = value;
|
this.prefs[name] = value;
|
||||||
return this._writeToStorage(this.prefs);
|
return this._writeToStorage(this.prefs);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -149,8 +149,8 @@ class BasePreferences {
|
|||||||
* @return {Promise} A promise that is resolved with a {boolean|number|string}
|
* @return {Promise} A promise that is resolved with a {boolean|number|string}
|
||||||
* containing the value of the preference.
|
* containing the value of the preference.
|
||||||
*/
|
*/
|
||||||
get(name) {
|
async get(name) {
|
||||||
return this._initializedPromise.then(() => {
|
await this._initializedPromise;
|
||||||
let defaultValue = this.defaults[name];
|
let defaultValue = this.defaults[name];
|
||||||
|
|
||||||
if (defaultValue === undefined) {
|
if (defaultValue === undefined) {
|
||||||
@ -163,7 +163,6 @@ class BasePreferences {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -171,10 +170,9 @@ class BasePreferences {
|
|||||||
* @return {Promise} A promise that is resolved with an {Object} containing
|
* @return {Promise} A promise that is resolved with an {Object} containing
|
||||||
* the values of all preferences.
|
* the values of all preferences.
|
||||||
*/
|
*/
|
||||||
getAll() {
|
async getAll() {
|
||||||
return this._initializedPromise.then(() => {
|
await this._initializedPromise;
|
||||||
return Object.assign(Object.create(null), this.defaults, this.prefs);
|
return Object.assign(Object.create(null), this.defaults, this.prefs);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,21 +56,19 @@ function formatL10nValue(text, args) {
|
|||||||
* @implements {IL10n}
|
* @implements {IL10n}
|
||||||
*/
|
*/
|
||||||
let NullL10n = {
|
let NullL10n = {
|
||||||
getLanguage() {
|
async getLanguage() {
|
||||||
return Promise.resolve('en-us');
|
return 'en-us';
|
||||||
},
|
},
|
||||||
|
|
||||||
getDirection() {
|
async getDirection() {
|
||||||
return Promise.resolve('ltr');
|
return 'ltr';
|
||||||
},
|
},
|
||||||
|
|
||||||
get(property, args, fallback) {
|
async get(property, args, fallback) {
|
||||||
return Promise.resolve(formatL10nValue(fallback, args));
|
return formatL10nValue(fallback, args);
|
||||||
},
|
},
|
||||||
|
|
||||||
translate(element) {
|
async translate(element) { },
|
||||||
return Promise.resolve();
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,56 +54,47 @@ class ViewHistory {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_writeToStorage() {
|
async _writeToStorage() {
|
||||||
return new Promise((resolve) => {
|
|
||||||
let databaseStr = JSON.stringify(this.database);
|
let databaseStr = JSON.stringify(this.database);
|
||||||
|
|
||||||
if (typeof PDFJSDev !== 'undefined' &&
|
if (typeof PDFJSDev !== 'undefined' &&
|
||||||
PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
|
PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
|
||||||
sessionStorage.setItem('pdfjs.history', databaseStr);
|
sessionStorage.setItem('pdfjs.history', databaseStr);
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
localStorage.setItem('pdfjs.history', databaseStr);
|
localStorage.setItem('pdfjs.history', databaseStr);
|
||||||
}
|
}
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
_readFromStorage() {
|
async _readFromStorage() {
|
||||||
return new Promise(function(resolve) {
|
|
||||||
if (typeof PDFJSDev !== 'undefined' &&
|
if (typeof PDFJSDev !== 'undefined' &&
|
||||||
PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
|
PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
|
||||||
resolve(sessionStorage.getItem('pdfjs.history'));
|
return sessionStorage.getItem('pdfjs.history');
|
||||||
} else {
|
|
||||||
resolve(localStorage.getItem('pdfjs.history'));
|
|
||||||
}
|
}
|
||||||
});
|
return localStorage.getItem('pdfjs.history');
|
||||||
}
|
}
|
||||||
|
|
||||||
set(name, val) {
|
async set(name, val) {
|
||||||
return this._initializedPromise.then(() => {
|
await this._initializedPromise;
|
||||||
this.file[name] = val;
|
this.file[name] = val;
|
||||||
return this._writeToStorage();
|
return this._writeToStorage();
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setMultiple(properties) {
|
async setMultiple(properties) {
|
||||||
return this._initializedPromise.then(() => {
|
await this._initializedPromise;
|
||||||
for (let name in properties) {
|
for (let name in properties) {
|
||||||
this.file[name] = properties[name];
|
this.file[name] = properties[name];
|
||||||
}
|
}
|
||||||
return this._writeToStorage();
|
return this._writeToStorage();
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get(name, defaultValue) {
|
async get(name, defaultValue) {
|
||||||
return this._initializedPromise.then(() => {
|
await this._initializedPromise;
|
||||||
let val = this.file[name];
|
let val = this.file[name];
|
||||||
return val !== undefined ? val : defaultValue;
|
return val !== undefined ? val : defaultValue;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getMultiple(properties) {
|
async getMultiple(properties) {
|
||||||
return this._initializedPromise.then(() => {
|
await this._initializedPromise;
|
||||||
let values = Object.create(null);
|
let values = Object.create(null);
|
||||||
|
|
||||||
for (let name in properties) {
|
for (let name in properties) {
|
||||||
@ -111,7 +102,6 @@ class ViewHistory {
|
|||||||
values[name] = val !== undefined ? val : properties[name];
|
values[name] = val !== undefined ? val : properties[name];
|
||||||
}
|
}
|
||||||
return values;
|
return values;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user