From: Mike Rylander Date: Wed, 25 Apr 2018 18:57:56 +0000 (-0400) Subject: LP#1746824: Provide CSV download titlecasing X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=829777c36432279802da3ac28ab6562020e1a565;p=working%2FEvergreen.git LP#1746824: Provide CSV download titlecasing The CSV download available from the grid cannot apply CSS to column content. To address that, we add a titlecase filter and make use of it during CSV creation. Signed-off-by: Mike Rylander --- diff --git a/Open-ILS/src/templates/staff/cat/item/t_list.tt2 b/Open-ILS/src/templates/staff/cat/item/t_list.tt2 index eb584c95e5..5dc4ecd610 100644 --- a/Open-ILS/src/templates/staff/cat/item/t_list.tt2 +++ b/Open-ILS/src/templates/staff/cat/item/t_list.tt2 @@ -69,7 +69,7 @@ + path="call_number.record.simple_record.title" titlecase visible> {{item['call_number.record.simple_record.title']}} @@ -78,7 +78,7 @@ - + diff --git a/Open-ILS/web/js/ui/default/staff/services/grid.js b/Open-ILS/web/js/ui/default/staff/services/grid.js index eb23385440..090ab3713e 100644 --- a/Open-ILS/web/js/ui/default/staff/services/grid.js +++ b/Open-ILS/web/js/ui/default/staff/services/grid.js @@ -952,10 +952,11 @@ angular.module('egGridMod', // prepares a string for inclusion within a CSV document // by escaping commas and quotes and removing newlines. - grid.csvDatum = function(str) { + grid.csvDatum = function(str,col) { str = ''+str; if (!str) return ''; str = str.replace(/\n/g, ''); + if (col && col.titlecase) str = $filter('titlecase')(str); if (str.match(/\,/) || str.match(/"/)) { str = str.replace(/"/g, '""'); str = '"' + str + '"'; @@ -1151,7 +1152,7 @@ angular.module('egGridMod', // items angular.forEach(items, function(item) { angular.forEach(columns, function(col) { - csvStr += grid.csvDatum(item[col.name]); + csvStr += grid.csvDatum(item[col.name],col); csvStr += ','; }); csvStr = csvStr.replace(/,$/,'\n'); @@ -1279,6 +1280,7 @@ angular.module('egGridMod', // boolean fields are presented as value-less attributes angular.forEach( [ + 'titlecase', 'visible', 'hidden', 'sortable', @@ -1557,6 +1559,7 @@ angular.module('egGridMod', linkpath : colSpec.linkpath, template : colSpec.template, visible : colSpec.visible, + titlecase: colSpec.titlecase, hidden : colSpec.hidden, datatype : colSpec.datatype, sortable : colSpec.sortable, @@ -2103,6 +2106,8 @@ angular.module('egGridMod', * 1. Passes dates through the angular date filter * 2. Translates bools to Booleans so the browser can display translated * value. (Though we could manually translate instead..) + * 3. Formats currency + * 4. Could (but doesn't yet) apply titlecasing when requested * Others likely to follow... */ .filter('egGridValueFilter', ['$filter','egCore', function($filter,egCore) { @@ -2152,6 +2157,8 @@ angular.module('egGridMod', case 'money': return $filter('currency')(value); default: + // Alternative to CSS titlecasing... + // if (column.titlecase) value = $filter('titlecase')(value); return value; } }; diff --git a/Open-ILS/web/js/ui/default/staff/services/ui.js b/Open-ILS/web/js/ui/default/staff/services/ui.js index 47632ed696..d6d6f7c166 100644 --- a/Open-ILS/web/js/ui/default/staff/services/ui.js +++ b/Open-ILS/web/js/ui/default/staff/services/ui.js @@ -137,6 +137,35 @@ function($timeout , $parse) { }; }) +// 'titlecase' filter +// Lowercases input, then upercases the first character of each word, excepting +// those that match the smallWords regex. +// https://gist.github.com/jeffjohnson9046/9789876 (original) +// Modified by mrylander@gmail.com +.filter('titlecase', function() { + return function (input) { + if (input === undefined) return null; + + var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i; + + input = input.toLowerCase(); + return input.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title) { + if (index > 0 && index + match.length !== title.length && + match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" && + (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') && + title.charAt(index - 1).search(/[^\s-]/) < 0) { + return match.toLowerCase(); + } + + if (match.substr(1).search(/[A-Z]|\../) > -1) { + return match; + } + + return match.charAt(0).toUpperCase() + match.substr(1); + }); + } +}) + // 'reverse' filter //
{{item.name}}
// http://stackoverflow.com/questions/15266671/angular-ng-repeat-in-reverse