LP#1646166 Hatch print requires no print CSS
authorBill Erickson <berickxx@gmail.com>
Wed, 11 Jan 2017 19:33:13 +0000 (14:33 -0500)
committerChris Sharp <csharp@georgialibraries.org>
Mon, 13 Feb 2017 17:59:40 +0000 (12:59 -0500)
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 <berickxx@gmail.com>
Conflicts:
Open-ILS/web/js/ui/default/staff/services/print.js

Open-ILS/web/js/ui/default/staff/services/print.js

index fa60193..98fb0c2 100644 (file)
@@ -56,49 +56,62 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg) {
     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);
         }
 
-        // TODO: link print context to template type
-        var context = args.context || 'default';
 
         return promise.then(function(html) {
+            return egHatch.remotePrint(
+                args.context || 'default',
+                args.content_type, 
+                html, 
+                args.show_dialog
+            );
+        });
+    }
 
-            return egHatch.remotePrint(context,
-                args.content_type, html, args.show_dialog)['catch'](
+    service.print_via_browser = function(args) {
+        var type = args.content_type;
+        var content = args.content;
+        var printScope = args.scope;
 
-                function(msg) {
-                    // remote print not available; 
+        if (type == 'text/csv' || type == 'text/plain') {
+            // preserve newlines, spaces, etc.
+            content = '<pre>' + content + '</pre>';
+        }
 
-                    if (egHatch.hatchRequired()) {
-                        console.error("Unable to print data; "
-                         + "hatchRequired=true, but hatch is not connected");
-                         return $q.reject();
-                    }
+        // Fetch the print CSS required for in-browser printing.
+        return $http.get(egEnv.basePath + 'css/print.css')
+        .then(function(response) {
 
-                    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() });
-                    } else {
-                        // HTML content is already ingested and accessible
-                        // within the page to the printer.  
-                        $window.print();
-                    }
-                }
-            );
+            // Add the bare CSS to the content
+            return '<style type="text/css" media="print">' +
+                  response.data +
+                  '</style>' +
+                  content;
+
+        }).then(function(content) {
+            // Ingest the content into the page DOM.
+            return service.ingest_print_content(type, content, printScope);
+
+        }).then(function() { 
+            $window.print();
         });
     }
 
@@ -145,13 +158,21 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg) {
  * 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.
  */
 
+<<<<<<< HEAD
 // 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.
+=======
+>>>>>>> 9ff498f... LP#1646166 Hatch print requires no print CSS
 .directive('egPrintContainer', ['$compile', function($compile) {
     return {
         restrict : 'AE',
@@ -163,13 +184,22 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg) {
                    ['$scope','$q','$window','$timeout','egHatch','egPrint',
             function($scope , $q , $window , $timeout , egHatch , egPrint) {
 
+                // 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) {
+<<<<<<< HEAD
 
                     if (type == 'text/csv' || type == 'text/plain') {
                         // preserve newlines, spaces, etc.
                         content = '<pre>' + content + '</pre>';
                     }
 
+=======
+>>>>>>> 9ff498f... LP#1646166 Hatch print requires no print CSS
                     $scope.elm.html(content);
 
                     var sub_scope = $scope.$new(true);