Module: TinyMCE::Helpers

Defined in:
lib/tiny_mce/helpers.rb

Overview

The helper module we include into ActionController::Base

Defined Under Namespace

Classes: TinyMCEInvalidOption, TinyMCEInvalidOptionType

Instance Method Summary collapse

Instance Method Details

#include_tiny_mce_if_needed(options = {}, raw_options = '') ⇒ Object

Form a JS include tag for the TinyMCE JS src, and form the raw JS and wrap in in a <script> tag for inclusion in the <head> for inclusion in the <head> (only if tiny mce is actually being used)



85
86
87
88
89
# File 'lib/tiny_mce/helpers.rb', line 85

def include_tiny_mce_if_needed(options = {}, raw_options = '')
  if using_tiny_mce?
    include_tiny_mce_js + tiny_mce_init(options, raw_options)
  end
end

#include_tiny_mce_jsObject

Form a JS include tag for the TinyMCE JS src for inclusion in the <head>



73
74
75
# File 'lib/tiny_mce/helpers.rb', line 73

def include_tiny_mce_js
  javascript_include_tag (Rails.env.development? ? "tiny_mce/tiny_mce_src" : "tiny_mce/tiny_mce")
end

#include_tiny_mce_js_if_neededObject

Form a JS include tag for the TinyMCE JS src for inclusion in the <head> (only if tiny mce is actually being used)



78
79
80
# File 'lib/tiny_mce/helpers.rb', line 78

def include_tiny_mce_js_if_needed
  include_tiny_mce_js if using_tiny_mce?
end

#raw_tiny_mce_init(options = {}, raw_options = '') ⇒ Object

Parse @tiny_mce_options and @raw_tiny_mce_options to create a raw JS string used by TinyMCE. Returns errors if the option or options type is invalid



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tiny_mce/helpers.rb', line 15

def raw_tiny_mce_init(options = {}, raw_options = '')
  # first we set some defaults, then we merge in the controller level options
  # and finally merge in the view level options (to give presidence)
  @tiny_mce_options ||= {}
  default_options = { 'mode' => 'textareas',
              'editor_selector' => 'mceEditor',
              'theme' => 'simple',
              'language' => (defined?(I18n) ? I18n.locale : :en) }
  options = default_options.merge(@tiny_mce_options.stringify_keys).merge(options.stringify_keys)
  raw_options = @raw_tiny_mce_options + raw_options unless @raw_tiny_mce_options.nil?

  unless options['plugins'].nil?
    raise TinyMCEInvalidOptionType.new("Invalid value of type #{options['plugins'].class} passed for TinyMCE option plugins") unless options['plugins'].kind_of?(Array)
    
    # Append the plugins we have enabled for this field to the OptionsValidator
    TinyMCE::OptionValidator.plugins += options['plugins']
  end

  tinymce_js = "tinyMCE.init({\n"
  options.sort.each_with_index do |values, index|
    key, value = values[0], values[1]
    raise TinyMCEInvalidOption.new("Invalid option #{key} passed to tinymce") unless TinyMCE::OptionValidator.valid?(key)
    tinymce_js += "#{key} : "
    case value
    when String, Symbol, Fixnum
      tinymce_js += "'#{value.to_s}'"
    when Array
      tinymce_js += '"' + value.join(',') + '"'
    when TrueClass
      tinymce_js += 'true'
    when FalseClass
      tinymce_js += 'false'
    else
      raise TinyMCEInvalidOptionType.new("Invalid value of type #{value.class} passed for TinyMCE option #{key}")
    end
    if (index < options.size - 1)
      # there are more options in this array
      tinymce_js += ",\n"
    else
      # no more options in this array. Finish it by adding the addition JS
      tinymce_js += ",\n#{raw_options}" unless raw_options.blank?
      tinymce_js += "\n"
    end
  end
  tinymce_js += "\n});"
end

#tiny_mce_init(options = {}, raw_options = '') ⇒ Object

Form the raw JS and wrap in in a <script> tag for inclusion in the <head>



63
64
65
# File 'lib/tiny_mce/helpers.rb', line 63

def tiny_mce_init(options = {}, raw_options = '')
  javascript_tag raw_tiny_mce_init(options, raw_options)
end

#tiny_mce_init_if_needed(options = {}, raw_options = '') ⇒ Object

Form the raw JS and wrap in in a <script> tag for inclusion in the <head> (only if tiny mce is actually being used)



68
69
70
# File 'lib/tiny_mce/helpers.rb', line 68

def tiny_mce_init_if_needed(options = {}, raw_options = '')
  tiny_mce_init(options, raw_options) if using_tiny_mce?
end

#using_tiny_mce?Boolean

Has uses_tiny_mce method been declared in the controller for this page?

Returns:

  • (Boolean)


9
10
11
# File 'lib/tiny_mce/helpers.rb', line 9

def using_tiny_mce?
  !@uses_tiny_mce.blank?
end