Move link creation logic to src/display/annotation_layer.js

Additionally simplify the div creation logic (it needs to happen only
once, so it should not be in the for-loop) and remove/rename variables
for shorter code.
This commit is contained in:
Tim van der Meij 2015-12-08 23:38:32 +01:00
parent 2dc3ee38c0
commit bce3214105
3 changed files with 56 additions and 62 deletions

View File

@ -274,7 +274,29 @@ var AnnotationLayer = (function AnnotationLayerClosure() {
return container; 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); var container = getContainer(item, page, viewport);
container.className = 'annotLink'; container.className = 'annotLink';
@ -285,19 +307,28 @@ var AnnotationLayer = (function AnnotationLayerClosure() {
link.target = LinkTargetStringMap[PDFJS.externalLinkTarget]; 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); container.appendChild(link);
return container; return container;
} }
function getHtmlElement(data, page, viewport) { function getHtmlElement(data, page, viewport, linkService) {
switch (data.annotationType) { switch (data.annotationType) {
case AnnotationType.WIDGET: case AnnotationType.WIDGET:
return getHtmlElementForTextWidgetAnnotation(data, page); return getHtmlElementForTextWidgetAnnotation(data, page);
case AnnotationType.TEXT: case AnnotationType.TEXT:
return getHtmlElementForTextAnnotation(data, page, viewport); return getHtmlElementForTextAnnotation(data, page, viewport);
case AnnotationType.LINK: case AnnotationType.LINK:
return getHtmlElementForLinkAnnotation(data, page, viewport); return getHtmlElementForLinkAnnotation(data, page, viewport,
linkService);
default: default:
throw new Error('Unsupported annotationType: ' + data.annotationType); throw new Error('Unsupported annotationType: ' + data.annotationType);
} }

View File

@ -48,47 +48,21 @@ var AnnotationsLayerBuilder = (function AnnotationsLayerBuilderClosure() {
* @param {PageViewport} viewport * @param {PageViewport} viewport
* @param {string} intent (default value is 'display') * @param {string} intent (default value is 'display')
*/ */
setupAnnotations: render: function AnnotationsLayerBuilder_render(viewport, intent) {
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;
var self = this; var self = this;
var getAnnotationsParams = { var parameters = {
intent: (intent === undefined ? 'display' : intent), intent: (intent === undefined ? 'display' : intent),
}; };
pdfPage.getAnnotations(getAnnotationsParams).then( this.pdfPage.getAnnotations(parameters).then(function (annotations) {
function (annotationsData) {
viewport = viewport.clone({ dontFlip: true }); viewport = viewport.clone({ dontFlip: true });
var data, element, i, ii; var data, element, i, ii;
if (self.div) { if (self.div) {
// If an annotationLayer already exists, refresh its children's // If an annotationLayer already exists, refresh its children's
// transformation matrices // transformation matrices.
for (i = 0, ii = annotationsData.length; i < ii; i++) { for (i = 0, ii = annotations.length; i < ii; i++) {
data = annotationsData[i]; data = annotations[i];
element = self.div.querySelector( element = self.div.querySelector(
'[data-annotation-id="' + data.id + '"]'); '[data-annotation-id="' + data.id + '"]');
if (element) { if (element) {
@ -96,46 +70,35 @@ var AnnotationsLayerBuilder = (function AnnotationsLayerBuilderClosure() {
'matrix(' + viewport.transform.join(',') + ')'); 'matrix(' + viewport.transform.join(',') + ')');
} }
} }
// See PDFPageView.reset()
self.div.removeAttribute('hidden'); self.div.removeAttribute('hidden');
} else { } else {
for (i = 0, ii = annotationsData.length; i < ii; i++) { if (annotations.length === 0) {
data = annotationsData[i]; 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) { if (!data || !data.hasHtml) {
continue; continue;
} }
element = PDFJS.AnnotationLayer.getHtmlElement(data, pdfPage, element = PDFJS.AnnotationLayer.getHtmlElement(data, self.pdfPage,
viewport); viewport,
self.linkService);
if (typeof mozL10n !== 'undefined') { if (typeof mozL10n !== 'undefined') {
mozL10n.translate(element); 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); self.div.appendChild(element);
} }
} }
}); });
}, },
hide: function () { hide: function AnnotationsLayerBuilder_hide() {
if (!this.div) { if (!this.div) {
return; return;
} }

View File

@ -275,7 +275,7 @@ var PDFPageView = (function PDFPageViewClosure() {
} }
if (redrawAnnotations && this.annotationLayer) { 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. this.annotationLayer = this.annotationsLayerFactory.
createAnnotationsLayerBuilder(div, this.pdfPage); createAnnotationsLayerBuilder(div, this.pdfPage);
} }
this.annotationLayer.setupAnnotations(this.viewport, 'display'); this.annotationLayer.render(this.viewport, 'display');
} }
div.setAttribute('data-loaded', true); div.setAttribute('data-loaded', true);