From 8e76a5920e2daaf0ea44b5b6195b75788c5de44c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 26 Mar 2005 15:35:06 +0000 Subject: Added other DOM manipulation positions than just replace git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1006 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/javascript_helper.rb | 5 +- .../action_view/helpers/javascripts/prototype.js | 67 +++++++++++++++++++++- 2 files changed, 68 insertions(+), 4 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 513074cddd..1d70073069 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -158,8 +158,9 @@ module ActionView js_options = build_callbacks(options) js_options['asynchronous'] = options[:type] != :synchronous - js_options['method'] = options[:method] if options[:method] - js_options['effect'] = ("\'"+options[:effect].to_s+"\'") if options[:effect] + js_options['method'] = options[:method] if options[:method] + js_options['position'] = options[:position] ? "'#{options[:position].to_s}'" : "'replace'" + js_options['effect'] = ("\'"+options[:effect].to_s+"\'") if options[:effect] if options[:form] js_options['parameters'] = 'Form.serialize(this)' diff --git a/actionpack/lib/action_view/helpers/javascripts/prototype.js b/actionpack/lib/action_view/helpers/javascripts/prototype.js index d634c5c6b5..60dbd5d419 100644 --- a/actionpack/lib/action_view/helpers/javascripts/prototype.js +++ b/actionpack/lib/action_view/helpers/javascripts/prototype.js @@ -135,7 +135,8 @@ Ajax.Base.prototype = { this.options = { method: 'post', asynchronous: true, - parameters: '' + parameters: '', + position: 'replace' }.extend(options || {}); } } @@ -204,9 +205,17 @@ Ajax.Updater.prototype = (new Ajax.Base()).extend({ updateContent: function() { this.container.innerHTML = this.request.transport.responseText; + + if (this.options.position.toLowerCase() == 'replace') { + this.container.innerHTML = this.request.transport.responseText; + } else { + Insert[this.options.position.toLowerCase()]( this.container, this.request.transport.responseText ); + } + switch(this.options.effect) { case 'highlight': new YellowFader(this.container); break; } + if (this.onComplete) this.onComplete(this.request); } }); @@ -382,4 +391,58 @@ YellowFader.prototype = { highlight: function(element, current) { element.style.backgroundColor = "#ffff" + current.toColorPart(); } -} \ No newline at end of file +} + +/*--------------------------------------------------------------------------*/ + +Insert = { + before_begin: function(dom, html) { + dom = $(dom); + if (dom.insertAdjacentHTML) { + dom.insertAdjacentHTML('BeforeBegin', html); + } else { + var r = dom.ownerDocument.createRange(); + r.setStartBefore(dom); + var df = r.createContextualFragment(html); + dom.parentNode.insertBefore(df, dom); + } + }, + + after_begin: function(dom, html) { + dom = $(dom); + if (dom.insertAdjacentHTML) { + dom.insertAdjacentHTML('AfterBegin', html); + } else { + var r = dom.ownerDocument.createRange(); + r.selectNodeContents(dom); + r.collapse(true); + var df = r.createContextualFragment( html ); + dom.insertBefore(df, dom.firstChild ); + } + }, + + before_end: function(dom, html) { + dom = $(dom); + if (dom.insertAdjacentHTML) { + dom.insertAdjacentHTML('BeforeEnd', html); + } else { + var r = dom.ownerDocument.createRange(); + r.selectNodeContents(dom); + r.collapse(dom); + var df = r.createContextualFragment(html); + dom.appendChild(df); + } + }, + + after_end: function(dom, html) { + dom = $(dom); + if (dom.insertAdjacentHTML) { + dom.insertAdjacentHTML('BeforeBegin', html); + } else { + var r = dom.ownerDocument.createRange(); + r.setStartAfter(dom); + var df = r.createContextualFragment( html ); + dom.parentNode.insertBefore(df, dom.nextSibling); + } + } +}; \ No newline at end of file -- cgit v1.2.3