From e6c7aa0980416931b03d163a39d8c40274bcce38 Mon Sep 17 00:00:00 2001
From: Mike Rylander <mrylander@gmail.com>
Date: Wed, 2 Aug 2017 16:56:20 -0400
Subject: [PATCH] LP#1705524: Override angular date filter

Here we'll use moment.js to format all dates that want to use the angular date
filter, for consistency and standards compliance.  The primary benefit is the
ability to use a proper timezone (region) rather than just a simple GMT offset.

Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Bill Erickson <berickxx@gmail.com>
---
 Open-ILS/web/js/ui/default/staff/services/ui.js | 63 +++++++++++++------------
 1 file changed, 34 insertions(+), 29 deletions(-)

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 25f650b023..9c4fc7ef1b 100644
--- a/Open-ILS/web/js/ui/default/staff/services/ui.js
+++ b/Open-ILS/web/js/ui/default/staff/services/ui.js
@@ -84,11 +84,10 @@ function($timeout , $parse) {
     };
 })
 
-// 'egOrgDate' filter
-// Uses moment.js and moment-timezone.js to put dates into the most appropriate
-// timezone for a given (optional) org unit based on its lib.timezone setting
-.filter('egOrgDate',['egCore',
-             function(egCore) {
+// 'date' filter
+// Overriding the core angular date filter with a moment-js based one for
+// better timezone and formatting support.
+.filter('date',function() {
 
     var formatMap = {
         short  : 'l LT',
@@ -119,15 +118,31 @@ function($timeout , $parse) {
         [ /Z/g,    'ZZ'   ]
     ];
 
-    var tzcache = {'*':null};
+    return function (date, format, tz) {
+        if (format) {
+            var fmt = formatMap[format] || format;
+            angular.forEach(formatReplace, function (r) {
+                fmt = fmt.replace(r[0],r[1]);
+            });
+        }
 
-    function eg_date_filter (date, format, ouID) {
-        var fmt = formatMap[format] || format;
-        angular.forEach(formatReplace, function (r) {
-            fmt = fmt.replace(r[0],r[1]);
-        });
+        var d = moment(date);
+        if (tz && tz !== '-') d.tz(tz);
 
-        var d;
+        return d.isValid() ? d.format(fmt) : '';
+    }
+
+})
+
+// 'egOrgDate' filter
+// Uses moment.js and moment-timezone.js to put dates into the most appropriate
+// timezone for a given (optional) org unit based on its lib.timezone setting
+.filter('egOrgDate',['$filter','egCore',
+             function($filter , egCore) {
+
+    var tzcache = {};
+
+    function eg_date_filter (date, fmt, ouID) {
         if (ouID) {
             if (angular.isObject(ouID)) {
                 if (angular.isFunction(ouID.id)) {
@@ -137,26 +152,16 @@ function($timeout , $parse) {
                 }
             }
     
-            if (tzcache[ouID] && tzcache[ouID] !== '-') {
-                d = moment(date).tz(tzcache[ouID]);
-            } else {
-    
-                if (!tzcache[ouID]) {
-                    tzcache[ouID] = '-';
-
-                    egCore.org.settings('lib.timezone', ouID)
-                    .then(function(s) {
-                        tzcache[ouID] = s['lib.timezone'] || OpenSRF.tz;
-                    });
-                }
-
-                d = moment(date);
+            if (!tzcache[ouID]) {
+                tzcache[ouID] = '-';
+                egCore.org.settings('lib.timezone', ouID)
+                .then(function(s) {
+                    tzcache[ouID] = s['lib.timezone'] || OpenSRF.tz;
+                });
             }
-        } else {
-            d = moment(date);
         }
 
-        return d.isValid() ? d.format(fmt) : '';
+        return $filter('date')(date, fmt, tzcache[ouID]);
     }
 
     eg_date_filter.$stateful = true;
-- 
2.11.0