[mobile-viewer] Add an async close
method to the example, and change open
to also be async (issue 7571)
Since the `mobile-viewer` example is based on the old FirefoxOS/B2G PDF viewer, it didn't need to have the same kind of `open/close` methods as the default viewer. However, now that it has been re-purposed as a simple `mobile-viewer` example, it seems like a good idea to ensure that it has proper asynchronous `open/close` methods. Fixes 7571.
This commit is contained in:
parent
0e20d44fef
commit
65e9ff68aa
@ -36,31 +36,48 @@ var MAX_SCALE = 10.0;
|
|||||||
var DEFAULT_SCALE_VALUE = 'auto';
|
var DEFAULT_SCALE_VALUE = 'auto';
|
||||||
|
|
||||||
var PDFViewerApplication = {
|
var PDFViewerApplication = {
|
||||||
|
pdfLoadingTask: null,
|
||||||
pdfDocument: null,
|
pdfDocument: null,
|
||||||
pdfViewer: null,
|
pdfViewer: null,
|
||||||
pdfHistory: null,
|
pdfHistory: null,
|
||||||
pdfLinkService: null,
|
pdfLinkService: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens PDF document specified by URL.
|
||||||
|
* @returns {Promise} - Returns the promise, which is resolved when document
|
||||||
|
* is opened.
|
||||||
|
*/
|
||||||
open: function (params) {
|
open: function (params) {
|
||||||
|
if (this.pdfLoadingTask) {
|
||||||
|
// We need to destroy already opened document
|
||||||
|
return this.close().then(function () {
|
||||||
|
// ... and repeat the open() call.
|
||||||
|
return this.open(params);
|
||||||
|
}.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
var url = params.url;
|
var url = params.url;
|
||||||
var self = this;
|
var self = this;
|
||||||
this.setTitleUsingUrl(url);
|
this.setTitleUsingUrl(url);
|
||||||
|
|
||||||
// Loading document.
|
// Loading document.
|
||||||
var loadingTask = PDFJS.getDocument(url);
|
var loadingTask = PDFJS.getDocument(url);
|
||||||
|
this.pdfLoadingTask = loadingTask;
|
||||||
|
|
||||||
loadingTask.onProgress = function (progressData) {
|
loadingTask.onProgress = function (progressData) {
|
||||||
self.progress(progressData.loaded / progressData.total);
|
self.progress(progressData.loaded / progressData.total);
|
||||||
};
|
};
|
||||||
loadingTask.then(function (pdfDocument) {
|
|
||||||
// Document loaded, specifying document for the viewer.
|
|
||||||
this.pdfDocument = pdfDocument;
|
|
||||||
this.pdfViewer.setDocument(pdfDocument);
|
|
||||||
this.pdfLinkService.setDocument(pdfDocument);
|
|
||||||
this.pdfHistory.initialize(pdfDocument.fingerprint);
|
|
||||||
|
|
||||||
this.loadingBar.hide();
|
return loadingTask.promise.then(function (pdfDocument) {
|
||||||
this.setTitleUsingMetadata(pdfDocument);
|
// Document loaded, specifying document for the viewer.
|
||||||
}.bind(this), function (exception) {
|
self.pdfDocument = pdfDocument;
|
||||||
|
self.pdfViewer.setDocument(pdfDocument);
|
||||||
|
self.pdfLinkService.setDocument(pdfDocument);
|
||||||
|
self.pdfHistory.initialize(pdfDocument.fingerprint);
|
||||||
|
|
||||||
|
self.loadingBar.hide();
|
||||||
|
self.setTitleUsingMetadata(pdfDocument);
|
||||||
|
}, function (exception) {
|
||||||
var message = exception && exception.message;
|
var message = exception && exception.message;
|
||||||
var loadingErrorMessage = mozL10n.get('loading_error', null,
|
var loadingErrorMessage = mozL10n.get('loading_error', null,
|
||||||
'An error occurred while loading the PDF.');
|
'An error occurred while loading the PDF.');
|
||||||
@ -86,6 +103,32 @@ var PDFViewerApplication = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes opened PDF document.
|
||||||
|
* @returns {Promise} - Returns the promise, which is resolved when all
|
||||||
|
* destruction is completed.
|
||||||
|
*/
|
||||||
|
close: function () {
|
||||||
|
var errorWrapper = document.getElementById('errorWrapper');
|
||||||
|
errorWrapper.setAttribute('hidden', 'true');
|
||||||
|
|
||||||
|
if (!this.pdfLoadingTask) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
var promise = this.pdfLoadingTask.destroy();
|
||||||
|
this.pdfLoadingTask = null;
|
||||||
|
|
||||||
|
if (this.pdfDocument) {
|
||||||
|
this.pdfDocument = null;
|
||||||
|
|
||||||
|
this.pdfViewer.setDocument(null);
|
||||||
|
this.pdfLinkService.setDocument(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
},
|
||||||
|
|
||||||
get loadingBar() {
|
get loadingBar() {
|
||||||
var bar = new PDFJS.ProgressBar('#loadingBar', {});
|
var bar = new PDFJS.ProgressBar('#loadingBar', {});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user