devel
This commit is contained in:
parent
081bf95ce7
commit
b9561278d0
53 changed files with 2893 additions and 9554 deletions
18
public/javascripts/jquery.js
vendored
Executable file
18
public/javascripts/jquery.js
vendored
Executable file
File diff suppressed because one or more lines are too long
191
public/javascripts/old_rails.js
Executable file
191
public/javascripts/old_rails.js
Executable file
|
@ -0,0 +1,191 @@
|
|||
(function() {
|
||||
// Technique from Juriy Zaytsev
|
||||
// http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
|
||||
function isEventSupported(eventName) {
|
||||
var el = document.createElement('div');
|
||||
eventName = 'on' + eventName;
|
||||
var isSupported = (eventName in el);
|
||||
if (!isSupported) {
|
||||
el.setAttribute(eventName, 'return;');
|
||||
isSupported = typeof el[eventName] == 'function';
|
||||
}
|
||||
el = null;
|
||||
return isSupported;
|
||||
}
|
||||
|
||||
function isForm(element) {
|
||||
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'
|
||||
}
|
||||
else return false
|
||||
}
|
||||
|
||||
var submitBubbles = isEventSupported('submit'),
|
||||
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)
|
||||
// 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
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (!submitBubbles) {
|
||||
// discover forms on the page by observing focus events which always bubble
|
||||
document.on('focusin', 'form', function(focusEvent, form) {
|
||||
// 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)
|
||||
// if custom event received preventDefault, cancel the real one too
|
||||
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) {
|
||||
// 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function handleRemote(element) {
|
||||
var method, url, params;
|
||||
|
||||
var event = element.fire("ajax:before");
|
||||
if (event.stopped) return false;
|
||||
|
||||
if (element.tagName.toLowerCase() === 'form') {
|
||||
method = element.readAttribute('method') || 'post';
|
||||
url = element.readAttribute('action');
|
||||
params = element.serialize();
|
||||
} else {
|
||||
method = element.readAttribute('data-method') || 'get';
|
||||
url = element.readAttribute('href');
|
||||
params = {};
|
||||
}
|
||||
|
||||
new Ajax.Request(url, {
|
||||
method: method,
|
||||
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); }
|
||||
});
|
||||
|
||||
element.fire("ajax:after");
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (method !== 'post') {
|
||||
var field = new Element('input', { type: 'hidden', name: '_method', value: method });
|
||||
form.insert(field);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
form.submit();
|
||||
}
|
||||
|
||||
|
||||
document.on("click", "*[data-confirm]", function(event, element) {
|
||||
var message = element.readAttribute('data-confirm');
|
||||
if (!confirm(message)) event.stop();
|
||||
});
|
||||
|
||||
document.on("click", "a[data-remote]", function(event, element) {
|
||||
if (event.stopped) return;
|
||||
handleRemote(element);
|
||||
event.stop();
|
||||
});
|
||||
|
||||
document.on("click", "a[data-method]", function(event, element) {
|
||||
if (event.stopped) return;
|
||||
handleMethod(element);
|
||||
event.stop();
|
||||
});
|
||||
|
||||
document.on("submit", function(event) {
|
||||
var element = event.findElement(),
|
||||
message = element.readAttribute('data-confirm');
|
||||
if (message && !confirm(message)) {
|
||||
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);
|
||||
event.stop();
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
});
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
})();
|
|
@ -1,191 +1,331 @@
|
|||
(function() {
|
||||
// Technique from Juriy Zaytsev
|
||||
// http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
|
||||
function isEventSupported(eventName) {
|
||||
var el = document.createElement('div');
|
||||
eventName = 'on' + eventName;
|
||||
var isSupported = (eventName in el);
|
||||
if (!isSupported) {
|
||||
el.setAttribute(eventName, 'return;');
|
||||
isSupported = typeof el[eventName] == 'function';
|
||||
}
|
||||
el = null;
|
||||
return isSupported;
|
||||
}
|
||||
/**
|
||||
* Unobtrusive scripting adapter for jQuery
|
||||
*
|
||||
* Requires jQuery 1.6.0 or later.
|
||||
* https://github.com/rails/jquery-ujs
|
||||
|
||||
function isForm(element) {
|
||||
return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
|
||||
}
|
||||
* Uploading file using rails.js
|
||||
* =============================
|
||||
*
|
||||
* By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
|
||||
* in the remote form, this adapter aborts the AJAX submission and allows the form to submit through standard means.
|
||||
*
|
||||
* The `ajax:aborted:file` event allows you to bind your own handler to process the form submission however you wish.
|
||||
*
|
||||
* Ex:
|
||||
* $('form').live('ajax:aborted:file', function(event, elements){
|
||||
* // Implement own remote file-transfer handler here for non-blank file inputs passed in `elements`.
|
||||
* // Returning false in this handler tells rails.js to disallow standard form submission
|
||||
* return false;
|
||||
* });
|
||||
*
|
||||
* The `ajax:aborted:file` event is fired when a file-type input is detected with a non-blank value.
|
||||
*
|
||||
* Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
|
||||
* techniques like the iframe method to upload the file instead.
|
||||
*
|
||||
* Required fields in rails.js
|
||||
* ===========================
|
||||
*
|
||||
* If any blank required inputs (required="required") are detected in the remote form, the whole form submission
|
||||
* is canceled. Note that this is unlike file inputs, which still allow standard (non-AJAX) form submission.
|
||||
*
|
||||
* The `ajax:aborted:required` event allows you to bind your own handler to inform the user of blank required inputs.
|
||||
*
|
||||
* !! Note that Opera does not fire the form's submit event if there are blank required inputs, so this event may never
|
||||
* get fired in Opera. This event is what causes other browsers to exhibit the same submit-aborting behavior.
|
||||
*
|
||||
* Ex:
|
||||
* $('form').live('ajax:aborted:required', function(event, elements){
|
||||
* // Returning false in this handler tells rails.js to submit the form anyway.
|
||||
* // The blank required inputs are passed to this function in `elements`.
|
||||
* return ! confirm("Would you like to submit the form with missing info?");
|
||||
* });
|
||||
*/
|
||||
|
||||
function isInput(element) {
|
||||
if (Object.isElement(element)) {
|
||||
var name = element.nodeName.toUpperCase()
|
||||
return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
|
||||
}
|
||||
else return false
|
||||
}
|
||||
(function($, undefined) {
|
||||
// Shorthand to make it a little easier to call public rails functions from within rails.js
|
||||
var rails;
|
||||
|
||||
var submitBubbles = isEventSupported('submit'),
|
||||
changeBubbles = isEventSupported('change')
|
||||
$.rails = rails = {
|
||||
// Link elements bound by jquery-ujs
|
||||
linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote]',
|
||||
|
||||
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)
|
||||
// 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
|
||||
// Select elements bound by jquery-ujs
|
||||
selectChangeSelector: 'select[data-remote]',
|
||||
|
||||
// Form elements bound by jquery-ujs
|
||||
formSubmitSelector: 'form',
|
||||
|
||||
// Form input elements bound by jquery-ujs
|
||||
formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])',
|
||||
|
||||
// Form input elements disabled during form submission
|
||||
disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]',
|
||||
|
||||
// Form input elements re-enabled after form submission
|
||||
enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled',
|
||||
|
||||
// Form required input elements
|
||||
requiredInputSelector: 'input[name][required]:not([disabled]),textarea[name][required]:not([disabled])',
|
||||
|
||||
// Form file input elements
|
||||
fileInputSelector: 'input:file',
|
||||
|
||||
// Make sure that every Ajax request sends the CSRF token
|
||||
CSRFProtection: function(xhr) {
|
||||
var token = $('meta[name="csrf-token"]').attr('content');
|
||||
if (token) xhr.setRequestHeader('X-CSRF-Token', token);
|
||||
},
|
||||
|
||||
// Triggers an event on an element and returns false if the event result is false
|
||||
fire: function(obj, name, data) {
|
||||
var event = $.Event(name);
|
||||
obj.trigger(event, data);
|
||||
return event.result !== false;
|
||||
},
|
||||
|
||||
// Default confirm dialog, may be overridden with custom confirm dialog in $.rails.confirm
|
||||
confirm: function(message) {
|
||||
return confirm(message);
|
||||
},
|
||||
|
||||
// Default ajax function, may be overridden with custom function in $.rails.ajax
|
||||
ajax: function(options) {
|
||||
return $.ajax(options);
|
||||
},
|
||||
|
||||
// Submits "remote" forms and links with ajax
|
||||
handleRemote: function(element) {
|
||||
var method, url, data,
|
||||
crossDomain = element.data('cross-domain') || null,
|
||||
dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
|
||||
|
||||
if (rails.fire(element, 'ajax:before')) {
|
||||
|
||||
if (element.is('form')) {
|
||||
method = element.attr('method');
|
||||
url = element.attr('action');
|
||||
data = element.serializeArray();
|
||||
// memoized value from clicked submit button
|
||||
var button = element.data('ujs:submit-button');
|
||||
if (button) {
|
||||
data.push(button);
|
||||
element.data('ujs:submit-button', null);
|
||||
}
|
||||
} else if (element.is('select')) {
|
||||
method = element.data('method');
|
||||
url = element.data('url');
|
||||
data = element.serialize();
|
||||
if (element.data('params')) data = data + "&" + element.data('params');
|
||||
} else {
|
||||
method = element.data('method');
|
||||
url = element.attr('href');
|
||||
data = element.data('params') || null;
|
||||
}
|
||||
|
||||
options = {
|
||||
type: method || 'GET', data: data, dataType: dataType, crossDomain: crossDomain,
|
||||
// stopping the "ajax:beforeSend" event will cancel the ajax request
|
||||
beforeSend: function(xhr, settings) {
|
||||
if (settings.dataType === undefined) {
|
||||
xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
|
||||
}
|
||||
return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
|
||||
},
|
||||
success: function(data, status, xhr) {
|
||||
element.trigger('ajax:success', [data, status, xhr]);
|
||||
},
|
||||
complete: function(xhr, status) {
|
||||
element.trigger('ajax:complete', [xhr, status]);
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
element.trigger('ajax:error', [xhr, status, error]);
|
||||
}
|
||||
};
|
||||
// Do not pass url to `ajax` options if blank
|
||||
if (url) { $.extend(options, { url: url }); }
|
||||
|
||||
rails.ajax(options);
|
||||
}
|
||||
)
|
||||
}
|
||||
},
|
||||
|
||||
if (!submitBubbles) {
|
||||
// discover forms on the page by observing focus events which always bubble
|
||||
document.on('focusin', 'form', function(focusEvent, form) {
|
||||
// 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)
|
||||
// if custom event received preventDefault, cancel the real one too
|
||||
if (emulated.returnValue === false) submitEvent.preventDefault()
|
||||
})
|
||||
form.store('emulated:submit', true)
|
||||
// Handles "data-method" on links such as:
|
||||
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
|
||||
handleMethod: function(link) {
|
||||
var href = link.attr('href'),
|
||||
method = link.data('method'),
|
||||
csrf_token = $('meta[name=csrf-token]').attr('content'),
|
||||
csrf_param = $('meta[name=csrf-param]').attr('content'),
|
||||
form = $('<form method="post" action="' + href + '"></form>'),
|
||||
metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
|
||||
|
||||
if (csrf_param !== undefined && csrf_token !== undefined) {
|
||||
metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (!changeBubbles) {
|
||||
// discover form inputs on the page
|
||||
document.on('focusin', 'input, select, texarea', 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)
|
||||
form.hide().append(metadata_input).appendTo('body');
|
||||
form.submit();
|
||||
},
|
||||
|
||||
/* Disables form elements:
|
||||
- Caches element value in 'ujs:enable-with' data store
|
||||
- Replaces element text with value of 'data-disable-with' attribute
|
||||
- Adds disabled=disabled attribute
|
||||
*/
|
||||
disableFormElements: function(form) {
|
||||
form.find(rails.disableSelector).each(function() {
|
||||
var element = $(this), method = element.is('button') ? 'html' : 'val';
|
||||
element.data('ujs:enable-with', element[method]());
|
||||
element[method](element.data('disable-with'));
|
||||
element.attr('disabled', 'disabled');
|
||||
});
|
||||
},
|
||||
|
||||
/* Re-enables disabled form elements:
|
||||
- Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
|
||||
- Removes disabled attribute
|
||||
*/
|
||||
enableFormElements: function(form) {
|
||||
form.find(rails.enableSelector).each(function() {
|
||||
var element = $(this), method = element.is('button') ? 'html' : 'val';
|
||||
if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
|
||||
element.removeAttr('disabled');
|
||||
});
|
||||
},
|
||||
|
||||
/* For 'data-confirm' attribute:
|
||||
- Fires `confirm` event
|
||||
- Shows the confirmation dialog
|
||||
- Fires the `confirm:complete` event
|
||||
|
||||
Returns `true` if no function stops the chain and user chose yes; `false` otherwise.
|
||||
Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog.
|
||||
Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function
|
||||
return false. The `confirm:complete` event is fired whether or not the user answered true or false to the dialog.
|
||||
*/
|
||||
allowAction: function(element) {
|
||||
var message = element.data('confirm'),
|
||||
answer = false, callback;
|
||||
if (!message) { return true; }
|
||||
|
||||
if (rails.fire(element, 'confirm')) {
|
||||
answer = rails.confirm(message);
|
||||
callback = rails.fire(element, 'confirm:complete', [answer]);
|
||||
}
|
||||
})
|
||||
}
|
||||
return answer && callback;
|
||||
},
|
||||
|
||||
function handleRemote(element) {
|
||||
var method, url, params;
|
||||
// Helper function which checks for blank inputs in a form that match the specified CSS selector
|
||||
blankInputs: function(form, specifiedSelector, nonBlank) {
|
||||
var inputs = $(), input,
|
||||
selector = specifiedSelector || 'input,textarea';
|
||||
form.find(selector).each(function() {
|
||||
input = $(this);
|
||||
// Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
|
||||
if (nonBlank ? input.val() : !input.val()) {
|
||||
inputs = inputs.add(input);
|
||||
}
|
||||
});
|
||||
return inputs.length ? inputs : false;
|
||||
},
|
||||
|
||||
var event = element.fire("ajax:before");
|
||||
if (event.stopped) return false;
|
||||
// Helper function which checks for non-blank inputs in a form that match the specified CSS selector
|
||||
nonBlankInputs: function(form, specifiedSelector) {
|
||||
return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
|
||||
},
|
||||
|
||||
if (element.tagName.toLowerCase() === 'form') {
|
||||
method = element.readAttribute('method') || 'post';
|
||||
url = element.readAttribute('action');
|
||||
params = element.serialize();
|
||||
} else {
|
||||
method = element.readAttribute('data-method') || 'get';
|
||||
url = element.readAttribute('href');
|
||||
params = {};
|
||||
// Helper function, needed to provide consistent behavior in IE
|
||||
stopEverything: function(e) {
|
||||
$(e.target).trigger('ujs:everythingStopped');
|
||||
e.stopImmediatePropagation();
|
||||
return false;
|
||||
},
|
||||
|
||||
// find all the submit events directly bound to the form and
|
||||
// manually invoke them. If anyone returns false then stop the loop
|
||||
callFormSubmitBindings: function(form) {
|
||||
var events = form.data('events'), continuePropagation = true;
|
||||
if (events !== undefined && events['submit'] !== undefined) {
|
||||
$.each(events['submit'], function(i, obj){
|
||||
if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
|
||||
});
|
||||
}
|
||||
return continuePropagation;
|
||||
}
|
||||
};
|
||||
|
||||
new Ajax.Request(url, {
|
||||
method: method,
|
||||
parameters: params,
|
||||
evalScripts: true,
|
||||
$.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});
|
||||
|
||||
onComplete: function(request) { element.fire("ajax:complete", request); },
|
||||
onSuccess: function(request) { element.fire("ajax:success", request); },
|
||||
onFailure: function(request) { element.fire("ajax:failure", request); }
|
||||
});
|
||||
$(rails.linkClickSelector).live('click.rails', function(e) {
|
||||
var link = $(this);
|
||||
if (!rails.allowAction(link)) return rails.stopEverything(e);
|
||||
|
||||
element.fire("ajax:after");
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (method !== 'post') {
|
||||
var field = new Element('input', { type: 'hidden', name: '_method', value: method });
|
||||
form.insert(field);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
form.submit();
|
||||
}
|
||||
|
||||
|
||||
document.on("click", "*[data-confirm]", function(event, element) {
|
||||
var message = element.readAttribute('data-confirm');
|
||||
if (!confirm(message)) event.stop();
|
||||
});
|
||||
|
||||
document.on("click", "a[data-remote]", function(event, element) {
|
||||
if (event.stopped) return;
|
||||
handleRemote(element);
|
||||
event.stop();
|
||||
});
|
||||
|
||||
document.on("click", "a[data-method]", function(event, element) {
|
||||
if (event.stopped) return;
|
||||
handleMethod(element);
|
||||
event.stop();
|
||||
});
|
||||
|
||||
document.on("submit", function(event) {
|
||||
var element = event.findElement(),
|
||||
message = element.readAttribute('data-confirm');
|
||||
if (message && !confirm(message)) {
|
||||
event.stop();
|
||||
if (link.data('remote') !== undefined) {
|
||||
rails.handleRemote(link);
|
||||
return false;
|
||||
} else if (link.data('method')) {
|
||||
rails.handleMethod(link);
|
||||
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');
|
||||
});
|
||||
$(rails.selectChangeSelector).live('change.rails', function(e) {
|
||||
var link = $(this);
|
||||
if (!rails.allowAction(link)) return rails.stopEverything(e);
|
||||
|
||||
var element = event.findElement("form[data-remote]");
|
||||
if (element) {
|
||||
handleRemote(element);
|
||||
event.stop();
|
||||
rails.handleRemote(link);
|
||||
return false;
|
||||
});
|
||||
|
||||
$(rails.formSubmitSelector).live('submit.rails', function(e) {
|
||||
var form = $(this),
|
||||
remote = form.data('remote') !== undefined,
|
||||
blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
|
||||
nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
|
||||
|
||||
if (!rails.allowAction(form)) return rails.stopEverything(e);
|
||||
|
||||
// skip other logic when required values are missing or file upload is present
|
||||
if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
|
||||
return rails.stopEverything(e);
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
});
|
||||
});
|
||||
|
||||
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;
|
||||
if (remote) {
|
||||
if (nonBlankFileInputs) {
|
||||
return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
|
||||
}
|
||||
|
||||
// If browser does not support submit bubbling, then this live-binding will be called before direct
|
||||
// bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
|
||||
if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) === false) return rails.stopEverything(e);
|
||||
|
||||
rails.handleRemote(form);
|
||||
return false;
|
||||
} else {
|
||||
// slight timeout so that the submit button gets properly serialized
|
||||
setTimeout(function(){ rails.disableFormElements(form); }, 13);
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
$(rails.formInputClickSelector).live('click.rails', function(event) {
|
||||
var button = $(this);
|
||||
|
||||
if (!rails.allowAction(button)) return rails.stopEverything(event);
|
||||
|
||||
// register the pressed submit button
|
||||
var name = button.attr('name'),
|
||||
data = name ? {name:name, value:button.val()} : null;
|
||||
|
||||
button.closest('form').data('ujs:submit-button', data);
|
||||
});
|
||||
|
||||
$(rails.formSubmitSelector).live('ajax:beforeSend.rails', function(event) {
|
||||
if (this == event.target) rails.disableFormElements($(this));
|
||||
});
|
||||
|
||||
$(rails.formSubmitSelector).live('ajax:complete.rails', function(event) {
|
||||
if (this == event.target) rails.enableFormElements($(this));
|
||||
});
|
||||
|
||||
})( jQuery );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue