From: Bill Erickson Date: Wed, 17 May 2017 20:34:43 +0000 (-0400) Subject: LP#1251394 Webstaff Display fields utility functions X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=1ffccff52c4a003c7d56c5e67477d173b6d3256f;p=Evergreen.git LP#1251394 Webstaff Display fields utility functions 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 Signed-off-by: Mike Rylander --- diff --git a/Open-ILS/web/js/ui/default/staff/cat/services/record.js b/Open-ILS/web/js/ui/default/staff/cat/services/record.js index 81a8cf7acd..8dbbef2d4d 100644 --- a/Open-ILS/web/js/ui/default/staff/cat/services/record.js +++ b/Open-ILS/web/js/ui/default/staff/cat/services/record.js @@ -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; +}])