aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-06-28 17:42:51 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-06-28 17:42:51 +0000
commit62ed6950c9119c12e6b17a68c79c45c77a7b1ca4 (patch)
treec00beda4e2f4ea5e88f39882dc421cb8b7e68a0d /actionpack/lib/action_view/helpers
parent96e590ca3c25e63d03c9ae5d74a40538cbe88f58 (diff)
downloadrails-62ed6950c9119c12e6b17a68c79c45c77a7b1ca4.tar.gz
rails-62ed6950c9119c12e6b17a68c79c45c77a7b1ca4.tar.bz2
rails-62ed6950c9119c12e6b17a68c79c45c77a7b1ca4.zip
Added support for upload progress indicators in Apache and lighttpd 1.4.x (won't work in WEBrick or lighttpd 1.3.x) #1475 [Sean Treadway]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1552 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r--actionpack/lib/action_view/helpers/javascript_helper.rb5
-rw-r--r--actionpack/lib/action_view/helpers/javascripts/prototype.js71
2 files changed, 67 insertions, 9 deletions
diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb
index 6495ebf655..a0a4076103 100644
--- a/actionpack/lib/action_view/helpers/javascript_helper.rb
+++ b/actionpack/lib/action_view/helpers/javascript_helper.rb
@@ -124,7 +124,7 @@ module ActionView
code = "new PeriodicalExecuter(function() {#{remote_function(options)}}, #{frequency})"
content_tag("script", code, options[:html_options] || {})
end
-
+
# Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular
# reloading POST arrangement. Even though it's using Javascript to serialize the form elements, the form submission
# will work just like a regular submission as viewed by the receiving side (all elements available in @params).
@@ -373,6 +373,7 @@ module ActionView
js_options['asynchronous'] = options[:type] != :synchronous
js_options['method'] = method_option_to_s(options[:method]) if options[:method]
js_options['insertion'] = "Insertion.#{options[:position].to_s.camelize}" if options[:position]
+ js_options['script'] = options[:script] == true if options[:script]
if options[:form]
js_options['parameters'] = 'Form.serialize(this)'
@@ -382,7 +383,7 @@ module ActionView
options_for_javascript(js_options)
end
-
+
def method_option_to_s(method)
(method.is_a?(String) and !method.index("'").nil?) ? method : "'#{method}'"
end
diff --git a/actionpack/lib/action_view/helpers/javascripts/prototype.js b/actionpack/lib/action_view/helpers/javascripts/prototype.js
index 02d103498e..0d23a344d9 100644
--- a/actionpack/lib/action_view/helpers/javascripts/prototype.js
+++ b/actionpack/lib/action_view/helpers/javascripts/prototype.js
@@ -254,6 +254,7 @@ Ajax.Updater.prototype = (new Ajax.Base()).extend({
failure: container.failure ? $(container.failure) : null
}
+ this.script_re = /<script.*?>((?:\n|.)*?)<\/script>/im;
this.setOptions(options);
if (this.options.asynchronous) {
@@ -271,23 +272,79 @@ Ajax.Updater.prototype = (new Ajax.Base()).extend({
var receiver =
(this.request.transport.status == 200) ?
this.containers.success : this.containers.failure;
+
+ var response = this.request.transport.responseText.replace(
+ this.script_re, '');
+
+ var scripts = this.request.transport.responseText.match(
+ this.script_re);
if (receiver) {
if (this.options.insertion) {
- new this.options.insertion(receiver,
- this.request.transport.responseText);
+ new this.options.insertion(receiver, response);
} else {
- receiver.innerHTML = this.request.transport.responseText;
+ receiver.innerHTML = response;
}
}
-
- if (this.request.transport.status == 200 && this.onComplete) {
- setTimeout((function() {this.onComplete(
- this.request.transport)}).bind(this), 10);
+
+ if (this.request.transport.status == 200) {
+ if (this.onComplete) {
+ setTimeout((function() {this.onComplete(
+ this.request.transport)}).bind(this), 10);
+ }
+ if (this.options.script && scripts) {
+ setTimeout((function() { eval(scripts[1]) }).bind(this), 10);
+ }
+ }
+ }
+});
+
+Ajax.PeriodicalUpdater = Class.create();
+Ajax.PeriodicalUpdater.prototype = (new Ajax.Base()).extend({
+ initialize: function(container, url, options) {
+ this.setOptions(options);
+ this.onComplete = this.options.onComplete;
+
+ this.frequency = (this.options.frequency || 2);
+ this.decay = 1;
+
+ this.updater = {};
+ this.container = container;
+ this.url = url;
+
+ this.start();
+ },
+
+ start: function() {
+ this.options.onComplete = this.updateComplete.bind(this);
+ this.onTimerEvent();
+ },
+
+ stop: function() {
+ this.updater.onComplete = undefined;
+ clearTimeout(this.timer);
+ (this.onComplete || Ajax.emptyFunction).apply(this, arguments);
+ },
+
+ updateComplete: function(request) {
+ if (this.options.decay) {
+ this.decay = (request.responseText == this.lastText ?
+ this.decay * this.options.decay : 1);
+
+ this.lastText = request.responseText;
}
+ this.timer = setTimeout(this.onTimerEvent.bind(this),
+ this.decay * this.frequency * 1000);
+ },
+
+ onTimerEvent: function() {
+ this.updater = new Ajax.Updater(this.container, this.url, this.options);
}
+
});
+/*--------------------------------------------------------------------------*/
+
document.getElementsByClassName = function(className) {
var children = document.getElementsByTagName('*') || document.all;
var elements = new Array();