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 'circ.staff_client.receipt.alert_text',
14 'circ.staff_client.receipt.event_text',
15 'circ.staff_client.receipt.footer_text',
16 'circ.staff_client.receipt.header_text',
17 'circ.staff_client.receipt.notice_text'
22 service.template_base_path = 'share/print_templates/t_';
25 * context : 'default', 'receipt','label', etc.
26 * scope : data loaded into the template environment
27 * template : template name (e.g. 'checkout', 'transit_slip'
28 * content : content to print. If 'template' is set, content is
29 * derived from the template.
30 * content_type : 'text/html', 'text/plain', 'text/csv'
31 * show_dialog : boolean, if true, print dialog is shown. This setting
32 * only affects remote printers, since browser printers
33 * do not allow such control
35 service.print = function(args) {
36 if (!args) return $q.when();
39 // fetch the template, then proceed to printing
41 return service.getPrintTemplate(args.template)
42 .then(function(content) {
43 args.content = content;
44 if (!args.content_type) args.content_type = 'text/html';
45 service.getPrintTemplateContext(args.template)
46 .then(function(context) {
47 args.context = context;
48 return service.print_content(args);
54 return service.print_content(args);
57 // add commonly used attributes to the print scope
58 service.fleshPrintScope = function(scope) {
59 if (!scope) scope = {};
60 scope.today = new Date().toISOString();
63 scope.staff = egIDL.toHash(egAuth.user());
64 scope.current_location =
65 egIDL.toHash(egOrg.get(egAuth.user().ws_ou()));
68 return service.fetch_includes(scope);
71 // Retrieve org settings for receipt includes and add them
72 // to the print scope under scope.includes.<name>
73 service.fetch_includes = function(scope) {
74 // org settings for the workstation org are cached
75 // within egOrg. No need to cache them locally.
76 return egOrg.settings(service.include_settings).then(
80 angular.forEach(settings, function(val, key) {
81 // strip the settings prefix so you just have
82 // e.g. scope.includes.alert_text
83 scope.includes[key.split(/\./).pop()] = val;
89 service.last_print = {};
91 // Template has been fetched (or no template needed)
92 // Process the template and send the result off to the printer.
93 service.print_content = function(args) {
94 return service.fleshPrintScope(args.scope).then(function() {
95 var promise = egHatch.usePrinting() ?
96 service.print_via_hatch(args) :
97 service.print_via_browser(args);
99 return promise['finally'](
100 function() { service.clear_print_content() });
104 service.print_via_hatch = function(args) {
107 if (args.content_type == 'text/html') {
108 promise = service.ingest_print_content(
109 args.content_type, args.content, args.scope
110 ).then(function(html) {
111 // For good measure, wrap the compiled HTML in container tags.
112 return "<html><body>" + html + "</body></html>";
115 // text content requires no compilation for remote printing.
116 promise = $q.when(args.content);
119 return promise.then(function(content) {
120 service.last_print.content = content;
121 service.last_print.context = args.context || 'default';
122 service.last_print.content_type = args.content_type;
123 service.last_print.show_dialog = args.show_dialog;
125 egHatch.setItem('eg.print.last_printed', service.last_print);
127 return service._remotePrint();
131 service._remotePrint = function () {
132 return egHatch.remotePrint(
133 service.last_print.context,
134 service.last_print.content_type,
135 service.last_print.content,
136 service.last_print.show_dialog
140 service.print_via_browser = function(args) {
141 var type = args.content_type;
142 var content = args.content;
143 var printScope = args.scope;
145 if (type == 'text/csv' || type == 'text/plain') {
146 // preserve newlines, spaces, etc.
147 content = '<pre>' + content + '</pre>';
150 // Fetch the print CSS required for in-browser printing.
151 return $http.get(egEnv.basePath + 'css/print.css')
152 .then(function(response) {
154 // Add the bare CSS to the content
155 return '<style type="text/css" media="print">' +
160 }).then(function(content) {
162 // Ingest the content into the page DOM.
163 return service.ingest_print_content(type, content, printScope);
165 }).then(function(html) {
167 // Note browser ignores print context
168 service.last_print.content = html;
169 service.last_print.content_type = type;
170 egHatch.setItem('eg.print.last_printed', service.last_print);
176 service.reprintLast = function () {
177 var deferred = $q.defer();
178 var promise = deferred.promise;
179 promise.finally( function() { service.clear_print_content() });
182 'eg.print.last_printed'
183 ).then(function (last) {
184 if (last && last.content) {
185 service.last_print = last;
187 if (egHatch.usePrinting()) {
188 promise.then(function () {
189 egHatch._remotePrint()
192 promise.then(function () {
193 service.ingest_print_content(
194 null, null, null, service.last_print.content
195 ).then(function() { $window.print() });
198 return deferred.resolve();
200 return deferred.reject();
205 // loads an HTML print template by name from the server
206 // If no template is available in local/hatch storage,
207 // fetch the template as an HTML file from the server.
208 service.getPrintTemplate = function(name) {
209 var deferred = $q.defer();
211 egHatch.getItem('eg.print.template.' + name)
212 .then(function(html) {
215 // we have a locally stored template
216 deferred.resolve(html);
220 var path = service.template_base_path + name;
221 console.debug('fetching template ' + path);
223 $http.get(path).then(
224 function(data) { deferred.resolve(data.data) },
226 console.error('unable to locate print template: ' + name);
232 return deferred.promise;
235 service.storePrintTemplate = function(name, html) {
236 return egHatch.setItem('eg.print.template.' + name, html);
239 service.removePrintTemplate = function(name) {
240 return egHatch.removeItem('eg.print.template.' + name);
243 service.getPrintTemplateContext = function(name) {
244 var deferred = $q.defer();
246 egHatch.getItem('eg.print.template_context.' + name)
248 function(context) { deferred.resolve(context); },
249 function() { deferred.resolve('default'); }
252 return deferred.promise;
254 service.storePrintTemplateContext = function(name, context) {
255 return egHatch.setItem('eg.print.template_context.' + name, context);
257 service.removePrintTemplateContext = function(name) {
258 return egHatch.removeItem('eg.print.template_context.' + name);
266 * Container for inserting print data into the browser page.
267 * On insert, $window.print() is called to print the data.
268 * The div housing eg-print-container must apply the correct
269 * print media CSS to ensure this content (and not the rest
270 * of the page) is printed.
272 * NOTE: There should only ever be 1 egPrintContainer instance per page.
273 * egPrintContainer attaches functions to the egPrint service with
274 * closures around the egPrintContainer instance's $scope (including its
275 * DOM element). Having multiple egPrintContainers could result in chaos.
278 .directive('egPrintContainer', ['$compile', function($compile) {
281 scope : {}, // isolate our scope
282 link : function(scope, element, attrs) {
286 ['$scope','$q','$window','$timeout','egHatch','egPrint','egEnv',
287 function($scope , $q , $window , $timeout , egHatch , egPrint , egEnv) {
289 egPrint.clear_print_content = function() {
291 $compile($scope.elm.contents())($scope.$new(true));
294 // Insert the printable content into the DOM.
295 // For remote printing, this lets us exract the compiled HTML
297 // For local printing, this lets us print directly from the
298 // DOM with print CSS.
299 // Returns a promise reolved with the compiled HTML as a string.
301 // If a pre-compiled HTML string is provided, it's inserted
302 // as-is into the DOM for browser printing without any
303 // additional interpolation. This is useful for reprinting,
304 // previously compiled content.
305 egPrint.ingest_print_content =
306 function(type, content, printScope, compiledHtml) {
309 $scope.elm.html(compiledHtml);
310 return $q.when(compiledHtml);
313 $scope.elm.html(content);
315 var sub_scope = $scope.$new(true);
316 angular.forEach(printScope, function(val, key) {
317 sub_scope[key] = val;
320 var resp = $compile($scope.elm.contents())(sub_scope);
323 var deferred = $q.defer();
325 // give the $digest a chance to complete then resolve
326 // with the compiled HTML from our print container
327 deferred.resolve($scope.elm.html());
330 return deferred.promise;