Merge pull request #8303 from Snuffleupagus/es6-preferences
Convert the `Preferences` to an ES6 class
This commit is contained in:
commit
5fe26bb9da
96
web/app.js
96
web/app.js
@ -42,7 +42,6 @@ import { PDFLinkService } from './pdf_link_service';
|
|||||||
import { PDFOutlineViewer } from './pdf_outline_viewer';
|
import { PDFOutlineViewer } from './pdf_outline_viewer';
|
||||||
import { PDFPresentationMode } from './pdf_presentation_mode';
|
import { PDFPresentationMode } from './pdf_presentation_mode';
|
||||||
import { PDFThumbnailViewer } from './pdf_thumbnail_viewer';
|
import { PDFThumbnailViewer } from './pdf_thumbnail_viewer';
|
||||||
import { Preferences } from './preferences';
|
|
||||||
import { SecondaryToolbar } from './secondary_toolbar';
|
import { SecondaryToolbar } from './secondary_toolbar';
|
||||||
import { Toolbar } from './toolbar';
|
import { Toolbar } from './toolbar';
|
||||||
import { ViewHistory } from './view_history';
|
import { ViewHistory } from './view_history';
|
||||||
@ -75,6 +74,9 @@ var DefaultExternalServices = {
|
|||||||
createDownloadManager: function () {
|
createDownloadManager: function () {
|
||||||
throw new Error('Not implemented: createDownloadManager');
|
throw new Error('Not implemented: createDownloadManager');
|
||||||
},
|
},
|
||||||
|
createPreferences() {
|
||||||
|
throw new Error('Not implemented: createPreferences');
|
||||||
|
},
|
||||||
supportsIntegratedFind: false,
|
supportsIntegratedFind: false,
|
||||||
supportsDocumentFonts: true,
|
supportsDocumentFonts: true,
|
||||||
supportsDocumentColors: true,
|
supportsDocumentColors: true,
|
||||||
@ -117,6 +119,8 @@ var PDFViewerApplication = {
|
|||||||
store: null,
|
store: null,
|
||||||
/** @type {DownloadManager} */
|
/** @type {DownloadManager} */
|
||||||
downloadManager: null,
|
downloadManager: null,
|
||||||
|
/** @type {Preferences} */
|
||||||
|
preferences: null,
|
||||||
/** @type {Toolbar} */
|
/** @type {Toolbar} */
|
||||||
toolbar: null,
|
toolbar: null,
|
||||||
/** @type {SecondaryToolbar} */
|
/** @type {SecondaryToolbar} */
|
||||||
@ -143,37 +147,34 @@ var PDFViewerApplication = {
|
|||||||
|
|
||||||
// called once when the document is loaded
|
// called once when the document is loaded
|
||||||
initialize: function pdfViewInitialize(appConfig) {
|
initialize: function pdfViewInitialize(appConfig) {
|
||||||
var self = this;
|
this.preferences = this.externalServices.createPreferences();
|
||||||
|
|
||||||
Preferences.initialize();
|
|
||||||
this.preferences = Preferences;
|
|
||||||
|
|
||||||
configure(PDFJS);
|
configure(PDFJS);
|
||||||
this.appConfig = appConfig;
|
this.appConfig = appConfig;
|
||||||
|
|
||||||
return this._readPreferences().then(function () {
|
return this._readPreferences().then(() => {
|
||||||
return self._initializeViewerComponents();
|
return this._initializeViewerComponents();
|
||||||
}).then(function () {
|
}).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.
|
||||||
self.bindEvents();
|
this.bindEvents();
|
||||||
self.bindWindowEvents();
|
this.bindWindowEvents();
|
||||||
|
|
||||||
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
|
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
|
||||||
// For backwards compatibility, we dispatch the 'localized' event on
|
// For backwards compatibility, we dispatch the 'localized' event on
|
||||||
// the `eventBus` once the viewer has been initialized.
|
// the `eventBus` once the viewer has been initialized.
|
||||||
localized.then(function () {
|
localized.then(() => {
|
||||||
self.eventBus.dispatch('localized');
|
this.eventBus.dispatch('localized');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self.isViewerEmbedded && !PDFJS.isExternalLinkTargetSet()) {
|
if (this.isViewerEmbedded && !PDFJS.isExternalLinkTargetSet()) {
|
||||||
// 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.
|
||||||
PDFJS.externalLinkTarget = PDFJS.LinkTarget.TOP;
|
PDFJS.externalLinkTarget = PDFJS.LinkTarget.TOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.initialized = true;
|
this.initialized = true;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -181,74 +182,74 @@ var PDFViewerApplication = {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_readPreferences: function () {
|
_readPreferences: function () {
|
||||||
var self = this;
|
var { preferences, viewerPrefs, } = this;
|
||||||
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
Preferences.get('enableWebGL').then(function resolved(value) {
|
preferences.get('enableWebGL').then(function resolved(value) {
|
||||||
PDFJS.disableWebGL = !value;
|
PDFJS.disableWebGL = !value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('sidebarViewOnLoad').then(function resolved(value) {
|
preferences.get('sidebarViewOnLoad').then(function resolved(value) {
|
||||||
self.viewerPrefs['sidebarViewOnLoad'] = value;
|
viewerPrefs['sidebarViewOnLoad'] = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('pdfBugEnabled').then(function resolved(value) {
|
preferences.get('pdfBugEnabled').then(function resolved(value) {
|
||||||
self.viewerPrefs['pdfBugEnabled'] = value;
|
viewerPrefs['pdfBugEnabled'] = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('showPreviousViewOnLoad').then(function resolved(value) {
|
preferences.get('showPreviousViewOnLoad').then(function resolved(value) {
|
||||||
self.viewerPrefs['showPreviousViewOnLoad'] = value;
|
viewerPrefs['showPreviousViewOnLoad'] = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('defaultZoomValue').then(function resolved(value) {
|
preferences.get('defaultZoomValue').then(function resolved(value) {
|
||||||
self.viewerPrefs['defaultZoomValue'] = value;
|
viewerPrefs['defaultZoomValue'] = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('enhanceTextSelection').then(function resolved(value) {
|
preferences.get('enhanceTextSelection').then(function resolved(value) {
|
||||||
self.viewerPrefs['enhanceTextSelection'] = value;
|
viewerPrefs['enhanceTextSelection'] = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('disableTextLayer').then(function resolved(value) {
|
preferences.get('disableTextLayer').then(function resolved(value) {
|
||||||
if (PDFJS.disableTextLayer === true) {
|
if (PDFJS.disableTextLayer === true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PDFJS.disableTextLayer = value;
|
PDFJS.disableTextLayer = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('disableRange').then(function resolved(value) {
|
preferences.get('disableRange').then(function resolved(value) {
|
||||||
if (PDFJS.disableRange === true) {
|
if (PDFJS.disableRange === true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PDFJS.disableRange = value;
|
PDFJS.disableRange = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('disableStream').then(function resolved(value) {
|
preferences.get('disableStream').then(function resolved(value) {
|
||||||
if (PDFJS.disableStream === true) {
|
if (PDFJS.disableStream === true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PDFJS.disableStream = value;
|
PDFJS.disableStream = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('disableAutoFetch').then(function resolved(value) {
|
preferences.get('disableAutoFetch').then(function resolved(value) {
|
||||||
PDFJS.disableAutoFetch = value;
|
PDFJS.disableAutoFetch = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('disableFontFace').then(function resolved(value) {
|
preferences.get('disableFontFace').then(function resolved(value) {
|
||||||
if (PDFJS.disableFontFace === true) {
|
if (PDFJS.disableFontFace === true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PDFJS.disableFontFace = value;
|
PDFJS.disableFontFace = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('useOnlyCssZoom').then(function resolved(value) {
|
preferences.get('useOnlyCssZoom').then(function resolved(value) {
|
||||||
PDFJS.useOnlyCssZoom = value;
|
PDFJS.useOnlyCssZoom = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('externalLinkTarget').then(function resolved(value) {
|
preferences.get('externalLinkTarget').then(function resolved(value) {
|
||||||
if (PDFJS.isExternalLinkTargetSet()) {
|
if (PDFJS.isExternalLinkTargetSet()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PDFJS.externalLinkTarget = value;
|
PDFJS.externalLinkTarget = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('renderer').then(function resolved(value) {
|
preferences.get('renderer').then(function resolved(value) {
|
||||||
self.viewerPrefs['renderer'] = value;
|
viewerPrefs['renderer'] = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('renderInteractiveForms').then(function resolved(value) {
|
preferences.get('renderInteractiveForms').then(function resolved(value) {
|
||||||
self.viewerPrefs['renderInteractiveForms'] = value;
|
viewerPrefs['renderInteractiveForms'] = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('disablePageLabels').then(function resolved(value) {
|
preferences.get('disablePageLabels').then(function resolved(value) {
|
||||||
self.viewerPrefs['disablePageLabels'] = value;
|
viewerPrefs['disablePageLabels'] = value;
|
||||||
}),
|
}),
|
||||||
Preferences.get('enablePrintAutoRotate').then(function resolved(value) {
|
preferences.get('enablePrintAutoRotate').then(function resolved(value) {
|
||||||
self.viewerPrefs['enablePrintAutoRotate'] = value;
|
viewerPrefs['enablePrintAutoRotate'] = value;
|
||||||
}),
|
}),
|
||||||
]).catch(function (reason) { });
|
]).catch(function (reason) { });
|
||||||
},
|
},
|
||||||
@ -260,7 +261,7 @@ var PDFViewerApplication = {
|
|||||||
var self = this;
|
var self = this;
|
||||||
var appConfig = this.appConfig;
|
var appConfig = this.appConfig;
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise((resolve, reject) => {
|
||||||
var eventBus = appConfig.eventBus || getGlobalEventBus();
|
var eventBus = appConfig.eventBus || getGlobalEventBus();
|
||||||
self.eventBus = eventBus;
|
self.eventBus = eventBus;
|
||||||
|
|
||||||
@ -337,8 +338,9 @@ var PDFViewerApplication = {
|
|||||||
self.overlayManager = OverlayManager;
|
self.overlayManager = OverlayManager;
|
||||||
|
|
||||||
self.handTool = new HandTool({
|
self.handTool = new HandTool({
|
||||||
container: container,
|
container,
|
||||||
eventBus: eventBus,
|
eventBus,
|
||||||
|
preferences: this.preferences,
|
||||||
});
|
});
|
||||||
|
|
||||||
self.pdfDocumentProperties =
|
self.pdfDocumentProperties =
|
||||||
@ -595,12 +597,12 @@ var PDFViewerApplication = {
|
|||||||
}
|
}
|
||||||
if (this.pdfLoadingTask) {
|
if (this.pdfLoadingTask) {
|
||||||
// We need to destroy already opened document.
|
// We need to destroy already opened document.
|
||||||
return this.close().then(function () {
|
return this.close().then(() => {
|
||||||
// Reload the preferences if a document was previously opened.
|
// Reload the preferences if a document was previously opened.
|
||||||
Preferences.reload();
|
this.preferences.reload();
|
||||||
// ... and repeat the open() call.
|
// ... and repeat the open() call.
|
||||||
return this.open(file, args);
|
return this.open(file, args);
|
||||||
}.bind(this));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var parameters = Object.create(null), scale;
|
var parameters = Object.create(null), scale;
|
||||||
|
@ -15,10 +15,10 @@
|
|||||||
/* globals chrome */
|
/* globals chrome */
|
||||||
|
|
||||||
import { DefaultExternalServices, PDFViewerApplication } from './app';
|
import { DefaultExternalServices, PDFViewerApplication } from './app';
|
||||||
|
import { BasePreferences } from './preferences';
|
||||||
import { DownloadManager } from './download_manager';
|
import { DownloadManager } from './download_manager';
|
||||||
import { OverlayManager } from './overlay_manager';
|
import { OverlayManager } from './overlay_manager';
|
||||||
import { PDFJS } from './pdfjs';
|
import { PDFJS } from './pdfjs';
|
||||||
import { Preferences } from './preferences';
|
|
||||||
|
|
||||||
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('CHROME')) {
|
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('CHROME')) {
|
||||||
throw new Error('Module "pdfjs-web/chromecom" shall not be used outside ' +
|
throw new Error('Module "pdfjs-web/chromecom" shall not be used outside ' +
|
||||||
@ -293,46 +293,48 @@ function setReferer(url, callback) {
|
|||||||
// chrome.storage.local to chrome.storage.sync when needed.
|
// chrome.storage.local to chrome.storage.sync when needed.
|
||||||
var storageArea = chrome.storage.sync || chrome.storage.local;
|
var storageArea = chrome.storage.sync || chrome.storage.local;
|
||||||
|
|
||||||
Preferences._writeToStorage = function (prefObj) {
|
class ChromePreferences extends BasePreferences {
|
||||||
return new Promise(function (resolve) {
|
_writeToStorage(prefObj) {
|
||||||
if (prefObj === Preferences.defaults) {
|
return new Promise((resolve) => {
|
||||||
var keysToRemove = Object.keys(Preferences.defaults);
|
if (prefObj === this.defaults) {
|
||||||
// If the storage is reset, remove the keys so that the values from
|
var keysToRemove = Object.keys(this.defaults);
|
||||||
// managed storage are applied again.
|
// If the storage is reset, remove the keys so that the values from
|
||||||
storageArea.remove(keysToRemove, function() {
|
// managed storage are applied again.
|
||||||
resolve();
|
storageArea.remove(keysToRemove, function() {
|
||||||
});
|
resolve();
|
||||||
} else {
|
});
|
||||||
storageArea.set(prefObj, function() {
|
} else {
|
||||||
resolve();
|
storageArea.set(prefObj, function() {
|
||||||
});
|
resolve();
|
||||||
}
|
});
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Preferences._readFromStorage = function (prefObj) {
|
|
||||||
return new Promise(function (resolve) {
|
|
||||||
if (chrome.storage.managed) {
|
|
||||||
// Get preferences as set by the system administrator.
|
|
||||||
// See extensions/chromium/preferences_schema.json for more information.
|
|
||||||
// These preferences can be overridden by the user.
|
|
||||||
chrome.storage.managed.get(Preferences.defaults, getPreferences);
|
|
||||||
} else {
|
|
||||||
// Managed storage not supported, e.g. in old Chromium versions.
|
|
||||||
getPreferences(Preferences.defaults);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPreferences(defaultPrefs) {
|
|
||||||
if (chrome.runtime.lastError) {
|
|
||||||
// Managed storage not supported, e.g. in Opera.
|
|
||||||
defaultPrefs = Preferences.defaults;
|
|
||||||
}
|
}
|
||||||
storageArea.get(defaultPrefs, function(readPrefs) {
|
});
|
||||||
resolve(readPrefs);
|
}
|
||||||
});
|
|
||||||
}
|
_readFromStorage(prefObj) {
|
||||||
});
|
return new Promise((resolve) => {
|
||||||
};
|
var getPreferences = (defaultPrefs) => {
|
||||||
|
if (chrome.runtime.lastError) {
|
||||||
|
// Managed storage not supported, e.g. in Opera.
|
||||||
|
defaultPrefs = this.defaults;
|
||||||
|
}
|
||||||
|
storageArea.get(defaultPrefs, function(readPrefs) {
|
||||||
|
resolve(readPrefs);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (chrome.storage.managed) {
|
||||||
|
// Get preferences as set by the system administrator.
|
||||||
|
// See extensions/chromium/preferences_schema.json for more information.
|
||||||
|
// These preferences can be overridden by the user.
|
||||||
|
chrome.storage.managed.get(this.defaults, getPreferences);
|
||||||
|
} else {
|
||||||
|
// Managed storage not supported, e.g. in old Chromium versions.
|
||||||
|
getPreferences(this.defaults);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var ChromeExternalServices = Object.create(DefaultExternalServices);
|
var ChromeExternalServices = Object.create(DefaultExternalServices);
|
||||||
ChromeExternalServices.initPassiveLoading = function (callbacks) {
|
ChromeExternalServices.initPassiveLoading = function (callbacks) {
|
||||||
@ -345,6 +347,9 @@ ChromeExternalServices.initPassiveLoading = function (callbacks) {
|
|||||||
ChromeExternalServices.createDownloadManager = function() {
|
ChromeExternalServices.createDownloadManager = function() {
|
||||||
return new DownloadManager();
|
return new DownloadManager();
|
||||||
};
|
};
|
||||||
|
ChromeExternalServices.createPreferences = function() {
|
||||||
|
return new ChromePreferences();
|
||||||
|
};
|
||||||
PDFViewerApplication.externalServices = ChromeExternalServices;
|
PDFViewerApplication.externalServices = ChromeExternalServices;
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
@ -13,11 +13,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import { createObjectURL, PDFDataRangeTransport, shadow } from './pdfjs';
|
||||||
createObjectURL, PDFDataRangeTransport, shadow
|
import { BasePreferences } from './preferences';
|
||||||
} from './pdfjs';
|
|
||||||
import { PDFViewerApplication } from './app';
|
import { PDFViewerApplication } from './app';
|
||||||
import { Preferences } from './preferences';
|
|
||||||
|
|
||||||
if (typeof PDFJSDev === 'undefined' ||
|
if (typeof PDFJSDev === 'undefined' ||
|
||||||
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
|
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
|
||||||
@ -127,20 +125,22 @@ var DownloadManager = (function DownloadManagerClosure() {
|
|||||||
return DownloadManager;
|
return DownloadManager;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
Preferences._writeToStorage = function (prefObj) {
|
class FirefoxPreferences extends BasePreferences {
|
||||||
return new Promise(function (resolve) {
|
_writeToStorage(prefObj) {
|
||||||
FirefoxCom.request('setPreferences', prefObj, resolve);
|
return new Promise(function(resolve) {
|
||||||
});
|
FirefoxCom.request('setPreferences', prefObj, resolve);
|
||||||
};
|
|
||||||
|
|
||||||
Preferences._readFromStorage = function (prefObj) {
|
|
||||||
return new Promise(function (resolve) {
|
|
||||||
FirefoxCom.request('getPreferences', prefObj, function (prefStr) {
|
|
||||||
var readPrefs = JSON.parse(prefStr);
|
|
||||||
resolve(readPrefs);
|
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
};
|
|
||||||
|
_readFromStorage(prefObj) {
|
||||||
|
return new Promise(function(resolve) {
|
||||||
|
FirefoxCom.request('getPreferences', prefObj, function (prefStr) {
|
||||||
|
var readPrefs = JSON.parse(prefStr);
|
||||||
|
resolve(readPrefs);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
(function listenFindEvents() {
|
(function listenFindEvents() {
|
||||||
var events = [
|
var events = [
|
||||||
@ -247,6 +247,10 @@ PDFViewerApplication.externalServices = {
|
|||||||
return new DownloadManager();
|
return new DownloadManager();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
createPreferences() {
|
||||||
|
return new FirefoxPreferences();
|
||||||
|
},
|
||||||
|
|
||||||
get supportsIntegratedFind() {
|
get supportsIntegratedFind() {
|
||||||
var support = FirefoxCom.requestSync('supportsIntegratedFind');
|
var support = FirefoxCom.requestSync('supportsIntegratedFind');
|
||||||
return shadow(this, 'supportsIntegratedFind', support);
|
return shadow(this, 'supportsIntegratedFind', support);
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { DefaultExternalServices, PDFViewerApplication } from './app';
|
import { DefaultExternalServices, PDFViewerApplication } from './app';
|
||||||
|
import { BasePreferences } from './preferences';
|
||||||
import { DownloadManager } from './download_manager';
|
import { DownloadManager } from './download_manager';
|
||||||
|
|
||||||
if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('GENERIC')) {
|
if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('GENERIC')) {
|
||||||
@ -23,10 +24,29 @@ if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('GENERIC')) {
|
|||||||
|
|
||||||
var GenericCom = {};
|
var GenericCom = {};
|
||||||
|
|
||||||
|
class GenericPreferences extends BasePreferences {
|
||||||
|
_writeToStorage(prefObj) {
|
||||||
|
return new Promise(function(resolve) {
|
||||||
|
localStorage.setItem('pdfjs.preferences', JSON.stringify(prefObj));
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_readFromStorage(prefObj) {
|
||||||
|
return new Promise(function(resolve) {
|
||||||
|
var readPrefs = JSON.parse(localStorage.getItem('pdfjs.preferences'));
|
||||||
|
resolve(readPrefs);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var GenericExternalServices = Object.create(DefaultExternalServices);
|
var GenericExternalServices = Object.create(DefaultExternalServices);
|
||||||
GenericExternalServices.createDownloadManager = function () {
|
GenericExternalServices.createDownloadManager = function() {
|
||||||
return new DownloadManager();
|
return new DownloadManager();
|
||||||
};
|
};
|
||||||
|
GenericExternalServices.createPreferences = function() {
|
||||||
|
return new GenericPreferences();
|
||||||
|
};
|
||||||
PDFViewerApplication.externalServices = GenericExternalServices;
|
PDFViewerApplication.externalServices = GenericExternalServices;
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
|
|
||||||
import { GrabToPan } from './grab_to_pan';
|
import { GrabToPan } from './grab_to_pan';
|
||||||
import { localized } from './ui_utils';
|
import { localized } from './ui_utils';
|
||||||
import { Preferences } from './preferences';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} HandToolOptions
|
* @typedef {Object} HandToolOptions
|
||||||
@ -34,6 +33,7 @@ var HandTool = (function HandToolClosure() {
|
|||||||
function HandTool(options) {
|
function HandTool(options) {
|
||||||
this.container = options.container;
|
this.container = options.container;
|
||||||
this.eventBus = options.eventBus;
|
this.eventBus = options.eventBus;
|
||||||
|
var preferences = options.preferences;
|
||||||
|
|
||||||
this.wasActive = false;
|
this.wasActive = false;
|
||||||
|
|
||||||
@ -46,12 +46,12 @@ var HandTool = (function HandToolClosure() {
|
|||||||
|
|
||||||
this.eventBus.on('togglehandtool', this.toggle.bind(this));
|
this.eventBus.on('togglehandtool', this.toggle.bind(this));
|
||||||
|
|
||||||
Promise.all([localized, Preferences.get('enableHandToolOnLoad')]).then(
|
Promise.all([localized,
|
||||||
function resolved(values) {
|
preferences.get('enableHandToolOnLoad')]).then((values) => {
|
||||||
if (values[1] === true) {
|
if (values[1] === true) {
|
||||||
this.handTool.activate();
|
this.handTool.activate();
|
||||||
}
|
}
|
||||||
}.bind(this)).catch(function rejected(reason) { });
|
}).catch(function rejected(reason) { });
|
||||||
|
|
||||||
this.eventBus.on('presentationmodechanged', function (e) {
|
this.eventBus.on('presentationmodechanged', function (e) {
|
||||||
if (e.switchInProgress) {
|
if (e.switchInProgress) {
|
||||||
|
@ -27,7 +27,7 @@ function getDefaultPreferences() {
|
|||||||
try {
|
try {
|
||||||
resolve(JSON.parse(xhr.responseText));
|
resolve(JSON.parse(xhr.responseText));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Unable to load default preferences: ' + e);
|
console.error(`Unable to load default preferences: ${e}`);
|
||||||
resolve({});
|
resolve({});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -49,24 +49,18 @@ function cloneObj(obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Preferences - Utility for storing persistent settings.
|
* BasePreferences - Abstract base class for storing persistent settings.
|
||||||
* Used for settings that should be applied to all opened documents,
|
* Used for settings that should be applied to all opened documents,
|
||||||
* or every time the viewer is loaded.
|
* or every time the viewer is loaded.
|
||||||
*/
|
*/
|
||||||
var Preferences = {
|
class BasePreferences {
|
||||||
prefs: null,
|
constructor() {
|
||||||
isInitializedPromiseResolved: false,
|
if (this.constructor === BasePreferences) {
|
||||||
initializedPromise: null,
|
throw new Error('Cannot initialize BasePreferences.');
|
||||||
|
}
|
||||||
/**
|
this.prefs = null;
|
||||||
* Initialize and fetch the current preference values from storage.
|
|
||||||
* @return {Promise} A promise that is resolved when the preferences
|
|
||||||
* have been initialized.
|
|
||||||
*/
|
|
||||||
initialize: function preferencesInitialize() {
|
|
||||||
return this.initializedPromise = getDefaultPreferences().then(
|
|
||||||
function (defaults) {
|
|
||||||
|
|
||||||
|
this._initializedPromise = getDefaultPreferences().then((defaults) => {
|
||||||
Object.defineProperty(this, 'defaults', {
|
Object.defineProperty(this, 'defaults', {
|
||||||
value: Object.freeze(defaults),
|
value: Object.freeze(defaults),
|
||||||
writable: false,
|
writable: false,
|
||||||
@ -76,62 +70,59 @@ var Preferences = {
|
|||||||
|
|
||||||
this.prefs = cloneObj(defaults);
|
this.prefs = cloneObj(defaults);
|
||||||
return this._readFromStorage(defaults);
|
return this._readFromStorage(defaults);
|
||||||
}.bind(this)).then(function(prefObj) {
|
}).then((prefObj) => {
|
||||||
this.isInitializedPromiseResolved = true;
|
|
||||||
if (prefObj) {
|
if (prefObj) {
|
||||||
this.prefs = prefObj;
|
this.prefs = prefObj;
|
||||||
}
|
}
|
||||||
}.bind(this));
|
});
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stub function for writing preferences to storage.
|
* Stub function for writing preferences to storage.
|
||||||
* NOTE: This should be overridden by a build-specific function defined below.
|
|
||||||
* @param {Object} prefObj The preferences that should be written to storage.
|
* @param {Object} prefObj The preferences that should be written to storage.
|
||||||
* @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: function preferences_writeToStorage(prefObj) {
|
_writeToStorage(prefObj) {
|
||||||
return Promise.resolve();
|
return Promise.reject(new Error('Not implemented: _writeToStorage'));
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stub function for reading preferences from storage.
|
* Stub function for reading preferences from storage.
|
||||||
* NOTE: This should be overridden by a build-specific function defined below.
|
|
||||||
* @param {Object} prefObj The preferences that should be read from storage.
|
* @param {Object} prefObj The preferences that should be read from storage.
|
||||||
* @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: function preferences_readFromStorage(prefObj) {
|
_readFromStorage(prefObj) {
|
||||||
return Promise.resolve();
|
return Promise.reject(new Error('Not implemented: _readFromStorage'));
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset the preferences to their default values and update storage.
|
* Reset the preferences to their default values and update storage.
|
||||||
* @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: function preferencesReset() {
|
reset() {
|
||||||
return this.initializedPromise.then(function() {
|
return this._initializedPromise.then(() => {
|
||||||
this.prefs = cloneObj(this.defaults);
|
this.prefs = cloneObj(this.defaults);
|
||||||
return this._writeToStorage(this.defaults);
|
return this._writeToStorage(this.defaults);
|
||||||
}.bind(this));
|
});
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace the current preference values with the ones from storage.
|
* Replace the current preference values with the ones from storage.
|
||||||
* @return {Promise} A promise that is resolved when the preference values
|
* @return {Promise} A promise that is resolved when the preference values
|
||||||
* have been updated.
|
* have been updated.
|
||||||
*/
|
*/
|
||||||
reload: function preferencesReload() {
|
reload() {
|
||||||
return this.initializedPromise.then(function () {
|
return this._initializedPromise.then(() => {
|
||||||
this._readFromStorage(this.defaults).then(function(prefObj) {
|
return this._readFromStorage(this.defaults);
|
||||||
if (prefObj) {
|
}).then((prefObj) => {
|
||||||
this.prefs = prefObj;
|
if (prefObj) {
|
||||||
}
|
this.prefs = prefObj;
|
||||||
}.bind(this));
|
}
|
||||||
}.bind(this));
|
});
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the value of a preference.
|
* Set the value of a preference.
|
||||||
@ -140,12 +131,12 @@ var Preferences = {
|
|||||||
* @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: function preferencesSet(name, value) {
|
set(name, value) {
|
||||||
return this.initializedPromise.then(function () {
|
return this._initializedPromise.then(() => {
|
||||||
if (this.defaults[name] === undefined) {
|
if (this.defaults[name] === undefined) {
|
||||||
throw new Error('preferencesSet: \'' + name + '\' is undefined.');
|
throw new Error(`Set preference: "${name}" is undefined.`);
|
||||||
} else if (value === undefined) {
|
} else if (value === undefined) {
|
||||||
throw new Error('preferencesSet: no value is specified.');
|
throw new Error('Set preference: no value is specified.');
|
||||||
}
|
}
|
||||||
var valueType = typeof value;
|
var valueType = typeof value;
|
||||||
var defaultType = typeof this.defaults[name];
|
var defaultType = typeof this.defaults[name];
|
||||||
@ -154,19 +145,18 @@ var Preferences = {
|
|||||||
if (valueType === 'number' && defaultType === 'string') {
|
if (valueType === 'number' && defaultType === 'string') {
|
||||||
value = value.toString();
|
value = value.toString();
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Preferences_set: \'' + value + '\' is a \"' +
|
throw new Error(`Set preference: "${value}" is a ${valueType}, ` +
|
||||||
valueType + '\", expected \"' + defaultType + '\".');
|
`expected a ${defaultType}.`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (valueType === 'number' && (value | 0) !== value) {
|
if (valueType === 'number' && (value | 0) !== value) {
|
||||||
throw new Error('Preferences_set: \'' + value +
|
throw new Error(`Set preference: "${value}" must be an integer.`);
|
||||||
'\' must be an \"integer\".');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.prefs[name] = value;
|
this.prefs[name] = value;
|
||||||
return this._writeToStorage(this.prefs);
|
return this._writeToStorage(this.prefs);
|
||||||
}.bind(this));
|
});
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the value of a preference.
|
* Get the value of a preference.
|
||||||
@ -174,12 +164,12 @@ var Preferences = {
|
|||||||
* @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: function preferencesGet(name) {
|
get(name) {
|
||||||
return this.initializedPromise.then(function () {
|
return this._initializedPromise.then(() => {
|
||||||
var defaultValue = this.defaults[name];
|
var defaultValue = this.defaults[name];
|
||||||
|
|
||||||
if (defaultValue === undefined) {
|
if (defaultValue === undefined) {
|
||||||
throw new Error('preferencesGet: \'' + name + '\' is undefined.');
|
throw new Error(`Get preference: "${name}" is undefined.`);
|
||||||
} else {
|
} else {
|
||||||
var prefValue = this.prefs[name];
|
var prefValue = this.prefs[name];
|
||||||
|
|
||||||
@ -188,27 +178,10 @@ var Preferences = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}.bind(this));
|
});
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
if (typeof PDFJSDev === 'undefined' ||
|
|
||||||
!PDFJSDev.test('FIREFOX || MOZCENTRAL || CHROME')) {
|
|
||||||
Preferences._writeToStorage = function (prefObj) {
|
|
||||||
return new Promise(function (resolve) {
|
|
||||||
localStorage.setItem('pdfjs.preferences', JSON.stringify(prefObj));
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Preferences._readFromStorage = function (prefObj) {
|
|
||||||
return new Promise(function (resolve) {
|
|
||||||
var readPrefs = JSON.parse(localStorage.getItem('pdfjs.preferences'));
|
|
||||||
resolve(readPrefs);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
Preferences,
|
BasePreferences,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user