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