service.template_base_path = 'share/print_templates/t_';
+ service.server_template_path = '/print_template';
/*
* context : 'default', 'receipt','label', etc.
* show_dialog : boolean, if true, print dialog is shown. This setting
* only affects remote printers, since browser printers
* do not allow such control
+ * server_hosted: if true, have the server compile the template for us.
*/
service.print = function(args) {
if (!args) return $q.when();
- if (args.template) {
+ if (args.server_hosted) {
+
+ return service.compileRemoteTemplate(args.template, args.scope)
+ .then(function(response) {
+ args.content_type = response.contentType;
+ args.content = response.content;
+ return service.print_content(args);
+ });
+
+ } else if (args.template) {
// fetch the template, then proceed to printing
return service.getPrintTemplate(args.template)
return service.print_content(args);
});
});
-
}
return service.print_content(args);
}
+ service.compileRemoteTemplate = function(templateName, templateData) {
+
+ var formData = new FormData();
+
+ formData.append('ses', egAuth.token());
+ formData.append('template_name', templateName);
+ formData.append('template_data', js2JSON(templateData));
+ formData.append('locale', OpenSRF.locale);
+
+ return new Promise((resolve, reject) => {
+ var xhttp = new XMLHttpRequest();
+ xhttp.onreadystatechange = function() {
+ if (this.readyState === 4) {
+ if (this.status === 200) {
+ resolve({
+ content: xhttp.responseText,
+ contentType: this.getResponseHeader('content-type')
+ });
+ } else {
+ reject('Error compiling print template');
+ }
+ }
+ };
+ xhttp.open('POST', service.server_template_path, true);
+ xhttp.send(formData);
+ });
+ }
+
// add commonly used attributes to the print scope
service.fleshPrintScope = function(scope) {
if (!scope) scope = {};