Simplify document properties field logic

This patch:
- Simplifies the way fields are passed to the document properties overlay
- Simplifies the way fields are filled internally in the document properties overlay
- Avoids passing a document properties reference to the secondary toolbar
This commit is contained in:
Tim van der Meij 2015-04-24 21:19:58 +02:00
parent 1a5de56675
commit 6abf3d6ba3
3 changed files with 38 additions and 52 deletions

View File

@ -20,7 +20,9 @@
/** /**
* @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 {HTMLButtonElement} closeButton - Button for closing the overlay.
*/ */
/** /**
@ -32,24 +34,12 @@ var PDFDocumentProperties = (function PDFDocumentPropertiesClosure() {
* @param {PDFDocumentPropertiesOptions} options * @param {PDFDocumentPropertiesOptions} options
*/ */
function PDFDocumentProperties(options) { function PDFDocumentProperties(options) {
this.fields = options.fields;
this.overlayName = options.overlayName;
this.rawFileSize = 0; this.rawFileSize = 0;
this.url = null; this.url = null;
this.pdfDocument = null; this.pdfDocument = null;
this.overlayName = options.overlayName;
// Set the document property fields.
this.fileNameField = options.fileNameField || null;
this.fileSizeField = options.fileSizeField || null;
this.titleField = options.titleField || null;
this.authorField = options.authorField || null;
this.subjectField = options.subjectField || null;
this.keywordsField = options.keywordsField || null;
this.creationDateField = options.creationDateField || null;
this.modificationDateField = options.modificationDateField || null;
this.creatorField = options.creatorField || null;
this.producerField = options.producerField || null;
this.versionField = options.versionField || null;
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) {
@ -125,33 +115,29 @@ var PDFDocumentProperties = (function PDFDocumentPropertiesClosure() {
return; return;
} }
this.setFileSize(data.length); this.setFileSize(data.length);
this._updateUI(this.fileSizeField, this._parseFileSize()); this._updateUI(this.fields['fileSize'], this._parseFileSize());
}.bind(this)); }.bind(this));
// Get the document properties. // Get the document properties.
this.pdfDocument.getMetadata().then(function(data) { this.pdfDocument.getMetadata().then(function(data) {
var fields = [ var content = {
{ field: this.fileNameField, 'fileName': getPDFFileNameFromURL(this.url),
content: getPDFFileNameFromURL(this.url) }, 'fileSize': this._parseFileSize(),
{ field: this.fileSizeField, content: this._parseFileSize() }, 'title': data.info.Title,
{ field: this.titleField, content: data.info.Title }, 'author': data.info.Author,
{ field: this.authorField, content: data.info.Author }, 'subject': data.info.Subject,
{ field: this.subjectField, content: data.info.Subject }, 'keywords': data.info.Keywords,
{ field: this.keywordsField, content: data.info.Keywords }, 'creationDate': this._parseDate(data.info.CreationDate),
{ field: this.creationDateField, 'modificationDate': this._parseDate(data.info.ModDate),
content: this._parseDate(data.info.CreationDate) }, 'creator': data.info.Creator,
{ field: this.modificationDateField, 'producer': data.info.Producer,
content: this._parseDate(data.info.ModDate) }, 'version': data.info.PDFFormatVersion,
{ field: this.creatorField, content: data.info.Creator }, 'pageCount': this.pdfDocument.numPages
{ field: this.producerField, content: data.info.Producer }, };
{ field: this.versionField, content: data.info.PDFFormatVersion },
{ field: this.pageCountField, content: this.pdfDocument.numPages }
];
// Show the properties in the dialog. // Show the properties in the dialog.
for (var item in fields) { for (var identifier in content) {
var element = fields[item]; this._updateUI(this.fields[identifier], content[identifier]);
this._updateUI(element.field, element.content);
} }
}.bind(this)); }.bind(this));
}, },

View File

@ -25,7 +25,6 @@ var SecondaryToolbar = {
initialize: function secondaryToolbarInitialize(options) { initialize: function secondaryToolbarInitialize(options) {
this.toolbar = options.toolbar; this.toolbar = options.toolbar;
this.documentProperties = options.documentProperties;
this.buttonContainer = this.toolbar.firstElementChild; this.buttonContainer = this.toolbar.firstElementChild;
// Define the toolbar buttons. // Define the toolbar buttons.
@ -115,7 +114,7 @@ var SecondaryToolbar = {
}, },
documentPropertiesClick: function secondaryToolbarDocumentPropsClick(evt) { documentPropertiesClick: function secondaryToolbarDocumentPropsClick(evt) {
this.documentProperties.open(); PDFViewerApplication.pdfDocumentProperties.open();
this.close(); this.close();
}, },

View File

@ -177,18 +177,20 @@ var PDFViewerApplication = {
this.pdfDocumentProperties = new PDFDocumentProperties({ this.pdfDocumentProperties = new PDFDocumentProperties({
overlayName: 'documentPropertiesOverlay', overlayName: 'documentPropertiesOverlay',
closeButton: document.getElementById('documentPropertiesClose'), closeButton: document.getElementById('documentPropertiesClose'),
fileNameField: document.getElementById('fileNameField'), fields: {
fileSizeField: document.getElementById('fileSizeField'), 'fileName': document.getElementById('fileNameField'),
titleField: document.getElementById('titleField'), 'fileSize': document.getElementById('fileSizeField'),
authorField: document.getElementById('authorField'), 'title': document.getElementById('titleField'),
subjectField: document.getElementById('subjectField'), 'author': document.getElementById('authorField'),
keywordsField: document.getElementById('keywordsField'), 'subject': document.getElementById('subjectField'),
creationDateField: document.getElementById('creationDateField'), 'keywords': document.getElementById('keywordsField'),
modificationDateField: document.getElementById('modificationDateField'), 'creationDate': document.getElementById('creationDateField'),
creatorField: document.getElementById('creatorField'), 'modificationDate': document.getElementById('modificationDateField'),
producerField: document.getElementById('producerField'), 'creator': document.getElementById('creatorField'),
versionField: document.getElementById('versionField'), 'producer': document.getElementById('producerField'),
pageCountField: document.getElementById('pageCountField') 'version': document.getElementById('versionField'),
'pageCount': document.getElementById('pageCountField')
}
}); });
SecondaryToolbar.initialize({ SecondaryToolbar.initialize({
@ -204,7 +206,6 @@ 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: this.pdfDocumentProperties,
documentPropertiesButton: document.getElementById('documentProperties') documentPropertiesButton: document.getElementById('documentProperties')
}); });