pdf_attachment_viewer.js 5.5 KB

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