PageRenderTime 63ms CodeModel.GetById 30ms app.highlight 12ms RepoModel.GetById 19ms app.codeStats 0ms

/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
  1/*
  2 * jquery-ujs
  3 *
  4 * http://github.com/rails/jquery-ujs/blob/master/src/rails.js
  5 *
  6 * This wheels.js file supports jQuery 1.4.3 and 1.4.4 .
  7 *
  8 * Edited by Raul Riera for ColdFusion on Wheels http://cfwheels.org
  9 */
 10
 11jQuery(function ($) {
 12    var csrf_token = $('meta[name=csrf-token]').attr('content'),
 13        csrf_param = $('meta[name=csrf-param]').attr('content');
 14
 15    $.fn.extend({
 16        /**
 17         * Triggers a custom event on an element and returns the event result
 18         * this is used to get around not being able to ensure callbacks are placed
 19         * at the end of the chain.
 20         *
 21         * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
 22         *       own events and placing ourselves at the end of the chain.
 23         */
 24        triggerAndReturn: function (name, data) {
 25            var event = new $.Event(name);
 26            this.trigger(event, data);
 27
 28            return event.result !== false;
 29        },
 30
 31        /**
 32         * Handles execution of remote calls. Provides following callbacks:
 33         *
 34         * - ajax:beforeSend  - is executed before firing ajax call
 35         * - ajax:success  - is executed when status is success
 36         * - ajax:complete - is executed when the request finishes, whether in failure or success.
 37         * - ajax:error    - is execute in case of error
 38         */
 39        callRemote: function () {
 40            var el      = this,
 41                method  = el.attr('method') || el.attr('data-method') || 'GET',
 42                url     = el.attr('action') || el.attr('href') || el.attr('data-this-action'),
 43                dataType  = el.attr('data-type')  || ($.ajaxSettings && $.ajaxSettings.dataType);
 44
 45            if (url === undefined) {
 46                throw "No URL specified for remote call (action or href must be present).";
 47            } else {
 48                    var $this = $(this), data = el.is('form') ? el.serializeArray() : [];
 49
 50                    $.ajax({
 51                        url: url,
 52                        data: data,
 53                        dataType: dataType,
 54                        type: method.toUpperCase(),
 55                        beforeSend: function (xhr) {
 56                            xhr.setRequestHeader("Accept", "text/javascript");
 57                            if (el.triggerAndReturn('ajax:beforeSend') === false) {
 58                              return false;
 59                            }
 60                        },
 61                        success: function (data, status, xhr) {
 62                            el.trigger('ajax:success', [data, status, xhr]);
 63                        },
 64                        complete: function (xhr) {
 65                            el.trigger('ajax:complete', xhr);
 66                        },
 67                        error: function (xhr, status, error) {
 68                            el.trigger('ajax:error', [xhr, status, error]);
 69                        }
 70                    });
 71            }
 72        }
 73    });
 74
 75    /**
 76     *  confirmation handler
 77     */
 78
 79    $('body').delegate('a[data-confirm], button[data-confirm], input[data-confirm]', 'click.wheels', function () {
 80        var el = $(this);
 81        if (el.triggerAndReturn('confirm')) {
 82            if (!confirm(el.attr('data-confirm'))) {
 83                return false;
 84            }
 85        }
 86    });
 87
 88
 89
 90    /**
 91     * remote handlers
 92     */
 93    $('form[data-remote]').live('submit.wheels', function (e) {
 94        $(this).callRemote();
 95        e.preventDefault();
 96    });
 97
 98    $('a[data-remote],input[data-remote]').live('click.wheels', function (e) {
 99        $(this).callRemote();
100        e.preventDefault();
101    });
102
103    /**
104     * #linkTo(text="delete", controller="users", action="delete", key=user.id, confirm="Are you sure?")#
105     *
106     * <a href="/users/delete/5" data-confirm="Are you sure?">Delete</a>
107     */
108    $('a[data-method]:not([data-remote])').live('click.wheels', function (e){
109        var link = $(this),
110            href = link.attr('href'),
111            method = link.attr('data-method'),
112            form = $('<form method="post" action="'+href+'"></form>'),
113            metadata_input = '<input name="_method" value="'+method+'" type="hidden" />';
114
115        if (csrf_param !== undefined && csrf_token !== undefined) {
116            metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />';
117        }
118
119        form.hide()
120            .append(metadata_input)
121            .appendTo('body');
122
123        e.preventDefault();
124        form.submit();
125    });
126
127    /**
128     * disable-with handlers
129     */
130    var disable_with_input_selector           = 'input[data-disable-with]',
131        disable_with_form_remote_selector     = 'form[data-remote]:has('       + disable_with_input_selector + ')',
132        disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')';
133
134    var disable_with_input_function = function () {
135        $(this).find(disable_with_input_selector).each(function () {
136            var input = $(this);
137			var enable_label = (input.data('disable-with') == true) ? input.val() :input.attr('data-disable-with');
138			
139			input.data('enable-with', input.val())
140                .attr('value', enable_label)
141                .attr('disabled', 'disabled');
142
143        });
144    };
145
146    $(disable_with_form_remote_selector).live('ajax:beforeSend.wheels', disable_with_input_function);
147    $(disable_with_form_not_remote_selector).live('submit.wheels', disable_with_input_function);
148
149    $(disable_with_form_remote_selector).live('ajax:complete.wheels', function () {
150        $(this).find(disable_with_input_selector).each(function () {
151
152            var input = $(this);
153            input.removeAttr('disabled')
154                 .val(input.data('enable-with'));
155        });
156    });
157
158    var jqueryVersion = $().jquery;
159
160	if (!( (jqueryVersion === '1.4.3') || (jqueryVersion === '1.4.4'))){
161		alert('This wheels.js does not support the jQuery version you are using. Please read documentation for more information.');
162	}
163
164
165});