From a96b0f80dd6883b9da366cbb430135827434aa24 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 8 Dec 2016 14:44:12 +0100 Subject: [PATCH] Convert the only remaining consumer (in `hand_tool.js`) of the 'localized' event to use the `localized` Promise instead, and only re-dispatch the 'localized' event on the `eventBus` for `GENERIC` builds Ideally we'd remove the 'localized' event from the `eventBus`, but for backwards compatibility we keep it in `GENERIC` builds. Note that while we want to ensure that the direction attribute of the HTML is updated as soon as the `localized` Promise is resolved, we purposely wait until the viewer has been initialized to ensure that the 'localized' event will always be dispatched. --- web/app.js | 17 +++++++++++------ web/hand_tool.js | 19 ++++++++++--------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/web/app.js b/web/app.js index d2fe73230..f7e76b3e9 100644 --- a/web/app.js +++ b/web/app.js @@ -215,6 +215,14 @@ var PDFViewerApplication = { self.bindEvents(); self.bindWindowEvents(); + if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) { + // For backwards compatibility, we dispatch the 'localized' event on + // the `eventBus` once the viewer has been initialized. + localized.then(function () { + self.eventBus.dispatch('localized'); + }); + } + if (self.isViewerEmbedded && !PDFJS.isExternalLinkTargetSet()) { // Prevent external links from "replacing" the viewer, // when it's embedded in e.g. an iframe or an object. @@ -1788,11 +1796,6 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) { }; } -function webViewerLocalized() { - document.getElementsByTagName('html')[0].dir = mozL10n.getDirection(); - PDFViewerApplication.eventBus.dispatch('localized'); -} - function webViewerPresentationMode() { PDFViewerApplication.requestPresentationMode(); } @@ -2241,7 +2244,9 @@ function webViewerKeyDown(evt) { } } -localized.then(webViewerLocalized); +localized.then(function webViewerLocalized() { + document.getElementsByTagName('html')[0].dir = mozL10n.getDirection(); +}); /* Abstract factory for the print service. */ var PDFPrintServiceFactory = { diff --git a/web/hand_tool.js b/web/hand_tool.js index e0db5ac6c..e4e401284 100644 --- a/web/hand_tool.js +++ b/web/hand_tool.js @@ -18,17 +18,19 @@ (function (root, factory) { if (typeof define === 'function' && define.amd) { define('pdfjs-web/hand_tool', ['exports', 'pdfjs-web/grab_to_pan', - 'pdfjs-web/preferences'], factory); + 'pdfjs-web/preferences', 'pdfjs-web/ui_utils'], factory); } else if (typeof exports !== 'undefined') { - factory(exports, require('./grab_to_pan.js'), require('./preferences.js')); + factory(exports, require('./grab_to_pan.js'), require('./preferences.js'), + require('./ui_utils.js')); } else { factory((root.pdfjsWebHandTool = {}), root.pdfjsWebGrabToPan, - root.pdfjsWebPreferences); + root.pdfjsWebPreferences, root.pdfjsWebUIUtils); } -}(this, function (exports, grabToPan, preferences) { +}(this, function (exports, grabToPan, preferences, uiUtils) { var GrabToPan = grabToPan.GrabToPan; var Preferences = preferences.Preferences; +var localized = uiUtils.localized; /** * @typedef {Object} HandToolOptions @@ -59,13 +61,12 @@ var HandTool = (function HandToolClosure() { this.eventBus.on('togglehandtool', this.toggle.bind(this)); - this.eventBus.on('localized', function (e) { - Preferences.get('enableHandToolOnLoad').then(function resolved(value) { - if (value) { + Promise.all([localized, Preferences.get('enableHandToolOnLoad')]).then( + function resolved(values) { + if (values[1] === true) { this.handTool.activate(); } - }.bind(this), function rejected(reason) {}); - }.bind(this)); + }.bind(this)).catch(function rejected(reason) { }); this.eventBus.on('presentationmodechanged', function (e) { if (e.switchInProgress) {