LP#1705524: Override angular date filter
authorMike Rylander <mrylander@gmail.com>
Wed, 2 Aug 2017 20:56:20 +0000 (16:56 -0400)
committerBill Erickson <berickxx@gmail.com>
Fri, 11 Aug 2017 19:09:27 +0000 (15:09 -0400)
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

index 25f650b..9c4fc7e 100644 (file)
@@ -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;