webstaff: add import and export of print templates
authorGalen Charlton <gmc@esilibrary.com>
Wed, 16 Nov 2016 02:36:15 +0000 (21:36 -0500)
committerKathy Lussier <klussier@masslnc.org>
Tue, 22 Nov 2016 19:10:06 +0000 (14:10 -0500)
The workstation print template page now has Import
and Export buttons to import or export any customized
templates from/to JSON files. Toasts are emitted
on import success or failure and export failure (if
there are no customized templates to export).

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

index a0f53d2..2d8b357 100644 (file)
@@ -9,6 +9,7 @@
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/grid.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/ui.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/user.js"></script>
+<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/file.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/admin/workstation/app.js"></script>
 <script>
 angular.module('egCoreMod').run(['egStrings', function(s) {
@@ -17,6 +18,9 @@ angular.module('egCoreMod').run(['egStrings', function(s) {
   s.DEFAULT_WS_LABEL = '[% l('[_1] (Default)', '{{ws}}') %]';
   s.WS_EXISTS = '[% l("Workstation name already exists.  Use it anyway?") %]';
   s.WS_USED = '[% l("Workstation is already registered") %]';
+  s.PRINT_TEMPLATES_FAIL_EXPORT = "[% l('There are no customized print template to export') %]";
+  s.PRINT_TEMPLATES_SUCCESS_IMPORT = "[% l('Imported one or more print template(s)') %]";
+  s.PRINT_TEMPLATES_FAIL_IMPORT = "[% l('Failed to import any print template(s)') %]";
 }]);
 </script>
 [% END %]
index 1d6ab02..7448a46 100644 (file)
     </select>
   </div>
   <div class="col-md-7">
-    <div class="pull-right">
-      <button class="btn btn-default" ng-click="save_locally()">[% l('Save Locally') %]</button>
+    <button class="btn btn-default pull-left" ng-click="save_locally()">[% l('Save Locally') %]</button>
+    <div class="btn-group pull-right">
+      <span class="btn btn-default btn-file">
+        [% l('Import') %]
+        <input type="file" eg-file-reader container="imported_print_templates.data">
+      </span>
+      <label class="btn btn-default"
+          eg-json-exporter generator="exportable_templates"
+          default-file-name="'[% l('print_templates.json') %]'">
+          [% l('Export Customized Templates') %]
+      </label>
     </div>
   </div>
   <!-- other stuff -->
index 750ed17..dd51bc8 100644 (file)
@@ -47,6 +47,13 @@ angular.module('egWorkstationAdmin',
     });
 }])
 
+.config(['ngToastProvider', function(ngToastProvider) {
+  ngToastProvider.configure({
+    verticalPosition: 'bottom',
+    animation: 'fade'
+  });
+}])
+
 .factory('workstationSvc',
        ['$q','$timeout','$location','egCore','egConfirmDialog',
 function($q , $timeout , $location , egCore , egConfirmDialog) {
@@ -356,8 +363,8 @@ function($scope , egCore) {
 }])
 
 .controller('PrintTemplatesCtrl',
-       ['$scope','$q','egCore',
-function($scope , $q , egCore) {
+       ['$scope','$q','egCore','ngToast',
+function($scope , $q , egCore , ngToast) {
 
     $scope.print = {
         template_name : 'bills_current',
@@ -510,6 +517,44 @@ function($scope , $q , egCore) {
         );
     }
 
+    $scope.exportable_templates = function() {
+        var templates = {};
+        var deferred = $q.defer();
+        var promises = [];
+        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;
+                }));
+            });
+            $q.all(promises).then(function() {
+                if (Object.keys(templates).length) {
+                    deferred.resolve(templates);
+                } else {
+                    ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_EXPORT);
+                    deferred.reject();
+                }
+            });
+        });
+        return deferred.promise;
+    }
+
+    $scope.imported_print_templates = { data : '' };
+    $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) {
+                    egCore.print.storePrintTemplate(template_name, template_content);
+                });
+                $scope.template_changed(); // refresh
+                ngToast.create(egCore.strings.PRINT_TEMPLATES_SUCCESS_IMPORT);
+            } catch (E) {
+                ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_IMPORT);
+            }
+        }
+    });
+
     $scope.template_changed(); // load the default
 }])