/wheels/vendor/ajax-adapters/wheels.jquery.js
http://cfwheels.googlecode.com/ · JavaScript · 165 lines · 99 code · 27 blank · 39 comment · 17 complexity · e3d6631519071a0086bcb435eb56cf2c MD5 · raw file
- /*
- * jquery-ujs
- *
- * http://github.com/rails/jquery-ujs/blob/master/src/rails.js
- *
- * This wheels.js file supports jQuery 1.4.3 and 1.4.4 .
- *
- * Edited by Raul Riera for ColdFusion on Wheels http://cfwheels.org
- */
- jQuery(function ($) {
- var csrf_token = $('meta[name=csrf-token]').attr('content'),
- csrf_param = $('meta[name=csrf-param]').attr('content');
- $.fn.extend({
- /**
- * Triggers a custom event on an element and returns the event result
- * this is used to get around not being able to ensure callbacks are placed
- * at the end of the chain.
- *
- * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
- * own events and placing ourselves at the end of the chain.
- */
- triggerAndReturn: function (name, data) {
- var event = new $.Event(name);
- this.trigger(event, data);
- return event.result !== false;
- },
- /**
- * Handles execution of remote calls. Provides following callbacks:
- *
- * - ajax:beforeSend - is executed before firing ajax call
- * - ajax:success - is executed when status is success
- * - ajax:complete - is executed when the request finishes, whether in failure or success.
- * - ajax:error - is execute in case of error
- */
- callRemote: function () {
- var el = this,
- method = el.attr('method') || el.attr('data-method') || 'GET',
- url = el.attr('action') || el.attr('href') || el.attr('data-this-action'),
- dataType = el.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);
- if (url === undefined) {
- throw "No URL specified for remote call (action or href must be present).";
- } else {
- var $this = $(this), data = el.is('form') ? el.serializeArray() : [];
- $.ajax({
- url: url,
- data: data,
- dataType: dataType,
- type: method.toUpperCase(),
- beforeSend: function (xhr) {
- xhr.setRequestHeader("Accept", "text/javascript");
- if (el.triggerAndReturn('ajax:beforeSend') === false) {
- return false;
- }
- },
- success: function (data, status, xhr) {
- el.trigger('ajax:success', [data, status, xhr]);
- },
- complete: function (xhr) {
- el.trigger('ajax:complete', xhr);
- },
- error: function (xhr, status, error) {
- el.trigger('ajax:error', [xhr, status, error]);
- }
- });
- }
- }
- });
- /**
- * confirmation handler
- */
- $('body').delegate('a[data-confirm], button[data-confirm], input[data-confirm]', 'click.wheels', function () {
- var el = $(this);
- if (el.triggerAndReturn('confirm')) {
- if (!confirm(el.attr('data-confirm'))) {
- return false;
- }
- }
- });
- /**
- * remote handlers
- */
- $('form[data-remote]').live('submit.wheels', function (e) {
- $(this).callRemote();
- e.preventDefault();
- });
- $('a[data-remote],input[data-remote]').live('click.wheels', function (e) {
- $(this).callRemote();
- e.preventDefault();
- });
- /**
- * #linkTo(text="delete", controller="users", action="delete", key=user.id, confirm="Are you sure?")#
- *
- * <a href="/users/delete/5" data-confirm="Are you sure?">Delete</a>
- */
- $('a[data-method]:not([data-remote])').live('click.wheels', function (e){
- var link = $(this),
- href = link.attr('href'),
- method = link.attr('data-method'),
- 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" />';
- }
- form.hide()
- .append(metadata_input)
- .appendTo('body');
- e.preventDefault();
- form.submit();
- });
- /**
- * disable-with handlers
- */
- var disable_with_input_selector = 'input[data-disable-with]',
- disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')',
- disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')';
- var disable_with_input_function = function () {
- $(this).find(disable_with_input_selector).each(function () {
- var input = $(this);
- var enable_label = (input.data('disable-with') == true) ? input.val() :input.attr('data-disable-with');
-
- input.data('enable-with', input.val())
- .attr('value', enable_label)
- .attr('disabled', 'disabled');
- });
- };
- $(disable_with_form_remote_selector).live('ajax:beforeSend.wheels', disable_with_input_function);
- $(disable_with_form_not_remote_selector).live('submit.wheels', disable_with_input_function);
- $(disable_with_form_remote_selector).live('ajax:complete.wheels', function () {
- $(this).find(disable_with_input_selector).each(function () {
- var input = $(this);
- input.removeAttr('disabled')
- .val(input.data('enable-with'));
- });
- });
- var jqueryVersion = $().jquery;
- if (!( (jqueryVersion === '1.4.3') || (jqueryVersion === '1.4.4'))){
- alert('This wheels.js does not support the jQuery version you are using. Please read documentation for more information.');
- }
- });