From 41ef5fa7898ce3d334c2e59ea2dc90da11e625c8 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 1 Nov 2018 07:33:12 +0100 Subject: [PATCH] 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). --- .../javascripts/app/lib/app_post/i18n.coffee | 19 ++++++++++--------- .../javascripts/app/lib/app_post/utils.coffee | 19 +++++++++++++------ public/assets/tests/html_utils.js | 17 ++++++++++++++++- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/app/lib/app_post/i18n.coffee b/app/assets/javascripts/app/lib/app_post/i18n.coffee index b8c385409..2cbdec418 100644 --- a/app/assets/javascripts/app/lib/app_post/i18n.coffee +++ b/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 diff --git a/app/assets/javascripts/app/lib/app_post/utils.coffee b/app/assets/javascripts/app/lib/app_post/utils.coffee index 2288bd41e..bc1206760 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.coffee +++ b/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 ) diff --git a/public/assets/tests/html_utils.js b/public/assets/tests/html_utils.js index f58d85797..12afc4f18 100644 --- a/public/assets/tests/html_utils.js +++ b/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 = "
#{user.firstname} #{user.lastname}
" var result = '
Bob Smith
' @@ -1399,7 +1414,7 @@ test("check replace tags", function() { created_at: '2018-10-31T10:00:00Z', }) message = "
#{user.firstname} #{user.created_at}
" - result = '
Bob 10/31/2018 10:00
' + result = '
Bob ' + formatTimestamp('2018-10-31T10:00:00Z') + '
' data = { user: user }