Moves mozPrintCallback specific code to firefox_printservice.js

This commit is contained in:
Yury Delendik 2016-10-06 16:46:30 -05:00
parent f8bd3d4473
commit 24a7a58da7
8 changed files with 213 additions and 118 deletions

View file

@ -143,7 +143,7 @@ var PDFViewerApplication = {
appConfig: null,
pdfDocument: null,
pdfLoadingTask: null,
printing: false,
printService: null,
/** @type {PDFViewer} */
pdfViewer: null,
/** @type {PDFThumbnailViewer} */
@ -428,11 +428,12 @@ var PDFViewerApplication = {
return this.pdfViewer.currentPageNumber;
},
get supportsPrinting() {
var canvas = document.createElement('canvas');
var value = 'mozPrintCallback' in canvas;
get printing() {
return !!this.printService;
},
return pdfjsLib.shadow(this, 'supportsPrinting', value);
get supportsPrinting() {
return PDFPrintServiceFactory.instance.supportsPrinting;
},
get supportsFullscreen() {
@ -1106,59 +1107,23 @@ var PDFViewerApplication = {
return;
}
var alertNotReady = false;
var i, ii;
if (!this.pdfDocument || !this.pagesCount) {
alertNotReady = true;
} else {
for (i = 0, ii = this.pagesCount; i < ii; ++i) {
if (!this.pdfViewer.getPageView(i).pdfPage) {
alertNotReady = true;
break;
}
}
}
if (alertNotReady) {
// The beforePrint is a sync method and we need to know layout before
// returning from this method. Ensure that we can get sizes of the pages.
if (!this.pdfViewer.pageViewsReady) {
var notReadyMessage = mozL10n.get('printing_not_ready', null,
'Warning: The PDF is not fully loaded for printing.');
window.alert(notReadyMessage);
return;
}
this.printing = true;
var pagesOverview = this.pdfViewer.getPagesOverview();
var printContainer = this.appConfig.printContainer;
var printService = PDFPrintServiceFactory.instance.createPrintService(
this.pdfDocument, pagesOverview, printContainer);
this.printService = printService;
this.forceRendering();
var printContainer = this.appConfig.printContainer;
var body = document.querySelector('body');
body.setAttribute('data-mozPrintCallback', true);
if (!this.hasEqualPageSizes) {
console.warn('Not all pages have the same size. The printed result ' +
'may be incorrect!');
}
// Insert a @page + size rule to make sure that the page size is correctly
// set. Note that we assume that all pages have the same size, because
// variable-size pages are not supported yet (at least in Chrome & Firefox).
// TODO(robwu): Use named pages when size calculation bugs get resolved
// (e.g. https://crbug.com/355116) AND when support for named pages is
// added (http://www.w3.org/TR/css3-page/#using-named-pages).
// In browsers where @page + size is not supported (such as Firefox,
// https://bugzil.la/851441), the next stylesheet will be ignored and the
// user has to select the correct paper size in the UI if wanted.
this.pageStyleSheet = document.createElement('style');
var pageSize = this.pdfViewer.getPageView(0).pdfPage.getViewport(1);
this.pageStyleSheet.textContent =
// "size:<width> <height>" is what we need. But also add "A4" because
// Firefox incorrectly reports support for the other value.
'@supports ((size:A4) and (size:1pt 1pt)) {' +
'@page { size: ' + pageSize.width + 'pt ' + pageSize.height + 'pt;}' +
'}';
body.appendChild(this.pageStyleSheet);
for (i = 0, ii = this.pagesCount; i < ii; ++i) {
this.pdfViewer.getPageView(i).beforePrint(printContainer);
}
printService.layout();
//#if !PRODUCTION
if (true) {
@ -1186,17 +1151,10 @@ var PDFViewerApplication = {
},
afterPrint: function pdfViewSetupAfterPrint() {
var div = this.appConfig.printContainer;
while (div.hasChildNodes()) {
div.removeChild(div.lastChild);
if (this.printService) {
this.printService.destroy();
this.printService = null;
}
if (this.pageStyleSheet && this.pageStyleSheet.parentNode) {
this.pageStyleSheet.parentNode.removeChild(this.pageStyleSheet);
this.pageStyleSheet = null;
}
this.printing = false;
this.forceRendering();
},
@ -2330,6 +2288,17 @@ window.addEventListener('afterprint', function afterPrint(evt) {
});
})();
/* Abstract factory for the print service. */
var PDFPrintServiceFactory = {
instance: {
supportsPrinting: false,
createPrintService: function () {
throw new Error('Not implemented: createPrintService');
}
}
};
exports.PDFViewerApplication = PDFViewerApplication;
exports.DefaultExernalServices = DefaultExernalServices;
exports.PDFPrintServiceFactory = PDFPrintServiceFactory;
}));