Fixes PDFViewerApplication.open/close methods signature.

This commit is contained in:
Yury Delendik 2015-10-23 08:49:02 -05:00
parent 5135aa9bec
commit 62afa9f695
2 changed files with 77 additions and 32 deletions

View File

@ -64,7 +64,7 @@ var ChromeCom = (function ChromeComClosure() {
var streamUrl = response.streamUrl; var streamUrl = response.streamUrl;
if (streamUrl) { if (streamUrl) {
console.log('Found data stream for ' + file); console.log('Found data stream for ' + file);
PDFViewerApplication.open(streamUrl, 0, undefined, undefined, { PDFViewerApplication.open(streamUrl, {
length: response.contentLength length: response.contentLength
}); });
PDFViewerApplication.setTitleUsingUrl(file); PDFViewerApplication.setTitleUsingUrl(file);
@ -91,7 +91,7 @@ var ChromeCom = (function ChromeComClosure() {
resolveLocalFileSystemURL(file, function onResolvedFSURL(fileEntry) { resolveLocalFileSystemURL(file, function onResolvedFSURL(fileEntry) {
fileEntry.file(function(fileObject) { fileEntry.file(function(fileObject) {
var blobUrl = URL.createObjectURL(fileObject); var blobUrl = URL.createObjectURL(fileObject);
PDFViewerApplication.open(blobUrl, 0, undefined, undefined, { PDFViewerApplication.open(blobUrl, {
length: fileObject.size length: fileObject.size
}); });
}); });
@ -100,7 +100,7 @@ var ChromeCom = (function ChromeComClosure() {
// usual way of getting the File's data (via the Web worker). // usual way of getting the File's data (via the Web worker).
console.warn('Cannot resolve file ' + file + ', ' + error.name + ' ' + console.warn('Cannot resolve file ' + file + ', ' + error.name + ' ' +
error.message); error.message);
PDFViewerApplication.open(file, 0); PDFViewerApplication.open(file);
}); });
return; return;
} }
@ -109,7 +109,7 @@ var ChromeCom = (function ChromeComClosure() {
// There is no UI to input a different URL, so this assumption will hold // There is no UI to input a different URL, so this assumption will hold
// for now. // for now.
setReferer(file, function() { setReferer(file, function() {
PDFViewerApplication.open(file, 0); PDFViewerApplication.open(file);
}); });
return; return;
} }
@ -122,14 +122,14 @@ var ChromeCom = (function ChromeComClosure() {
} }
isAllowedFileSchemeAccess(function(isAllowedAccess) { isAllowedFileSchemeAccess(function(isAllowedAccess) {
if (isAllowedAccess) { if (isAllowedAccess) {
PDFViewerApplication.open(file, 0); PDFViewerApplication.open(file);
} else { } else {
requestAccessToLocalFile(file); requestAccessToLocalFile(file);
} }
}); });
return; return;
} }
PDFViewerApplication.open(file, 0); PDFViewerApplication.open(file);
}); });
}; };

View File

@ -92,6 +92,7 @@ var PDFViewerApplication = {
initialized: false, initialized: false,
fellback: false, fellback: false,
pdfDocument: null, pdfDocument: null,
pdfLoadingTask: null,
sidebarOpen: false, sidebarOpen: false,
printing: false, printing: false,
/** @type {PDFViewer} */ /** @type {PDFViewer} */
@ -434,8 +435,8 @@ var PDFViewerApplication = {
pdfDataRangeTransport = pdfDataRangeTransport =
new FirefoxComDataRangeTransport(args.length, args.data); new FirefoxComDataRangeTransport(args.length, args.data);
PDFViewerApplication.open(args.pdfUrl, 0, undefined, PDFViewerApplication.open(args.pdfUrl,
pdfDataRangeTransport); {range: pdfDataRangeTransport});
if (args.length) { if (args.length) {
PDFViewerApplication.pdfDocumentProperties PDFViewerApplication.pdfDocumentProperties
@ -460,7 +461,7 @@ var PDFViewerApplication = {
'An error occurred while loading the PDF.'), e); 'An error occurred while loading the PDF.'), e);
break; break;
} }
PDFViewerApplication.open(args.data, 0); PDFViewerApplication.open(args.data);
break; break;
} }
}); });
@ -487,36 +488,76 @@ var PDFViewerApplication = {
document.title = title; document.title = title;
}, },
/**
* Closes opened PDF document.
* @returns {Promise} - Returns the promise, which is resolved when all
* destruction is completed.
*/
close: function pdfViewClose() { close: function pdfViewClose() {
var errorWrapper = document.getElementById('errorWrapper'); var errorWrapper = document.getElementById('errorWrapper');
errorWrapper.setAttribute('hidden', 'true'); errorWrapper.setAttribute('hidden', 'true');
if (!this.pdfDocument) { if (!this.pdfLoadingTask) {
return; return Promise.resolve();
} }
this.pdfDocument.destroy(); var promise = this.pdfLoadingTask.destroy();
this.pdfDocument = null; this.pdfLoadingTask = null;
this.pdfThumbnailViewer.setDocument(null); if (this.pdfDocument) {
this.pdfViewer.setDocument(null); this.pdfDocument = null;
this.pdfLinkService.setDocument(null, null);
this.pdfThumbnailViewer.setDocument(null);
this.pdfViewer.setDocument(null);
this.pdfLinkService.setDocument(null, null);
}
if (typeof PDFBug !== 'undefined') { if (typeof PDFBug !== 'undefined') {
PDFBug.cleanup(); PDFBug.cleanup();
} }
return promise;
}, },
// TODO(mack): This function signature should really be pdfViewOpen(url, args) /**
open: function pdfViewOpen(file, scale, password, * Opens PDF document specified by URL or array with additional arguments.
pdfDataRangeTransport, args) { * @param {string|TypedArray|ArrayBuffer} file - PDF location or binary data.
if (this.pdfDocument) { * @param {Object} args - (optional) Additional arguments for the getDocument
// Reload the preferences if a document was previously opened. * call, e.g. HTTP headers ('httpHeaders') or
Preferences.reload(); * alternative data transport ('range').
* @returns {Promise} - Returns the promise, which is resolved when document
* is opened.
*/
open: function pdfViewOpen(file, args) {
var scale = 0;
if (arguments.length > 2 || typeof args === 'number') {
console.warn('Call of open() with obsolete signature.');
if (typeof args === 'number') {
scale = args; // scale argument was found
}
args = arguments[4] || null;
if (arguments[3] && typeof arguments[3] === 'object') {
// The pdfDataRangeTransport argument is present.
args = Object.create(args);
args.range = arguments[3];
}
if (typeof arguments[2] === 'string') {
// The password argument is present.
args = Object.create(args);
args.password = arguments[2];
}
} }
this.close();
var parameters = {password: password}; if (this.pdfLoadingTask) {
// We need to destroy already opened document.
return this.close().then(function () {
// Reload the preferences if a document was previously opened.
Preferences.reload();
// ... and repeat the open() call.
return this.open(file, args);
}.bind(this));
}
var parameters = Object.create(null);
if (typeof file === 'string') { // URL if (typeof file === 'string') { // URL
this.setTitleUsingUrl(file); this.setTitleUsingUrl(file);
parameters.url = file; parameters.url = file;
@ -526,9 +567,6 @@ var PDFViewerApplication = {
this.setTitleUsingUrl(file.originalUrl); this.setTitleUsingUrl(file.originalUrl);
parameters.url = file.url; parameters.url = file.url;
} }
if (pdfDataRangeTransport) {
parameters.range = pdfDataRangeTransport;
}
if (args) { if (args) {
for (var prop in args) { for (var prop in args) {
parameters[prop] = args[prop]; parameters[prop] = args[prop];
@ -539,6 +577,7 @@ var PDFViewerApplication = {
self.downloadComplete = false; self.downloadComplete = false;
var loadingTask = PDFJS.getDocument(parameters); var loadingTask = PDFJS.getDocument(parameters);
this.pdfLoadingTask = loadingTask;
loadingTask.onPassword = function passwordNeeded(updatePassword, reason) { loadingTask.onPassword = function passwordNeeded(updatePassword, reason) {
PasswordPrompt.updatePassword = updatePassword; PasswordPrompt.updatePassword = updatePassword;
@ -550,7 +589,7 @@ var PDFViewerApplication = {
self.progress(progressData.loaded / progressData.total); self.progress(progressData.loaded / progressData.total);
}; };
loadingTask.promise.then( var result = loadingTask.promise.then(
function getDocumentCallback(pdfDocument) { function getDocumentCallback(pdfDocument) {
self.load(pdfDocument, scale); self.load(pdfDocument, scale);
}, },
@ -576,12 +615,15 @@ var PDFViewerApplication = {
message: message message: message
}; };
self.error(loadingErrorMessage, moreInfo); self.error(loadingErrorMessage, moreInfo);
throw new Error(loadingErrorMessage);
} }
); );
if (args && args.length) { if (args && args.length) {
PDFViewerApplication.pdfDocumentProperties.setFileSize(args.length); PDFViewerApplication.pdfDocumentProperties.setFileSize(args.length);
} }
return result;
}, },
download: function pdfViewDownload() { download: function pdfViewDownload() {
@ -1008,6 +1050,9 @@ var PDFViewerApplication = {
}, },
cleanup: function pdfViewCleanup() { cleanup: function pdfViewCleanup() {
if (!this.pdfDocument) {
return; // run cleanup when document is loaded
}
this.pdfViewer.cleanup(); this.pdfViewer.cleanup();
this.pdfThumbnailViewer.cleanup(); this.pdfThumbnailViewer.cleanup();
this.pdfDocument.cleanup(); this.pdfDocument.cleanup();
@ -1502,7 +1547,7 @@ function webViewerInitialized() {
PDFViewerApplication.setTitleUsingUrl(file); PDFViewerApplication.setTitleUsingUrl(file);
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.onload = function() { xhr.onload = function() {
PDFViewerApplication.open(new Uint8Array(xhr.response), 0); PDFViewerApplication.open(new Uint8Array(xhr.response));
}; };
try { try {
xhr.open('GET', file); xhr.open('GET', file);
@ -1516,7 +1561,7 @@ function webViewerInitialized() {
} }
if (file) { if (file) {
PDFViewerApplication.open(file, 0); PDFViewerApplication.open(file);
} }
//#endif //#endif
//#if CHROME //#if CHROME
@ -1731,14 +1776,14 @@ window.addEventListener('change', function webViewerChange(evt) {
if (!PDFJS.disableCreateObjectURL && if (!PDFJS.disableCreateObjectURL &&
typeof URL !== 'undefined' && URL.createObjectURL) { typeof URL !== 'undefined' && URL.createObjectURL) {
PDFViewerApplication.open(URL.createObjectURL(file), 0); PDFViewerApplication.open(URL.createObjectURL(file));
} else { } else {
// Read the local file into a Uint8Array. // Read the local file into a Uint8Array.
var fileReader = new FileReader(); var fileReader = new FileReader();
fileReader.onload = function webViewerChangeFileReaderOnload(evt) { fileReader.onload = function webViewerChangeFileReaderOnload(evt) {
var buffer = evt.target.result; var buffer = evt.target.result;
var uint8Array = new Uint8Array(buffer); var uint8Array = new Uint8Array(buffer);
PDFViewerApplication.open(uint8Array, 0); PDFViewerApplication.open(uint8Array);
}; };
fileReader.readAsArrayBuffer(file); fileReader.readAsArrayBuffer(file);
} }