]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/print.js
LP#1646166 Hatch printing multi root-node templates
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / print.js
1 /**
2  * egPrint : manage print templates, process templates, print content
3  *
4  */
5 angular.module('egCoreMod')
6
7 .factory('egPrint',
8        ['$q','$window','$timeout','$http','egHatch','egAuth','egIDL','egOrg','egEnv',
9 function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , egEnv) {
10
11     var service = {};
12
13     service.template_base_path = 'share/print_templates/t_';
14
15     /*
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
25      */
26     service.print = function(args) {
27         if (!args) return $q.when();
28
29         if (args.template) {
30             // fetch the template, then proceed to printing
31
32             return service.getPrintTemplate(args.template)
33             .then(function(content) {
34                 args.content = content;
35                 if (!args.content_type) args.content_type = 'text/html';
36                 service.getPrintTemplateContext(args.template)
37                 .then(function(context) {
38                     args.context = context;
39                     return service.print_content(args);
40                 });
41             });
42
43         } 
44
45         return service.print_content(args);
46     }
47
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()));
55     }
56
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);
61
62         var promise = egHatch.usePrinting() ?
63             service.print_via_hatch(args) :
64             service.print_via_browser(args);
65
66         return promise['finally'](
67             function() { service.clear_print_content() });
68     }
69
70     service.print_via_hatch = function(args) {
71         var promise;
72
73         if (args.content_type == 'text/html') {
74             promise = service.ingest_print_content(
75                 args.content_type, args.content, args.scope);
76         } else {
77             // text content requires no compilation for remote printing.
78             promise = $q.when(args.content);
79         }
80
81         return promise.then(function(html) {
82             // For good measure, wrap the compiled HTML in container tags.
83             html = "<html><body>" + html + "</body></html>";
84             return egHatch.remotePrint(
85                 args.context || 'default',
86                 args.content_type, 
87                 html, 
88                 args.show_dialog
89             );
90         });
91     }
92
93     service.print_via_browser = function(args) {
94         var type = args.content_type;
95         var content = args.content;
96         var printScope = args.scope;
97
98         if (type == 'text/csv' || type == 'text/plain') {
99             // preserve newlines, spaces, etc.
100             content = '<pre>' + content + '</pre>';
101         }
102
103         // Fetch the print CSS required for in-browser printing.
104         return $http.get(egEnv.basePath + 'css/print.css')
105         .then(function(response) {
106
107             // Add the bare CSS to the content
108             return '<style type="text/css" media="print">' +
109                   response.data +
110                   '</style>' +
111                   content;
112
113         }).then(function(content) {
114             // Ingest the content into the page DOM.
115             return service.ingest_print_content(type, content, printScope);
116
117         }).then(function() { 
118             $window.print();
119         });
120     }
121
122     // loads an HTML print template by name from the server
123     // If no template is available in local/hatch storage, 
124     // fetch the template as an HTML file from the server.
125     service.getPrintTemplate = function(name) {
126         var deferred = $q.defer();
127
128         egHatch.getItem('eg.print.template.' + name)
129         .then(function(html) {
130
131             if (html) {
132                 // we have a locally stored template
133                 deferred.resolve(html);
134                 return;
135             }
136
137             var path = service.template_base_path + name;
138             console.debug('fetching template ' + path);
139
140             $http.get(path)
141             .success(function(data) { deferred.resolve(data) })
142             .error(function() {
143                 console.error('unable to locate print template: ' + name);
144                 deferred.reject();
145             });
146         });
147
148         return deferred.promise;
149     }
150
151     service.storePrintTemplate = function(name, html) {
152         return egHatch.setItem('eg.print.template.' + name, html);
153     }
154
155     service.getPrintTemplateContext = function(name) {
156         var deferred = $q.defer();
157
158         egHatch.getItem('eg.print.template_context.' + name)
159         .then(
160             function(context) { deferred.resolve(context); },
161             function()        { deferred.resolve('default'); }
162         );
163
164         return deferred.promise;
165     }
166     service.storePrintTemplateContext = function(name, context) {
167         return egHatch.setItem('eg.print.template_context.' + name, context);
168     }
169
170     return service;
171 }])
172
173
174 /**
175  * Container for inserting print data into the browser page.
176  * On insert, $window.print() is called to print the data.
177  * The div housing eg-print-container must apply the correct
178  * print media CSS to ensure this content (and not the rest
179  * of the page) is printed.
180  *
181  * NOTE: There should only ever be 1 egPrintContainer instance per page.
182  * egPrintContainer attaches functions to the egPrint service with
183  * closures around the egPrintContainer instance's $scope (including its
184  * DOM element). Having multiple egPrintContainers could result in chaos.
185  */
186
187 .directive('egPrintContainer', ['$compile', function($compile) {
188     return {
189         restrict : 'AE',
190         scope : {}, // isolate our scope
191         link : function(scope, element, attrs) {
192             scope.elm = element;
193         },
194         controller : 
195                    ['$scope','$q','$window','$timeout','egHatch','egPrint','egEnv',
196             function($scope , $q , $window , $timeout , egHatch , egPrint , egEnv) {
197
198                 egPrint.clear_print_content = function() {
199                     $scope.elm.html('');
200                     $compile($scope.elm.contents())($scope.$new(true));
201                 }
202
203                 // Insert the printable content into the DOM.
204                 // For remote printing, this lets us exract the compiled HTML
205                 // from the DOM.
206                 // For local printing, this lets us print directly from the
207                 // DOM with print CSS.
208                 // Returns a promise reolved with the compiled HTML as a string.
209                 egPrint.ingest_print_content = function(type, content, printScope) {
210                     $scope.elm.html(content);
211
212                     var sub_scope = $scope.$new(true);
213                     angular.forEach(printScope, function(val, key) {
214                         sub_scope[key] = val;
215                     })
216
217                     var resp = $compile($scope.elm.contents())(sub_scope);
218
219
220                     var deferred = $q.defer();
221                     $timeout(function(){
222                         // give the $digest a chance to complete then resolve
223                         // with the compiled HTML from our print container
224                         deferred.resolve($scope.elm.html());
225                     });
226
227                     return deferred.promise;
228                 }
229             }
230         ]
231     }
232 }])
233