Permalink
Browse files

Merge pull request #1995 from guilleiguaran/prototype-ujs-fix

Prototype rails.js fixes for 3-0-stable
  • Loading branch information...
2 parents c8ec8f7 + 8226dfc commit d7d0c256655e362c4307de0be73c7e6b4b96637c @tenderlove tenderlove committed Aug 23, 2011
Showing with 86 additions and 75 deletions.
  1. +86 −75 railties/lib/rails/generators/rails/app/templates/public/javascripts/rails.js
@@ -1,4 +1,14 @@
(function() {
+ Ajax.Responders.register({
+ onCreate: function(request) {
+ var token = $$('meta[name=csrf-token]')[0];
+ if (token) {
+ if (!request.options.requestHeaders) request.options.requestHeaders = {};
+ request.options.requestHeaders['X-CSRF-Token'] = token.readAttribute('content');
+ }
+ }
+ });
+
// Technique from Juriy Zaytsev
// http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
function isEventSupported(eventName) {
@@ -14,33 +24,33 @@
}
function isForm(element) {
- return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM';
}
function isInput(element) {
if (Object.isElement(element)) {
- var name = element.nodeName.toUpperCase()
- return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
+ var name = element.nodeName.toUpperCase();
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA';
}
- else return false
+ else return false;
}
var submitBubbles = isEventSupported('submit'),
- changeBubbles = isEventSupported('change')
+ changeBubbles = isEventSupported('change');
if (!submitBubbles || !changeBubbles) {
// augment the Event.Handler class to observe custom events when needed
Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
function(init, element, eventName, selector, callback) {
- init(element, eventName, selector, callback)
+ init(element, eventName, selector, callback);
// is the handler being attached to an element that doesn't support this event?
if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
(!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
// "submit" => "emulated:submit"
- this.eventName = 'emulated:' + this.eventName
+ this.eventName = 'emulated:' + this.eventName;
}
}
- )
+ );
}
if (!submitBubbles) {
@@ -49,26 +59,26 @@
// special handler for the real "submit" event (one-time operation)
if (!form.retrieve('emulated:submit')) {
form.on('submit', function(submitEvent) {
- var emulated = form.fire('emulated:submit', submitEvent, true)
+ var emulated = form.fire('emulated:submit', submitEvent, true);
// if custom event received preventDefault, cancel the real one too
- if (emulated.returnValue === false) submitEvent.preventDefault()
- })
- form.store('emulated:submit', true)
+ if (emulated.returnValue === false) submitEvent.preventDefault();
+ });
+ form.store('emulated:submit', true);
}
- })
+ });
}
if (!changeBubbles) {
// discover form inputs on the page
- document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
+ document.on('focusin', 'input, select, textarea', function(focusEvent, input) {
// special handler for real "change" events
if (!input.retrieve('emulated:change')) {
input.on('change', function(changeEvent) {
- input.fire('emulated:change', changeEvent, true)
- })
- input.store('emulated:change', true)
+ input.fire('emulated:change', changeEvent, true);
+ });
+ input.store('emulated:change', true);
}
- })
+ });
}
function handleRemote(element) {
@@ -80,7 +90,10 @@
if (element.tagName.toLowerCase() === 'form') {
method = element.readAttribute('method') || 'post';
url = element.readAttribute('action');
- params = element.serialize();
+ // serialize the form with respect to the submit button that was pressed
+ params = element.serialize({ submit: element.retrieve('rails:submit-button') });
+ // clear the pressed submit button information
+ element.store('rails:submit-button', null);
} else {
method = element.readAttribute('data-method') || 'get';
url = element.readAttribute('href');
@@ -92,100 +105,98 @@
parameters: params,
evalScripts: true,
- onComplete: function(request) { element.fire("ajax:complete", request); },
- onSuccess: function(request) { element.fire("ajax:success", request); },
- onFailure: function(request) { element.fire("ajax:failure", request); }
+ onCreate: function(response) { element.fire("ajax:create", response); },
+ onComplete: function(response) { element.fire("ajax:complete", response); },
+ onSuccess: function(response) { element.fire("ajax:success", response); },
+ onFailure: function(response) { element.fire("ajax:failure", response); }
});
element.fire("ajax:after");
}
+ function insertHiddenField(form, name, value) {
+ form.insert(new Element('input', { type: 'hidden', name: name, value: value }));
+ }
+
function handleMethod(element) {
var method = element.readAttribute('data-method'),
url = element.readAttribute('href'),
csrf_param = $$('meta[name=csrf-param]')[0],
csrf_token = $$('meta[name=csrf-token]')[0];
var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
- element.parentNode.insert(form);
+ $(element.parentNode).insert(form);
if (method !== 'post') {
- var field = new Element('input', { type: 'hidden', name: '_method', value: method });
- form.insert(field);
+ insertHiddenField(form, '_method', method);
}
if (csrf_param) {
- var param = csrf_param.readAttribute('content'),
- token = csrf_token.readAttribute('content'),
- field = new Element('input', { type: 'hidden', name: param, value: token });
- form.insert(field);
+ insertHiddenField(form, csrf_param.readAttribute('content'), csrf_token.readAttribute('content'));
}
form.submit();
}
+ function disableFormElements(form) {
+ form.select('input[type=submit][data-disable-with]').each(function(input) {
+ input.store('rails:original-value', input.getValue());
+ input.setValue(input.readAttribute('data-disable-with')).disable();
+ });
+ }
+
+ function enableFormElements(form) {
+ form.select('input[type=submit][data-disable-with]').each(function(input) {
+ input.setValue(input.retrieve('rails:original-value')).enable();
+ });
+ }
- document.on("click", "*[data-confirm]", function(event, element) {
+ function allowAction(element) {
var message = element.readAttribute('data-confirm');
- if (!confirm(message)) event.stop();
- });
+ return !message || confirm(message);
+ }
+
+ document.on('click', 'a[data-confirm], a[data-remote], a[data-method]', function(event, link) {
+ if (!allowAction(link)) {
+ event.stop();
+ return false;
+ }
- document.on("click", "a[data-remote]", function(event, element) {
- if (event.stopped) return;
- handleRemote(element);
- event.stop();
+ if (link.readAttribute('data-remote')) {
+ handleRemote(link);
+ event.stop();
+ } else if (link.readAttribute('data-method')) {
+ handleMethod(link);
+ event.stop();
+ }
});
- document.on("click", "a[data-method]", function(event, element) {
- if (event.stopped) return;
- handleMethod(element);
- event.stop();
+ document.on("click", "form input[type=submit], form button[type=submit], form button:not([type])", function(event, button) {
+ // register the pressed submit button
+ event.findElement('form').store('rails:submit-button', button.name || false);
});
document.on("submit", function(event) {
- var element = event.findElement(),
- message = element.readAttribute('data-confirm');
- if (message && !confirm(message)) {
+ var form = event.findElement();
+
+ if (!allowAction(form)) {
event.stop();
return false;
}
- var inputs = element.select("input[type=submit][data-disable-with]");
- inputs.each(function(input) {
- input.disabled = true;
- input.writeAttribute('data-original-value', input.value);
- input.value = input.readAttribute('data-disable-with');
- });
-
- var element = event.findElement("form[data-remote]");
- if (element) {
- handleRemote(element);
+ if (form.readAttribute('data-remote')) {
+ handleRemote(form);
event.stop();
+ } else {
+ disableFormElements(form);
}
});
- document.on("ajax:after", "form", function(event, element) {
- var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
- inputs.each(function(input) {
- input.value = input.readAttribute('data-original-value');
- input.removeAttribute('data-original-value');
- input.disabled = false;
- });
+ document.on('ajax:create', 'form', function(event, form) {
+ if (form == event.findElement()) disableFormElements(form);
});
-
- Ajax.Responders.register({
- onCreate: function(request) {
- var csrf_meta_tag = $$('meta[name=csrf-token]')[0];
-
- if (csrf_meta_tag) {
- var header = 'X-CSRF-Token',
- token = csrf_meta_tag.readAttribute('content');
-
- if (!request.options.requestHeaders) {
- request.options.requestHeaders = {};
- }
- request.options.requestHeaders[header] = token;
- }
- }
+
+ document.on('ajax:complete', 'form', function(event, form) {
+ if (form == event.findElement()) enableFormElements(form);
});
})();

0 comments on commit d7d0c25

Please sign in to comment.