2 * egPrint : manage print templates, process templates, print content
5 angular.module('egCoreMod')
8 ['$q','$window','$timeout','$http','egHatch','egAuth','egIDL','egOrg','egEnv',
9 function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , egEnv) {
13 service.template_base_path = 'share/print_templates/t_';
16 * context : 'default', 'receipt','label', etc.
17 * scope : data loaded into the template environment
18 * template : template name (e.g. 'checkout', 'transit_slip'
19 * content : content to print. If 'template' is set, content is
20 * derived from the template.
21 * content_type : 'text/html', 'text/plain', 'text/csv'
22 * show_dialog : boolean, if true, print dialog is shown. This setting
23 * only affects remote printers, since browser printers
24 * do not allow such control
26 service.print = function(args) {
27 if (!args) return $q.when();
30 // fetch the template, then proceed to printing
32 return service.getPrintTemplate(args.template)
33 .then(function(content) {
34 args.content = content;
35 if (!args.content_type) args.content_type = 'html';
36 service.getPrintTemplateContext(args.template)
37 .then(function(context) {
38 args.context = context;
39 return service.print_content(args);
45 return service.print_content(args);
48 // add commonly used attributes to the print scope
49 service.fleshPrintScope = function(scope) {
50 if (!scope) scope = {};
51 scope.today = new Date().toISOString();
52 scope.staff = egIDL.toHash(egAuth.user());
53 scope.current_location =
54 egIDL.toHash(egOrg.get(egAuth.user().ws_ou()));
57 // Template has been fetched (or no template needed)
58 // Process the template and send the result off to the printer.
59 service.print_content = function(args) {
60 service.fleshPrintScope(args.scope);
62 var promise = egHatch.hatchAvailable ?
63 service.print_via_hatch(args) :
64 service.print_via_browser(args);
66 return promise['finally'](
67 function() { service.clear_print_content() });
70 service.print_via_hatch = function(args) {
73 if (args.content_type == 'text/html') {
74 promise = service.ingest_print_content(
75 args.content_type, args.content, args.scope);
77 // text content requires no compilation for remote printing.
78 promise = $q.when(args.content);
81 return promise.then(function(html) {
82 return egHatch.remotePrint(
83 args.context || 'default',
91 service.print_via_browser = function(args) {
92 var type = args.content_type;
93 var content = args.content;
94 var printScope = args.scope;
96 if (type == 'text/csv' || type == 'text/plain') {
97 // preserve newlines, spaces, etc.
98 content = '<pre>' + content + '</pre>';
101 // Fetch the print CSS required for in-browser printing.
102 return $http.get(egEnv.basePath + 'css/print.css')
103 .then(function(response) {
105 // Add the bare CSS to the content
106 return '<style type="text/css" media="print">' +
111 }).then(function(content) {
112 // Ingest the content into the page DOM.
113 return service.ingest_print_content(type, content, printScope);
120 // loads an HTML print template by name from the server
121 // If no template is available in local/hatch storage,
122 // fetch the template as an HTML file from the server.
123 service.getPrintTemplate = function(name) {
124 var deferred = $q.defer();
126 egHatch.getItem('eg.print.template.' + name)
127 .then(function(html) {
130 // we have a locally stored template
131 deferred.resolve(html);
135 var path = service.template_base_path + name;
136 console.debug('fetching template ' + path);
139 .success(function(data) { deferred.resolve(data) })
141 console.error('unable to locate print template: ' + name);
146 return deferred.promise;
149 service.storePrintTemplate = function(name, html) {
150 return egHatch.setItem('eg.print.template.' + name, html);
153 service.getPrintTemplateContext = function(name) {
154 var deferred = $q.defer();
156 egHatch.getItem('eg.print.template_context.' + name)
158 function(context) { deferred.resolve(context); },
159 function() { deferred.resolve('default'); }
162 return deferred.promise;
164 service.storePrintTemplateContext = function(name, context) {
165 return egHatch.setItem('eg.print.template_context.' + name, context);
173 * Container for inserting print data into the browser page.
174 * On insert, $window.print() is called to print the data.
175 * The div housing eg-print-container must apply the correct
176 * print media CSS to ensure this content (and not the rest
177 * of the page) is printed.
179 * NOTE: There should only ever be 1 egPrintContainer instance per page.
180 * egPrintContainer attaches functions to the egPrint service with
181 * closures around the egPrintContainer instance's $scope (including its
182 * DOM element). Having multiple egPrintContainers could result in chaos.
185 .directive('egPrintContainer', ['$compile', function($compile) {
188 scope : {}, // isolate our scope
189 link : function(scope, element, attrs) {
193 ['$scope','$q','$window','$timeout','egHatch','egPrint','egEnv',
194 function($scope , $q , $window , $timeout , egHatch , egPrint , egEnv) {
196 egPrint.clear_print_content = function() {
198 $compile($scope.elm.contents())($scope.$new(true));
201 // Insert the printable content into the DOM.
202 // For remote printing, this lets us exract the compiled HTML
204 // For local printing, this lets us print directly from the
205 // DOM with print CSS.
206 // Returns a promise reolved with the compiled HTML as a string.
207 egPrint.ingest_print_content = function(type, content, printScope) {
208 $scope.elm.html(content);
210 var sub_scope = $scope.$new(true);
211 angular.forEach(printScope, function(val, key) {
212 sub_scope[key] = val;
215 var resp = $compile($scope.elm.contents())(sub_scope);
217 var deferred = $q.defer();
219 // give the $digest a chance to complete then
220 // resolve with the compiled HTML from our
224 resp.contents()[0].parentNode.innerHTML
228 return deferred.promise;