pdf_attachment_viewer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  29. 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); } }
  30. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  31. var PDFAttachmentViewer =
  32. /*#__PURE__*/
  33. function () {
  34. function PDFAttachmentViewer(_ref) {
  35. var container = _ref.container,
  36. eventBus = _ref.eventBus,
  37. downloadManager = _ref.downloadManager;
  38. _classCallCheck(this, PDFAttachmentViewer);
  39. this.container = container;
  40. this.eventBus = eventBus;
  41. this.downloadManager = downloadManager;
  42. this.reset();
  43. this.eventBus.on('fileattachmentannotation', this._appendAttachment.bind(this));
  44. }
  45. _createClass(PDFAttachmentViewer, [{
  46. key: "reset",
  47. value: function reset() {
  48. var keepRenderedCapability = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  49. this.attachments = null;
  50. this.container.textContent = '';
  51. if (!keepRenderedCapability) {
  52. this._renderedCapability = (0, _pdf.createPromiseCapability)();
  53. }
  54. }
  55. }, {
  56. key: "_dispatchEvent",
  57. value: function _dispatchEvent(attachmentsCount) {
  58. this._renderedCapability.resolve();
  59. this.eventBus.dispatch('attachmentsloaded', {
  60. source: this,
  61. attachmentsCount: attachmentsCount
  62. });
  63. }
  64. }, {
  65. key: "_bindPdfLink",
  66. value: function _bindPdfLink(button, content, filename) {
  67. if (this.downloadManager.disableCreateObjectURL) {
  68. throw new Error('bindPdfLink: Unsupported "disableCreateObjectURL" value.');
  69. }
  70. var blobUrl;
  71. button.onclick = function () {
  72. if (!blobUrl) {
  73. blobUrl = (0, _pdf.createObjectURL)(content, 'application/pdf');
  74. }
  75. var viewerUrl;
  76. viewerUrl = '?file=' + encodeURIComponent(blobUrl + '#' + filename);
  77. window.open(viewerUrl);
  78. return false;
  79. };
  80. }
  81. }, {
  82. key: "_bindLink",
  83. value: function _bindLink(button, content, filename) {
  84. var _this = this;
  85. button.onclick = function () {
  86. _this.downloadManager.downloadData(content, filename, '');
  87. return false;
  88. };
  89. }
  90. }, {
  91. key: "render",
  92. value: function render(_ref2) {
  93. var attachments = _ref2.attachments,
  94. _ref2$keepRenderedCap = _ref2.keepRenderedCapability,
  95. keepRenderedCapability = _ref2$keepRenderedCap === void 0 ? false : _ref2$keepRenderedCap;
  96. var attachmentsCount = 0;
  97. if (this.attachments) {
  98. this.reset(keepRenderedCapability === true);
  99. }
  100. this.attachments = attachments || null;
  101. if (!attachments) {
  102. this._dispatchEvent(attachmentsCount);
  103. return;
  104. }
  105. var names = Object.keys(attachments).sort(function (a, b) {
  106. return a.toLowerCase().localeCompare(b.toLowerCase());
  107. });
  108. attachmentsCount = names.length;
  109. for (var i = 0; i < attachmentsCount; i++) {
  110. var item = attachments[names[i]];
  111. var filename = (0, _pdf.removeNullCharacters)((0, _pdf.getFilenameFromUrl)(item.filename));
  112. var div = document.createElement('div');
  113. div.className = 'attachmentsItem';
  114. var button = document.createElement('button');
  115. button.textContent = filename;
  116. if (/\.pdf$/i.test(filename) && !this.downloadManager.disableCreateObjectURL) {
  117. this._bindPdfLink(button, item.content, filename);
  118. } else {
  119. this._bindLink(button, item.content, filename);
  120. }
  121. div.appendChild(button);
  122. this.container.appendChild(div);
  123. }
  124. this._dispatchEvent(attachmentsCount);
  125. }
  126. }, {
  127. key: "_appendAttachment",
  128. value: function _appendAttachment(_ref3) {
  129. var _this2 = this;
  130. var id = _ref3.id,
  131. filename = _ref3.filename,
  132. content = _ref3.content;
  133. this._renderedCapability.promise.then(function () {
  134. var attachments = _this2.attachments;
  135. if (!attachments) {
  136. attachments = Object.create(null);
  137. } else {
  138. for (var name in attachments) {
  139. if (id === name) {
  140. return;
  141. }
  142. }
  143. }
  144. attachments[id] = {
  145. filename: filename,
  146. content: content
  147. };
  148. _this2.render({
  149. attachments: attachments,
  150. keepRenderedCapability: true
  151. });
  152. });
  153. }
  154. }]);
  155. return PDFAttachmentViewer;
  156. }();
  157. exports.PDFAttachmentViewer = PDFAttachmentViewer;