Refactor PDFDocumentProperties to be more class-like

This commit is contained in:
Tim van der Meij 2015-04-24 20:47:38 +02:00
parent ca8f842d87
commit 7cf440c560
2 changed files with 177 additions and 179 deletions

View File

@ -18,42 +18,33 @@
'use strict'; 'use strict';
var PDFDocumentProperties = { /**
overlayName: null, * @class
rawFileSize: 0, */
var PDFDocumentProperties = (function PDFDocumentPropertiesClosure() {
// Document property fields (in the viewer). /**
fileNameField: null, * @constructs PDFDocumentProperties
fileSizeField: null, * @param {PDFDocumentPropertiesOptions} options
titleField: null, */
authorField: null, function PDFDocumentProperties(options) {
subjectField: null, this.rawFileSize = 0;
keywordsField: null, this.url = null;
creationDateField: null, this.pdfDocument = null;
modificationDateField: null,
creatorField: null,
producerField: null,
versionField: null,
pageCountField: null,
url: null,
pdfDocument: null,
initialize: function documentPropertiesInitialize(options) {
this.overlayName = options.overlayName; this.overlayName = options.overlayName;
// Set the document property fields. // Set the document property fields.
this.fileNameField = options.fileNameField; this.fileNameField = options.fileNameField || null;
this.fileSizeField = options.fileSizeField; this.fileSizeField = options.fileSizeField || null;
this.titleField = options.titleField; this.titleField = options.titleField || null;
this.authorField = options.authorField; this.authorField = options.authorField || null;
this.subjectField = options.subjectField; this.subjectField = options.subjectField || null;
this.keywordsField = options.keywordsField; this.keywordsField = options.keywordsField || null;
this.creationDateField = options.creationDateField; this.creationDateField = options.creationDateField || null;
this.modificationDateField = options.modificationDateField; this.modificationDateField = options.modificationDateField || null;
this.creatorField = options.creatorField; this.creatorField = options.creatorField || null;
this.producerField = options.producerField; this.producerField = options.producerField || null;
this.versionField = options.versionField; this.versionField = options.versionField || null;
this.pageCountField = options.pageCountField; this.pageCountField = options.pageCountField || null;
// Bind the event listener for the Close button. // Bind the event listener for the Close button.
if (options.closeButton) { if (options.closeButton) {
@ -65,9 +56,10 @@ var PDFDocumentProperties = {
}.bind(this)); }.bind(this));
OverlayManager.register(this.overlayName, this.close.bind(this)); OverlayManager.register(this.overlayName, this.close.bind(this));
}, }
getProperties: function documentPropertiesGetProperties() { PDFDocumentProperties.prototype = {
getProperties: function PDFDocumentProperties_getProperties() {
if (!OverlayManager.active) { if (!OverlayManager.active) {
// If the dialog was closed before dataAvailablePromise was resolved, // If the dialog was closed before dataAvailablePromise was resolved,
// don't bother updating the properties. // don't bother updating the properties.
@ -110,19 +102,19 @@ var PDFDocumentProperties = {
}.bind(this)); }.bind(this));
}, },
updateUI: function documentPropertiesUpdateUI(field, content) { updateUI: function PDFDocumentProperties_updateUI(field, content) {
if (field && content !== undefined && content !== '') { if (field && content !== undefined && content !== '') {
field.textContent = content; field.textContent = content;
} }
}, },
setFileSize: function documentPropertiesSetFileSize(fileSize) { setFileSize: function PDFDocumentProperties_setFileSize(fileSize) {
if (fileSize > 0) { if (fileSize > 0) {
this.rawFileSize = fileSize; this.rawFileSize = fileSize;
} }
}, },
parseFileSize: function documentPropertiesParseFileSize() { parseFileSize: function PDFDocumentProperties_parseFileSize() {
var fileSize = this.rawFileSize, kb = fileSize / 1024; var fileSize = this.rawFileSize, kb = fileSize / 1024;
if (!kb) { if (!kb) {
return; return;
@ -139,18 +131,18 @@ var PDFDocumentProperties = {
} }
}, },
open: function documentPropertiesOpen() { open: function PDFDocumentProperties_open() {
Promise.all([OverlayManager.open(this.overlayName), Promise.all([OverlayManager.open(this.overlayName),
this.dataAvailablePromise]).then(function () { this.dataAvailablePromise]).then(function () {
this.getProperties(); this.getProperties();
}.bind(this)); }.bind(this));
}, },
close: function documentPropertiesClose() { close: function PDFDocumentProperties_close() {
OverlayManager.close(this.overlayName); OverlayManager.close(this.overlayName);
}, },
parseDate: function documentPropertiesParseDate(inputDate) { parseDate: function PDFDocumentProperties_parseDate(inputDate) {
// This is implemented according to the PDF specification, but note that // This is implemented according to the PDF specification, but note that
// Adobe Reader doesn't handle changing the date to universal time // Adobe Reader doesn't handle changing the date to universal time
// and doesn't use the user's time zone (they're effectively ignoring // and doesn't use the user's time zone (they're effectively ignoring
@ -196,4 +188,7 @@ var PDFDocumentProperties = {
{date: dateString, time: timeString}, {date: dateString, time: timeString},
'{{date}}, {{time}}'); '{{date}}, {{time}}');
} }
}; };
return PDFDocumentProperties;
})();

View File

@ -109,6 +109,8 @@ var PDFViewerApplication = {
pdfRenderingQueue: null, pdfRenderingQueue: null,
/** @type {PDFPresentationMode} */ /** @type {PDFPresentationMode} */
pdfPresentationMode: null, pdfPresentationMode: null,
/** @type {PDFDocumentProperties} */
pdfDocumentProperties: null,
pageRotation: 0, pageRotation: 0,
updateScaleControls: true, updateScaleControls: true,
isInitialViewSet: false, isInitialViewSet: false,
@ -172,6 +174,23 @@ var PDFViewerApplication = {
toggleHandTool: document.getElementById('toggleHandTool') toggleHandTool: document.getElementById('toggleHandTool')
}); });
this.pdfDocumentProperties = new PDFDocumentProperties({
overlayName: 'documentPropertiesOverlay',
closeButton: document.getElementById('documentPropertiesClose'),
fileNameField: document.getElementById('fileNameField'),
fileSizeField: document.getElementById('fileSizeField'),
titleField: document.getElementById('titleField'),
authorField: document.getElementById('authorField'),
subjectField: document.getElementById('subjectField'),
keywordsField: document.getElementById('keywordsField'),
creationDateField: document.getElementById('creationDateField'),
modificationDateField: document.getElementById('modificationDateField'),
creatorField: document.getElementById('creatorField'),
producerField: document.getElementById('producerField'),
versionField: document.getElementById('versionField'),
pageCountField: document.getElementById('pageCountField')
});
SecondaryToolbar.initialize({ SecondaryToolbar.initialize({
toolbar: document.getElementById('secondaryToolbar'), toolbar: document.getElementById('secondaryToolbar'),
toggleButton: document.getElementById('secondaryToolbarToggle'), toggleButton: document.getElementById('secondaryToolbarToggle'),
@ -185,7 +204,7 @@ var PDFViewerApplication = {
lastPage: document.getElementById('lastPage'), lastPage: document.getElementById('lastPage'),
pageRotateCw: document.getElementById('pageRotateCw'), pageRotateCw: document.getElementById('pageRotateCw'),
pageRotateCcw: document.getElementById('pageRotateCcw'), pageRotateCcw: document.getElementById('pageRotateCcw'),
documentProperties: PDFDocumentProperties, documentProperties: this.pdfDocumentProperties,
documentPropertiesButton: document.getElementById('documentProperties') documentPropertiesButton: document.getElementById('documentProperties')
}); });
@ -216,23 +235,6 @@ var PDFViewerApplication = {
passwordCancel: document.getElementById('passwordCancel') passwordCancel: document.getElementById('passwordCancel')
}); });
PDFDocumentProperties.initialize({
overlayName: 'documentPropertiesOverlay',
closeButton: document.getElementById('documentPropertiesClose'),
fileNameField: document.getElementById('fileNameField'),
fileSizeField: document.getElementById('fileSizeField'),
titleField: document.getElementById('titleField'),
authorField: document.getElementById('authorField'),
subjectField: document.getElementById('subjectField'),
keywordsField: document.getElementById('keywordsField'),
creationDateField: document.getElementById('creationDateField'),
modificationDateField: document.getElementById('modificationDateField'),
creatorField: document.getElementById('creatorField'),
producerField: document.getElementById('producerField'),
versionField: document.getElementById('versionField'),
pageCountField: document.getElementById('pageCountField')
});
var self = this; var self = this;
var initializedPromise = Promise.all([ var initializedPromise = Promise.all([
Preferences.get('enableWebGL').then(function resolved(value) { Preferences.get('enableWebGL').then(function resolved(value) {
@ -410,7 +412,8 @@ var PDFViewerApplication = {
pdfDataRangeTransport); pdfDataRangeTransport);
if (args.length) { if (args.length) {
PDFDocumentProperties.setFileSize(args.length); PDFViewerApplication.pdfDocumentProperties
.setFileSize(args.length);
} }
break; break;
case 'range': case 'range':
@ -556,7 +559,7 @@ var PDFViewerApplication = {
); );
if (args && args.length) { if (args && args.length) {
PDFDocumentProperties.setFileSize(args.length); PDFViewerApplication.pdfDocumentProperties.setFileSize(args.length);
} }
}, },
@ -853,9 +856,9 @@ var PDFViewerApplication = {
this.pdfDocument = pdfDocument; this.pdfDocument = pdfDocument;
PDFDocumentProperties.url = this.url; this.pdfDocumentProperties.url = this.url;
PDFDocumentProperties.pdfDocument = pdfDocument; this.pdfDocumentProperties.pdfDocument = pdfDocument;
PDFDocumentProperties.resolveDataAvailable(); this.pdfDocumentProperties.resolveDataAvailable();
var downloadedPromise = pdfDocument.getDownloadInfo().then(function() { var downloadedPromise = pdfDocument.getDownloadInfo().then(function() {
self.downloadComplete = true; self.downloadComplete = true;