pdf_attachment_viewer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFAttachmentViewer = void 0;
  27. var _pdf = require("../pdf");
  28. var _base_tree_viewer = require("./base_tree_viewer.js");
  29. class PDFAttachmentViewer extends _base_tree_viewer.BaseTreeViewer {
  30. constructor(options) {
  31. super(options);
  32. this.downloadManager = options.downloadManager;
  33. this.eventBus._on("fileattachmentannotation", this._appendAttachment.bind(this));
  34. }
  35. reset(keepRenderedCapability = false) {
  36. super.reset();
  37. this._attachments = null;
  38. if (!keepRenderedCapability) {
  39. this._renderedCapability = (0, _pdf.createPromiseCapability)();
  40. }
  41. if (this._pendingDispatchEvent) {
  42. clearTimeout(this._pendingDispatchEvent);
  43. }
  44. this._pendingDispatchEvent = null;
  45. }
  46. _dispatchEvent(attachmentsCount) {
  47. this._renderedCapability.resolve();
  48. if (this._pendingDispatchEvent) {
  49. clearTimeout(this._pendingDispatchEvent);
  50. this._pendingDispatchEvent = null;
  51. }
  52. if (attachmentsCount === 0) {
  53. this._pendingDispatchEvent = setTimeout(() => {
  54. this.eventBus.dispatch("attachmentsloaded", {
  55. source: this,
  56. attachmentsCount: 0
  57. });
  58. this._pendingDispatchEvent = null;
  59. });
  60. return;
  61. }
  62. this.eventBus.dispatch("attachmentsloaded", {
  63. source: this,
  64. attachmentsCount
  65. });
  66. }
  67. _bindLink(element, {
  68. content,
  69. filename
  70. }) {
  71. element.onclick = () => {
  72. this.downloadManager.openOrDownloadData(element, content, filename);
  73. return false;
  74. };
  75. }
  76. render({
  77. attachments,
  78. keepRenderedCapability = false
  79. }) {
  80. if (this._attachments) {
  81. this.reset(keepRenderedCapability);
  82. }
  83. this._attachments = attachments || null;
  84. if (!attachments) {
  85. this._dispatchEvent(0);
  86. return;
  87. }
  88. const names = Object.keys(attachments).sort(function (a, b) {
  89. return a.toLowerCase().localeCompare(b.toLowerCase());
  90. });
  91. const fragment = document.createDocumentFragment();
  92. let attachmentsCount = 0;
  93. for (const name of names) {
  94. const item = attachments[name];
  95. const content = item.content,
  96. filename = (0, _pdf.getFilenameFromUrl)(item.filename);
  97. const div = document.createElement("div");
  98. div.className = "treeItem";
  99. const element = document.createElement("a");
  100. this._bindLink(element, {
  101. content,
  102. filename
  103. });
  104. element.textContent = this._normalizeTextContent(filename);
  105. div.appendChild(element);
  106. fragment.appendChild(div);
  107. attachmentsCount++;
  108. }
  109. this._finishRendering(fragment, attachmentsCount);
  110. }
  111. _appendAttachment({
  112. id,
  113. filename,
  114. content
  115. }) {
  116. const renderedPromise = this._renderedCapability.promise;
  117. renderedPromise.then(() => {
  118. if (renderedPromise !== this._renderedCapability.promise) {
  119. return;
  120. }
  121. let attachments = this._attachments;
  122. if (!attachments) {
  123. attachments = Object.create(null);
  124. } else {
  125. for (const name in attachments) {
  126. if (id === name) {
  127. return;
  128. }
  129. }
  130. }
  131. attachments[id] = {
  132. filename,
  133. content
  134. };
  135. this.render({
  136. attachments,
  137. keepRenderedCapability: true
  138. });
  139. });
  140. }
  141. }
  142. exports.PDFAttachmentViewer = PDFAttachmentViewer;