Skip to content

Commit

Permalink
Merge pull request mozilla#8450 from timvandermeij/es6-overlay-manager
Browse files Browse the repository at this point in the history
Convert the overlay manager to ES6 syntax
  • Loading branch information
timvandermeij authored May 28, 2017
2 parents 4dfe20e + 45ce34a commit c4dca92
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 92 deletions.
18 changes: 11 additions & 7 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ var PDFViewerApplication = {
store: null,
/** @type {DownloadManager} */
downloadManager: null,
/** @type {OverlayManager} */
overlayManager: null,
/** @type {Preferences} */
preferences: null,
/** @type {Toolbar} */
Expand Down Expand Up @@ -261,6 +263,8 @@ var PDFViewerApplication = {
let appConfig = this.appConfig;

return new Promise((resolve, reject) => {
this.overlayManager = new OverlayManager();

let eventBus = appConfig.eventBus || getGlobalEventBus();
this.eventBus = eventBus;

Expand Down Expand Up @@ -329,16 +333,15 @@ var PDFViewerApplication = {

this.pdfViewer.setFindController(this.findController);

// FIXME better PDFFindBar constructor parameters
// TODO: improve `PDFFindBar` constructor parameter passing
let findBarConfig = Object.create(appConfig.findBar);
findBarConfig.findController = this.findController;
findBarConfig.eventBus = eventBus;
this.findBar = new PDFFindBar(findBarConfig);

this.overlayManager = OverlayManager;

this.pdfDocumentProperties =
new PDFDocumentProperties(appConfig.documentProperties);
new PDFDocumentProperties(appConfig.documentProperties,
this.overlayManager);

this.pdfCursorTools = new PDFCursorTools({
container,
Expand All @@ -361,7 +364,8 @@ var PDFViewerApplication = {
});
}

this.passwordPrompt = new PasswordPrompt(appConfig.passwordOverlay);
this.passwordPrompt = new PasswordPrompt(appConfig.passwordOverlay,
this.overlayManager);

this.pdfOutlineViewer = new PDFOutlineViewer({
container: appConfig.sidebar.outlineView,
Expand All @@ -375,7 +379,7 @@ var PDFViewerApplication = {
downloadManager,
});

// FIXME better PDFSidebar constructor parameters
// TODO: improve `PDFSidebar` constructor parameter passing
let sidebarConfig = Object.create(appConfig.sidebar);
sidebarConfig.pdfViewer = this.pdfViewer;
sidebarConfig.pdfThumbnailViewer = this.pdfThumbnailViewer;
Expand Down Expand Up @@ -1911,7 +1915,7 @@ function webViewerClick(evt) {
}

function webViewerKeyDown(evt) {
if (OverlayManager.active) {
if (PDFViewerApplication.overlayManager.active) {
return;
}

Expand Down
22 changes: 11 additions & 11 deletions web/chromecom.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import { DefaultExternalServices, PDFViewerApplication } from './app';
import { BasePreferences } from './preferences';
import { DownloadManager } from './download_manager';
import { OverlayManager } from './overlay_manager';
import { PDFJS } from './pdfjs';

if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('CHROME')) {
Expand Down Expand Up @@ -56,10 +55,11 @@ ChromeCom.request = function ChromeCom_request(action, data, callback) {
/**
* Resolves a PDF file path and attempts to detects length.
*
* @param {String} file Absolute URL of PDF file.
* @param {Function} callback A callback with resolved URL and file length.
* @param {String} file - Absolute URL of PDF file.
* @param {OverlayManager} overlayManager - Manager for the viewer overlays.
* @param {Function} callback - A callback with resolved URL and file length.
*/
ChromeCom.resolvePDFFile = function ChromeCom_resolvePDFFile(file, callback) {
ChromeCom.resolvePDFFile = function(file, overlayManager, callback) {
// Expand drive:-URLs to filesystem URLs (Chrome OS)
file = file.replace(/^drive:/i,
'filesystem:' + location.origin + '/external/');
Expand Down Expand Up @@ -110,7 +110,7 @@ ChromeCom.resolvePDFFile = function ChromeCom_resolvePDFFile(file, callback) {
if (isAllowedAccess) {
callback(file);
} else {
requestAccessToLocalFile(file);
requestAccessToLocalFile(file, overlayManager);
}
});
});
Expand Down Expand Up @@ -155,7 +155,7 @@ function reloadIfRuntimeIsUnavailable() {
}

var chromeFileAccessOverlayPromise;
function requestAccessToLocalFile(fileUrl) {
function requestAccessToLocalFile(fileUrl, overlayManager) {
var onCloseOverlay = null;
if (top !== window) {
// When the extension reloads after receiving new permissions, the pages
Expand All @@ -169,11 +169,11 @@ function requestAccessToLocalFile(fileUrl) {
onCloseOverlay = function() {
window.removeEventListener('focus', reloadIfRuntimeIsUnavailable);
reloadIfRuntimeIsUnavailable();
OverlayManager.close('chromeFileAccessOverlay');
overlayManager.close('chromeFileAccessOverlay');
};
}
if (!chromeFileAccessOverlayPromise) {
chromeFileAccessOverlayPromise = OverlayManager.register(
chromeFileAccessOverlayPromise = overlayManager.register(
'chromeFileAccessOverlay',
document.getElementById('chromeFileAccessOverlay'),
onCloseOverlay, true);
Expand Down Expand Up @@ -215,7 +215,7 @@ function requestAccessToLocalFile(fileUrl) {
// why this permission request is shown.
document.getElementById('chrome-url-of-local-file').textContent = fileUrl;

OverlayManager.open('chromeFileAccessOverlay');
overlayManager.open('chromeFileAccessOverlay');
});
}

Expand Down Expand Up @@ -338,8 +338,8 @@ class ChromePreferences extends BasePreferences {

var ChromeExternalServices = Object.create(DefaultExternalServices);
ChromeExternalServices.initPassiveLoading = function (callbacks) {
var appConfig = PDFViewerApplication.appConfig;
ChromeCom.resolvePDFFile(appConfig.defaultUrl,
let { appConfig, overlayManager, } = PDFViewerApplication;
ChromeCom.resolvePDFFile(appConfig.defaultUrl, overlayManager,
function (url, length, originalURL) {
callbacks.onOpenWithURL(url, length, originalURL);
});
Expand Down
107 changes: 56 additions & 51 deletions web/overlay_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,132 +13,137 @@
* limitations under the License.
*/

var OverlayManager = {
overlays: {},
active: null,
class OverlayManager {
constructor() {
this._overlays = {};
this._active = null;
this._keyDownBound = this._keyDown.bind(this);
}

get active() {
return this._active;
}

/**
* @param {string} name The name of the overlay that is registered.
* @param {HTMLDivElement} element The overlay's DOM element.
* @param {function} callerCloseMethod (optional) The method that, if present,
* will call OverlayManager.close from the Object
* @param {string} name - The name of the overlay that is registered.
* @param {HTMLDivElement} element - The overlay's DOM element.
* @param {function} callerCloseMethod - (optional) The method that, if
* present, calls `OverlayManager.close` from the object
* registering the overlay. Access to this method is
* necessary in order to run cleanup code when e.g.
* the overlay is force closed. The default is null.
* @param {boolean} canForceClose (optional) Indicates if opening the overlay
* will close an active overlay. The default is false.
* the overlay is force closed. The default is `null`.
* @param {boolean} canForceClose - (optional) Indicates if opening the
* overlay closes an active overlay. The default is `false`.
* @returns {Promise} A promise that is resolved when the overlay has been
* registered.
*/
register(name, element, callerCloseMethod, canForceClose) {
register(name, element, callerCloseMethod = null, canForceClose = false) {
return new Promise((resolve) => {
var container;
let container;
if (!name || !element || !(container = element.parentNode)) {
throw new Error('Not enough parameters.');
} else if (this.overlays[name]) {
} else if (this._overlays[name]) {
throw new Error('The overlay is already registered.');
}
this.overlays[name] = {
this._overlays[name] = {
element,
container,
callerCloseMethod: (callerCloseMethod || null),
canForceClose: (canForceClose || false),
callerCloseMethod,
canForceClose,
};
resolve();
});
},
}

/**
* @param {string} name The name of the overlay that is unregistered.
* @param {string} name - The name of the overlay that is unregistered.
* @returns {Promise} A promise that is resolved when the overlay has been
* unregistered.
*/
unregister(name) {
return new Promise((resolve) => {
if (!this.overlays[name]) {
if (!this._overlays[name]) {
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.');
}
delete this.overlays[name];

delete this._overlays[name];
resolve();
});
},
}

/**
* @param {string} name The name of the overlay that should be opened.
* @param {string} name - The name of the overlay that should be opened.
* @returns {Promise} A promise that is resolved when the overlay has been
* opened.
*/
open(name) {
return new Promise((resolve) => {
if (!this.overlays[name]) {
if (!this._overlays[name]) {
throw new Error('The overlay does not exist.');
} else if (this.active) {
if (this.overlays[name].canForceClose) {
} else if (this._active) {
if (this._overlays[name].canForceClose) {
this._closeThroughCaller();
} else if (this.active === name) {
} else if (this._active === name) {
throw new Error('The overlay is already active.');
} else {
throw new Error('Another overlay is currently active.');
}
}
this.active = name;
this.overlays[this.active].element.classList.remove('hidden');
this.overlays[this.active].container.classList.remove('hidden');
this._active = name;
this._overlays[this._active].element.classList.remove('hidden');
this._overlays[this._active].container.classList.remove('hidden');

window.addEventListener('keydown', this._keyDown);
window.addEventListener('keydown', this._keyDownBound);
resolve();
});
},
}

/**
* @param {string} name The name of the overlay that should be closed.
* @param {string} name - The name of the overlay that should be closed.
* @returns {Promise} A promise that is resolved when the overlay has been
* closed.
*/
close(name) {
return new Promise((resolve) => {
if (!this.overlays[name]) {
if (!this._overlays[name]) {
throw new Error('The overlay does not exist.');
} else if (!this.active) {
} else if (!this._active) {
throw new Error('The overlay is currently not active.');
} else if (this.active !== name) {
} else if (this._active !== name) {
throw new Error('Another overlay is currently active.');
}
this.overlays[this.active].container.classList.add('hidden');
this.overlays[this.active].element.classList.add('hidden');
this.active = null;
this._overlays[this._active].container.classList.add('hidden');
this._overlays[this._active].element.classList.add('hidden');
this._active = null;

window.removeEventListener('keydown', this._keyDown);
window.removeEventListener('keydown', this._keyDownBound);
resolve();
});
},
}

/**
* @private
*/
_keyDown(evt) {
var self = OverlayManager;
if (self.active && evt.keyCode === 27) { // Esc key.
self._closeThroughCaller();
if (this._active && evt.keyCode === 27) { // Esc key.
this._closeThroughCaller();
evt.preventDefault();
}
},
}

/**
* @private
*/
_closeThroughCaller() {
if (this.overlays[this.active].callerCloseMethod) {
this.overlays[this.active].callerCloseMethod();
if (this._overlays[this._active].callerCloseMethod) {
this._overlays[this._active].callerCloseMethod();
}
if (this.active) {
this.close(this.active);
if (this._active) {
this.close(this._active);
}
}
};
}

export {
OverlayManager,
Expand Down
13 changes: 7 additions & 6 deletions web/password_prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

import { mozL10n } from './ui_utils';
import { OverlayManager } from './overlay_manager';
import { PasswordResponses } from './pdfjs';

/**
Expand All @@ -33,14 +32,16 @@ import { PasswordResponses } from './pdfjs';
class PasswordPrompt {
/**
* @param {PasswordPromptOptions} options
* @param {OverlayManager} overlayManager - Manager for the viewer overlays.
*/
constructor(options) {
constructor(options, overlayManager) {
this.overlayName = options.overlayName;
this.container = options.container;
this.label = options.label;
this.input = options.input;
this.submitButton = options.submitButton;
this.cancelButton = options.cancelButton;
this.overlayManager = overlayManager;

this.updateCallback = null;
this.reason = null;
Expand All @@ -54,12 +55,12 @@ class PasswordPrompt {
}
});

OverlayManager.register(this.overlayName, this.container,
this.close.bind(this), true);
this.overlayManager.register(this.overlayName, this.container,
this.close.bind(this), true);
}

open() {
OverlayManager.open(this.overlayName).then(() => {
this.overlayManager.open(this.overlayName).then(() => {
this.input.focus();

var promptString = mozL10n.get('password_label', null,
Expand All @@ -75,7 +76,7 @@ class PasswordPrompt {
}

close() {
OverlayManager.close(this.overlayName).then(() => {
this.overlayManager.close(this.overlayName).then(() => {
this.input.value = '';
});
}
Expand Down
Loading

0 comments on commit c4dca92

Please sign in to comment.