Browse Source

Convert the document properties to ES6 syntax

Tim van der Meij 8 years ago
parent
commit
6d7a0ff8f2
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
  1. 328
      web/pdf_document_properties.js

328
web/pdf_document_properties.js

@ -21,20 +21,17 @@ import { OverlayManager } from './overlay_manager';
* @typedef {Object} PDFDocumentPropertiesOptions * @typedef {Object} PDFDocumentPropertiesOptions
* @property {string} overlayName - Name/identifier for the overlay. * @property {string} overlayName - Name/identifier for the overlay.
* @property {Object} fields - Names and elements of the overlay's fields. * @property {Object} fields - Names and elements of the overlay's fields.
* @property {HTMLDivElement} container - Div container for the overlay.
* @property {HTMLButtonElement} closeButton - Button for closing the overlay. * @property {HTMLButtonElement} closeButton - Button for closing the overlay.
*/ */
/** class PDFDocumentProperties {
* @class
*/
var PDFDocumentProperties = (function PDFDocumentPropertiesClosure() {
/** /**
* @constructs PDFDocumentProperties
* @param {PDFDocumentPropertiesOptions} options * @param {PDFDocumentPropertiesOptions} options
*/ */
function PDFDocumentProperties(options) { constructor(options) {
this.fields = options.fields;
this.overlayName = options.overlayName; this.overlayName = options.overlayName;
this.fields = options.fields;
this.container = options.container; this.container = options.container;
this.rawFileSize = 0; this.rawFileSize = 0;
@ -51,176 +48,171 @@ var PDFDocumentProperties = (function PDFDocumentPropertiesClosure() {
this.close.bind(this)); this.close.bind(this));
} }
PDFDocumentProperties.prototype = { /**
/** * Open the document properties overlay.
* Open the document properties overlay. */
*/ open() {
open: function PDFDocumentProperties_open() { Promise.all([OverlayManager.open(this.overlayName),
Promise.all([OverlayManager.open(this.overlayName), this._dataAvailableCapability.promise]).then(() => {
this._dataAvailableCapability.promise]).then(() => { this._getProperties();
this._getProperties(); });
}); }
},
/**
/** * Close the document properties overlay.
* Close the document properties overlay. */
*/ close() {
close: function PDFDocumentProperties_close() { OverlayManager.close(this.overlayName);
OverlayManager.close(this.overlayName); }
},
/**
/** * Set the file size of the PDF document. This method is used to
* Set the file size of the PDF document. This method is used to * update the file size in the document properties overlay once it
* update the file size in the document properties overlay once it * is known so we do not have to wait until the entire file is loaded.
* is known so we do not have to wait until the entire file is loaded. *
* * @param {number} fileSize - The file size of the PDF document.
* @param {number} fileSize - The file size of the PDF document. */
*/ setFileSize(fileSize) {
setFileSize: function PDFDocumentProperties_setFileSize(fileSize) { if (fileSize > 0) {
if (fileSize > 0) { this.rawFileSize = fileSize;
this.rawFileSize = fileSize; }
} }
},
/**
/** * Set a reference to the PDF document and the URL in order
* Set a reference to the PDF document and the URL in order * to populate the overlay fields with the document properties.
* to populate the overlay fields with the document properties. * Note that the overlay will contain no information if this method
* Note that the overlay will contain no information if this method * is not called.
* is not called. *
* * @param {Object} pdfDocument - A reference to the PDF document.
* @param {Object} pdfDocument - A reference to the PDF document. * @param {string} url - The URL of the document.
* @param {string} url - The URL of the document. */
*/ setDocumentAndUrl(pdfDocument, url) {
setDocumentAndUrl: this.pdfDocument = pdfDocument;
function PDFDocumentProperties_setDocumentAndUrl(pdfDocument, url) { this.url = url;
this.pdfDocument = pdfDocument; this._dataAvailableCapability.resolve();
this.url = url; }
this._dataAvailableCapability.resolve();
}, /**
* @private
/** */
* @private _getProperties() {
*/ if (!OverlayManager.active) {
_getProperties: function PDFDocumentProperties_getProperties() { // If the dialog was closed before `_dataAvailableCapability` was
if (!OverlayManager.active) { // resolved, don't bother updating the properties.
// If the dialog was closed before dataAvailablePromise was resolved, return;
// don't bother updating the properties. }
// Get the file size (if it hasn't already been set).
this.pdfDocument.getDownloadInfo().then((data) => {
if (data.length === this.rawFileSize) {
return; return;
} }
// Get the file size (if it hasn't already been set). this.setFileSize(data.length);
this.pdfDocument.getDownloadInfo().then((data) => { this._updateUI(this.fields['fileSize'], this._parseFileSize());
if (data.length === this.rawFileSize) { });
return;
} // Get the document properties.
this.setFileSize(data.length); this.pdfDocument.getMetadata().then((data) => {
this._updateUI(this.fields['fileSize'], this._parseFileSize()); var content = {
}); 'fileName': getPDFFileNameFromURL(this.url),
'fileSize': this._parseFileSize(),
// Get the document properties. 'title': data.info.Title,
this.pdfDocument.getMetadata().then((data) => { 'author': data.info.Author,
var content = { 'subject': data.info.Subject,
'fileName': getPDFFileNameFromURL(this.url), 'keywords': data.info.Keywords,
'fileSize': this._parseFileSize(), 'creationDate': this._parseDate(data.info.CreationDate),
'title': data.info.Title, 'modificationDate': this._parseDate(data.info.ModDate),
'author': data.info.Author, 'creator': data.info.Creator,
'subject': data.info.Subject, 'producer': data.info.Producer,
'keywords': data.info.Keywords, 'version': data.info.PDFFormatVersion,
'creationDate': this._parseDate(data.info.CreationDate), 'pageCount': this.pdfDocument.numPages
'modificationDate': this._parseDate(data.info.ModDate), };
'creator': data.info.Creator,
'producer': data.info.Producer, // Show the properties in the dialog.
'version': data.info.PDFFormatVersion, for (var identifier in content) {
'pageCount': this.pdfDocument.numPages this._updateUI(this.fields[identifier], content[identifier]);
};
// Show the properties in the dialog.
for (var identifier in content) {
this._updateUI(this.fields[identifier], content[identifier]);
}
});
},
/**
* @private
*/
_updateUI: function PDFDocumentProperties_updateUI(field, content) {
if (field && content !== undefined && content !== '') {
field.textContent = content;
}
},
/**
* @private
*/
_parseFileSize: function PDFDocumentProperties_parseFileSize() {
var fileSize = this.rawFileSize, kb = fileSize / 1024;
if (!kb) {
return;
} else if (kb < 1024) {
return mozL10n.get('document_properties_kb', {
size_kb: (+kb.toPrecision(3)).toLocaleString(),
size_b: fileSize.toLocaleString()
}, '{{size_kb}} KB ({{size_b}} bytes)');
} }
return mozL10n.get('document_properties_mb', { });
size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(), }
/**
* @private
*/
_updateUI(field, content) {
if (field && content !== undefined && content !== '') {
field.textContent = content;
}
}
/**
* @private
*/
_parseFileSize() {
var fileSize = this.rawFileSize, kb = fileSize / 1024;
if (!kb) {
return;
} else if (kb < 1024) {
return mozL10n.get('document_properties_kb', {
size_kb: (+kb.toPrecision(3)).toLocaleString(),
size_b: fileSize.toLocaleString() size_b: fileSize.toLocaleString()
}, '{{size_mb}} MB ({{size_b}} bytes)'); }, '{{size_kb}} KB ({{size_b}} bytes)');
}, }
return mozL10n.get('document_properties_mb', {
/** size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
* @private size_b: fileSize.toLocaleString()
*/ }, '{{size_mb}} MB ({{size_b}} bytes)');
_parseDate: function PDFDocumentProperties_parseDate(inputDate) { }
// This is implemented according to the PDF specification, but note that
// Adobe Reader doesn't handle changing the date to universal time
// and doesn't use the user's time zone (they're effectively ignoring
// the HH' and mm' parts of the date string).
var dateToParse = inputDate;
if (dateToParse === undefined) {
return '';
}
// Remove the D: prefix if it is available. /**
if (dateToParse.substring(0, 2) === 'D:') { * @private
dateToParse = dateToParse.substring(2); */
} _parseDate(inputDate) {
// This is implemented according to the PDF specification, but note that
// Adobe Reader doesn't handle changing the date to universal time
// and doesn't use the user's time zone (they're effectively ignoring
// the HH' and mm' parts of the date string).
var dateToParse = inputDate;
if (dateToParse === undefined) {
return '';
}
// Get all elements from the PDF date string. // Remove the D: prefix if it is available.
// JavaScript's Date object expects the month to be between if (dateToParse.substring(0, 2) === 'D:') {
// 0 and 11 instead of 1 and 12, so we're correcting for this. dateToParse = dateToParse.substring(2);
var year = parseInt(dateToParse.substring(0, 4), 10); }
var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
var day = parseInt(dateToParse.substring(6, 8), 10);
var hours = parseInt(dateToParse.substring(8, 10), 10);
var minutes = parseInt(dateToParse.substring(10, 12), 10);
var seconds = parseInt(dateToParse.substring(12, 14), 10);
var utRel = dateToParse.substring(14, 15);
var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
// As per spec, utRel = 'Z' means equal to universal time.
// The other cases ('-' and '+') have to be handled here.
if (utRel === '-') {
hours += offsetHours;
minutes += offsetMinutes;
} else if (utRel === '+') {
hours -= offsetHours;
minutes -= offsetMinutes;
}
// Return the new date format from the user's locale. // Get all elements from the PDF date string.
var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds)); // JavaScript's `Date` object expects the month to be between
var dateString = date.toLocaleDateString(); // 0 and 11 instead of 1 and 12, so we're correcting for this.
var timeString = date.toLocaleTimeString(); var year = parseInt(dateToParse.substring(0, 4), 10);
return mozL10n.get('document_properties_date_string', var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
{date: dateString, time: timeString}, var day = parseInt(dateToParse.substring(6, 8), 10);
'{{date}}, {{time}}'); var hours = parseInt(dateToParse.substring(8, 10), 10);
var minutes = parseInt(dateToParse.substring(10, 12), 10);
var seconds = parseInt(dateToParse.substring(12, 14), 10);
var utRel = dateToParse.substring(14, 15);
var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
// As per spec, utRel = 'Z' means equal to universal time.
// The other cases ('-' and '+') have to be handled here.
if (utRel === '-') {
hours += offsetHours;
minutes += offsetMinutes;
} else if (utRel === '+') {
hours -= offsetHours;
minutes -= offsetMinutes;
} }
};
return PDFDocumentProperties; // Return the new date format from the user's locale.
})(); var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
var dateString = date.toLocaleDateString();
var timeString = date.toLocaleTimeString();
return mozL10n.get('document_properties_date_string',
{ date: dateString, time: timeString },
'{{date}}, {{time}}');
}
}
export { export {
PDFDocumentProperties, PDFDocumentProperties,

Loading…
Cancel
Save