From dfa37eb332ce4d7974bba692430eccd88770bc67 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Tue, 16 Apr 2019 14:13:21 -0400 Subject: [PATCH] server-driven print templates AngJS support/example Signed-off-by: Bill Erickson --- .../web/js/ui/default/staff/circ/patron/app.js | 7 ++-- Open-ILS/web/js/ui/default/staff/services/print.js | 42 ++++++++++++++++++++-- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Open-ILS/web/js/ui/default/staff/circ/patron/app.js b/Open-ILS/web/js/ui/default/staff/circ/patron/app.js index d007257e4f..08374b84d7 100644 --- a/Open-ILS/web/js/ui/default/staff/circ/patron/app.js +++ b/Open-ILS/web/js/ui/default/staff/circ/patron/app.js @@ -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 } }); } 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 d12a6cd84f..ebe9faa651 100644 --- a/Open-ILS/web/js/ui/default/staff/services/print.js +++ b/Open-ILS/web/js/ui/default/staff/services/print.js @@ -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 = {}; -- 2.11.0