From 0a96e8b4216b67acfadd09901e4354b203345886 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 28 Jul 2014 00:24:42 +0200 Subject: [PATCH] Improved pretty time. --- .../_application_controller.js.coffee | 87 +---------------- .../app/lib/app_post/pretty_date.js.coffee | 94 +++++++++++++++++++ app/controllers/tests_controller.rb | 7 ++ app/views/tests/form.html.erb | 2 - app/views/tests/ui.html.erb | 15 +++ config/routes/test.rb | 1 + db/seeds.rb | 4 +- public/assets/tests/ui.js | 76 +++++++++++++++ test/browser/aab_unit_test.rb | 22 +++++ 9 files changed, 221 insertions(+), 87 deletions(-) create mode 100644 app/assets/javascripts/app/lib/app_post/pretty_date.js.coffee create mode 100644 app/views/tests/ui.html.erb create mode 100644 public/assets/tests/ui.js diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index 106ced9c9..5ebdb6fb6 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -151,32 +151,6 @@ class App.Controller extends Spine.Controller formValidate: (data) -> App.ControllerForm.validate(data) - ticketTableAttributes: (attributes) => - all_attributes = [ - { name: 'number', type: 'link', title: 'title', dataType: 'edit' }, - { name: 'title', type: 'link', title: 'title', dataType: 'edit' }, - { name: 'customer', class: 'user-popover', data: { id: true } }, - { name: 'state', translate: true, title: true }, - { name: 'priority', translate: true, title: true }, - { name: 'group', title: 'group' }, - { name: 'owner', class: 'user-popover', data: { id: true } }, - { name: 'created_at', callback: @frontendTime }, - { name: 'last_contact', callback: @frontendTime }, - { name: 'last_contact_agent', callback: @frontendTime }, - { name: 'last_contact_customer', callback: @frontendTime }, - { name: 'first_response', callback: @frontendTime }, - { name: 'close_time', callback: @frontendTime }, - { name: 'escalation_time', callback: @frontendTime, subclass: 'escalation' }, - { name: 'article_count', }, - ] - shown_all_attributes = [] - for all_attribute in all_attributes - for attribute in attributes - if all_attribute['name'] is attribute - shown_all_attributes.push all_attribute - break - return shown_all_attributes - # redirectToLogin: (data) -> # @@ -188,66 +162,11 @@ class App.Controller extends Spine.Controller size = Math.round( size / 1024 ) + ' KBytes' else size = size + ' Bytes' - return size + size # human readable time - humanTime: ( time, escalation ) => - current = new Date() - created = new Date(time) - string = '' - diff = ( current - created ) / 1000 - escalated = '' - if escalation - if diff > 0 - escalated = '-' - if diff >= 0 - style = "class=\"label label-danger\"" - else if diff > -60 * 60 - style = "class=\"label label-warning\"" - else - style = "class=\"label label-success\"" - - if diff.toString().match('-') - diff = diff.toString().replace('-', '') - diff = parseFloat(diff) - - if diff >= 86400 - unit = Math.floor( ( diff / 86400 ) ) -# if unit > 1 -# return unit + ' ' + App.i18n.translateContent('days') -# else -# return unit + ' ' + App.i18n.translateContent('day') - string = unit + ' ' + App.i18n.translateInline('d') - if diff >= 3600 - unit = Math.floor( ( diff / 3600 ) % 24 ) -# if unit > 1 -# return unit + ' ' + App.i18n.translateContent('hours') -# else -# return unit + ' ' + App.i18n.translateContent('hour') - if string isnt '' - string = string + ' ' + unit + ' ' + App.i18n.translateInline('h') - if escalation - string = "#{escalated}#{string}" - return string - else - string = unit + ' ' + App.i18n.translateInline('h') - if diff <= 86400 - unit = Math.floor( ( diff / 60 ) % 60 ) -# if unit > 1 -# return unit + ' ' + App.i18n.translateContent('minutes') -# else -# return unit + ' ' + App.i18n.translateContent('minute') - if string isnt '' - string = string + ' ' + unit + ' ' + App.i18n.translateInline('m') - if escalation - string = "#{escalated}#{string}" - return string - else - string = unit + ' ' + App.i18n.translateInline('m') - - if escalation - string = "#{escalated}#{string}" - return string + humanTime: ( time, escalation, long = true ) => + App.PrettyDate.humanTime( time, escalation, long ) userInfo: (data) => el = data.el || $('[data-id="customer_info"]') diff --git a/app/assets/javascripts/app/lib/app_post/pretty_date.js.coffee b/app/assets/javascripts/app/lib/app_post/pretty_date.js.coffee new file mode 100644 index 000000000..223947c35 --- /dev/null +++ b/app/assets/javascripts/app/lib/app_post/pretty_date.js.coffee @@ -0,0 +1,94 @@ +class App.PrettyDate + + # human readable time + @humanTime: ( time, escalation, long = true ) => + return '' if !time + current = new Date() + created = new Date(time) + diff = ( current - created ) / 1000 + + escalated = '' + if escalation + if diff > 0 + escalated = '-' + if diff >= 0 + style = "class=\"label label-danger\"" + else if diff > -60 * 60 + style = "class=\"label label-warning\"" + else + style = "class=\"label label-success\"" + + # remember past/future + direction = 'future' + if diff > -1 + direction = 'past' + + # strip not longer needed - + if diff.toString().match('-') + diff = diff.toString().replace('-', '') + diff = parseFloat(diff) + + # days + string = '' + count = 0 + if diff >= 86400 + count++ + unit = Math.floor( ( diff / 86400 ) ) + day = App.i18n.translateInline('d') + if long + if unit > 1 || unit is 0 + day = App.i18n.translateContent('days') + else + day = App.i18n.translateContent('day') + string = unit + ' ' + day + diff = diff - ( unit * 86400 ) + if unit >= 9 || diff < 3600 || count is 2 + if direction is 'past' + string = App.i18n.translateInline('%s ago', string) + else + string = App.i18n.translateInline('in %s', string) + if escalation + string = "#{string}" + return string + + # hours + if diff >= 3600 + count++ + unit = Math.floor( ( diff / 3600 ) % 24 ) + hour = App.i18n.translateInline('h') + if long + if unit > 1 || unit is 0 + hour = App.i18n.translateInline('hours') + else + hour = App.i18n.translateInline('hour') + if string isnt '' + string = string + ' ' + string = string + unit + ' ' + hour + diff = diff - ( unit * 3600 ) + if unit >= 9 || diff < 60 || count is 2 + if direction is 'past' + string = App.i18n.translateInline('%s ago', string) + else + string = App.i18n.translateInline('in %s', string) + if escalation + string = "#{string}" + return string + + # minutes + unit = Math.floor( ( diff / 60 ) % 60 ) + minute = App.i18n.translateInline('m') + if long + if unit > 1 || unit is 0 + minute = App.i18n.translateContent('minutes') + else + minute = App.i18n.translateContent('minute') + if string isnt '' + string = string + ' ' + string = string + unit + ' ' + minute + if direction is 'past' + string = App.i18n.translateInline('%s ago', string) + else + string = App.i18n.translateInline('in %s', string) + if escalation + string = "#{string}" + return string diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 20a702216..02b7885dd 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -9,6 +9,13 @@ class TestsController < ApplicationController end end + # GET /tests/ui + def ui + respond_to do |format| + format.html # index.html.erb + end + end + # GET /tests/from def form respond_to do |format| diff --git a/app/views/tests/form.html.erb b/app/views/tests/form.html.erb index dc71181cf..adde368d2 100644 --- a/app/views/tests/form.html.erb +++ b/app/views/tests/form.html.erb @@ -20,5 +20,3 @@ body { - - diff --git a/app/views/tests/ui.html.erb b/app/views/tests/ui.html.erb new file mode 100644 index 000000000..b8011c739 --- /dev/null +++ b/app/views/tests/ui.html.erb @@ -0,0 +1,15 @@ + + + + + + + + + +
diff --git a/config/routes/test.rb b/config/routes/test.rb index 55406c6aa..3eb4206f7 100644 --- a/config/routes/test.rb +++ b/config/routes/test.rb @@ -1,6 +1,7 @@ Zammad::Application.routes.draw do match '/tests-core', :to => 'tests#core', :via => :get + match '/tests-ui', :to => 'tests#ui', :via => :get match '/tests-form', :to => 'tests#form', :via => :get match '/tests-table', :to => 'tests#table', :via => :get match '/tests/wait/:sec', :to => 'tests#wait', :via => :get diff --git a/db/seeds.rb b/db/seeds.rb index 23775ab31..2578c095b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1700,7 +1700,7 @@ Translation.create_if_not_exists( :locale => 'de', :source => "Close time", :tar Translation.create_if_not_exists( :locale => 'de', :source => "First response", :target => "Erste Reaktion" ) Translation.create_if_not_exists( :locale => 'de', :source => "Ticket %s created!", :target => "Ticket %s erstellt!" ) Translation.create_if_not_exists( :locale => 'de', :source => "day", :target => "Tag" ) -Translation.create_if_not_exists( :locale => 'de', :source => "days", :target => "Tage" ) +Translation.create_if_not_exists( :locale => 'de', :source => "days", :target => "Tagen" ) Translation.create_if_not_exists( :locale => 'de', :source => "hour", :target => "Stunde" ) Translation.create_if_not_exists( :locale => 'de', :source => "hours", :target => "Stunden" ) Translation.create_if_not_exists( :locale => 'de', :source => "minute", :target => "Minute" ) @@ -1864,6 +1864,8 @@ Translation.create_if_not_exists( :locale => 'de', :source => "Here you can crea Translation.create_if_not_exists( :locale => 'de', :source => "Fold in", :target => "Einklappen" ) Translation.create_if_not_exists( :locale => 'de', :source => "from", :target => "von" ) Translation.create_if_not_exists( :locale => 'de', :source => "to", :target => "nach" ) +Translation.create_if_not_exists( :locale => 'de', :source => "%s ago", :target => "vor %s" ) +Translation.create_if_not_exists( :locale => 'de', :source => "in %s", :target => "in %s" ) #Translation.create_if_not_exists( :locale => 'de', :source => "", :target => "" ) # install all packages in auto_install diff --git a/public/assets/tests/ui.js b/public/assets/tests/ui.js new file mode 100644 index 000000000..ef0004024 --- /dev/null +++ b/public/assets/tests/ui.js @@ -0,0 +1,76 @@ + +// pretty date +test( "check pretty date", function() { + var current = new Date() + + // past + var result = App.PrettyDate.humanTime( current ); + equal( result, '0 minutes ago', 'right now') + + result = App.PrettyDate.humanTime( current - 60000 ); + equal( result, '1 minute ago', '1 min ago') + + result = App.PrettyDate.humanTime( current - ( 2 * 60000 ) ); + equal( result, '2 minutes ago', '2 min ago') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 ) ) ; + equal( result, '1 hour ago', '1 hour') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 2 ) ); + equal( result, '2 hours ago', '2 hours') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 2.5 ) ); + equal( result, '2 hours 30 minutes ago', '2.5 hours') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 12.5 ) ); + equal( result, '12 hours ago', '12.5 hours') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 ) ) ; + equal( result, '1 day ago', '1 day') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 2 ) ); + equal( result, '2 days ago', '2 days') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 2 ) - ( 60000 * 5 ) ); + equal( result, '2 days ago', '2 days') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 2.5 ) ); + equal( result, '2 days 12 hours ago', '2.5 days') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 2.5 ) - ( 60000 * 5 ) ); + equal( result, '2 days 12 hours ago', '2.5 days') + + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 10.5 ) ); + equal( result, '10 days ago', '10.5 days') + + // future + current = new Date() + result = App.PrettyDate.humanTime( current ); + equal( result, '0 minutes ago', 'right now') + + result = App.PrettyDate.humanTime( current.getTime() + 65000 ); + equal( result, 'in 1 minute', 'in 1 min') + + result = App.PrettyDate.humanTime( current.getTime() + ( 2 * 65000 ) ); + equal( result, 'in 2 minutes', 'in 2 min') + + result = App.PrettyDate.humanTime( current.getTime() + ( 60500 * 60 ) ) ; + equal( result, 'in 1 hour', 'in 1 hour') + + result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 2 ) ); + equal( result, 'in 2 hours', 'in 2 hours') + + result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 2.5 ) ); + equal( result, 'in 2 hours 30 minutes', 'in 2.5 hours') + + result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 ) ) ; + equal( result, 'in 1 day', 'in 1 day') + + result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 * 2 ) ); + equal( result, 'in 2 days', 'in 2 days') + + result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 * 2.5 ) ); + equal( result, 'in 2 days 12 hours', 'in 2.5 days') + + +}); diff --git a/test/browser/aab_unit_test.rb b/test/browser/aab_unit_test.rb index 4446d1346..fa67511cb 100644 --- a/test/browser/aab_unit_test.rb +++ b/test/browser/aab_unit_test.rb @@ -24,6 +24,28 @@ class AAbUnitTest < TestCase ] browser_single_test(tests) end + def test_ui + tests = [ + { + :name => 'start', + :instance => browser_instance, + :url => browser_url + '/tests-ui', + :action => [ + { + :execute => 'wait', + :value => 8, + }, + { + :execute => 'match', + :css => '.result .failed', + :value => '0', + :match_result => true, + }, + ], + }, + ] + browser_single_test(tests) + end def test_form tests = [ {