jipiboily / spree_multi_lingual

Spree multi-lingual is a plugin to make multi locale store with Spree possible
BSD 3-Clause "New" or "Revised" License
49 stars 56 forks source link

spree_multi_lingual and spree_editor doesn't work together until we create a new coffeescript class #26

Closed bricesanchez closed 9 years ago

bricesanchez commented 11 years ago

On my spree 1.2.x, i have spree_multi_lingual and spree_editor.

In the admin, when i switch locale with the spree_multi_lingual dropdown, the tinyMCE doesn't reload with the translated content because translated_fields are created only if we change the locale.

I worked on a solution who need to be improved.

At this time, i'm able to use those extensions if i override the spree_multi_lingual_class.js.coffee. It works only for 2 languages on the website, it use a setTimeout (i hate that) and he doesn't check if spree_editor is running or not.

This is my concept proof :

$(document).ready ->
  ml = new SpreeMultiLingual
  ml.create_localized_fields()

  $("#spree_multi_lingual_dropdown").change (e) ->
    ml.change_language(@.value)

# declare the class in global scope so we can extend it from within our app
class window.SpreeMultiLingual
  constructor: ->
    @translated_fields = window.spree_multi_lingual.translated_fields
    @default_locale = window.spree_multi_lingual.backend_locale
    @current_locale = @default_locale
    @other_locale = $('#spree_multi_lingual_dropdown option').not(':selected').attr('value')

  create_localized_fields: =>
    for field in @translated_fields
      field_name = @localized_field_name(field)
      if !$("#" + field_name).length
        @duplicate_field(field)

    setTimeout =>
      @show_fields()
    , 1000

  change_language: (locale) =>
    @current_locale = locale
    @make_sure_field_exists_for_language()
    @show_fields()

  duplicate_field: (field) =>
    field_name = @localized_field_name(field)
    if $("#" + field)[0]
      new_field_name_value = $("#" + field).attr("name").substring(0,$("#" + field).attr("name").length - 1) + "_" + @other_locale + "]"
      new_field = $("#" + field).clone().attr("id", field_name).attr("name", new_field_name_value).removeClass("sml-localized-field-"+@default_locale)
      value = eval("window.spree_multi_lingual.translated_fields_values['#{field_name}']")
      new_field.val(value) if value
      $("#" + field).after(new_field)
      @add_localized_class(field_name)
      @add_localized_class(field, @default_locale)

  make_sure_field_exists_for_language: =>
    for field in @translated_fields
      field_name = @localized_field_name(field)
      if !$("#" + field_name).length
        @duplicate_field(field)

  show_fields: =>
    for field in $(".sml-localized-field")
      $(field).hide()
      $(field).next('.mceEditor').hide()

      if $(field).hasClass("sml-localized-field-#{@current_locale}")
        if $(field).is('textarea')
          $(field).next('.mceEditor').show()
        else
          $(field).show()

  localized_field_name: (field) =>
    (field + "_" + @other_locale)

  add_localized_class: (id, locale = @other_locale) =>
    @add_class(id, "sml-localized-field")
    @add_class(id, "sml-localized-field-" + locale)

  add_class: (id, klass) ->
    $("#" + id).addClass(klass) unless $("#" + id).hasClass(klass)

Could you help me to improve this class ?

yanicklandry commented 11 years ago

I think I got it here but haven't got the time yet to clean up the code for a pull request : https://github.com/yanicklandry/spree_multi_lingual/commit/affeb93be41e688283d624397a1569d26429577f