pdf.js/web/dom_events.js
Jonas Jenwald 0b1f41c5b3 Add general support for re-dispatching events, on EventBus instances, to the DOM
This patch is the first step to be able to eventually get rid of the `attachDOMEventsToEventBus` function, by allowing `EventBus` instances to simply re-dispatch most[1] events to the DOM.
Note that the re-dispatching is purposely implemented to occur *after* all registered `EventBus` listeners have been serviced, to prevent the ordering issues that necessitated the duplicated page/scale-change events.

The DOM events are currently necessary for the `mozilla-central` tests, see https://hg.mozilla.org/mozilla-central/file/tip/browser/extensions/pdfjs/test, and perhaps also for custom deployments of the PDF.js default viewer.

Once this have landed, and been successfully uplifted to `mozilla-central`, I intent to submit a patch to update the test-code to utilize the new preference. This will thus, eventually, make it possible to remove the `attachDOMEventsToEventBus` functionality.

*Please note:* I've successfully ran all `mozilla-central` tests locally, with these patches applied.

---
[1] The exception being events that originated on the `window` or `document`, since those are already globally available anyway.
2018-08-30 17:28:12 +02:00

146 lines
5.0 KiB
JavaScript

/* Copyright 2016 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.
*/
import { EventBus } from './ui_utils';
// Attaching to the application event bus to dispatch events to the DOM for
// backwards viewer API compatibility.
function attachDOMEventsToEventBus(eventBus) {
eventBus.on('documentload', function() {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('documentload', true, true, {});
window.dispatchEvent(event);
});
eventBus.on('pagerendered', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('pagerendered', true, true, {
pageNumber: evt.pageNumber,
cssTransform: evt.cssTransform,
});
evt.source.div.dispatchEvent(event);
});
eventBus.on('textlayerrendered', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('textlayerrendered', true, true, {
pageNumber: evt.pageNumber,
});
evt.source.textLayerDiv.dispatchEvent(event);
});
eventBus.on('pagechange', function(evt) {
let event = document.createEvent('UIEvents');
event.initUIEvent('pagechange', true, true, window, 0);
event.pageNumber = evt.pageNumber;
evt.source.container.dispatchEvent(event);
});
eventBus.on('pagesinit', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('pagesinit', true, true, null);
evt.source.container.dispatchEvent(event);
});
eventBus.on('pagesloaded', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('pagesloaded', true, true, {
pagesCount: evt.pagesCount,
});
evt.source.container.dispatchEvent(event);
});
eventBus.on('scalechange', function(evt) {
let event = document.createEvent('UIEvents');
event.initUIEvent('scalechange', true, true, window, 0);
event.scale = evt.scale;
event.presetValue = evt.presetValue;
evt.source.container.dispatchEvent(event);
});
eventBus.on('updateviewarea', function(evt) {
let event = document.createEvent('UIEvents');
event.initUIEvent('updateviewarea', true, true, window, 0);
event.location = evt.location;
evt.source.container.dispatchEvent(event);
});
eventBus.on('find', function(evt) {
if (evt.source === window) {
return; // event comes from FirefoxCom, no need to replicate
}
let event = document.createEvent('CustomEvent');
event.initCustomEvent('find' + evt.type, true, true, {
query: evt.query,
phraseSearch: evt.phraseSearch,
caseSensitive: evt.caseSensitive,
highlightAll: evt.highlightAll,
findPrevious: evt.findPrevious,
});
window.dispatchEvent(event);
});
eventBus.on('attachmentsloaded', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('attachmentsloaded', true, true, {
attachmentsCount: evt.attachmentsCount,
});
evt.source.container.dispatchEvent(event);
});
eventBus.on('sidebarviewchanged', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('sidebarviewchanged', true, true, {
view: evt.view,
});
evt.source.outerContainer.dispatchEvent(event);
});
eventBus.on('pagemode', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('pagemode', true, true, {
mode: evt.mode,
});
evt.source.pdfViewer.container.dispatchEvent(event);
});
eventBus.on('namedaction', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('namedaction', true, true, {
action: evt.action,
});
evt.source.pdfViewer.container.dispatchEvent(event);
});
eventBus.on('presentationmodechanged', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('presentationmodechanged', true, true, {
active: evt.active,
switchInProgress: evt.switchInProgress,
});
window.dispatchEvent(event);
});
eventBus.on('outlineloaded', function(evt) {
let event = document.createEvent('CustomEvent');
event.initCustomEvent('outlineloaded', true, true, {
outlineCount: evt.outlineCount,
});
evt.source.container.dispatchEvent(event);
});
}
let globalEventBus = null;
function getGlobalEventBus(dispatchToDOM = false) {
if (!globalEventBus) {
globalEventBus = new EventBus({ dispatchToDOM, });
if (!dispatchToDOM) {
attachDOMEventsToEventBus(globalEventBus);
}
}
return globalEventBus;
}
export {
attachDOMEventsToEventBus,
getGlobalEventBus,
};