From 39be2dd0e9b0ecb670c93dd616acaf15f8c9a899 Mon Sep 17 00:00:00 2001 From: bohung Date: Wed, 20 Jul 2022 22:38:59 +0800 Subject: [PATCH] Add time type setting.(12 or 24 hour clock) --- app/assets/javascripts/calendar.js | 26 ++++++++++++++----- app/assets/javascripts/calendar_frontend.js | 26 ++++++++++++++----- app/assets/javascripts/calendar_widget.js | 24 +++++++++++++---- app/assets/javascripts/calendar_widget2.js | 24 +++++++++++++---- app/models/calendar_setting.rb | 2 ++ .../admin/calendars/calendar_setting.html.erb | 6 +++++ .../calendars/_calendar_variable.html.erb | 1 + config/locales/en.yml | 3 +++ config/locales/zh_tw.yml | 3 +++ 9 files changed, 93 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/calendar.js b/app/assets/javascripts/calendar.js index 652287d..9fcda58 100644 --- a/app/assets/javascripts/calendar.js +++ b/app/assets/javascripts/calendar.js @@ -28,25 +28,39 @@ if(window.calendar_variable.is_chinese){ window.calendar_variable.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; } if(window.calendar_variable.date_type == 2){ - window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd b h:m' : 'dd/MM/yy h:m b'; window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'yy/MM' : 'MM/yy'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'yy/MM/dd (W)' : 'W, MM/dd/yy'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MM/dd (W)" : "W, dd/MM"); - window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) b h:m" : "W, dd/MM h:m b"); + if(window.calendar_variable.time_type == 1){ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd H:m' : 'dd/MM/yy H:m'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) H:m" : "W, dd/MM H:m"); + }else{ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd b h:m' : 'dd/MM/yy h:m b'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) b h:m" : "W, dd/MM h:m b"); + } }else{ - window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d b h:m' : 'd MMM, y h:m b'; window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'y MMM' : 'MMM y'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'y MMM d' : 'd MMM, y'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'y M d' : 'd M, y'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'y MMM d (W)' : 'W, MMM d, y'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MMM d (W)" : "W, d MMM"); - window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) b h:m" : "W, d MMM h:m b"); + if(window.calendar_variable.time_type == 1){ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d H:m' : 'd MMM, y H:m'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) H:m" : "W, d MMM H:m"); + }else{ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d b h:m' : 'd MMM, y h:m b'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) b h:m" : "W, d MMM h:m b"); + } } window.calendar_variable.date_format_numeric = 'yy/MM/dd'; window.calendar_variable.short_date_numeric = (calendar_variable.is_chinese ? "MM/dd (w)" : "w MM/dd"); -window.calendar_variable.time_format = (calendar_variable.is_chinese ? "b h:m" : "h:m b"); +if(window.calendar_variable.time_type == 1){ + window.calendar_variable.time_format = "H:m"; +}else{ + window.calendar_variable.time_format = (calendar_variable.is_chinese ? "b h:m" : "h:m b"); +} window.calendar_variable.std_date_format = 'y-MM-d'; window.calendar_variable.short_day = (calendar_variable.is_chinese ? "d (W)" : "W d"); $.fn.fullCalendar = function(args){ @@ -348,7 +362,7 @@ var Calendar = function(dom){ windowResize : function(view){ c.calendar_dom.calendar.refetchEvents(); }, - eventTimeFormat: { hour12: true, hour: '2-digit', minute: '2-digit', omitZeroMinute: true, meridiem: 'narrow' }, + eventTimeFormat: { hour12: (window.calendar_variable.time_type != 1), hour: '2-digit', minute: '2-digit', omitZeroMinute: true, meridiem: 'narrow' }, eventClick: function(eventClickInfo) { var calEvent = {"event": eventClickInfo.event}, originalEvent = eventClickInfo.jsEvent, diff --git a/app/assets/javascripts/calendar_frontend.js b/app/assets/javascripts/calendar_frontend.js index c90e655..2794564 100644 --- a/app/assets/javascripts/calendar_frontend.js +++ b/app/assets/javascripts/calendar_frontend.js @@ -28,25 +28,39 @@ if(window.calendar_variable.is_chinese){ window.calendar_variable.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; } if(window.calendar_variable.date_type == 2){ - window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd b h:m' : 'dd/MM/yy h:m b'; window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'yy/MM' : 'MM/yy'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'yy/MM/dd (W)' : 'W, MM/dd/yy'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MM/dd (W)" : "W, dd/MM"); - window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) b h:m" : "W, dd/MM h:m b"); + if(window.calendar_variable.time_type == 1){ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd H:m' : 'dd/MM/yy H:m'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) H:m" : "W, dd/MM H:m"); + }else{ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd b h:m' : 'dd/MM/yy h:m b'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) b h:m" : "W, dd/MM h:m b"); + } }else{ - window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d b h:m' : 'd MMM, y h:m b'; window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'y MMM' : 'MMM y'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'y MMM d' : 'd MMM, y'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'y M d' : 'd M, y'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'y MMM d (W)' : 'W, MMM d, y'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MMM d (W)" : "W, d MMM"); - window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) b h:m" : "W, d MMM h:m b"); + if(window.calendar_variable.time_type == 1){ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d H:m' : 'd MMM, y H:m'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) H:m" : "W, d MMM H:m"); + }else{ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d b h:m' : 'd MMM, y h:m b'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) b h:m" : "W, d MMM h:m b"); + } } window.calendar_variable.date_format_numeric = 'yy/MM/dd'; window.calendar_variable.short_date_numeric = (calendar_variable.is_chinese ? "MM/dd (w)" : "w MM/dd"); -window.calendar_variable.time_format = (calendar_variable.is_chinese ? "b h:m" : "h:m b"); +if(window.calendar_variable.time_type == 1){ + window.calendar_variable.time_format = "H:m"; +}else{ + window.calendar_variable.time_format = (calendar_variable.is_chinese ? "b h:m" : "h:m b"); +} window.calendar_variable.std_date_format = 'y-MM-d'; window.calendar_variable.short_day = (calendar_variable.is_chinese ? "d (W)" : "W d"); $.fn.fullCalendar = function(args){ @@ -225,7 +239,7 @@ var Calendar = function(dom,page_id,event_date=''){ windowResize : function(view){ c.calendar_dom.calendar.refetchEvents(); }, - eventTimeFormat: { hour12: true, hour: '2-digit', minute: '2-digit', omitZeroMinute: true, meridiem: 'narrow' }, + eventTimeFormat: { hour12: (window.calendar_variable.time_type != 1), hour: '2-digit', minute: '2-digit', omitZeroMinute: true, meridiem: 'narrow' }, eventClick: function(eventClickInfo) { var calEvent = {"event": eventClickInfo.event}, originalEvent = eventClickInfo.jsEvent, diff --git a/app/assets/javascripts/calendar_widget.js b/app/assets/javascripts/calendar_widget.js index 0250147..8ace494 100644 --- a/app/assets/javascripts/calendar_widget.js +++ b/app/assets/javascripts/calendar_widget.js @@ -27,25 +27,39 @@ if(window.calendar_variable.is_chinese){ window.calendar_variable.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; } if(window.calendar_variable.date_type == 2){ - window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd b h:m' : 'dd/MM/yy h:m b'; window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'yy/MM' : 'MM/yy'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'yy/MM/dd (W)' : 'W, MM/dd/yy'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MM/dd (W)" : "W, dd/MM"); - window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) b h:m" : "W, dd/MM h:m b"); + if(window.calendar_variable.time_type == 1){ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd H:m' : 'dd/MM/yy H:m'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) H:m" : "W, dd/MM H:m"); + }else{ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd b h:m' : 'dd/MM/yy h:m b'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) b h:m" : "W, dd/MM h:m b"); + } }else{ - window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d b h:m' : 'd MMM, y h:m b'; window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'y MMM' : 'MMM y'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'y MMM d' : 'd MMM, y'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'y M d' : 'd M, y'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'y MMM d (W)' : 'W, MMM d, y'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MMM d (W)" : "W, d MMM"); - window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) b h:m" : "W, d MMM h:m b"); + if(window.calendar_variable.time_type == 1){ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d H:m' : 'd MMM, y H:m'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) H:m" : "W, d MMM H:m"); + }else{ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d b h:m' : 'd MMM, y h:m b'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) b h:m" : "W, d MMM h:m b"); + } } window.calendar_variable.date_format_numeric = 'yy/MM/dd'; window.calendar_variable.short_date_numeric = (calendar_variable.is_chinese ? "MM/dd (w)" : "w MM/dd"); -window.calendar_variable.time_format = (calendar_variable.is_chinese ? "b h:m" : "h:m b"); +if(window.calendar_variable.time_type == 1){ + window.calendar_variable.time_format = "H:m"; +}else{ + window.calendar_variable.time_format = (calendar_variable.is_chinese ? "b h:m" : "h:m b"); +} window.calendar_variable.std_date_format = 'y-MM-d'; window.calendar_variable.short_day = (calendar_variable.is_chinese ? "d (W)" : "W d"); var CalendarModuleMonth1 = function(date,dom,subpart,url,index_flag){ diff --git a/app/assets/javascripts/calendar_widget2.js b/app/assets/javascripts/calendar_widget2.js index 7447260..a53a16b 100644 --- a/app/assets/javascripts/calendar_widget2.js +++ b/app/assets/javascripts/calendar_widget2.js @@ -27,25 +27,39 @@ if(window.calendar_variable.is_chinese){ window.calendar_variable.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; } if(window.calendar_variable.date_type == 2){ - window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd b h:m' : 'dd/MM/yy h:m b'; window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'yy/MM' : 'MM/yy'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'yy/MM/dd (W)' : 'W, MM/dd/yy'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MM/dd (W)" : "W, dd/MM"); - window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) b h:m" : "W, dd/MM h:m b"); + if(window.calendar_variable.time_type == 1){ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd H:m' : 'dd/MM/yy H:m'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) H:m" : "W, dd/MM H:m"); + }else{ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd b h:m' : 'dd/MM/yy h:m b'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) b h:m" : "W, dd/MM h:m b"); + } }else{ - window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d b h:m' : 'd MMM, y h:m b'; window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'y MMM' : 'MMM y'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'y MMM d' : 'd MMM, y'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'y M d' : 'd M, y'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'y MMM d (W)' : 'W, MMM d, y'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MMM d (W)" : "W, d MMM"); - window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) b h:m" : "W, d MMM h:m b"); + if(window.calendar_variable.time_type == 1){ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d H:m' : 'd MMM, y H:m'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) H:m" : "W, d MMM H:m"); + }else{ + window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d b h:m' : 'd MMM, y h:m b'; + window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) b h:m" : "W, d MMM h:m b"); + } } window.calendar_variable.date_format_numeric = 'yy/MM/dd'; window.calendar_variable.short_date_numeric = (calendar_variable.is_chinese ? "MM/dd (w)" : "w MM/dd"); -window.calendar_variable.time_format = (calendar_variable.is_chinese ? "b h:m" : "h:m b"); +if(window.calendar_variable.time_type == 1){ + window.calendar_variable.time_format = "H:m"; +}else{ + window.calendar_variable.time_format = (calendar_variable.is_chinese ? "b h:m" : "h:m b"); +} window.calendar_variable.std_date_format = 'y-MM-d'; window.calendar_variable.short_day = (calendar_variable.is_chinese ? "d (W)" : "W d"); var CalendarModuleMonth2 = function(date,dom,subpart,url,index_flag){ diff --git a/app/models/calendar_setting.rb b/app/models/calendar_setting.rb index b254927..f9a71ab 100644 --- a/app/models/calendar_setting.rb +++ b/app/models/calendar_setting.rb @@ -6,6 +6,7 @@ class CalendarSetting field :days, type: Array, default: [], localize: true field :sunday_first, type: Boolean, localize: true field :date_type, type: Integer, localize: true, default: 0 + field :time_type, type: Integer, localize: true, default: 0 field :titleFormat, localize: true All_days = (0...7).to_a ModesInfo = {'day'=>'timeGridDay', 'week'=>'timeGridWeek', 'month'=>'dayGridMonth', 'agenda'=>'agenda'} @@ -14,6 +15,7 @@ class CalendarSetting "zh_tw"=>["[LocaleString]: 2022年7月12日 (週三)", "[en-US format]: weekday, monthname dd, YYYY", "[Locale Numeric]: YYYY/mm/dd (weekday)"], "en"=>["[LocaleString]: weekday, monthname dd, YYYY", "[en-US format]: weekday, monthname dd, YYYY", "[Locale Numeric]: weekday, mm/dd/YYYY"] } + TimeTypes = ["12_hour_clock", "24_hour_clock"] def week_title if self.days.present? self.days diff --git a/app/views/admin/calendars/calendar_setting.html.erb b/app/views/admin/calendars/calendar_setting.html.erb index eefb467..fd2cd54 100644 --- a/app/views/admin/calendars/calendar_setting.html.erb +++ b/app/views/admin/calendars/calendar_setting.html.erb @@ -44,6 +44,12 @@ <%= select_tag "#{f.object_name}[#{locale}]" , options_for_select(tmp.map.with_index{|t, idx| [t, idx]}, f.object.date_type_translations[locale]), {style: "width: auto;"} %> <% end %> + <%= f.label :time_type, t("calendar.time_type"), :class => "control-label muted" %> +
+ <%= f.fields_for :time_type_translations ,f.object do |f| %> + <%= select_tag "#{f.object_name}[#{locale}]" , options_for_select(CalendarSetting::TimeTypes.map.with_index{|type, idx| [t("calendar.#{type}"), idx]}, f.object.time_type_translations[locale]), {style: "width: auto;"} %> + <% end %> +

<%= t('calendar.calendar_mode') %>

<% CalendarSetting::All_modes.each do |mode| %> diff --git a/app/views/calendars/_calendar_variable.html.erb b/app/views/calendars/_calendar_variable.html.erb index 4f1b825..66d02e1 100644 --- a/app/views/calendars/_calendar_variable.html.erb +++ b/app/views/calendars/_calendar_variable.html.erb @@ -7,6 +7,7 @@ window.calendar_variable.sunday_first = <%=@calendar_setting.sunday_first%>; window.calendar_variable.week_title = <%=@calendar_setting.week_title.to_s.html_safe%>; window.calendar_variable.date_type = <%=@calendar_setting.date_type%>; + window.calendar_variable.time_type = <%=@calendar_setting.time_type%>; window.calendar_variable.dayNames = <%=@calendar_setting.days.to_s.html_safe%>; <% if I18n.locale == :zh_tw %> window.calendar_variable.dayNamesShort = <%=@calendar_setting.days.map{|d| d[-1]}.to_s.html_safe%>; diff --git a/config/locales/en.yml b/config/locales/en.yml index 9319eb7..cfa8769 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,5 +1,8 @@ en: calendar: + "12_hour_clock": "12 hour clock" + "24_hour_clock": "24 hour clock" + time_type: "Time Type" date_type: "Date Type" today: "Today" sunday_first: "Sunday First" diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index f504227..5742ffb 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -1,5 +1,8 @@ zh_tw: calendar: + "12_hour_clock": "12小時制" + "24_hour_clock": "24小時制" + time_type: "時間格式" date_type: "日期格式" today: "今日" sunday_first: "週日先"