Files
nairobi/app/assets/javascripts/globalize.js.coffee
Javi Martín 71601bd3f8 Validate translations in banners
This change forces us to use nested attributes for translations, instead
of using the more convenient `:"title_#{locale}"` methods.

On the other hand, we can use Rails' native `_destroy` attribute to
remove existing translations, so we don't have to use our custom
`delete_translations`, which was a bit buggy since it didn't consider
failed updates.
2018-10-22 15:43:28 +02:00

64 lines
2.1 KiB
CoffeeScript

App.Globalize =
display_locale: (locale) ->
App.Globalize.enable_locale(locale)
$(".js-globalize-locale-link").each ->
if $(this).data("locale") == locale
$(this).show()
App.Globalize.highlight_locale($(this))
$(".js-globalize-locale option:selected").removeAttr("selected");
return
display_translations: (locale) ->
$(".js-globalize-attribute").each ->
if $(this).data("locale") == locale
$(this).show()
else
$(this).hide()
$('.js-delete-language').hide()
$('#delete-' + locale).show()
highlight_locale: (element) ->
$('.js-globalize-locale-link').removeClass('is-active');
element.addClass('is-active');
remove_language: (locale) ->
$(".js-globalize-attribute[data-locale=" + locale + "]").each ->
$(this).val('').hide()
if CKEDITOR.instances[$(this).attr('id')]
CKEDITOR.instances[$(this).attr('id')].setData('')
$(".js-globalize-locale-link[data-locale=" + locale + "]").hide()
next = $(".js-globalize-locale-link:visible").first()
App.Globalize.highlight_locale(next)
App.Globalize.display_translations(next.data("locale"))
App.Globalize.disable_locale(locale)
enable_locale: (locale) ->
App.Globalize.destroy_locale_field(locale).val(false)
disable_locale: (locale) ->
App.Globalize.destroy_locale_field(locale).val(true)
destroy_locale_field: (locale) ->
$(".destroy_locale[data-locale=" + locale + "]")
refresh_visible_translations: ->
locale = $('.js-globalize-locale-link.is-active').data("locale")
App.Globalize.display_translations(locale)
initialize: ->
$('.js-globalize-locale').on 'change', ->
App.Globalize.display_translations($(this).val())
App.Globalize.display_locale($(this).val())
$('.js-globalize-locale-link').on 'click', ->
locale = $(this).data("locale")
App.Globalize.display_translations(locale)
App.Globalize.highlight_locale($(this))
$('.js-delete-language').on 'click', ->
locale = $(this).data("locale")
$(this).hide()
App.Globalize.remove_language(locale)