pdf_document_properties.js 8.6 KB

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