Merge pull request #10096 from Snuffleupagus/rm-attachDOMEventsToEventBus
Remove the `attachDOMEventsToEventBus` functionality, since `EventBus` instances are able to re-dispatch events to the DOM (PR 10019, bug 1492849 follow-up)
This commit is contained in:
commit
f6bc93402e
@ -50,7 +50,7 @@ var pdfViewer = new pdfjsViewer.PDFViewer({
|
||||
});
|
||||
pdfLinkService.setViewer(pdfViewer);
|
||||
|
||||
container.addEventListener('pagesinit', function () {
|
||||
document.addEventListener('pagesinit', function () {
|
||||
// We can use pdfViewer now, e.g. let's change default scale.
|
||||
pdfViewer.currentScaleValue = 'page-width';
|
||||
|
||||
|
@ -50,7 +50,7 @@ var pdfSinglePageViewer = new pdfjsViewer.PDFSinglePageViewer({
|
||||
});
|
||||
pdfLinkService.setViewer(pdfSinglePageViewer);
|
||||
|
||||
container.addEventListener('pagesinit', function () {
|
||||
document.addEventListener('pagesinit', function () {
|
||||
// We can use pdfSinglePageViewer now, e.g. let's change default scale.
|
||||
pdfSinglePageViewer.currentScaleValue = 'page-width';
|
||||
|
||||
|
@ -346,13 +346,13 @@ var PDFViewerApplication = {
|
||||
}
|
||||
});
|
||||
|
||||
container.addEventListener('pagesinit', function () {
|
||||
document.addEventListener('pagesinit', function () {
|
||||
// We can use pdfViewer now, e.g. let's change default scale.
|
||||
pdfViewer.currentScaleValue = DEFAULT_SCALE_VALUE;
|
||||
});
|
||||
|
||||
container.addEventListener('pagechange', function (evt) {
|
||||
var page = evt.pageNumber;
|
||||
document.addEventListener('pagechanging', function (evt) {
|
||||
var page = evt.detail.pageNumber;
|
||||
var numPages = PDFViewerApplication.pagesCount;
|
||||
|
||||
document.getElementById('pageNumber').value = page;
|
||||
|
@ -45,7 +45,7 @@ var pdfViewer = new pdfjsViewer.PDFViewer({
|
||||
});
|
||||
pdfLinkService.setViewer(pdfViewer);
|
||||
|
||||
container.addEventListener('pagesinit', function () {
|
||||
document.addEventListener('pagesinit', function () {
|
||||
// We can use pdfViewer now, e.g. let's change default scale.
|
||||
pdfViewer.currentScaleValue = 'page-width';
|
||||
});
|
||||
|
12
web/app.js
12
web/app.js
@ -15,10 +15,10 @@
|
||||
/* globals PDFBug, Stats */
|
||||
|
||||
import {
|
||||
animationStarted, DEFAULT_SCALE_VALUE, getPDFFileNameFromURL, isValidRotation,
|
||||
MAX_SCALE, MIN_SCALE, noContextMenuHandler, normalizeWheelEventDelta,
|
||||
parseQueryString, PresentationModeState, ProgressBar, RendererType,
|
||||
TextLayerMode
|
||||
animationStarted, DEFAULT_SCALE_VALUE, getGlobalEventBus,
|
||||
getPDFFileNameFromURL, isValidRotation, MAX_SCALE, MIN_SCALE,
|
||||
noContextMenuHandler, normalizeWheelEventDelta, parseQueryString,
|
||||
PresentationModeState, ProgressBar, RendererType, TextLayerMode
|
||||
} from './ui_utils';
|
||||
import {
|
||||
build, createObjectURL, getDocument, getFilenameFromUrl, GlobalWorkerOptions,
|
||||
@ -30,7 +30,6 @@ import { CursorTool, PDFCursorTools } from './pdf_cursor_tools';
|
||||
import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue';
|
||||
import { PDFSidebar, SidebarView } from './pdf_sidebar';
|
||||
import { AppOptions } from './app_options';
|
||||
import { getGlobalEventBus } from './dom_events';
|
||||
import { OverlayManager } from './overlay_manager';
|
||||
import { PasswordPrompt } from './password_prompt';
|
||||
import { PDFAttachmentViewer } from './pdf_attachment_viewer';
|
||||
@ -869,9 +868,6 @@ let PDFViewerApplication = {
|
||||
|
||||
firstPagePromise.then(() => {
|
||||
this.eventBus.dispatch('documentloaded', { source: this, });
|
||||
// TODO: Remove the following event, i.e. 'documentload',
|
||||
// once the mozilla-central tests have been updated.
|
||||
this.eventBus.dispatch('documentload', { source: this, });
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -14,15 +14,14 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, isPortraitOrientation,
|
||||
isValidRotation, MAX_AUTO_SCALE, moveToEndOfArray, NullL10n,
|
||||
PresentationModeState, RendererType, SCROLLBAR_PADDING, TextLayerMode,
|
||||
UNKNOWN_SCALE, VERTICAL_PADDING, watchScroll
|
||||
CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, getGlobalEventBus,
|
||||
isPortraitOrientation, isValidRotation, MAX_AUTO_SCALE, moveToEndOfArray,
|
||||
NullL10n, PresentationModeState, RendererType, SCROLLBAR_PADDING,
|
||||
TextLayerMode, UNKNOWN_SCALE, VERTICAL_PADDING, watchScroll
|
||||
} from './ui_utils';
|
||||
import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue';
|
||||
import { AnnotationLayerBuilder } from './annotation_layer_builder';
|
||||
import { createPromiseCapability } from 'pdfjs-lib';
|
||||
import { getGlobalEventBus } from './dom_events';
|
||||
import { PDFPageView } from './pdf_page_view';
|
||||
import { SimpleLinkService } from './pdf_link_service';
|
||||
import { TextLayerBuilder } from './text_layer_builder';
|
||||
@ -232,15 +231,13 @@ class BaseViewer {
|
||||
`${this._name}._setCurrentPageNumber: "${val}" is out of bounds.`);
|
||||
return;
|
||||
}
|
||||
this._currentPageNumber = val;
|
||||
|
||||
let arg = {
|
||||
this.eventBus.dispatch('pagechanging', {
|
||||
source: this,
|
||||
pageNumber: val,
|
||||
pageLabel: this._pageLabels && this._pageLabels[val - 1],
|
||||
};
|
||||
this._currentPageNumber = val;
|
||||
this.eventBus.dispatch('pagechanging', arg);
|
||||
this.eventBus.dispatch('pagechange', arg);
|
||||
});
|
||||
|
||||
if (resetCurrentPageView) {
|
||||
this._resetCurrentPageView();
|
||||
@ -541,22 +538,16 @@ class BaseViewer {
|
||||
throw new Error('Not implemented: _scrollIntoView');
|
||||
}
|
||||
|
||||
_setScaleDispatchEvent(newScale, newValue, preset = false) {
|
||||
let arg = {
|
||||
source: this,
|
||||
scale: newScale,
|
||||
presetValue: preset ? newValue : undefined,
|
||||
};
|
||||
this.eventBus.dispatch('scalechanging', arg);
|
||||
this.eventBus.dispatch('scalechange', arg);
|
||||
}
|
||||
|
||||
_setScaleUpdatePages(newScale, newValue, noScroll = false, preset = false) {
|
||||
this._currentScaleValue = newValue.toString();
|
||||
|
||||
if (isSameScale(this._currentScale, newScale)) {
|
||||
if (preset) {
|
||||
this._setScaleDispatchEvent(newScale, newValue, true);
|
||||
this.eventBus.dispatch('scalechanging', {
|
||||
source: this,
|
||||
scale: newScale,
|
||||
presetValue: newValue,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -581,7 +572,11 @@ class BaseViewer {
|
||||
});
|
||||
}
|
||||
|
||||
this._setScaleDispatchEvent(newScale, newValue, preset);
|
||||
this.eventBus.dispatch('scalechanging', {
|
||||
source: this,
|
||||
scale: newScale,
|
||||
presetValue: preset ? newValue : undefined,
|
||||
});
|
||||
|
||||
if (this.defaultRenderingQueue) {
|
||||
this.update();
|
||||
|
@ -1,145 +0,0 @@
|
||||
/* 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,
|
||||
};
|
@ -15,7 +15,7 @@
|
||||
|
||||
import { createPromiseCapability } from 'pdfjs-lib';
|
||||
import { getCharacterType } from './pdf_find_utils';
|
||||
import { getGlobalEventBus } from './dom_events';
|
||||
import { getGlobalEventBus } from './ui_utils';
|
||||
|
||||
const FindState = {
|
||||
FOUND: 0,
|
||||
|
@ -14,9 +14,8 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
isValidRotation, parseQueryString, waitOnEventOrTimeout
|
||||
getGlobalEventBus, isValidRotation, parseQueryString, waitOnEventOrTimeout
|
||||
} from './ui_utils';
|
||||
import { getGlobalEventBus } from './dom_events';
|
||||
|
||||
// Heuristic value used when force-resetting `this._blockHashChange`.
|
||||
const HASH_CHANGE_TIMEOUT = 1000; // milliseconds
|
||||
|
@ -13,8 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { getGlobalEventBus } from './dom_events';
|
||||
import { parseQueryString } from './ui_utils';
|
||||
import { getGlobalEventBus, parseQueryString } from './ui_utils';
|
||||
|
||||
/**
|
||||
* @typedef {Object} PDFLinkServiceOptions
|
||||
|
@ -14,13 +14,12 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
approximateFraction, CSS_UNITS, DEFAULT_SCALE, getOutputScale, NullL10n,
|
||||
RendererType, roundToDivide, TextLayerMode
|
||||
approximateFraction, CSS_UNITS, DEFAULT_SCALE, getGlobalEventBus,
|
||||
getOutputScale, NullL10n, RendererType, roundToDivide, TextLayerMode
|
||||
} from './ui_utils';
|
||||
import {
|
||||
createPromiseCapability, RenderingCancelledException, SVGGraphics
|
||||
} from 'pdfjs-lib';
|
||||
import { getGlobalEventBus } from './dom_events';
|
||||
import { RenderingStates } from './pdf_rendering_queue';
|
||||
import { viewerCompatibilityParams } from './viewer_compatibility';
|
||||
|
||||
|
@ -20,7 +20,9 @@ import {
|
||||
import {
|
||||
DefaultTextLayerFactory, TextLayerBuilder
|
||||
} from './text_layer_builder.js';
|
||||
import { EventBus, NullL10n, ProgressBar } from './ui_utils.js';
|
||||
import {
|
||||
EventBus, getGlobalEventBus, NullL10n, ProgressBar
|
||||
} from './ui_utils.js';
|
||||
import { PDFLinkService, SimpleLinkService } from './pdf_link_service.js';
|
||||
import { DownloadManager } from './download_manager.js';
|
||||
import { GenericL10n } from './genericl10n.js';
|
||||
@ -33,6 +35,9 @@ import { PDFViewer } from './pdf_viewer.js';
|
||||
const pdfjsVersion = PDFJSDev.eval('BUNDLE_VERSION');
|
||||
const pdfjsBuild = PDFJSDev.eval('BUNDLE_BUILD');
|
||||
|
||||
// For backwards compatibility, ensure that events are re-dispatched to the DOM.
|
||||
getGlobalEventBus(/* dispatchToDOM = */ true);
|
||||
|
||||
export {
|
||||
PDFViewer,
|
||||
PDFSinglePageViewer,
|
||||
|
@ -13,9 +13,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { getGlobalEventBus } from './dom_events';
|
||||
import { getGlobalEventBus, scrollIntoView } from './ui_utils';
|
||||
import { renderTextLayer } from 'pdfjs-lib';
|
||||
import { scrollIntoView } from './ui_utils';
|
||||
|
||||
const EXPAND_DIVS_TIMEOUT = 300; // ms
|
||||
const MATCH_SCROLL_OFFSET_TOP = -50;
|
||||
|
@ -732,9 +732,6 @@ class EventBus {
|
||||
* @private
|
||||
*/
|
||||
_dispatchDOMEvent(eventName, args = null) {
|
||||
if (!this._dispatchToDOM) {
|
||||
return;
|
||||
}
|
||||
const details = Object.create(null);
|
||||
if (args && args.length > 0) {
|
||||
const obj = args[0];
|
||||
@ -755,6 +752,14 @@ class EventBus {
|
||||
}
|
||||
}
|
||||
|
||||
let globalEventBus = null;
|
||||
function getGlobalEventBus(dispatchToDOM = false) {
|
||||
if (!globalEventBus) {
|
||||
globalEventBus = new EventBus({ dispatchToDOM, });
|
||||
}
|
||||
return globalEventBus;
|
||||
}
|
||||
|
||||
function clamp(v, min, max) {
|
||||
return Math.min(Math.max(v, min), max);
|
||||
}
|
||||
@ -868,6 +873,7 @@ export {
|
||||
TextLayerMode,
|
||||
NullL10n,
|
||||
EventBus,
|
||||
getGlobalEventBus,
|
||||
ProgressBar,
|
||||
getPDFFileNameFromURL,
|
||||
noContextMenuHandler,
|
||||
|
@ -60,7 +60,7 @@ function getViewerConfiguration() {
|
||||
appContainer: document.body,
|
||||
mainContainer: document.getElementById('viewerContainer'),
|
||||
viewerContainer: document.getElementById('viewer'),
|
||||
eventBus: null, // using global event bus with DOM events
|
||||
eventBus: null, // Using global event bus with (optional) DOM events.
|
||||
toolbar: {
|
||||
container: document.getElementById('toolbarViewer'),
|
||||
numPages: document.getElementById('numPages'),
|
||||
|
Loading…
x
Reference in New Issue
Block a user