Module: Ckeditor::Utils

Defined in:
lib/ckeditor/utils.rb,
lib/ckeditor/utils/javascript_code.rb,
lib/ckeditor/utils/content_type_detector.rb

Defined Under Namespace

Classes: ContentTypeDetector, JavascriptCode

Class Method Summary collapse

Class Method Details

.assets_pipeline_enabled?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
# File 'lib/ckeditor/utils.rb', line 66

def assets_pipeline_enabled?
  if Gem::Version.new(::Rails.version.to_s) >= Gem::Version.new('4.0.0')
    defined?(Sprockets::Rails)
  elsif Gem::Version.new(::Rails.version.to_s) >= Gem::Version.new('3.0.0')
    Rails.application.config.assets.enabled
  else
    false
  end
end

.escape_single_quotes(str) ⇒ Object



11
12
13
# File 'lib/ckeditor/utils.rb', line 11

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

.filethumb(filename) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/ckeditor/utils.rb', line 56

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

  image = 'unknown.gif' unless File.exist?(File.join(source, image))

  File.join(Ckeditor.relative_path, 'filebrowser/thumbs', image)
end

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ckeditor/utils.rb', line 35

def js_fileuploader(uploader_type, options = {})
  options = { multiple: true, element: 'fileupload' }.merge!(options)

  case uploader_type.to_s.downcase
  when 'image' then
    options[:action] = JavascriptCode.new('EDITOR.config.filebrowserImageUploadUrl')
    options[:allowedExtensions] = Ckeditor.image_file_types
  when 'flash' then
    options[:action] = JavascriptCode.new('EDITOR.config.filebrowserFlashUploadUrl')
    options[:allowedExtensions] = Ckeditor.flash_file_types
  else
    options[:action] = JavascriptCode.new('EDITOR.config.filebrowserUploadUrl')
    options[:allowedExtensions] = Ckeditor.attachment_file_types
  end

  js_options = ActiveSupport::JSON.encode(options)
  js_options.gsub!(/"(EDITOR\.config\.filebrowser(Image|Flash|)UploadUrl)"/, '\1')

  "(function() { new qq.FileUploaderInput(#{js_options}); }).call(this);".html_safe
end

.js_init_ckeditor(dom_id, replace) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/ckeditor/utils.rb', line 26

def js_init_ckeditor(dom_id, replace)
  %((function() {
      if (typeof CKEDITOR != 'undefined') {
        if (CKEDITOR.instances['#{dom_id}']) { CKEDITOR.instances['#{dom_id}'].destroy(); }
        #{replace}
      }
    })();)
end

.js_replace(dom_id, options = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/ckeditor/utils.rb', line 15

def js_replace(dom_id, options = nil)
  replace = if options && !options.keys.empty?
              js_options = ActiveSupport::JSON.encode(options)
              "CKEDITOR.replace('#{dom_id}', #{js_options});"
            else
              "CKEDITOR.replace('#{dom_id}');"
            end

  js_init_ckeditor(dom_id, replace)
end

.select_assets(path, relative_path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ckeditor/utils.rb', line 76

def select_assets(path, relative_path)
  relative_folder = Ckeditor.root_path.join(relative_path)
  folder = relative_folder.join(path)
  extensions = '*.{js,css,png,gif,jpg,html}'
  languages = (Ckeditor.assets_languages || [])

  # Files at root
  files = Dir[folder.join(extensions)]

  # Filter plugins
  if Ckeditor.assets_plugins.nil?
    files += Dir[folder.join('plugins', '**', extensions)]
  else
    Ckeditor.assets_plugins.each do |plugin|
      files += Dir[folder.join('plugins', plugin, '**', extensions)]
    end
  end

  # Other folders
  Dir[folder.join('*/')].each do |subfolder|
    path = Pathname.new(subfolder)
    next if ['plugins'].include?(path.basename.to_s)
    files += Dir[path.join('**', extensions)]
  end

  files.inject([]) do |items, name|
    file = Pathname.new(name)
    base = file.basename('.*').to_s

    if !name.include?('/lang/') || languages.include?(base)
      items << file.relative_path_from(relative_folder).to_s
    end

    items
  end
end