Sha256: 37326bb1b035a0faebcc3c448a63d2d45e914afb50bfa4c6dedd997fb1bd7f02

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

class CkeditorInput < Formtastic::Inputs::TextInput

  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::JavaScriptHelper

  def input_html_options
    input_html = (options.delete(:input_html) || {})
    super.merge(input_html).merge(:rows => nil)
  end

  def to_html
    hash = input_html_options.stringify_keys
    super + javascript_tag(js_replace(hash['id'], hash))
  end

  def js_replace(dom_id, options = {})
    js_options = applay_options(options)
    js = "if (CKEDITOR.instances['#{dom_id}']) {\nCKEDITOR.remove(CKEDITOR.instances['#{dom_id}']);\n}\n"
    js_options.blank? ? js += "CKEDITOR.replace('#{dom_id}');" : js += "CKEDITOR.replace('#{dom_id}', { #{js_options} });"
  end

  def applay_options(options)
    str = []
    options.each do |key, value|
      item = case value
             when String then
               value.split(//).first == '^' ? value.slice(1..-1) : "'#{value}'"
             when Hash then
               "{ #{applay_options(value)} }"
             when Array then
               arr = value.collect { |v| "'#{v}'" }
               "[ #{arr.join(',')} ]"
             when nil then
               next
             else value
             end
      str << %Q{'#{key}': #{item}}
    end
    str.sort.join(', ')
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
esp-ckeditor-0.1.3 app/inputs/ckeditor_input.rb
esp-ckeditor-0.1.2 app/inputs/ckeditor_input.rb
esp-ckeditor-0.1.1 app/inputs/ckeditor_input.rb
esp-ckeditor-0.1.0 app/inputs/ckeditor_input.rb