}
}
- service.interpolateHtmlTemplate = 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 html = $interpolate(template)(subScope);
- subScope.$destroy();
- return html;
- }
+ service.printFromTemplate = function(context, templateName, printScope) {
+ // populate some common print scope values...
+ printScope.today = new Date();
+ return service.getPrintTemplate(templateName)
+ .then(function(template) {
+ service.print(context, 'text/html', template, printScope);
+ });
+ }
- // supported values for contentType are 'text/html' and 'text/plain'
service.print = function(
context, contentType, content, printScope, withDialog) {
- // generate our HTML content if necessary
+ var promise;
if (contentType == 'text/html') {
- content = service.interpolateHtmlTemplate(content, printScope);
+ // all HTML content is assumed to require compilation, regardless
+ // of the print destination
+ promise = service.ingestPrintContent(
+ contentType, content, printScope);
+ } else {
+ // text content does not require compilation for remote printing
+ promise = $q.when(content);
}
- return service.remotePrint(
- context, contentType, content, printScope, withDialog)['catch'](
- function(msg) {
- // remote print not available; print locally
- return service.browserPrint(msg);
- }
- );
+ return promise.then(function(html) {
+ return service.remotePrint(
+ context, contentType, html, withDialog)['catch'](
+
+ function(msg) {
+ // remote print not available;
+ if (contentType != 'text/html') {
+ // text content does require compilation
+ // (absorption) for browser printing
+ service.ingestPrintContent(contentType, content, {})
+ .then(function() { $window.print() });
+ } else {
+ $window.print();
+ }
+ }
+ );
+ });
}
service.remotePrint = function(
- context, contentType, content, printScope, withDialog) {
+ context, contentType, content, withDialog) {
return service.getPrintConfig().then(
function(conf) {
);
}
- // print locally via the browser
- service.browserPrint = function(msg) {
- service.onBrowserPrint(msg.contentType, msg.content);
- }
-
// -------------
// print configuration is always stored as remote items,
// since there is no concept of a local printer
// 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', function() {
+.directive('egPrintContainer', ['$compile', function($compile) {
return {
restrict : 'AE',
- template : '<div id="eg-print-container-for-html"></div>',
+ scope : {}, // isolate our scope
+ link : function(scope, element, attrs) {
+ scope.elm = element;
+ },
controller :
- ['$scope','$window','$timeout','egHatch',
- function($scope , $window , $timeout , egHatch) {
-
- egHatch.onBrowserPrint = function(contentType, content) {
- switch(contentType) {
- case 'text/csv':
- case 'text/plain':
- // preserve newlines, spaces, etc.
- content = '<pre>' + content + '</pre>';
- case 'text/html':
- // TODO: make this angular-y
- var div = document.getElementById('eg-print-container-for-html');
- while (div.childNodes[0])
- div.removeChild(div.childNodes[0]);
- div.innerHTML = content;
+ ['$scope','$q','$window','$timeout','egHatch',
+ function($scope , $q , $window , $timeout , egHatch) {
+
+ egHatch.ingestPrintContent = function(type, content, printScope) {
+
+ if (type == 'text/csv' || type == 'text/plain') {
+ // preserve newlines, spaces, etc.
+ content = '<pre>' + content + '</pre>';
}
- // force the template to absorb the data before printing
- // if an apply/digest loop is not already in progress
- //if (!$scope.$$phase) $scope.$apply();
- //
- // TODO: apply() may no longer be necessary
-
- $timeout(
- function() {
- $scope.$apply();
- $window.print();
- }
- );
+ $scope.elm.html(content);
+
+ var sub_scope = $scope.$new(true);
+ angular.forEach(printScope, function(val, key) {
+ sub_scope[key] = val;
+ })
+
+ var resp = $compile($scope.elm.contents())(sub_scope);
+
+ var deferred = $q.defer();
+ $timeout(function(){
+ // give the $digest a chance to complete then
+ // resolve with the compiled HTML from our
+ // print container
+
+ deferred.resolve(
+ resp.contents()[0].parentNode.innerHTML
+ );
+ });
+
+ return deferred.promise;
}
}
]
}
-});
-
+}])