Module: ActionView::Helpers::FormHelper

Defined in:
lib/textile_editor.rb

Instance Method Summary collapse

Instance Method Details

#textile_editor(object_name, method, options = {}) ⇒ Object

Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). Additional options on the input tag can be passed as a hash with options and places the textile toolbar above it

Examples

textile_editor(:post, :body, :cols => 20, :rows => 40)
# => <textarea cols="20" rows="40" id="post_body" name="post[body]">
#      #{@post.body}
#    </textarea>

textile_editor(:comment, :text, :size => "20x30")
# => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
#      #{@comment.text}
#    </textarea>

textile_editor(:application, :notes, :cols => 40, :rows => 15, :class => 'app_input')
# => <textarea cols="40" rows="15" id="application_notes" name="application[notes]" class="app_input">
#      #{@application.notes}
#    </textarea>

textile_editor(:entry, :body, :size => "20x20", :disabled => 'disabled')
# => <textarea cols="20" rows="20" id="entry_body" name="entry[body]" disabled="disabled">
#      #{@entry.body}
#    </textarea>


38
39
40
41
42
43
44
# File 'lib/textile_editor.rb', line 38

def textile_editor(object_name, method, options = {})        
  editor_id = options[:id] || '%s_%s' % [object_name, method]
  mode      = options.delete(:simple) ? 'simple' : 'extended'
  (@textile_editor_ids ||= []) << [editor_id.to_s, mode.to_s]

  InstanceTag.new(object_name, method, self, options.delete(:object)).to_text_area_tag(options)
end

#textile_editor_button(text, options = {}) ⇒ Object

registers a new button for the Textile Editor toolbar Parameters:

* +text+: text to display (contents of button tag, so HTML is valid as well)
* +options+: options Hash as supported by +content_tag+ helper in Rails

Example:

The following example adds a button labeled 'Greeting' which triggers an
alert:

<% textile_editor_button 'Greeting', :onclick => "alert('Hello!')" %>

Note: this method must be called before textile_editor_initialize



69
70
71
72
73
74
# File 'lib/textile_editor.rb', line 69

def textile_editor_button(text, options={})
  return textile_editor_button_separator  if text == :separator
  button = (:button, text, options)
  button = "TextileEditor.buttons.push(\"%s\");" % escape_javascript(button)
  (@textile_editor_buttons ||= []) << button
end

#textile_editor_button_separator(options = {}) ⇒ Object



76
77
78
79
# File 'lib/textile_editor.rb', line 76

def textile_editor_button_separator(options={})
  button = "TextileEditor.buttons.push(new TextileEditorButtonSeparator('%s'));" % (options[:simple] || '')
  (@textile_editor_buttons ||= []) << button
end

#textile_editor_initialize(*dom_ids) ⇒ Object

adds the necessary javascript include tags, stylesheet tags, and load event with necessary javascript to active textile editor(s) sample output:

<link href="/stylesheets/textile-editor.css" media="screen" rel="Stylesheet" type="text/css" />
<script src="/javascripts/textile-editor.js" type="text/javascript"></script>
<script type="text/javascript">
document.observe('dom:loaded', function() {
TextileEditor.initialize('article_body', 'extended');
TextileEditor.initialize('article_body_excerpt', 'simple');
});
</script>

Note: in the case of this helper being called via AJAX, the output will be reduced:

<script type="text/javascript">
TextileEditor.initialize('article_body', 'extended');
TextileEditor.initialize('article_body_excerpt', 'simple');
</script>

This means that the support files must be loaded outside of the AJAX request, either via a call to this helper or the textile_editor_support() helper



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/textile_editor.rb', line 108

def textile_editor_initialize(*dom_ids)
  options = textile_editor_options.dup

  # extract options from last argument if it's a hash
  if dom_ids.last.is_a?(Hash)
    hash = dom_ids.last.dup
    options.merge! hash
    dom_ids.last.delete :framework
  end

  editor_ids = (@textile_editor_ids || []) + textile_extract_dom_ids(*dom_ids)
  editor_buttons = (@textile_editor_buttons || [])
  output = []
  output << textile_editor_support unless request.xhr?
  output << '<script type="text/javascript">'
  output << '/* <![CDATA[ */'
  
  if !request.xhr?
    case options[:framework]
    when :prototype
      output << %{document.observe('dom:loaded', function() \{}
    when :jquery
      output << %{$(document).ready(function() \{}
    end
  end      

  # output << %q{TextileEditor.framework = '%s';} % options[:framework].to_s
  output << editor_buttons.join("\n") if editor_buttons.any?
  editor_ids.each do |editor_id, mode|
    output << %q{TextileEditor.initialize('%s', '%s');} % [editor_id, mode || 'extended']
  end
  output << '});' unless request.xhr?

  output << '/* ]]> */'
  output << '</script>'
  output.join("\n").html_safe
end

#textile_editor_options(options = {}) ⇒ Object



46
47
48
# File 'lib/textile_editor.rb', line 46

def textile_editor_options(options={})
  (@textile_editor_options ||= { :framework => :prototype }).merge! options
end

#textile_editor_supportObject



50
51
52
53
54
55
# File 'lib/textile_editor.rb', line 50

def textile_editor_support
  output = []
  output << stylesheet_link_tag('textile-editor') 
  output << javascript_include_tag('textile-editor')
  output.join("\n").html_safe
end

#textile_extract_dom_ids(*dom_ids) ⇒ Object



81
82
83
84
85
86
# File 'lib/textile_editor.rb', line 81

def textile_extract_dom_ids(*dom_ids)
  hash = dom_ids.last.is_a?(Hash) ? dom_ids.pop : {}
  hash.inject(dom_ids) do |ids, (object, fields)|
    ids + Array(fields).map { |field| "%s_%s" % [object, field] }
  end
end