2
0

pdf_attachment_viewer.js 5.6 KB

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