Module: Ckeditor::ViewHelper

Includes:
ActionView::Helpers
Defined in:
lib/ckeditor/view_helper.rb

Instance Method Summary collapse

Instance Method Details

#ckeditor_ajax_script(backend = 'jquery') ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/ckeditor/view_helper.rb', line 61

def ckeditor_ajax_script(backend = 'jquery')
  javascript_tag("$(document).ready(function(){  
    $('form[data-remote]').bind('ajax:before', function(){
      for (instance in CKEDITOR.instances){
        CKEDITOR.instances[instance].updateElement();
      }
    });
  });")
end

#ckeditor_textarea(object, field, options = {}) ⇒ Object

Ckeditor helper: <%= ckeditor_textarea(“object”, “field”, :width => ‘100%’, :height => ‘200px’) %>

Two forms on one page: <%= form_tag “one” %>

<%= ckeditor_textarea("object", "field", :index => "1") %>

<% end %> … <%= form_tag “two” %>

<%= ckeditor_textarea("object", "field", :index => "2") %>

<% end %>



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
# File 'lib/ckeditor/view_helper.rb', line 17

def ckeditor_textarea(object, field, options = {})
  options = options.dup.symbolize_keys
  
  var = options.delete(:object) if options.key?(:object)
  var ||= @template.instance_variable_get("@#{object}")
  
  value = var.send(field.to_sym) if var
  value ||= options.delete(:value) || ""
  
  element_id = options.delete(:id) || ckeditor_element_id(object, field, options.delete(:index))
  width  = options.delete(:width) || '100%'
  height = options.delete(:height) || '100%'
  
  textarea_options = { :id => element_id }
  
  textarea_options[:cols]  = (options.delete(:cols) || 70).to_i
  textarea_options[:rows]  = (options.delete(:rows) || 20).to_i
  textarea_options[:class] = (options.delete(:class) || 'editor').to_s
  textarea_options[:style] = "width:#{width};height:#{height}"
  
  ckeditor_options = {:width => width, :height => height }
  ckeditor_options[:language] = (options.delete(:language) || I18n.locale).to_s
  ckeditor_options[:toolbar]  = options.delete(:toolbar) if options[:toolbar]
  ckeditor_options[:skin]     = options.delete(:skin) if options[:skin]
  
  ckeditor_options[:swf_params] = options.delete(:swf_params) if options[:swf_params]
  
  ckeditor_options[:filebrowserBrowseUrl] = Ckeditor.file_manager_uri
  ckeditor_options[:filebrowserUploadUrl] = Ckeditor.file_manager_upload_uri
  
  ckeditor_options[:filebrowserImageBrowseUrl] = Ckeditor.file_manager_image_uri
  ckeditor_options[:filebrowserImageUploadUrl] = Ckeditor.file_manager_image_upload_uri
  
  output_buffer = ActiveSupport::SafeBuffer.new
    
  output_buffer << ActionView::Base::InstanceTag.new(object, field, self, var).to_text_area_tag(textarea_options.merge(options))
  
  output_buffer << javascript_tag("if (CKEDITOR.instances['#{element_id}']) { 
    CKEDITOR.remove(CKEDITOR.instances['#{element_id}']);}
    CKEDITOR.replace('#{element_id}', { #{ckeditor_applay_options(ckeditor_options)} });")
    
  output_buffer
end