webstaff: add support for per-template printer contexts
authorGalen Charlton <gmc@esilibrary.com>
Wed, 16 Nov 2016 06:53:45 +0000 (01:53 -0500)
committerKathy Lussier <klussier@masslnc.org>
Mon, 9 Jan 2017 15:58:57 +0000 (10:58 -0500)
Per-template printer contexts can now be set, imported, exported,
and passed along to Hatch during printing.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/src/templates/staff/admin/workstation/t_print_templates.tt2
Open-ILS/web/js/ui/default/staff/admin/workstation/app.js
Open-ILS/web/js/ui/default/staff/services/print.js

index 7448a46..1b09453 100644 (file)
@@ -9,23 +9,35 @@
 <h2>[% l('Print Templates') %]</h2>
 
 <div class="row">
-  <div class="col-md-2">[% l('Template Name') %]</div>
-  <div class="col-md-3">
-    <select class="form-control" ng-model="print.template_name" ng-change="template_changed()">
-      <option value="bills_current">[% l('Bills, Current') %]</option>
-      <option value="bills_historical">[% l('Bills, Historical') %]</option>
-      <option value="bill_payment">[% l('Bills, Payment') %]</option>
-      <option value="checkout">[% l('Checkout') %]</option>
-      <option value="hold_transit_slip">[% l('Hold Transit Slip') %]</option>
-      <option value="hold_shelf_slip">[% l('Hold Shelf Slip') %]</option>
-      <option value="holds_for_bib">[% l('Holds for Bib Record') %]</option>
-      <option value="holds_for_patron">[% l('Holds for Patron') %]</option>
-      <option value="hold_pull_list">[% l('Hold Pull List') %]</option>
-      <option value="hold_shelf_list">[% l('Hold Shelf List') %]</option>
-      <option value="patron_address">[% l('Patron Address') %]</option>
-      <option value="patron_note">[% l('Patron Note') %]</option>
-      <option value="transit_slip">[% l('Transit Slip') %]</option>
-    </select>
+  <div class="col-md-5">
+    <div class="form-inline">
+      <div class="form-group">
+        <label for="print_tempate_name">[% l('Template Name') %]</label>
+        <select id="print_template_name" class="form-control" ng-model="print.template_name" ng-change="template_changed()">
+          <option value="bills_current">[% l('Bills, Current') %]</option>
+          <option value="bills_historical">[% l('Bills, Historical') %]</option>
+          <option value="bill_payment">[% l('Bills, Payment') %]</option>
+          <option value="checkout">[% l('Checkout') %]</option>
+          <option value="hold_transit_slip">[% l('Hold Transit Slip') %]</option>
+          <option value="hold_shelf_slip">[% l('Hold Shelf Slip') %]</option>
+          <option value="holds_for_bib">[% l('Holds for Bib Record') %]</option>
+          <option value="holds_for_patron">[% l('Holds for Patron') %]</option>
+          <option value="hold_pull_list">[% l('Hold Pull List') %]</option>
+          <option value="hold_shelf_list">[% l('Hold Shelf List') %]</option>
+          <option value="patron_address">[% l('Patron Address') %]</option>
+          <option value="patron_note">[% l('Patron Note') %]</option>
+          <option value="transit_slip">[% l('Transit Slip') %]</option>
+        </select>
+        <label for="print_context">[% l('Force Printer Context') %]</label>
+        <select class="form-control" ng-model="print.template_context">
+          <option value="default">[% l('Default') %]</option>
+          <option value="receipt">[% l('Receipt') %]</option>
+          <option value="label">[% l('Label') %]</option>
+          <option value="mail">[% l('Mail') %]</option>
+          <option value="offline">[% l('Offline') %]</option>
+        </select>
+      </div>
+    </div>
   </div>
   <div class="col-md-7">
     <button class="btn btn-default pull-left" ng-click="save_locally()">[% l('Save Locally') %]</button>
index dd51bc8..de37438 100644 (file)
@@ -368,7 +368,8 @@ function($scope , $q , egCore , ngToast) {
 
     $scope.print = {
         template_name : 'bills_current',
-        template_output : ''
+        template_output : '',
+        template_context : 'default'
     };
 
     // print preview scope data
@@ -508,6 +509,10 @@ function($scope , $q , egCore , ngToast) {
                 $scope.print.load_failed = true;
             }
         );
+        egCore.print.getPrintTemplateContext($scope.print.template_name)
+        .then(function(template_context) {
+            $scope.print.template_context = template_context;
+        });
     }
 
     $scope.save_locally = function() {
@@ -515,21 +520,35 @@ function($scope , $q , egCore , ngToast) {
             $scope.print.template_name,
             $scope.print.template_content
         );
+        egCore.print.storePrintTemplateContext(
+            $scope.print.template_name,
+            $scope.print.template_context
+        );
     }
 
     $scope.exportable_templates = function() {
         var templates = {};
+        var contexts = {};
         var deferred = $q.defer();
         var promises = [];
-        egCore.hatch.getKeys('eg.print.template.').then(function(keys) {
+        egCore.hatch.getKeys('eg.print.template').then(function(keys) {
             angular.forEach(keys, function(key) {
-                promises.push(egCore.hatch.getItem(key).then(function(value) {
-                    templates[key.replace('eg.print.template.', '')] = value;
-                }));
+                if (key.match(/^eg\.print\.template\./)) {
+                    promises.push(egCore.hatch.getItem(key).then(function(value) {
+                        templates[key.replace('eg.print.template.', '')] = value;
+                    }));
+                } else {
+                    promises.push(egCore.hatch.getItem(key).then(function(value) {
+                        contexts[key.replace('eg.print.template_context.', '')] = value;
+                    }));
+                }
             });
             $q.all(promises).then(function() {
                 if (Object.keys(templates).length) {
-                    deferred.resolve(templates);
+                    deferred.resolve({
+                        templates: templates,
+                        contexts: contexts
+                    });
                 } else {
                     ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_EXPORT);
                     deferred.reject();
@@ -543,10 +562,13 @@ function($scope , $q , egCore , ngToast) {
     $scope.$watch('imported_print_templates.data', function(newVal, oldVal) {
         if (newVal && newVal != oldVal) {
             try {
-                var templates = JSON.parse(newVal);
-                angular.forEach(templates, function(template_content, template_name) {
+                var data = JSON.parse(newVal);
+                angular.forEach(data.templates, function(template_content, template_name) {
                     egCore.print.storePrintTemplate(template_name, template_content);
                 });
+                angular.forEach(data.contexts, function(template_context, template_name) {
+                    egCore.print.storePrintTemplateContext(template_name, template_context);
+                });
                 $scope.template_changed(); // refresh
                 ngToast.create(egCore.strings.PRINT_TEMPLATES_SUCCESS_IMPORT);
             } catch (E) {
index 30a1751..2c90818 100644 (file)
@@ -1,7 +1,6 @@
 /**
  * egPrint : manage print templates, process templates, print content
  *
- * TODO: create configurable links between print template and context.
  */
 angular.module('egCoreMod')
 
@@ -34,7 +33,11 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
             .then(function(content) {
                 args.content = content;
                 if (!args.content_type) args.content_type = 'html';
-                return service.print_content(args);
+                service.getPrintTemplateContext(args.template)
+                .then(function(context) {
+                    args.context = context;
+                    return service.print_content(args);
+                });
             });
 
         } 
@@ -69,7 +72,6 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
             promise = $q.when(args.content);
         }
 
-        // TODO: link print context to template type
         var context = args.context || 'default';
 
         return promise.then(function(html) {
@@ -136,6 +138,21 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
         return egHatch.setItem('eg.print.template.' + name, html);
     }
 
+    service.getPrintTemplateContext = function(name) {
+        var deferred = $q.defer();
+
+        egHatch.getItem('eg.print.template_context.' + name)
+        .then(
+            function(context) { deferred.resolve(context); },
+            function()        { deferred.resolve('default'); }
+        );
+
+        return deferred.promise;
+    }
+    service.storePrintTemplateContext = function(name, context) {
+        return egHatch.setItem('eg.print.template_context.' + name, context);
+    }
+
     return service;
 }])