From: Bill Erickson Date: Wed, 16 Apr 2014 21:37:48 +0000 (-0400) Subject: web staff: printing X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=461a0cc7d7bac2b394539eb38a91d42543dc19ed;p=working%2FEvergreen.git web staff: printing Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/web/js/ui/default/staff/services/printstore.js b/Open-ILS/web/js/ui/default/staff/services/printstore.js index 2b2ede6e9c..b9596383c5 100644 --- a/Open-ILS/web/js/ui/default/staff/services/printstore.js +++ b/Open-ILS/web/js/ui/default/staff/services/printstore.js @@ -20,8 +20,8 @@ angular.module('egCoreMod') .factory('egPrintStore', - ['$q','$window','$timeout', - function($q , $window , $timeout) { + ['$q','$window','$timeout','$compile','$rootScope', + function($q , $window , $timeout , $compile , $rootScope) { var service = {}; service.msgId = 0; @@ -38,7 +38,7 @@ angular.module('egCoreMod') var msg2 = {}; // shallow copy and scrub msg before sending angular.forEach(msg, function(val, key) { - if (key == 'deferred') return; + if (key.match(/deferred|printScope/)) return; msg2[key] = val; }); service.socket.send(JSON.stringify(msg2)); @@ -181,13 +181,36 @@ angular.module('egCoreMod') // print locally via the browser service.browserPrint = function(msg) { // let our local print container handle printing - service.onBrowserPrint(msg.contentType, msg.content); + var content = msg.content; + if (msg.contentType == 'text/html') { + content = + service.processHtmlTemplate(msg.content, msg.printScope) + } + service.onBrowserPrint(msg.contentType, content); msg.success = true; // assume browser print succeeded } + /** + * TODO: local and hatch templates need to go through generation.. + * */ + + service.processHtmlTemplate = function(template, printScope) { + // TODO: for print template security, we must scrub + // the scope object and remove any references to + // functions or objects. Otherwise, print templates + // would have the power to modify data via the scope + var subScope = $rootScope.$new(); + angular.forEach(printScope, function(val, key) {subScope[key] = val}); + var element = angular.element(template); + element = $compile(element)(subScope); + console.log('element zero ' + element[0]); + return element[0]; + } + + // print the provided content // supported values for contentType are 'text/html' and 'text/plain' - service.print = function(contentType, content) { + service.print = function(contentType, content, printScope) { if (service.hatchAvailable === false) { service.browserPrint(contentType, content); return $q.when(); @@ -197,7 +220,8 @@ angular.module('egCoreMod') key : 'no-op', action : 'print', content : content, - contentType : contentType + contentType : contentType, + printScope : printScope //printer : printer // TODO: prefs, etc. }); } @@ -283,19 +307,32 @@ angular.module('egCoreMod') .directive('egPrintContainer', function() { return { restrict : 'AE', - template : '
{{printContent}}
', - controller : ['$scope','$window','$timeout','egPrintStore', - function($scope, $window, $timeout, egPrintStore) { + template : '
' + + '
' + + '
' + + '' + + '
{{printContent}}
' + + '
', + + controller : + ['$scope','$window','$timeout','egPrintStore', + function($scope , $window , $timeout, egPrintStore) { + + // if contentType == 'text/html', content is a DOM node egPrintStore.onBrowserPrint = function(contentType, content) { - console.log('printing ' + content.length + ' chars of ' + contentType); + console.log('print content ' + content); + $scope.contentType = contentType; switch(contentType) { case 'text/csv': case 'text/plain': $scope.printContent = content; break; case 'text/html': - console.error('print text/html not yet supported'); - break; + // TODO: make this angular-y + var div = document.getElementById('eg-print-container-for-html'); + while (div.childNodes[0]) + div.removeChild(div.childNodes[0]); + div.appendChild(content); } // force the template to absorb the data before printing