| | 155 | |
|---|
| | 156 | # helper function to send a javascript command to the client. |
|---|
| | 157 | # options hash should have: |
|---|
| | 158 | # :action => (:insert, :replace, :delete, :empty) |
|---|
| | 159 | # :target => DOM element. (string name) |
|---|
| | 160 | # :position => (:before, :top, :bottom, :after) |
|---|
| | 161 | # postion is only used in the insert routines |
|---|
| | 162 | # pass in a block of captured erb html for functions that make sense to do so. |
|---|
| | 163 | def send_ajax(options, &block) |
|---|
| | 164 | |
|---|
| | 165 | if block then |
|---|
| | 166 | # execute the block= |
|---|
| | 167 | # this code stolen from the capture helper |
|---|
| | 168 | buffer = eval("_erbout", block.binding) |
|---|
| | 169 | pos = buffer.length |
|---|
| | 170 | block.call |
|---|
| | 171 | |
|---|
| | 172 | # extract the block |
|---|
| | 173 | data = buffer[pos..-1] |
|---|
| | 174 | |
|---|
| | 175 | # replace it in the original with empty string |
|---|
| | 176 | buffer[pos..-1] = '' |
|---|
| | 177 | text = escape_javascript(data) |
|---|
| | 178 | end |
|---|
| | 179 | |
|---|
| | 180 | case options[:action] |
|---|
| | 181 | when :insert |
|---|
| | 182 | case options[:position] |
|---|
| | 183 | when :before |
|---|
| | 184 | "new Insertion.Before('#{options[:target]}', '#{text}')" |
|---|
| | 185 | when :top |
|---|
| | 186 | "new Insertion.Top('#{options[:target]}', '#{text}')" |
|---|
| | 187 | when :bottom |
|---|
| | 188 | "new Insertion.Bottom('#{options[:target]}', '#{text}')" |
|---|
| | 189 | when :after |
|---|
| | 190 | "new Insertion.After('#{options[:target]}', '#{text}')" |
|---|
| | 191 | else |
|---|
| | 192 | raise ArgumentError, "Invalid position, choose one of :before, :top, :bottom, :after" |
|---|
| | 193 | end |
|---|
| | 194 | when :replace |
|---|
| | 195 | "$('#{target}').innerHTML = '#{text}'" |
|---|
| | 196 | when :empty |
|---|
| | 197 | "$('#{target}').innerHTML = ''" |
|---|
| | 198 | when :delete |
|---|
| | 199 | "$('#{target}').parentNode.removeChild($('#{options[:target]}'))" |
|---|
| | 200 | else |
|---|
| | 201 | raise ArgumentError, "Invalid action, choose one of :insert, :replace, :delete, :empty" |
|---|
| | 202 | end |
|---|
| | 203 | |
|---|
| | 204 | end |
|---|