module XmtFroala::Helper

Public Instance Methods

froalaeditor(name, method, options = {}) click to toggle source
# File lib/xmt_froala/helper.rb, line 12
def froalaeditor(name, method, options = {})
  # TODO: Refactory options: 1. kindeditor_option 2. html_option
  input_html = (options.delete(:input_html) || {}).stringify_keys
  output_buffer = ActiveSupport::SafeBuffer.new
  output_buffer << build_text_area_tag(name, method, self, merge_assets_info(options), input_html)
  output_buffer << javascript_tag(js_replace(input_html['id'], options))
end
froalaeditor_file_manager_json_path() click to toggle source
# File lib/xmt_froala/helper.rb, line 46
def froalaeditor_file_manager_json_path
  "#{main_app_root_url}xmt_froala/filemanager"
end
froalaeditor_tag(name, content = nil, options = {}) click to toggle source
# File lib/xmt_froala/helper.rb, line 3
def froalaeditor_tag(name, content = nil, options = {})
  id = sanitize_to_id(name)
  input_html = { :id => id }.merge(options.delete(:input_html) || {})
  input_html[:class] = "#{input_html[:class]} xmt_froala"
  output = ActiveSupport::SafeBuffer.new
  output << text_area_tag(name, content, input_html)
  output << javascript_tag(js_replace(id, options))
end
froalaeditor_upload_json_path(*args) click to toggle source
# File lib/xmt_froala/helper.rb, line 35
def froalaeditor_upload_json_path(*args)
  options = args.extract_options!
  owner_id_query_string = options[:owner_id] ? "?owner_id=#{options[:owner_id]}" : ''
  owner_type_query_string = options[:owner_type] ? "&owner_type=#{options[:owner_type]}" : ''
  if owner_id_query_string == '' && owner_type_query_string == ''
    "#{main_app_root_url}xmt_froala/upload"
  else
    "#{main_app_root_url}xmt_froala/upload#{owner_id_query_string}#{owner_type_query_string}"
  end
end
merge_assets_info(options) click to toggle source
# File lib/xmt_froala/helper.rb, line 20
def merge_assets_info(options)
  owner = options.delete(:owner)
  options[:class] = "#{options[:class]} xmt_froala"
  if (!owner.nil?) && (!owner.id.nil?)
    begin
      owner_id = owner.id
      owner_type = owner.class.name
      options.reverse_merge!(owner_id: owner_id, owner_type: owner_type, data: {upload: froalaeditor_upload_json_path(owner_id: owner_id, owner_type: owner_type), filemanager: froalaeditor_file_manager_json_path})
      return options
    end
  else
    options.reverse_merge!(data: {upload: froalaeditor_upload_json_path, filemanager: froalaeditor_file_manager_json_path})
  end
end

Private Instance Methods

build_text_area_tag(name, method, template, options, input_html) click to toggle source
# File lib/xmt_froala/helper.rb, line 83
def build_text_area_tag(name, method, template, options, input_html)
  if Rails.version >= '4.0.0'
    text_area_tag = ActionView::Helpers::Tags::TextArea.new(name, method, template, options)
    text_area_tag.send(:add_default_name_and_id, input_html)
    text_area_tag.render
  elsif Rails.version >= '3.1.0'
    text_area_tag = ActionView::Base::InstanceTag.new(name, method, template, options.delete(:object))
    text_area_tag.send(:add_default_name_and_id, input_html)
    text_area_tag.to_text_area_tag(input_html)
  elsif Rails.version >= '3.0.0'
    raise 'Please use xmt_froala v0.2.8 for Rails v3.0.x'
  else
    raise 'Please upgrade your Rails !'
  end
end
get_options(options) click to toggle source
# File lib/xmt_froala/helper.rb, line 79
def get_options(options)
  upload_url = froalaeditor_upload_json_path(:owner_id => options.delete(:owner_id), :owner_type => options.delete(:owner_type))
end
js_replace(dom_id, options = {}) click to toggle source
# File lib/xmt_froala/helper.rb, line 60
def js_replace(dom_id, options = {})
  editor_id = options[:editor_id].nil? ? '' : "#{options[:editor_id].to_s.downcase} = "
  upload_url = froalaeditor_upload_json_path(:owner_id => options.delete(:owner_id), :owner_type => options.delete(:owner_type))
  imageUploadURL = upload_url+'&dir=image'
  fileUploadURL = upload_url+'&dir=file'
  videoUploadURL = upload_url+'&dir=media'
  imageManagerLoadURL = froalaeditor_file_manager_json_path+'?path=&order=datetime&dir=image'
  require 'securerandom'
  random_name = SecureRandom.hex;
  "$('##{dom_id}').froalaEditor({
                  language: 'zh_cn',
                  imageUploadURL: '#{imageUploadURL}',
                  imageManagerLoadURL:'#{imageManagerLoadURL}',
                  fileUploadURL: '#{fileUploadURL}',
                  videoUploadURL: '#{videoUploadURL}'
              });
    "
end
main_app_root_url() click to toggle source
# File lib/xmt_froala/helper.rb, line 52
def main_app_root_url
  begin
    main_app.root_url.slice(0, main_app.root_url.rindex(main_app.root_path)) + '/'
  rescue
    '/'
  end
end