Skip to content

Commit c161cc3

Browse files
committed
In translated_row, use gray flags for empty values and transparent flags for inactive value
1 parent afc011c commit c161cc3

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

app/assets/javascripts/active_admin/active_admin_globalize.js.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ $ ->
148148
$td = $(this).closest('td')
149149
$('.field-translation', $td).hide()
150150
$(".locale-#{$locale}", $td).show()
151+
$(this).parent().children('a.ui-translation-trigger').removeClass('active')
152+
$(this).addClass('active')
151153
e.preventDefault()
152154

153155
translations()

app/assets/stylesheets/active_admin/active_admin_globalize_flags.sass

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
// Used to distantiate inline locale selector
4141
span.inline-locale-selector
4242
margin-right: 10px
43+
> .ui-translation-trigger
44+
opacity: .4
45+
&.empty
46+
filter: grayscale(100%)
47+
&.active
48+
opacity: 1
4349

4450
.field-translation.hidden
4551
display: none

lib/active_admin/globalize/attributes_table_extension.rb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ def translated_row(*args, &block)
5353
if options[:inline]
5454
''.html_safe.tap do |value|
5555
# Add selectors for inline locale
56-
value << inline_locale_selectors
56+
value << inline_locale_selectors(field, options[:locale], &block)
5757
# Build translations spans
5858
value << field_translations(field, :span, options[:locale], &block)
5959
end
6060
else
6161
content_tag(:div, class: 'activeadmin-translations') do
6262
''.html_safe.tap do |value|
6363
# Render selectors as in translation ui
64-
value << block_locale_selectors
64+
value << block_locale_selectors(field, options[:locale], &block)
6565
# Build translations divs for actual translations
6666
value << field_translations(field, :div, options[:locale], &block)
6767
end
@@ -82,16 +82,14 @@ def translatable?(field)
8282
# @param [String] field field name to render
8383
# @param [Symbol] tag tag to enclose field translation
8484
# @param [Symbol] initial_locale locale to set as not hidden
85-
def field_translations(field, tag, initial_locale)
85+
def field_translations(field, tag, initial_locale, &block)
8686
available_translations.map do |translation|
8787
# Classes for translation span only first element is visible
8888
css_classes = ['field-translation', "locale-#{translation.locale}"]
8989
# Initially only element for selected locale is visible
9090
css_classes.push 'hidden' unless translation.locale == initial_locale.to_sym
9191
# Build content for cell or div using translation locale and given block
92-
content = I18n.with_locale(translation.locale) do
93-
block_given? ? yield(translation) : translation.send(field)
94-
end
92+
content = field_translation_value(translation, field, &block)
9593
# return element
9694
if tag == :span # inline element
9795
# attach class to span if inline
@@ -107,12 +105,15 @@ def field_translations(field, tag, initial_locale)
107105
end.join(' ').html_safe
108106
end
109107

110-
def block_locale_selectors
108+
def block_locale_selectors(field, initial_locale, &block)
111109
content_tag(:ul, class: 'available-locales locale-selector') do
112110
available_translations.map(&:locale).map do |locale|
113-
default = 'default' if locale == I18n.default_locale
114-
content_tag(:li, class: 'translation-tab') do
111+
css_classes = ['translation-tab']
112+
css_classes << 'active' if translation.locale == initial_locale.to_sym
113+
css_classes << 'empty' unless content.presence
114+
content_tag(:li, class: css_classes) do
115115
I18n.with_locale(locale) do
116+
default = 'default' if translation.locale == initial_locale.to_sym
116117
content_tag(:a, I18n.t(:"active_admin.globalize.language.#{locale}"), href: ".locale-#{locale}", class: default)
117118
end
118119
end
@@ -121,11 +122,15 @@ def block_locale_selectors
121122
end
122123

123124
# Return flag elements to show the given locale using javascript
124-
def inline_locale_selectors
125+
def inline_locale_selectors(field, initial_locale, &block)
125126
content_tag(:span, class: 'inline-locale-selector') do
126127
available_translations.map do |translation|
128+
content = field_translation_value(translation, field, &block)
129+
css_classes = ['ui-translation-trigger']
130+
css_classes << 'active' if translation.locale == initial_locale.to_sym
131+
css_classes << 'empty' unless content.presence
127132
# Build a link to show the given translation
128-
link_to(flag_icon(translation.locale), '#', class: 'ui-translation-trigger', data: {locale: translation.locale})
133+
link_to(flag_icon(translation.locale), '#', class: css_classes, data: {locale: translation.locale})
129134
end.join(' ').html_safe
130135
end
131136
end
@@ -134,6 +139,11 @@ def available_translations
134139
@record_translations ||= @collection.first.translations.order(:locale)
135140
end
136141

142+
def field_translation_value(translation, field)
143+
I18n.with_locale(translation.locale) do
144+
block_given? ? yield(translation) : translation.send(field)
145+
end
146+
end
137147
end
138148
end
139149
end

0 commit comments

Comments
 (0)