Sha256: de0e8a56b63ab4fa5453210b3de619b8f3978db355401fbb3c20ed2ff2a88a09

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module RailsKindeditor
  module Helper
    def kindeditor_tag(name, content = nil, options = {})
      id = sanitize_to_id(name)
      input_html = { :id => id }.merge(options.delete(:input_html) || {})
      output = ActiveSupport::SafeBuffer.new
      output << text_area_tag(name, content, input_html)
      output << javascript_tag(js_replace(id, options))
    end
    
    def kindeditor(name, method, options = {})
      input_html = (options.delete(:input_html) || {})
      hash = input_html.stringify_keys
      instance_tag = ActionView::Base::InstanceTag.new(name, method, self, options.delete(:object))
      instance_tag.send(:add_default_name_and_id, hash)      
      output_buffer = ActiveSupport::SafeBuffer.new
      output_buffer << instance_tag.to_text_area_tag(input_html)
      output_buffer << javascript_tag(js_replace(hash['id'], options))
    end
    
    private
    def js_replace(dom_id, options = {})
      "KindEditor.ready(function(K){
      	#{"#{options[:editor_id]} = " if options[:editor_id]}K.create('##{dom_id}', #{get_options(options).to_json});
      });"
    end

    def get_options(options)
      options.delete(:editor_id)
      options.reverse_merge!(:width => '100%')
      options.reverse_merge!(:height => 300)
      options.reverse_merge!(:allowFileManager => true)
      options.merge!(:uploadJson => '/kindeditor/upload')
      options.merge!(:fileManagerJson => '/kindeditor/filemanager')
      if options[:simple_mode] == true
        options.delete(:simple_mode)
        options.merge!(:items => %w{fontname fontsize | forecolor hilitecolor bold italic underline removeformat | justifyleft justifycenter justifyright insertorderedlist insertunorderedlist | emoticons image link})
      end
      options
    end    
  end
  
  module Builder
    def kindeditor(method, options = {})
      @template.send("kindeditor", @object_name, method, objectify_options(options))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_kindeditor-0.3.12 lib/rails_kindeditor/helper.rb