From d0093ed2e3c56c8093c3def5b48a6ac359969ecd Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Wed, 11 Jan 2017 14:33:13 -0500 Subject: [PATCH] LP#1646166 Hatch print requires no print CSS Avoid inserting the print CSS into the print content when printing remotely. Commit includes some refactoring of the print code to create more obvious separation between remote vs. in-browser printing. Signed-off-by: Bill Erickson Signed-off-by: Kathy Lussier --- Open-ILS/web/js/ui/default/staff/services/print.js | 118 ++++++++++----------- 1 file changed, 57 insertions(+), 61 deletions(-) diff --git a/Open-ILS/web/js/ui/default/staff/services/print.js b/Open-ILS/web/js/ui/default/staff/services/print.js index 2c90818f8d..d2ffbc95d1 100644 --- a/Open-ILS/web/js/ui/default/staff/services/print.js +++ b/Open-ILS/web/js/ui/default/staff/services/print.js @@ -59,52 +59,64 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg service.print_content = function(args) { service.fleshPrintScope(args.scope); + var promise = egHatch.hatchAvailable ? + service.print_via_hatch(args) : + service.print_via_browser(args); + + return promise['finally']( + function() { service.clear_print_content() }); + } + + service.print_via_hatch = function(args) { var promise; - if (args.content_type == 'text/html') { - // all HTML content is assumed to require compilation, - // regardless of the print destination + if (args.content_type == 'text/html') { promise = service.ingest_print_content( args.content_type, args.content, args.scope); - } else { - // text content does not require compilation for remote printing + // text content requires no compilation for remote printing. promise = $q.when(args.content); } - var context = args.context || 'default'; - return promise.then(function(html) { - - return egHatch.remotePrint(context, - args.content_type, html, args.show_dialog)['catch']( - - function(msg) { - // remote print not available; - - if (egHatch.hatchRequired()) { - console.error("Unable to print data; " - + "hatchRequired=true, but hatch is not connected"); - return $q.reject(); - } - - if (args.content_type != 'text/html') { - // text content does require compilation - // (absorption) for browser printing - return service.ingest_print_content( - args.content_type, args.content, args.scope - ).then(function() { $window.print(); service.clear_print_content(); }); - } else { - // HTML content is already ingested and accessible - // within the page to the printer. - $window.print(); - service.clear_print_content(); - } - } + return egHatch.remotePrint( + args.context || 'default', + args.content_type, + html, + args.show_dialog ); }); } + service.print_via_browser = function(args) { + var type = args.content_type; + var content = args.content; + var printScope = args.scope; + + if (type == 'text/csv' || type == 'text/plain') { + // preserve newlines, spaces, etc. + content = '
' + content + '
'; + } + + // Fetch the print CSS required for in-browser printing. + return $http.get(egEnv.basePath + 'css/print.css') + .then(function(response) { + + // Add the bare CSS to the content + return '' + + content; + + }).then(function(content) { + // Ingest the content into the page DOM. + return service.ingest_print_content(type, content, printScope); + + }).then(function() { + $window.print(); + }); + } + // loads an HTML print template by name from the server // If no template is available in local/hatch storage, // fetch the template as an HTML file from the server. @@ -163,14 +175,14 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg * The div housing eg-print-container must apply the correct * print media CSS to ensure this content (and not the rest * of the page) is printed. + * + * NOTE: There should only ever be 1 egPrintContainer instance per page. + * egPrintContainer attaches functions to the egPrint service with + * closures around the egPrintContainer instance's $scope (including its + * DOM element). Having multiple egPrintContainers could result in chaos. */ -// FIXME: only apply print CSS when print commands are issued via the -// print container, otherwise using the browser's native print page -// option will always result in empty pages. Move the print CSS -// out of the standalone CSS file and put it into a template file -// for this directive. -.directive('egPrintContainer', ['$compile', '$http', function($compile, $http) { +.directive('egPrintContainer', ['$compile', function($compile) { return { restrict : 'AE', scope : {}, // isolate our scope @@ -186,29 +198,13 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg $compile($scope.elm.contents())($scope.$new(true)); } + // Insert the printable content into the DOM. + // For remote printing, this lets us exract the compiled HTML + // from the DOM. + // For local printing, this lets us print directly from the + // DOM with print CSS. + // Returns a promise reolved with the compiled HTML as a string. egPrint.ingest_print_content = function(type, content, printScope) { - - if (type == 'text/csv' || type == 'text/plain') { - // preserve newlines, spaces, etc. - content = '
' + content + '
'; - } - - return $http.get(egEnv.basePath + 'css/print.css').then( - function(response) { - content = '' + - content; - return finish_ingest_print_content(type, content, printScope); - }, - function() { - return finish_ingest_print_content(type, content, printScope); - } - ); - - } - - function finish_ingest_print_content(type, content, printScope) { $scope.elm.html(content); var sub_scope = $scope.$new(true); -- 2.11.0