@ -29,20 +29,24 @@ var _overlay_manager = require('./overlay_manager');
@@ -29,20 +29,24 @@ var _overlay_manager = require('./overlay_manager');
function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
var DEFAULT _FIELD _CONTENT = '-' ;
var PDFDocumentProperties = function ( ) {
function PDFDocumentProperties ( options ) {
function PDFDocumentProperties ( _ref ) {
var overlayName = _ref . overlayName ,
fields = _ref . fields ,
container = _ref . container ,
closeButton = _ref . closeButton ;
_classCallCheck ( this , PDFDocumentProperties ) ;
this . overlayName = options . overlayName ;
this . fields = options . fields ;
this . container = options . container ;
this . rawFileSize = 0 ;
this . url = null ;
this . pdfDocument = null ;
if ( options . closeButton ) {
options . closeButton . addEventListener ( 'click' , this . close . bind ( this ) ) ;
this . overlayName = overlayName ;
this . fields = fields ;
this . container = container ;
this . _reset ( ) ;
if ( closeButton ) {
closeButton . addEventListener ( 'click' , this . close . bind ( this ) ) ;
}
this . _dataAvailableCapability = ( 0 , _pdfjs . createPromiseCapability ) ( ) ;
_overlay _manager . OverlayManager . register ( this . overlayName , this . container , this . close . bind ( this ) ) ;
}
@ -51,8 +55,47 @@ var PDFDocumentProperties = function () {
@@ -51,8 +55,47 @@ var PDFDocumentProperties = function () {
value : function open ( ) {
var _this = this ;
var freezeFieldData = function freezeFieldData ( data ) {
Object . defineProperty ( _this , 'fieldData' , {
value : Object . freeze ( data ) ,
writable : false ,
enumerable : true ,
configurable : true
} ) ;
} ;
Promise . all ( [ _overlay _manager . OverlayManager . open ( this . overlayName ) , this . _dataAvailableCapability . promise ] ) . then ( function ( ) {
_this . _getProperties ( ) ;
if ( _this . fieldData ) {
_this . _updateUI ( ) ;
return ;
}
_this . pdfDocument . getMetadata ( ) . then ( function ( _ref2 ) {
var info = _ref2 . info ,
metadata = _ref2 . metadata ;
freezeFieldData ( {
'fileName' : ( 0 , _ui _utils . getPDFFileNameFromURL ) ( _this . url ) ,
'fileSize' : _this . _parseFileSize ( _this . maybeFileSize ) ,
'title' : info . Title ,
'author' : info . Author ,
'subject' : info . Subject ,
'keywords' : info . Keywords ,
'creationDate' : _this . _parseDate ( info . CreationDate ) ,
'modificationDate' : _this . _parseDate ( info . ModDate ) ,
'creator' : info . Creator ,
'producer' : info . Producer ,
'version' : info . PDFFormatVersion ,
'pageCount' : _this . pdfDocument . numPages
} ) ;
_this . _updateUI ( ) ;
return _this . pdfDocument . getDownloadInfo ( ) ;
} ) . then ( function ( _ref3 ) {
var length = _ref3 . length ;
var data = ( 0 , _ui _utils . cloneObj ) ( _this . fieldData ) ;
data [ 'fileSize' ] = _this . _parseFileSize ( length ) ;
freezeFieldData ( data ) ;
_this . _updateUI ( ) ;
} ) ;
} ) ;
}
} , {
@ -61,66 +104,60 @@ var PDFDocumentProperties = function () {
@@ -61,66 +104,60 @@ var PDFDocumentProperties = function () {
_overlay _manager . OverlayManager . close ( this . overlayName ) ;
}
} , {
key : 'setFileSize' ,
value : function setFileSize ( fileSize ) {
if ( fileSize > 0 ) {
this . rawFileSize = fileSize ;
key : 'setDocument' ,
value : function setDocument ( pdfDocument , url ) {
if ( this . pdfDocument ) {
this . _reset ( ) ;
this . _updateUI ( true ) ;
}
if ( ! pdfDocument ) {
return ;
}
}
} , {
key : 'setDocumentAndUrl' ,
value : function setDocumentAndUrl ( pdfDocument , url ) {
this . pdfDocument = pdfDocument ;
this . url = url ;
this . _dataAvailableCapability . resolve ( ) ;
}
} , {
key : '_getProperties' ,
value : function _getProperties ( ) {
var _this2 = this ;
if ( ! _overlay _manager . OverlayManager . active ) {
return ;
key : 'setFileSize' ,
value : function setFileSize ( fileSize ) {
if ( typeof fileSize === 'number' && fileSize > 0 ) {
this . maybeFileSize = fileSize ;
}
this . pdfDocument . getDownloadInfo ( ) . then ( function ( data ) {
if ( data . length === _this2 . rawFileSize ) {
return ;
}
_this2 . setFileSize ( data . length ) ;
_this2 . _updateUI ( _this2 . fields [ 'fileSize' ] , _this2 . _parseFileSize ( ) ) ;
} ) ;
this . pdfDocument . getMetadata ( ) . then ( function ( data ) {
var content = {
'fileName' : ( 0 , _ui _utils . getPDFFileNameFromURL ) ( _this2 . url ) ,
'fileSize' : _this2 . _parseFileSize ( ) ,
'title' : data . info . Title ,
'author' : data . info . Author ,
'subject' : data . info . Subject ,
'keywords' : data . info . Keywords ,
'creationDate' : _this2 . _parseDate ( data . info . CreationDate ) ,
'modificationDate' : _this2 . _parseDate ( data . info . ModDate ) ,
'creator' : data . info . Creator ,
'producer' : data . info . Producer ,
'version' : data . info . PDFFormatVersion ,
'pageCount' : _this2 . pdfDocument . numPages
} ;
for ( var identifier in content ) {
_this2 . _updateUI ( _this2 . fields [ identifier ] , content [ identifier ] ) ;
}
} ) ;
}
} , {
key : '_reset' ,
value : function _reset ( ) {
this . pdfDocument = null ;
this . url = null ;
this . maybeFileSize = 0 ;
delete this . fieldData ;
this . _dataAvailableCapability = ( 0 , _pdfjs . createPromiseCapability ) ( ) ;
}
} , {
key : '_updateUI' ,
value : function _updateUI ( field , content ) {
if ( field && content !== undefined && content !== '' ) {
field . textContent = content ;
value : function _updateUI ( ) {
var reset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : false ;
if ( reset || ! this . fieldData ) {
for ( var id in this . fields ) {
this . fields [ id ] . textContent = DEFAULT _FIELD _CONTENT ;
}
return ;
}
if ( _overlay _manager . OverlayManager . active !== this . overlayName ) {
return ;
}
for ( var _id in this . fields ) {
var content = this . fieldData [ _id ] ;
this . fields [ _id ] . textContent = content || content === 0 ? content : DEFAULT _FIELD _CONTENT ;
}
}
} , {
key : '_parseFileSize' ,
value : function _parseFileSize ( ) {
var fileSize = this . rawFileSize ,
kb = fileSize / 1024 ;
var fileSize = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : 0 ;
var kb = fileSize / 1024 ;
if ( ! kb ) {
return ;
} else if ( kb < 1024 ) {
@ -137,10 +174,10 @@ var PDFDocumentProperties = function () {
@@ -137,10 +174,10 @@ var PDFDocumentProperties = function () {
} , {
key : '_parseDate' ,
value : function _parseDate ( inputDate ) {
var dateToParse = inputDate ;
if ( dateToParse === undefined ) {
return '' ;
if ( ! inputDate ) {
return ;
}
var dateToParse = inputDate ;
if ( dateToParse . substring ( 0 , 2 ) === 'D:' ) {
dateToParse = dateToParse . substring ( 2 ) ;
}