UNPKG

13.8 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2017 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * Javascript code in this page
21 */
22'use strict';
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.PDFDocumentProperties = undefined;
28
29var _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"); } }; }();
30
31var _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; }; }();
32
33var _ui_utils = require('./ui_utils');
34
35var _pdf = require('../pdf');
36
37function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
38
39var DEFAULT_FIELD_CONTENT = '-';
40var NON_METRIC_LOCALES = ['en-us', 'en-lr', 'my'];
41var US_PAGE_NAMES = {
42 '8.5x11': 'Letter',
43 '8.5x14': 'Legal'
44};
45var METRIC_PAGE_NAMES = {
46 '297x420': 'A3',
47 '210x297': 'A4'
48};
49function getPageName(size, isPortrait, pageNames) {
50 var width = isPortrait ? size.width : size.height;
51 var height = isPortrait ? size.height : size.width;
52 return pageNames[width + 'x' + height];
53}
54
55var PDFDocumentProperties = function () {
56 function PDFDocumentProperties(_ref, overlayManager, eventBus) {
57 var overlayName = _ref.overlayName,
58 fields = _ref.fields,
59 container = _ref.container,
60 closeButton = _ref.closeButton;
61
62 var _this = this;
63
64 var l10n = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _ui_utils.NullL10n;
65
66 _classCallCheck(this, PDFDocumentProperties);
67
68 this.overlayName = overlayName;
69 this.fields = fields;
70 this.container = container;
71 this.overlayManager = overlayManager;
72 this.l10n = l10n;
73 this._reset();
74 if (closeButton) {
75 closeButton.addEventListener('click', this.close.bind(this));
76 }
77 this.overlayManager.register(this.overlayName, this.container, this.close.bind(this));
78 if (eventBus) {
79 eventBus.on('pagechanging', function (evt) {
80 _this._currentPageNumber = evt.pageNumber;
81 });
82 eventBus.on('rotationchanging', function (evt) {
83 _this._pagesRotation = evt.pagesRotation;
84 });
85 }
86 this._isNonMetricLocale = true;
87 l10n.getLanguage().then(function (locale) {
88 _this._isNonMetricLocale = NON_METRIC_LOCALES.includes(locale);
89 });
90 }
91
92 _createClass(PDFDocumentProperties, [{
93 key: 'open',
94 value: function open() {
95 var _this2 = this;
96
97 var freezeFieldData = function freezeFieldData(data) {
98 Object.defineProperty(_this2, 'fieldData', {
99 value: Object.freeze(data),
100 writable: false,
101 enumerable: true,
102 configurable: true
103 });
104 };
105 Promise.all([this.overlayManager.open(this.overlayName), this._dataAvailableCapability.promise]).then(function () {
106 var currentPageNumber = _this2._currentPageNumber;
107 var pagesRotation = _this2._pagesRotation;
108 if (_this2.fieldData && currentPageNumber === _this2.fieldData['_currentPageNumber'] && pagesRotation === _this2.fieldData['_pagesRotation']) {
109 _this2._updateUI();
110 return;
111 }
112 _this2.pdfDocument.getMetadata().then(function (_ref2) {
113 var info = _ref2.info,
114 metadata = _ref2.metadata,
115 contentDispositionFilename = _ref2.contentDispositionFilename;
116
117 return Promise.all([info, metadata, contentDispositionFilename || (0, _ui_utils.getPDFFileNameFromURL)(_this2.url), _this2._parseFileSize(_this2.maybeFileSize), _this2._parseDate(info.CreationDate), _this2._parseDate(info.ModDate), _this2.pdfDocument.getPage(currentPageNumber).then(function (pdfPage) {
118 return _this2._parsePageSize((0, _ui_utils.getPageSizeInches)(pdfPage), pagesRotation);
119 })]);
120 }).then(function (_ref3) {
121 var _ref4 = _slicedToArray(_ref3, 7),
122 info = _ref4[0],
123 metadata = _ref4[1],
124 fileName = _ref4[2],
125 fileSize = _ref4[3],
126 creationDate = _ref4[4],
127 modDate = _ref4[5],
128 pageSize = _ref4[6];
129
130 freezeFieldData({
131 'fileName': fileName,
132 'fileSize': fileSize,
133 'title': info.Title,
134 'author': info.Author,
135 'subject': info.Subject,
136 'keywords': info.Keywords,
137 'creationDate': creationDate,
138 'modificationDate': modDate,
139 'creator': info.Creator,
140 'producer': info.Producer,
141 'version': info.PDFFormatVersion,
142 'pageCount': _this2.pdfDocument.numPages,
143 'pageSize': pageSize,
144 '_currentPageNumber': currentPageNumber,
145 '_pagesRotation': pagesRotation
146 });
147 _this2._updateUI();
148 return _this2.pdfDocument.getDownloadInfo();
149 }).then(function (_ref5) {
150 var length = _ref5.length;
151
152 _this2.maybeFileSize = length;
153 return _this2._parseFileSize(length);
154 }).then(function (fileSize) {
155 if (fileSize === _this2.fieldData['fileSize']) {
156 return;
157 }
158 var data = (0, _ui_utils.cloneObj)(_this2.fieldData);
159 data['fileSize'] = fileSize;
160 freezeFieldData(data);
161 _this2._updateUI();
162 });
163 });
164 }
165 }, {
166 key: 'close',
167 value: function close() {
168 this.overlayManager.close(this.overlayName);
169 }
170 }, {
171 key: 'setDocument',
172 value: function setDocument(pdfDocument, url) {
173 if (this.pdfDocument) {
174 this._reset();
175 this._updateUI(true);
176 }
177 if (!pdfDocument) {
178 return;
179 }
180 this.pdfDocument = pdfDocument;
181 this.url = url;
182 this._dataAvailableCapability.resolve();
183 }
184 }, {
185 key: 'setFileSize',
186 value: function setFileSize(fileSize) {
187 if (Number.isInteger(fileSize) && fileSize > 0) {
188 this.maybeFileSize = fileSize;
189 }
190 }
191 }, {
192 key: '_reset',
193 value: function _reset() {
194 this.pdfDocument = null;
195 this.url = null;
196 this.maybeFileSize = 0;
197 delete this.fieldData;
198 this._dataAvailableCapability = (0, _pdf.createPromiseCapability)();
199 this._currentPageNumber = 1;
200 this._pagesRotation = 0;
201 }
202 }, {
203 key: '_updateUI',
204 value: function _updateUI() {
205 var reset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
206
207 if (reset || !this.fieldData) {
208 for (var id in this.fields) {
209 this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
210 }
211 return;
212 }
213 if (this.overlayManager.active !== this.overlayName) {
214 return;
215 }
216 for (var _id in this.fields) {
217 var content = this.fieldData[_id];
218 this.fields[_id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
219 }
220 }
221 }, {
222 key: '_parseFileSize',
223 value: function _parseFileSize() {
224 var fileSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
225
226 var kb = fileSize / 1024;
227 if (!kb) {
228 return Promise.resolve(undefined);
229 } else if (kb < 1024) {
230 return this.l10n.get('document_properties_kb', {
231 size_kb: (+kb.toPrecision(3)).toLocaleString(),
232 size_b: fileSize.toLocaleString()
233 }, '{{size_kb}} KB ({{size_b}} bytes)');
234 }
235 return this.l10n.get('document_properties_mb', {
236 size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
237 size_b: fileSize.toLocaleString()
238 }, '{{size_mb}} MB ({{size_b}} bytes)');
239 }
240 }, {
241 key: '_parsePageSize',
242 value: function _parsePageSize(pageSizeInches, pagesRotation) {
243 var _this3 = this;
244
245 if (!pageSizeInches) {
246 return Promise.resolve(undefined);
247 }
248 if (pagesRotation % 180 !== 0) {
249 pageSizeInches = {
250 width: pageSizeInches.height,
251 height: pageSizeInches.width
252 };
253 }
254 var isPortrait = (0, _ui_utils.isPortraitOrientation)(pageSizeInches);
255 var sizeInches = {
256 width: Math.round(pageSizeInches.width * 100) / 100,
257 height: Math.round(pageSizeInches.height * 100) / 100
258 };
259 var sizeMillimeters = {
260 width: Math.round(pageSizeInches.width * 25.4 * 10) / 10,
261 height: Math.round(pageSizeInches.height * 25.4 * 10) / 10
262 };
263 var pageName = null;
264 var name = getPageName(sizeInches, isPortrait, US_PAGE_NAMES) || getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);
265 if (!name && !(Number.isInteger(sizeMillimeters.width) && Number.isInteger(sizeMillimeters.height))) {
266 var exactMillimeters = {
267 width: pageSizeInches.width * 25.4,
268 height: pageSizeInches.height * 25.4
269 };
270 var intMillimeters = {
271 width: Math.round(sizeMillimeters.width),
272 height: Math.round(sizeMillimeters.height)
273 };
274 if (Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 && Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1) {
275 name = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
276 if (name) {
277 sizeInches = {
278 width: Math.round(intMillimeters.width / 25.4 * 100) / 100,
279 height: Math.round(intMillimeters.height / 25.4 * 100) / 100
280 };
281 sizeMillimeters = intMillimeters;
282 }
283 }
284 }
285 if (name) {
286 pageName = this.l10n.get('document_properties_page_size_name_' + name.toLowerCase(), null, name);
287 }
288 return Promise.all([this._isNonMetricLocale ? sizeInches : sizeMillimeters, this.l10n.get('document_properties_page_size_unit_' + (this._isNonMetricLocale ? 'inches' : 'millimeters'), null, this._isNonMetricLocale ? 'in' : 'mm'), pageName, this.l10n.get('document_properties_page_size_orientation_' + (isPortrait ? 'portrait' : 'landscape'), null, isPortrait ? 'portrait' : 'landscape')]).then(function (_ref6) {
289 var _ref7 = _slicedToArray(_ref6, 4),
290 _ref7$ = _ref7[0],
291 width = _ref7$.width,
292 height = _ref7$.height,
293 unit = _ref7[1],
294 name = _ref7[2],
295 orientation = _ref7[3];
296
297 return _this3.l10n.get('document_properties_page_size_dimension_' + (name ? 'name_' : '') + 'string', {
298 width: width.toLocaleString(),
299 height: height.toLocaleString(),
300 unit: unit,
301 name: name,
302 orientation: orientation
303 }, '{{width}} × {{height}} {{unit}} (' + (name ? '{{name}}, ' : '') + '{{orientation}})');
304 });
305 }
306 }, {
307 key: '_parseDate',
308 value: function _parseDate(inputDate) {
309 if (!inputDate) {
310 return;
311 }
312 var dateToParse = inputDate;
313 if (dateToParse.substring(0, 2) === 'D:') {
314 dateToParse = dateToParse.substring(2);
315 }
316 var year = parseInt(dateToParse.substring(0, 4), 10);
317 var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
318 var day = parseInt(dateToParse.substring(6, 8), 10);
319 var hours = parseInt(dateToParse.substring(8, 10), 10);
320 var minutes = parseInt(dateToParse.substring(10, 12), 10);
321 var seconds = parseInt(dateToParse.substring(12, 14), 10);
322 var utRel = dateToParse.substring(14, 15);
323 var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
324 var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
325 if (utRel === '-') {
326 hours += offsetHours;
327 minutes += offsetMinutes;
328 } else if (utRel === '+') {
329 hours -= offsetHours;
330 minutes -= offsetMinutes;
331 }
332 var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
333 var dateString = date.toLocaleDateString();
334 var timeString = date.toLocaleTimeString();
335 return this.l10n.get('document_properties_date_string', {
336 date: dateString,
337 time: timeString
338 }, '{{date}}, {{time}}');
339 }
340 }]);
341
342 return PDFDocumentProperties;
343}();
344
345exports.PDFDocumentProperties = PDFDocumentProperties;
\No newline at end of file