AnnotationsLayerBuilder.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. (function (name, context, definition) {
  2. if (typeof module != 'undefined' && module.exports) module.exports = definition()
  3. else if (typeof define == 'function' && define.amd) define(definition)
  4. else context[name] = definition()
  5. })('AnnotationsLayerBuilder', this, function (name, context) {
  6. function AnnotationsLayerBuilder(pageView, annotationsLayerDiv) {
  7. this.annotationsLayerDiv = annotationsLayerDiv;
  8. this.pageView = pageView;
  9. };
  10. AnnotationsLayerBuilder.EXTERNAL_LINK_TARGET = "_blank";
  11. AnnotationsLayerBuilder.prototype = {
  12. setAnnotations: function(annotations) {
  13. for (var i = 0; i < annotations.length; i++) {
  14. var annotation = annotations[i];
  15. switch (annotation.subtype) {
  16. case 'Link':
  17. this.addLink(annotation);
  18. break;
  19. case 'Text':
  20. // TODO
  21. break;
  22. }
  23. }
  24. },
  25. addLink: function(link) {
  26. var element = this.buildLinkElementFromRect(link.rect);
  27. this.setLinkTarget(element, link);
  28. this.annotationsLayerDiv.appendChild(element);
  29. },
  30. buildLinkElementFromRect: function(rect) {
  31. rect = this.pageView.viewport.convertToViewportRectangle(rect);
  32. rect = PDFJS.Util.normalizeRect(rect);
  33. var element = document.createElement("a");
  34. element.style.left = Math.floor(rect[0]) + 'px';
  35. element.style.top = Math.floor(rect[1]) + 'px';
  36. element.style.width = Math.ceil(rect[2] - rect[0]) + 'px';
  37. element.style.height = Math.ceil(rect[3] - rect[1]) + 'px';
  38. return element;
  39. },
  40. setLinkTarget: function(element, link) {
  41. if (link.url) {
  42. element.href = link.url;
  43. element.target = this.EXTERNAL_LINK_TARGET;
  44. } else if (link.dest) {
  45. element.href = "#" + link.dest;
  46. var listView = this.pageView.listView;
  47. element.onclick = function(e) {
  48. e.preventDefault()
  49. listView.navigateTo(link.dest);
  50. }
  51. // TODO
  52. }
  53. }
  54. }
  55. return AnnotationsLayerBuilder;
  56. });