diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index 2602f419b..eea2878c3 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -274,7 +274,29 @@ var AnnotationLayer = (function AnnotationLayerClosure() { return container; } - function getHtmlElementForLinkAnnotation(item, page, viewport) { + function getHtmlElementForLinkAnnotation(item, page, viewport, linkService) { + function bindLink(link, dest) { + link.href = linkService.getDestinationHash(dest); + link.onclick = function annotationsLayerBuilderLinksOnclick() { + if (dest) { + linkService.navigateTo(dest); + } + return false; + }; + if (dest) { + link.className = 'internalLink'; + } + } + + function bindNamedAction(link, action) { + link.href = linkService.getAnchorUrl(''); + link.onclick = function annotationsLayerBuilderNamedActionOnClick() { + linkService.executeNamedAction(action); + return false; + }; + link.className = 'internalLink'; + } + var container = getContainer(item, page, viewport); container.className = 'annotLink'; @@ -285,19 +307,28 @@ var AnnotationLayer = (function AnnotationLayerClosure() { link.target = LinkTargetStringMap[PDFJS.externalLinkTarget]; } + if (!item.url) { + if (item.action) { + bindNamedAction(link, item.action); + } else { + bindLink(link, ('dest' in item) ? item.dest : null); + } + } + container.appendChild(link); return container; } - function getHtmlElement(data, page, viewport) { + function getHtmlElement(data, page, viewport, linkService) { switch (data.annotationType) { case AnnotationType.WIDGET: return getHtmlElementForTextWidgetAnnotation(data, page); case AnnotationType.TEXT: return getHtmlElementForTextAnnotation(data, page, viewport); case AnnotationType.LINK: - return getHtmlElementForLinkAnnotation(data, page, viewport); + return getHtmlElementForLinkAnnotation(data, page, viewport, + linkService); default: throw new Error('Unsupported annotationType: ' + data.annotationType); } diff --git a/web/annotations_layer_builder.js b/web/annotations_layer_builder.js index 5a72873c0..37e23d853 100644 --- a/web/annotations_layer_builder.js +++ b/web/annotations_layer_builder.js @@ -48,47 +48,21 @@ var AnnotationsLayerBuilder = (function AnnotationsLayerBuilderClosure() { * @param {PageViewport} viewport * @param {string} intent (default value is 'display') */ - setupAnnotations: - function AnnotationsLayerBuilder_setupAnnotations(viewport, intent) { - function bindLink(link, dest) { - link.href = linkService.getDestinationHash(dest); - link.onclick = function annotationsLayerBuilderLinksOnclick() { - if (dest) { - linkService.navigateTo(dest); - } - return false; - }; - if (dest) { - link.className = 'internalLink'; - } - } - - function bindNamedAction(link, action) { - link.href = linkService.getAnchorUrl(''); - link.onclick = function annotationsLayerBuilderNamedActionOnClick() { - linkService.executeNamedAction(action); - return false; - }; - link.className = 'internalLink'; - } - - var linkService = this.linkService; - var pdfPage = this.pdfPage; + render: function AnnotationsLayerBuilder_render(viewport, intent) { var self = this; - var getAnnotationsParams = { + var parameters = { intent: (intent === undefined ? 'display' : intent), }; - pdfPage.getAnnotations(getAnnotationsParams).then( - function (annotationsData) { + this.pdfPage.getAnnotations(parameters).then(function (annotations) { viewport = viewport.clone({ dontFlip: true }); var data, element, i, ii; if (self.div) { // If an annotationLayer already exists, refresh its children's - // transformation matrices - for (i = 0, ii = annotationsData.length; i < ii; i++) { - data = annotationsData[i]; + // transformation matrices. + for (i = 0, ii = annotations.length; i < ii; i++) { + data = annotations[i]; element = self.div.querySelector( '[data-annotation-id="' + data.id + '"]'); if (element) { @@ -96,46 +70,35 @@ var AnnotationsLayerBuilder = (function AnnotationsLayerBuilderClosure() { 'matrix(' + viewport.transform.join(',') + ')'); } } - // See PDFPageView.reset() self.div.removeAttribute('hidden'); } else { - for (i = 0, ii = annotationsData.length; i < ii; i++) { - data = annotationsData[i]; + if (annotations.length === 0) { + return; + } + + self.div = document.createElement('div'); + self.div.className = 'annotationLayer'; + self.pageDiv.appendChild(self.div); + + for (i = 0, ii = annotations.length; i < ii; i++) { + data = annotations[i]; if (!data || !data.hasHtml) { continue; } - element = PDFJS.AnnotationLayer.getHtmlElement(data, pdfPage, - viewport); + element = PDFJS.AnnotationLayer.getHtmlElement(data, self.pdfPage, + viewport, + self.linkService); if (typeof mozL10n !== 'undefined') { mozL10n.translate(element); } - - if (data.subtype === 'Link' && !data.url) { - var link = element.getElementsByTagName('a')[0]; - if (link) { - if (data.action) { - bindNamedAction(link, data.action); - } else { - bindLink(link, ('dest' in data) ? data.dest : null); - } - } - } - - if (!self.div) { - var annotationLayerDiv = document.createElement('div'); - annotationLayerDiv.className = 'annotationLayer'; - self.pageDiv.appendChild(annotationLayerDiv); - self.div = annotationLayerDiv; - } - self.div.appendChild(element); } } }); }, - hide: function () { + hide: function AnnotationsLayerBuilder_hide() { if (!this.div) { return; } diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index 440b31fb4..1e1e68507 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -275,7 +275,7 @@ var PDFPageView = (function PDFPageViewClosure() { } if (redrawAnnotations && this.annotationLayer) { - this.annotationLayer.setupAnnotations(this.viewport, 'display'); + this.annotationLayer.render(this.viewport, 'display'); } }, @@ -507,7 +507,7 @@ var PDFPageView = (function PDFPageViewClosure() { this.annotationLayer = this.annotationsLayerFactory. createAnnotationsLayerBuilder(div, this.pdfPage); } - this.annotationLayer.setupAnnotations(this.viewport, 'display'); + this.annotationLayer.render(this.viewport, 'display'); } div.setAttribute('data-loaded', true);