LP#1251394 Webstaff Display fields utility functions user/berick/lp1251394-metabib-display-fields-rebase-circa-2.12
authorBill Erickson <berickxx@gmail.com>
Wed, 17 May 2017 20:34:43 +0000 (16:34 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 17 May 2017 20:34:46 +0000 (16:34 -0400)
Adds functions for translating bib record display field data into a
variety of useful formats and structures.  Some are best suited for
grids, others for lists of values, etc.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/web/js/ui/default/staff/cat/services/record.js
Open-ILS/web/js/ui/default/staff/circ/patron/items_out.js

index 81a8cf7..8dbbef2 100644 (file)
@@ -223,3 +223,70 @@ angular.module('egCoreMod')
         ]
     }
 })
+
+/**
+ * Utility functions for translating bib record display fields into
+ * various formats / structures.
+ *
+ * Note that 'mwde' objects (which are proper IDL objects) only contain
+ * the prescribed fields from the IDL (and database view), while the
+ * 'mfde' hash-based objects contain all configured display fields,
+ * including local/custom fields.
+ */
+.factory('egBibDisplay', ['$q', 'egCore', function($q, egCore) {
+    var service = {};
+
+    /**
+     * Converts JSON-encoded values within a mwde object to Javascript
+     * native strings, numbers, and arrays.
+     */
+    service.mwdeJSONToJS = function(entry) {
+        angular.forEach(egCore.idl.classes.mwde.fields, function(f) {
+            if (f.virtual) return;
+            entry[f.name](JSON.parse(entry[f.name]()));
+        });
+    }
+
+    /**
+     * Converts a list of 'mfde' entry objects to a simple key=>value hash.
+     * Non-multi values are strings or numbers.
+     * Multi values are arrays of strings or numbers.
+     */
+    service.mfdeToHash = function(entries) {
+        var hash = service.mfdeToMetaHash(entries);
+        angular.forEach(hash, 
+            function(sub_hash, name) { hash[name] = sub_hash.value });
+        return hash;
+    }
+
+    /**
+     * Converts a list of 'mfde' entry objects to a nested hash like so:
+     * {name => field_name, label => field_label, value => scalar_or_array}
+     * The scalar_or_array value is a string/number or an array of
+     * string/numbers
+     */
+    service.mfdeToMetaHash = function(entries) {
+        var hash = {};
+        angular.forEach(entries, function(entry) {
+
+            if (!hash[entry.name()]) {
+                hash[entry.name()] = {
+                    name : entry.name(),
+                    label : entry.label(),
+                    multi : entry.multi() == 't',
+                    value : entry.multi() == 't' ? [] : null
+                }
+            }
+
+            if (entry.multi() == 't') {
+                hash[entry.name()].value.push(entry.value());
+            } else {
+                hash[entry.name()].value = entry.value();
+            }
+        });
+
+        return hash;
+    }
+
+    return service;
+}])
index ebba762..55825be 100644 (file)
@@ -7,8 +7,10 @@ angular.module('egPatronApp')
 .controller('PatronItemsOutCtrl',
        ['$scope','$q','$routeParams','$timeout','egCore','egUser','patronSvc','$location',
         'egGridDataProvider','$uibModal','egCirc','egConfirmDialog','egBilling','$window',
+        'egBibDisplay',
 function($scope,  $q,  $routeParams,  $timeout,  egCore , egUser,  patronSvc , $location, 
-         egGridDataProvider , $uibModal , egCirc , egConfirmDialog , egBilling , $window) {
+         egGridDataProvider , $uibModal , egCirc , egConfirmDialog , egBilling , $window,
+         egBibDisplay) {
 
     // list of noncatatloged circulations. Define before initTab to 
     // avoid any possibility of race condition, since they are loaded
@@ -114,13 +116,8 @@ function($scope,  $q,  $routeParams,  $timeout,  egCore , egUser,  patronSvc , $
         }).then(null, null, function(circ) {
             circ.circ_lib(egCore.org.get(circ.circ_lib())); // local fleshing
 
-            // TODO: move to shared service
-            // Un-JSON-ify display field data
-            var wde = circ.target_copy().call_number().record().wide_display_entry();
-            angular.forEach(egCore.idl.classes.mwde.fields, function(f) {
-                if (f.virtual) return;
-                wde[f.name](JSON.parse(wde[f.name]()));
-            });
+            egBibDisplay.mwdeJSONToJS(
+                circ.target_copy().call_number().record().wide_display_entry());
 
             if (circ.target_copy().call_number().id() == -1) {
                 // dummy-up a record for precat items