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;
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));
// 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();
key : 'no-op',
action : 'print',
content : content,
- contentType : contentType
+ contentType : contentType,
+ printScope : printScope
//printer : printer // TODO: prefs, etc.
});
}
.directive('egPrintContainer', function() {
return {
restrict : 'AE',
- template : '<div>{{printContent}}</div>',
- controller : ['$scope','$window','$timeout','egPrintStore',
- function($scope, $window, $timeout, egPrintStore) {
+ template : '<div>' +
+ '<div id="eg-print-container-for-html"></div>' +
+ '<div ng-if="contentType==\'text/plain\'">' +
+ '<style>pre {border:none}</style>' +
+ '<pre>{{printContent}}</pre></div>' +
+ '</div>',
+
+ 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