LP1825851 AngJS tries server-managed templates first
authorBill Erickson <berickxx@gmail.com>
Fri, 24 May 2019 16:29:31 +0000 (12:29 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 10 Jul 2019 20:05:19 +0000 (16:05 -0400)
Print requests in the AngJS app now attempt to generate template content
via the server by default, falling back to traditional TT2 templates as
needed.  When a template is known to be not server managed, future
requests in the same AngJS app for the template will bypass the
server-managed template attempt.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/web/js/ui/default/staff/services/print.js

index 1dd163d..3c3d9b9 100644 (file)
@@ -22,6 +22,10 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
     service.template_base_path = 'share/print_templates/t_';
     service.server_template_path = '/print_template';
 
+    // Cache of template names that are known to be generated from
+    // traditional TT2 files instead of being server-manged.
+    service.localTemplates = {};
+
     /*
      * context  : 'default', 'receipt','label', etc. 
      * scope    : data loaded into the template environment
@@ -77,7 +81,14 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
                             content: xhttp.responseText,
                             contentType: this.getResponseHeader('content-type')
                         });
+
+                    } else if (this.status === 404) {
+                        console.debug('Template ' + templateName + ' is not '
+                            + 'hosted on the server; using local template');
+                        reject();
+
                     } else {
+                        // other error
                         reject('Error compiling print template');
                     }
                 }
@@ -238,6 +249,24 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
         });
     }
 
+
+    service.getTt2PrintTemplate = function(name) {
+        var path = service.template_base_path + name;
+        console.debug('fetching TT2 print template: ' + path);
+
+        return $http.get(path).then(
+            function(data) { 
+                console.debug('Found server template file for ' + name);
+                service.localTemplates[name] = true;
+                return data.data;
+            },
+            function() {
+                console.error('unable to locate print template: ' + name);
+                return $q.reject();
+            }
+        );
+    }
+
     // loads an HTML print template by name from the server If no
     // template is available in local/hatch storage, fetch the template
     // as an HTML file from the server. if no HTML file is available,
@@ -256,38 +285,26 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
                 return;
             }
 
-            var path = service.template_base_path + name;
-            console.debug('fetching template ' + path);
-
-            $http.get(path).then(
-                function(data) { 
-
-                    if (data.data.match(/Print Template Not Found/)) {
-
-                        // AngJS templates return a dummy template w/ the
-                        // above text if the template is not found instead
-                        // of a 404.
-                        return service.compileRemoteTemplate(name, args.scope)
-                        .then(
-                            function(response) {
-                                console.debug('Found server-hosted template for ' + name);
-                                args.content_type = response.contentType;
-                                args.content = response.content;
-                                deferred.resolve(args.content);
-                            },
-                            function() {
-                                console.error('unable to locate print template: ' + name);
-                                deferred.reject();
-                            }
-                        );
-                    }
+            if (service.localTemplates[name]) {
+                service.getTt2PrintTemplate(name)
+                .then(deferred.resolve, deferred.reject);
+                return;
+            }
 
-                    console.debug('Found server template file for ' + name);
-                    deferred.resolve(data.data) 
+            // Template may be TT2 or server-managed.
+            // Try server-managed first, then fall back to TT2.
+            return service.compileRemoteTemplate(name, args.scope)
+            .then(
+                function(response) {
+                    console.debug('Found server-hosted template for ' + name);
+                    args.content_type = response.contentType;
+                    args.content = response.content;
+                    deferred.resolve(args.content);
                 },
                 function() {
-                    console.error('unable to locate print template: ' + name);
-                    deferred.reject();
+                    console.debug('Template ' + name + ' is not server-managed');
+                    service.getTt2PrintTemplate(name)
+                    .then(deferred.resolve, deferred.reject);
                 }
             );
         });