pdf_attachment_viewer.js 5.7 KB

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