server-driven print templates AngJS support/example
authorBill Erickson <berickxx@gmail.com>
Tue, 16 Apr 2019 18:13:21 +0000 (14:13 -0400)
committerBill Erickson <berickxx@gmail.com>
Fri, 19 Apr 2019 17:31:22 +0000 (13:31 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/web/js/ui/default/staff/circ/patron/app.js
Open-ILS/web/js/ui/default/staff/services/print.js

index d007257..08374b8 100644 (file)
@@ -331,10 +331,11 @@ function($scope,  $q , $location , $filter , egCore , egNet , egUser , egAlertDi
     $scope.print_address = function(addr) {
         egCore.print.print({
             context : 'default', 
-            template : 'patron_address', 
+            template: 'address-label',
+            server_hosted: true,
             scope : {
-                patron : egCore.idl.toHash(patronSvc.current),
-                address : egCore.idl.toHash(addr)
+                patron : patronSvc.current,
+                address : addr
             }
         });
     }
index d12a6cd..ebe9faa 100644 (file)
@@ -20,6 +20,7 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
 
 
     service.template_base_path = 'share/print_templates/t_';
+    service.server_template_path = '/print_template';
 
     /*
      * context  : 'default', 'receipt','label', etc. 
@@ -31,11 +32,21 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
      * show_dialog  : boolean, if true, print dialog is shown.  This setting
      *                only affects remote printers, since browser printers
      *                do not allow such control
+     * server_hosted: if true, have the server compile the template for us.
      */
     service.print = function(args) {
         if (!args) return $q.when();
 
-        if (args.template) {
+        if (args.server_hosted) {
+
+            return service.compileRemoteTemplate(args.template, args.scope)
+            .then(function(response) {
+                args.content_type = response.contentType;
+                args.content = response.content;
+                return service.print_content(args);
+            });
+
+        } else if (args.template) {
             // fetch the template, then proceed to printing
 
             return service.getPrintTemplate(args.template)
@@ -48,12 +59,39 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
                     return service.print_content(args);
                 });
             });
-
         } 
 
         return service.print_content(args);
     }
 
+    service.compileRemoteTemplate = function(templateName, templateData) {
+
+        var formData = new FormData();
+
+        formData.append('ses', egAuth.token());
+        formData.append('template_name', templateName);
+        formData.append('template_data', js2JSON(templateData));
+        formData.append('locale', OpenSRF.locale);
+
+        return new Promise((resolve, reject) => {
+            var xhttp = new XMLHttpRequest();
+            xhttp.onreadystatechange = function() {
+                if (this.readyState === 4) {
+                    if (this.status === 200) {
+                        resolve({
+                            content: xhttp.responseText,
+                            contentType: this.getResponseHeader('content-type')
+                        });
+                    } else {
+                        reject('Error compiling print template');
+                    }
+                }
+            };
+            xhttp.open('POST', service.server_template_path, true);
+            xhttp.send(formData);
+        });
+    }
+
     // add commonly used attributes to the print scope
     service.fleshPrintScope = function(scope) {
         if (!scope) scope = {};