From d58bbd561aef5b2b48f28b2c84423dd233c78264 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 26 Oct 2017 15:10:17 -0400 Subject: [PATCH] LP#1727487 Webstaff display fields collapseMulti option Support an option in the webstaff-side display field munging code to collapse array/multi values down to a single comma-separated string. This is useful for buidling displays (grids especially) where you have a single spot to put a field's value (e.g. a list of ISBN's) and don't want to munge the data by hand in each UI. Signed-off-by: Bill Erickson Signed-off-by: Kathy Lussier Signed-off-by: Dan Wells --- .../ui/default/staff/cat/services/record.js | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) 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 7aa25e13c2..ba66c0047e 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 @@ -280,11 +280,19 @@ angular.module('egCoreMod') /** * Converts JSON-encoded values within a mwde object to Javascript * native strings, numbers, and arrays. + * + * @collapseMulti collapse array (multi) fields down to a single string + * with values separated by a comma+space. Useful for quickly + * building displays (e.g. grids) without having to first munge + * the array into a string. */ - service.mwdeJSONToJS = function(entry) { + service.mwdeJSONToJS = function(entry, collapseMulti) { angular.forEach(egCore.idl.classes.mwde.fields, function(f) { if (f.virtual) return; - entry[f.name](JSON.parse(entry[f.name]())); + var val = JSON.parse(entry[f.name]()); + if (collapseMulti && angular.isArray(val)) + val = val.join(', '); + entry[f.name](val); }); } @@ -292,9 +300,11 @@ angular.module('egCoreMod') * 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. + * + * @collapseMulti See service.mwdeJSONToJS() */ - service.mfdeToHash = function(entries) { - var hash = service.mfdeToMetaHash(entries); + service.mfdeToHash = function(entries, collapseMulti) { + var hash = service.mfdeToMetaHash(entries, collapseMulti); angular.forEach(hash, function(sub_hash, name) { hash[name] = sub_hash.value }); return hash; @@ -305,8 +315,10 @@ angular.module('egCoreMod') * {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 + * + * @collapseMulti See service.mwdeJSONToJS() */ - service.mfdeToMetaHash = function(entries) { + service.mfdeToMetaHash = function(entries, collapseMulti) { var hash = {}; angular.forEach(entries, function(entry) { @@ -320,7 +332,17 @@ angular.module('egCoreMod') } if (entry.multi() == 't') { - hash[entry.name()].value.push(entry.value()); + if (collapseMulti) { + if (angular.isArray(hash[entry.name()].value)) { + // new collapsed string + hash[entry.name()].value = entry.value(); + } else { + // append to collapsed string + hash[entry.name()].value += ', ' + entry.value(); + } + } else { + hash[entry.name()].value.push(entry.value()); + } } else { hash[entry.name()].value = entry.value(); } -- 2.43.2