From 131d360c75e4b77ff658a98d75fab1bd3977a993 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 21 Apr 2017 11:25:28 +0200 Subject: [PATCH] Implemented issue #645 - Alternative pretty date format. --- .../app/lib/app_post/pretty_date.coffee | 27 ++++++++++- ...0170421000001_pretty_date_options_added.rb | 46 +++++++++++++++++++ db/seeds.rb | 29 +++++++++++- 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20170421000001_pretty_date_options_added.rb diff --git a/app/assets/javascripts/app/lib/app_post/pretty_date.coffee b/app/assets/javascripts/app/lib/app_post/pretty_date.coffee index e58dcca74..9c607eee0 100644 --- a/app/assets/javascripts/app/lib/app_post/pretty_date.coffee +++ b/app/assets/javascripts/app/lib/app_post/pretty_date.coffee @@ -1,7 +1,7 @@ class App.PrettyDate # human readable time - @humanTime: ( time, escalation, long = true ) -> + @humanTime: (time, escalation, long = true, type = undefined) -> return '' if !time current = new Date() created = new Date(time) @@ -29,6 +29,26 @@ class App.PrettyDate if diff < 60 return App.i18n.translateInline('just now') + if type is undefined && window.App && window.App.Config + type = window.App.Config.get('pretty_date_format') + if type is 'absolute' && direction is 'past' + weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] + weekday = weekdays[created.getDay()] + + months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] + month = months[created.getMonth()] + + # for less than 7 days + if diff < (60 * 60 * 24 * 7) + string = "#{App.i18n.translateInline(weekday)} #{created.getHours()}:#{@s(created.getMinutes(), 2)}" + else if diff < (60 * 60 * 24 * 7) * 365 + string = "#{App.i18n.translateInline(weekday)} #{created.getDate()}. #{App.i18n.translateInline(month)} #{created.getHours()}:#{@s(created.getMinutes(), 2)}" + else + string = "#{App.i18n.translateInline(weekday)} #{App.i18n.translateTimestamp(time)}" + if escalation + string = "#{string}" + return string + if direction is 'past' && !escalation && diff > ( 60 * 60 * 24 * 7 ) return App.i18n.translateDate(time) @@ -99,3 +119,8 @@ class App.PrettyDate if escalation string = "#{string}" return string + + @s: (num, digits) -> + while num.toString().length < digits + num = '0' + num + num \ No newline at end of file diff --git a/db/migrate/20170421000001_pretty_date_options_added.rb b/db/migrate/20170421000001_pretty_date_options_added.rb new file mode 100644 index 000000000..343def0f7 --- /dev/null +++ b/db/migrate/20170421000001_pretty_date_options_added.rb @@ -0,0 +1,46 @@ +class PrettyDateOptionsAdded < ActiveRecord::Migration + def up + + # return if it's a new setup + return if !Setting.find_by(name: 'system_init_done') + + Setting.create_or_update( + title: 'Pretty Date', + name: 'pretty_date_format', + area: 'System::Branding', + description: 'Defines pretty date format.', + options: { + form: [ + { + display: '', + null: false, + name: 'pretty_date_format', + tag: 'select', + options: { + 'relative': 'relative - e. g. "2 hours ago" or "2 days and 15 minutes ago"', + 'absolute': 'absolute - e. g. "Monday 09:30" or "Tuesday 23. Feb 14:20"', + }, + }, + ], + }, + preferences: { + render: true, + prio: 10, + permission: ['admin.branding'], + }, + state: 'relative', + frontend: true + ) + + Scheduler.create_or_update( + name: 'Import Jobs', + method: 'ImportJob.start_registered', + period: 1.hour, + prio: 1, + active: true, + updated_by_id: 1, + created_by_id: 1 + ) + end + +end diff --git a/db/seeds.rb b/db/seeds.rb index f762725ef..6b7546828 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -168,6 +168,33 @@ Setting.create_if_not_exists( }, frontend: true ) +Setting.create_or_update( + title: 'Pretty Date', + name: 'pretty_date_format', + area: 'System::Branding', + description: 'Defines pretty date format.', + options: { + form: [ + { + display: '', + null: false, + name: 'pretty_date_format', + tag: 'select', + options: { + 'relative': 'relative - e. g. "2 hours ago" or "2 days and 15 minutes ago"', + 'absolute': 'absolute - e. g. "Monday 09:30" or "Tuesday 23. Feb 14:20"', + }, + }, + ], + }, + preferences: { + render: true, + prio: 10, + permission: ['admin.branding'], + }, + state: 'relative', + frontend: true +) options = {} (10..99).each { |item| options[item] = item @@ -5553,7 +5580,7 @@ Scheduler.create_if_not_exists( ) Scheduler.create_if_not_exists( name: 'Import Jobs', - method: 'ImportJob.start', + method: 'ImportJob.start_registered', period: 1.hour, prio: 1, active: true,