Module: Ckeditor::Utils

Defined in:
lib/forge/config/initializers/ckeditor.rb

Class Method Summary collapse

Class Method Details

.applay_options(options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/forge/config/initializers/ckeditor.rb', line 48

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(',')} ]"
      else value
    end
    
    str << %Q{"#{key}": #{item}}
  end
  
  str.sort.join(',')
end

.escape_single_quotes(str) ⇒ Object



4
5
6
# File 'lib/forge/config/initializers/ckeditor.rb', line 4

def escape_single_quotes(str)
  str.gsub('\\','\0\0').gsub('</','<\/').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
end

.filethumb(filename) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/forge/config/initializers/ckeditor.rb', line 69

def filethumb(filename)
  extname = filename.blank? ? "unknown" : File.extname(filename).gsub(/^\./, '')
  image = "#{extname}.gif"
  source = Ckeditor.root_path.join("vendor/assets/javascripts/ckeditor/filebrowser/images/thumbs")
  
  unless File.exists?(File.join(source, image))
    image = "unknown.gif"
  end
  
  File.join(Ckeditor.relative_path, "filebrowser/images/thumbs", image)
end

.js_fileuploader(uploader_type, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/forge/config/initializers/ckeditor.rb', line 28

def js_fileuploader(uploader_type, options = {})
  options = { :multiple => true, :element => "fileupload" }.merge(options)
  
  case uploader_type.to_s.downcase
    when "image" then
      options[:action] = "^EDITOR.config.filebrowserImageUploadUrl"
      options[:allowedExtensions] = Ckeditor.image_file_types
    when "flash" then
      options[:action] = "^EDITOR.config.filebrowserFlashUploadUrl"
      options[:allowedExtensions] = ["swf"]
    else
      options[:action] = "^EDITOR.config.filebrowserUploadUrl"
      options[:allowedExtensions] = Ckeditor.attachment_file_types
  end
  
  js_options = applay_options(options)
  
  "$(document).ready(function(){ new qq.FileUploaderInput({ #{js_options} }); });".html_safe
end

.js_replace(dom_id, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/forge/config/initializers/ckeditor.rb', line 15

def js_replace(dom_id, options = {})
  js_options = applay_options(options)
  js = ["if (CKEDITOR.instances['#{dom_id}']) {CKEDITOR.remove(CKEDITOR.instances['#{dom_id}']);}"]
  
  if js_options.blank?
    js << "CKEDITOR.replace('#{dom_id}');"
  else
    js << "CKEDITOR.replace('#{dom_id}', { #{js_options} });"
  end
  
  js.join
end

.parameterize_filename(filename) ⇒ Object



8
9
10
11
12
13
# File 'lib/forge/config/initializers/ckeditor.rb', line 8

def parameterize_filename(filename)
  extension = File.extname(filename)
  basename = filename.gsub(/#{extension}$/, "")
  
  [basename.parameterize('_'), extension].join.downcase
end