annotation_layer_builder.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.DefaultAnnotationLayerFactory = exports.AnnotationLayerBuilder = undefined;
  20. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  21. var _pdf = require('../pdf');
  22. var _ui_utils = require('./ui_utils');
  23. var _pdf_link_service = require('./pdf_link_service');
  24. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  25. var AnnotationLayerBuilder = function () {
  26. function AnnotationLayerBuilder(_ref) {
  27. var pageDiv = _ref.pageDiv,
  28. pdfPage = _ref.pdfPage,
  29. linkService = _ref.linkService,
  30. downloadManager = _ref.downloadManager,
  31. _ref$renderInteractiv = _ref.renderInteractiveForms,
  32. renderInteractiveForms = _ref$renderInteractiv === undefined ? false : _ref$renderInteractiv,
  33. _ref$l10n = _ref.l10n,
  34. l10n = _ref$l10n === undefined ? _ui_utils.NullL10n : _ref$l10n;
  35. _classCallCheck(this, AnnotationLayerBuilder);
  36. this.pageDiv = pageDiv;
  37. this.pdfPage = pdfPage;
  38. this.linkService = linkService;
  39. this.downloadManager = downloadManager;
  40. this.renderInteractiveForms = renderInteractiveForms;
  41. this.l10n = l10n;
  42. this.div = null;
  43. this._cancelled = false;
  44. }
  45. _createClass(AnnotationLayerBuilder, [{
  46. key: 'render',
  47. value: function render(viewport) {
  48. var _this = this;
  49. var intent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'display';
  50. this.pdfPage.getAnnotations({ intent: intent }).then(function (annotations) {
  51. if (_this._cancelled) {
  52. return;
  53. }
  54. var parameters = {
  55. viewport: viewport.clone({ dontFlip: true }),
  56. div: _this.div,
  57. annotations: annotations,
  58. page: _this.pdfPage,
  59. renderInteractiveForms: _this.renderInteractiveForms,
  60. linkService: _this.linkService,
  61. downloadManager: _this.downloadManager
  62. };
  63. if (_this.div) {
  64. _pdf.AnnotationLayer.update(parameters);
  65. } else {
  66. if (annotations.length === 0) {
  67. return;
  68. }
  69. _this.div = document.createElement('div');
  70. _this.div.className = 'annotationLayer';
  71. _this.pageDiv.appendChild(_this.div);
  72. parameters.div = _this.div;
  73. _pdf.AnnotationLayer.render(parameters);
  74. _this.l10n.translate(_this.div);
  75. }
  76. });
  77. }
  78. }, {
  79. key: 'cancel',
  80. value: function cancel() {
  81. this._cancelled = true;
  82. }
  83. }, {
  84. key: 'hide',
  85. value: function hide() {
  86. if (!this.div) {
  87. return;
  88. }
  89. this.div.setAttribute('hidden', 'true');
  90. }
  91. }]);
  92. return AnnotationLayerBuilder;
  93. }();
  94. var DefaultAnnotationLayerFactory = function () {
  95. function DefaultAnnotationLayerFactory() {
  96. _classCallCheck(this, DefaultAnnotationLayerFactory);
  97. }
  98. _createClass(DefaultAnnotationLayerFactory, [{
  99. key: 'createAnnotationLayerBuilder',
  100. value: function createAnnotationLayerBuilder(pageDiv, pdfPage) {
  101. var renderInteractiveForms = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  102. var l10n = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _ui_utils.NullL10n;
  103. return new AnnotationLayerBuilder({
  104. pageDiv: pageDiv,
  105. pdfPage: pdfPage,
  106. renderInteractiveForms: renderInteractiveForms,
  107. linkService: new _pdf_link_service.SimpleLinkService(),
  108. l10n: l10n
  109. });
  110. }
  111. }]);
  112. return DefaultAnnotationLayerFactory;
  113. }();
  114. exports.AnnotationLayerBuilder = AnnotationLayerBuilder;
  115. exports.DefaultAnnotationLayerFactory = DefaultAnnotationLayerFactory;