pdf_document_properties.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.PDFDocumentProperties = 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 _ui_utils = require('./ui_utils');
  22. var _pdfjs = require('./pdfjs');
  23. var _overlay_manager = require('./overlay_manager');
  24. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  25. var DEFAULT_FIELD_CONTENT = '-';
  26. var PDFDocumentProperties = function () {
  27. function PDFDocumentProperties(_ref) {
  28. var overlayName = _ref.overlayName,
  29. fields = _ref.fields,
  30. container = _ref.container,
  31. closeButton = _ref.closeButton;
  32. _classCallCheck(this, PDFDocumentProperties);
  33. this.overlayName = overlayName;
  34. this.fields = fields;
  35. this.container = container;
  36. this._reset();
  37. if (closeButton) {
  38. closeButton.addEventListener('click', this.close.bind(this));
  39. }
  40. _overlay_manager.OverlayManager.register(this.overlayName, this.container, this.close.bind(this));
  41. }
  42. _createClass(PDFDocumentProperties, [{
  43. key: 'open',
  44. value: function open() {
  45. var _this = this;
  46. var freezeFieldData = function freezeFieldData(data) {
  47. Object.defineProperty(_this, 'fieldData', {
  48. value: Object.freeze(data),
  49. writable: false,
  50. enumerable: true,
  51. configurable: true
  52. });
  53. };
  54. Promise.all([_overlay_manager.OverlayManager.open(this.overlayName), this._dataAvailableCapability.promise]).then(function () {
  55. if (_this.fieldData) {
  56. _this._updateUI();
  57. return;
  58. }
  59. _this.pdfDocument.getMetadata().then(function (_ref2) {
  60. var info = _ref2.info,
  61. metadata = _ref2.metadata;
  62. freezeFieldData({
  63. 'fileName': (0, _ui_utils.getPDFFileNameFromURL)(_this.url),
  64. 'fileSize': _this._parseFileSize(_this.maybeFileSize),
  65. 'title': info.Title,
  66. 'author': info.Author,
  67. 'subject': info.Subject,
  68. 'keywords': info.Keywords,
  69. 'creationDate': _this._parseDate(info.CreationDate),
  70. 'modificationDate': _this._parseDate(info.ModDate),
  71. 'creator': info.Creator,
  72. 'producer': info.Producer,
  73. 'version': info.PDFFormatVersion,
  74. 'pageCount': _this.pdfDocument.numPages
  75. });
  76. _this._updateUI();
  77. return _this.pdfDocument.getDownloadInfo();
  78. }).then(function (_ref3) {
  79. var length = _ref3.length;
  80. var data = (0, _ui_utils.cloneObj)(_this.fieldData);
  81. data['fileSize'] = _this._parseFileSize(length);
  82. freezeFieldData(data);
  83. _this._updateUI();
  84. });
  85. });
  86. }
  87. }, {
  88. key: 'close',
  89. value: function close() {
  90. _overlay_manager.OverlayManager.close(this.overlayName);
  91. }
  92. }, {
  93. key: 'setDocument',
  94. value: function setDocument(pdfDocument, url) {
  95. if (this.pdfDocument) {
  96. this._reset();
  97. this._updateUI(true);
  98. }
  99. if (!pdfDocument) {
  100. return;
  101. }
  102. this.pdfDocument = pdfDocument;
  103. this.url = url;
  104. this._dataAvailableCapability.resolve();
  105. }
  106. }, {
  107. key: 'setFileSize',
  108. value: function setFileSize(fileSize) {
  109. if (typeof fileSize === 'number' && fileSize > 0) {
  110. this.maybeFileSize = fileSize;
  111. }
  112. }
  113. }, {
  114. key: '_reset',
  115. value: function _reset() {
  116. this.pdfDocument = null;
  117. this.url = null;
  118. this.maybeFileSize = 0;
  119. delete this.fieldData;
  120. this._dataAvailableCapability = (0, _pdfjs.createPromiseCapability)();
  121. }
  122. }, {
  123. key: '_updateUI',
  124. value: function _updateUI() {
  125. var reset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  126. if (reset || !this.fieldData) {
  127. for (var id in this.fields) {
  128. this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
  129. }
  130. return;
  131. }
  132. if (_overlay_manager.OverlayManager.active !== this.overlayName) {
  133. return;
  134. }
  135. for (var _id in this.fields) {
  136. var content = this.fieldData[_id];
  137. this.fields[_id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
  138. }
  139. }
  140. }, {
  141. key: '_parseFileSize',
  142. value: function _parseFileSize() {
  143. var fileSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  144. var kb = fileSize / 1024;
  145. if (!kb) {
  146. return;
  147. } else if (kb < 1024) {
  148. return _ui_utils.mozL10n.get('document_properties_kb', {
  149. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  150. size_b: fileSize.toLocaleString()
  151. }, '{{size_kb}} KB ({{size_b}} bytes)');
  152. }
  153. return _ui_utils.mozL10n.get('document_properties_mb', {
  154. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  155. size_b: fileSize.toLocaleString()
  156. }, '{{size_mb}} MB ({{size_b}} bytes)');
  157. }
  158. }, {
  159. key: '_parseDate',
  160. value: function _parseDate(inputDate) {
  161. if (!inputDate) {
  162. return;
  163. }
  164. var dateToParse = inputDate;
  165. if (dateToParse.substring(0, 2) === 'D:') {
  166. dateToParse = dateToParse.substring(2);
  167. }
  168. var year = parseInt(dateToParse.substring(0, 4), 10);
  169. var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
  170. var day = parseInt(dateToParse.substring(6, 8), 10);
  171. var hours = parseInt(dateToParse.substring(8, 10), 10);
  172. var minutes = parseInt(dateToParse.substring(10, 12), 10);
  173. var seconds = parseInt(dateToParse.substring(12, 14), 10);
  174. var utRel = dateToParse.substring(14, 15);
  175. var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
  176. var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
  177. if (utRel === '-') {
  178. hours += offsetHours;
  179. minutes += offsetMinutes;
  180. } else if (utRel === '+') {
  181. hours -= offsetHours;
  182. minutes -= offsetMinutes;
  183. }
  184. var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
  185. var dateString = date.toLocaleDateString();
  186. var timeString = date.toLocaleTimeString();
  187. return _ui_utils.mozL10n.get('document_properties_date_string', {
  188. date: dateString,
  189. time: timeString
  190. }, '{{date}}, {{time}}');
  191. }
  192. }]);
  193. return PDFDocumentProperties;
  194. }();
  195. exports.PDFDocumentProperties = PDFDocumentProperties;