Browse Source

Follow up for issue #2324 - improved comments and test - Improved error handling of invalid tag for text modules and add current time format (to not use 2018-10-31T08:02:21.917Z format).

Martin Edenhofer 6 years ago
parent
commit
41ef5fa789

+ 10 - 9
app/assets/javascripts/app/lib/app_post/i18n.coffee

@@ -343,11 +343,12 @@ class _i18nSingleton extends Spine.Module
     return time if !time
     @convert(time, offset, @mapTime['timestamp'] || @timestampFormat)
 
+  formatNumber: (num, digits) ->
+    while num.toString().length < digits
+      num = '0' + num
+    num
+
   convert: (time, offset, format) ->
-    s = (num, digits) ->
-      while num.toString().length < digits
-        num = '0' + num
-      num
 
     timeObject = new Date(time)
 
@@ -363,13 +364,13 @@ class _i18nSingleton extends Spine.Module
     M      = timeObject.getMinutes()
     H      = timeObject.getHours()
     format = format
-      .replace(/dd/, s(d, 2))
+      .replace(/dd/, @formatNumber(d, 2))
       .replace(/d/, d)
-      .replace(/mm/, s(m, 2))
+      .replace(/mm/, @formatNumber(m, 2))
       .replace(/m/, m)
       .replace(/yyyy/, yfull)
       .replace(/yy/, yshort)
-      .replace(/SS/, s(S, 2))
-      .replace(/MM/, s(M, 2))
-      .replace(/HH/, s(H, 2))
+      .replace(/SS/, @formatNumber(S, 2))
+      .replace(/MM/, @formatNumber(M, 2))
+      .replace(/HH/, @formatNumber(H, 2))
     format

+ 13 - 6
app/assets/javascripts/app/lib/app_post/utils.coffee

@@ -718,25 +718,32 @@ class App.Utils
           dataRef = ''
           break
       value = undefined
+
+      # if value is a function, execute function
       if typeof dataRef is 'function'
         value = dataRef()
+
+      # if value has content
       else if dataRef isnt undefined && dataRef isnt null && dataRef.toString
-        if dataRefLast && dataRefLast.constructor && dataRefLast.constructor.className
+
+        # in case if we have a references object, check what datatype the attribute has
+        # and e. g. convert timestamps/dates to browser locale
+        if dataRefLast?.constructor?.className
           localClassRef = App[dataRefLast.constructor.className]
-          if localClassRef && localClassRef.attributesGet
+          if localClassRef?.attributesGet
             attributes = localClassRef.attributesGet()
-            if attributes && attributes[level]
+            if attributes?[level]
               if attributes[level]['tag'] is 'datetime'
                 value = App.i18n.translateTimestamp(dataRef)
               else if attributes[level]['tag'] is 'date'
                 value = App.i18n.translateDate(dataRef)
+
+        # as fallback use value of toString()
         if !value
           value = dataRef.toString()
       else
         value = ''
-      #console.log( "tag replacement #{key}, #{value} env: ", objects)
-      if value is ''
-        value = '-'
+      value = '-' if value is ''
       value
     )
 

+ 16 - 1
public/assets/tests/html_utils.js

@@ -1285,6 +1285,21 @@ test("check check attachment reference", function() {
 
 // replace tags
 test("check replace tags", function() {
+  var formatNumber = function(num, digits) {
+    while (num.toString().length < digits) {
+      num = '0' + num
+    }
+    return num
+  }
+  var formatTimestamp = function(timestamp) {
+    localTime = new Date(Date.parse(timestamp))
+    d         = formatNumber(localTime.getDate(), 2)
+    m         = formatNumber(localTime.getMonth() + 1, 2)
+    yfull     = localTime.getFullYear()
+    M         = formatNumber(localTime.getMinutes(), 2)
+    H         = formatNumber(localTime.getHours(), 2)
+    return m + '/' + d + '/' + yfull + ' ' + H + ':' + M
+  }
 
   var message = "<div>#{user.firstname} #{user.lastname}</div>"
   var result  = '<div>Bob Smith</div>'
@@ -1399,7 +1414,7 @@ test("check replace tags", function() {
     created_at: '2018-10-31T10:00:00Z',
   })
   message = "<div>#{user.firstname} #{user.created_at}</div>"
-  result  = '<div>Bob 10/31/2018 10:00</div>'
+  result  = '<div>Bob ' + formatTimestamp('2018-10-31T10:00:00Z') + '</div>'
   data    = {
     user: user
   }