ckeditor.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:4k
源码类别:

Ajax

开发平台:

Others

  1. # Ckeditor
  2. module Ckeditor
  3.   begin
  4.     CONFIG = YAML.load_file("#{RAILS_ROOT}/config/ckeditor.yml")[RAILS_ENV]
  5.   rescue => e
  6.     CONFIG = nil
  7.   end
  8.   PLUGIN_NAME = 'easy-ckeditor'
  9.   PLUGIN_PATH = "#{RAILS_ROOT}/vendor/plugins/#{PLUGIN_NAME}"
  10.   PLUGIN_PUBLIC_PATH = "#{RAILS_ROOT}/public/files"
  11.   PLUGIN_PUBLIC_URI = "/files"
  12.   PLUGIN_CONTROLLER_PATH = "#{PLUGIN_PATH}/app/controllers"
  13.   PLUGIN_VIEWS_PATH = "#{PLUGIN_PATH}/app/views"
  14.   PLUGIN_HELPER_PATH = "#{PLUGIN_PATH}/app/helpers"
  15.   PLUGIN_FILE_MANAGER_URI = ''
  16.   PLUGIN_FILE_MANAGER_UPLOAD_URI = '/ckeditor/upload'
  17.   module Helper
  18.     def ckeditor_textarea(object, field, options = {})
  19.       var = instance_variable_get("@#{object}")
  20.       if var
  21.         value = var.send(field.to_sym)
  22.         value = value.nil? ? "" : value
  23.       else
  24.         value = ""
  25.         klass = "#{object}".camelcase.constantize
  26.         instance_variable_set("@#{object}", eval("#{klass}.new()"))
  27.       end
  28.       id = ckeditor_element_id(object, field)
  29.       cols = options[:cols].nil? ? "cols='20'" : "cols='"+options[:cols]+"'"
  30.       rows = options[:rows].nil? ? "rows='20'" : "rows='"+options[:rows]+"'"
  31.       width = options[:width].nil? ? '100%' : options[:width]
  32.       height = options[:height].nil? ? '100%' : options[:height]
  33.       classy = options[:class].nil? ? '' : "class='#{options[:class]}'"
  34.       toolbarSet = options[:toolbarSet].nil? ? 'Default' : options[:toolbarSet]
  35.       if options[:ajax]
  36.         inputs = "<input type='hidden' id='#{id}_hidden' name='#{object}[#{field}]'>n" <<
  37.                  "<textarea id='#{id}' #{cols} #{rows} name='#{id}'>#{value}</textarea>n"
  38.       else
  39.         inputs = "<textarea id='#{id}' style='width:#{width};height:#{height}' #{cols} #{rows} #{classy} name='#{object}[#{field}]'>#{h value}</textarea>n"
  40.       end
  41.       js_path = "#{controller.relative_url_root}/javascripts"
  42.       base_path = "#{js_path}/ckeditor/"
  43.       return inputs <<
  44.         javascript_tag("CKEDITOR.replace('#{object}[#{field}]', {
  45.     filebrowserBrowseUrl : '#{PLUGIN_FILE_MANAGER_URI}',
  46.     filebrowserUploadUrl : '#{PLUGIN_FILE_MANAGER_UPLOAD_URI}'
  47. });n")
  48.     end
  49.     def ckeditor_form_remote_tag(options = {})
  50.       editors = options[:editors]
  51.       before = ""
  52.       editors.keys.each do |e|
  53.         editors[e].each do |f|
  54.           before += ckeditor_before_js(e, f)
  55.         end
  56.       end
  57.       options[:before] = options[:before].nil? ? before : before + options[:before]
  58.       form_remote_tag(options)
  59.     end
  60.     def ckeditor_remote_form_for(object_name, *args, &proc)
  61.       options = args.last.is_a?(Hash) ? args.pop : {}
  62.       concat(ckeditor_form_remote_tag(options), proc.binding)
  63.       fields_for(object_name, *(args << options), &proc)
  64.       concat('</form>', proc.binding)
  65.     end
  66.     alias_method :ckeditor_form_remote_for, :ckeditor_remote_form_for
  67.     def ckeditor_element_id(object, field)
  68.       "#{object}__#{field}_editor"
  69.     end
  70.     def ckeditor_div_id(object, field)
  71.       id = eval("@#{object}.id")
  72.       "div-#{object}-#{id}-#{field}-editor"
  73.     end
  74.     def ckeditor_before_js(object, field)
  75.       id = ckeditor_element_id(object, field)
  76.       "var oEditor = CKEDITOR.instances.#{id}.getData();"
  77.     end
  78.   end
  79. end
  80. include ActionView
  81. module ActionView::Helpers::AssetTagHelper
  82.   alias_method :rails_javascript_include_tag, :javascript_include_tag
  83.   #  <%= javascript_include_tag :defaults, :ckeditor %>
  84.   def javascript_include_tag(*sources)
  85.     main_sources, application_source = [], []
  86.     if sources.include?(:ckeditor)
  87.       sources.delete(:ckeditor)
  88.       sources.push('ckeditor/ckeditor')
  89.     end
  90.     unless sources.empty?
  91.       main_sources = rails_javascript_include_tag(*sources).split("n")
  92.       application_source = main_sources.pop if main_sources.last.include?('application.js')
  93.     end
  94.     [main_sources.join("n"), application_source].join("n")
  95.   end
  96. end