From 9ab896e30793926d8b6b4c6ad471852d8b4d611c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 19 Apr 2015 12:27:51 +0200 Subject: [PATCH] [api-minor] Add an option to PDFJS for specifying the |target| attribute of external links Replaces `PDFJS.openExternalLinksInNewWindow` with a more generic configuration option. *Note:* `PDFJS.openExternalLinksInNewWindow = true;` is equal to `PDFJS.externalLinkTarget = PDFJS.LinkTarget.BLANK;`. --- src/display/annotation_helper.js | 7 +++--- src/display/api.js | 17 +++++++++++-- src/shared/util.js | 43 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/display/annotation_helper.js b/src/display/annotation_helper.js index 922535960..13d1db69a 100644 --- a/src/display/annotation_helper.js +++ b/src/display/annotation_helper.js @@ -15,7 +15,7 @@ * limitations under the License. */ /* globals PDFJS, Util, AnnotationType, AnnotationBorderStyleType, warn, - CustomStyle */ + CustomStyle, isExternalLinkTargetSet, LinkTargetStringMap */ 'use strict'; @@ -261,8 +261,9 @@ var AnnotationUtils = (function AnnotationUtilsClosure() { var link = document.createElement('a'); link.href = link.title = item.url || ''; - if (item.url && PDFJS.openExternalLinksInNewWindow) { - link.target = '_blank'; + + if (item.url && isExternalLinkTargetSet()) { + link.target = LinkTargetStringMap[PDFJS.externalLinkTarget]; } container.appendChild(link); diff --git a/src/display/api.js b/src/display/api.js index 5d7f8093b..c89c931a8 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -169,14 +169,27 @@ PDFJS.maxCanvasPixels = (PDFJS.maxCanvasPixels === undefined ? 16777216 : PDFJS.maxCanvasPixels); /** - * Opens external links in a new window if enabled. The default behavior opens - * external links in the PDF.js window. + * (Deprecated) Opens external links in a new window if enabled. + * The default behavior opens external links in the PDF.js window. * @var {boolean} */ PDFJS.openExternalLinksInNewWindow = ( PDFJS.openExternalLinksInNewWindow === undefined ? false : PDFJS.openExternalLinksInNewWindow); +/** + * Specifies the |target| attribute for external links. + * The constants from PDFJS.LinkTarget should be used: + * - NONE [default] + * - SELF + * - BLANK + * - PARENT + * - TOP + * @var {number} + */ +PDFJS.externalLinkTarget = (PDFJS.externalLinkTarget === undefined ? + PDFJS.LinkTarget.NONE : PDFJS.externalLinkTarget); + /** * Determines if we can eval strings as JS. Primarily used to improve * performance for font rendering. diff --git a/src/shared/util.js b/src/shared/util.js index b003b4a92..f4821bcc9 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -328,6 +328,49 @@ function shadow(obj, prop, value) { } PDFJS.shadow = shadow; +var LinkTarget = PDFJS.LinkTarget = { + NONE: 0, // Default value. + SELF: 1, + BLANK: 2, + PARENT: 3, + TOP: 4, +}; +var LinkTargetStringMap = [ + '', + '_self', + '_blank', + '_parent', + '_top' +]; + +function isExternalLinkTargetSet() { +//#if GENERIC + if (PDFJS.openExternalLinksInNewWindow) { + warn('PDFJS.openExternalLinksInNewWindow is deprecated, ' + + 'use PDFJS.externalLinkTarget instead.'); + if (PDFJS.externalLinkTarget === LinkTarget.NONE) { + PDFJS.externalLinkTarget = LinkTarget.BLANK; + } + // Reset the deprecated parameter, to suppress further warnings. + PDFJS.openExternalLinksInNewWindow = false; + } +//#endif + switch (PDFJS.externalLinkTarget) { + case LinkTarget.NONE: + return false; + case LinkTarget.SELF: + case LinkTarget.BLANK: + case LinkTarget.PARENT: + case LinkTarget.TOP: + return true; + } + warn('PDFJS.externalLinkTarget is invalid: ' + PDFJS.externalLinkTarget); + // Reset the external link target, to suppress further warnings. + PDFJS.externalLinkTarget = LinkTarget.NONE; + return false; +} +PDFJS.isExternalLinkTargetSet = isExternalLinkTargetSet; + var PasswordResponses = PDFJS.PasswordResponses = { NEED_PASSWORD: 1, INCORRECT_PASSWORD: 2