| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
def send_ajax(options, text = "", &block) |
|---|
| 11 |
|
|---|
| 12 |
if block then |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
buffer = eval("_erbout", block.binding) |
|---|
| 16 |
pos = buffer.length |
|---|
| 17 |
block.call |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
data = buffer[pos..-1] |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
buffer[pos..-1] = '' |
|---|
| 24 |
text = data |
|---|
| 25 |
end |
|---|
| 26 |
|
|---|
| 27 |
text = escape_javascript(text) |
|---|
| 28 |
|
|---|
| 29 |
case options[:action] |
|---|
| 30 |
when :insert |
|---|
| 31 |
case options[:position] |
|---|
| 32 |
when :before |
|---|
| 33 |
"new Insertion.Before('#{options[:target]}', '#{text}')" |
|---|
| 34 |
when :top |
|---|
| 35 |
"new Insertion.Top('#{options[:target]}', '#{text}')" |
|---|
| 36 |
when :bottom |
|---|
| 37 |
"new Insertion.Bottom('#{options[:target]}', '#{text}')" |
|---|
| 38 |
when :after |
|---|
| 39 |
"new Insertion.After('#{options[:target]}', '#{text}')" |
|---|
| 40 |
else |
|---|
| 41 |
raise ArgumentError, "Invalid position, choose one of :before, :top, :bottom, :after" |
|---|
| 42 |
end |
|---|
| 43 |
when :replace |
|---|
| 44 |
"$('#{options[:target]}').innerHTML = '#{text}'" |
|---|
| 45 |
when :empty |
|---|
| 46 |
"$('#{options[:target]}').innerHTML = ''" |
|---|
| 47 |
when :delete |
|---|
| 48 |
"$('#{options[:target]}').parentNode.removeChild($('#{options[:target]}'))" |
|---|
| 49 |
when :formfill |
|---|
| 50 |
raise ArgumentError, ":formfill requires :formobject" unless options[:formobject] |
|---|
| 51 |
ret = '' |
|---|
| 52 |
objectname = options[:formobject].class.to_s.underscore |
|---|
| 53 |
for key,value in options[:formobject].attributes |
|---|
| 54 |
ret << "$('#{objectname}_#{key}').value = '#{escape_javascript(value.to_s)}'\n" |
|---|
| 55 |
end |
|---|
| 56 |
return ret |
|---|
| 57 |
else |
|---|
| 58 |
raise ArgumentError, "Invalid action, choose one of :insert, :replace, :delete, :empty, :formfill" |
|---|
| 59 |
end |
|---|
| 60 |
|
|---|
| 61 |
end |
|---|