PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/public/javascripts/rails.js

https://github.com/devkantmohan/Rails-3-template-application
JavaScript | 156 lines | 98 code | 23 blank | 35 comment | 16 complexity | 9467941544001f17290c5c3c68762114 MD5 | raw file
  1. /*
  2. * jquery-ujs
  3. *
  4. * http://github.com/rails/jquery-ujs/blob/master/src/rails.js
  5. *
  6. * This rails.js file supports jQuery 1.4.3 and 1.4.4 .
  7. *
  8. */
  9. jQuery(function ($) {
  10. var csrf_token = $('meta[name=csrf-token]').attr('content'),
  11. csrf_param = $('meta[name=csrf-param]').attr('content');
  12. $.fn.extend({
  13. /**
  14. * Triggers a custom event on an element and returns the event result
  15. * this is used to get around not being able to ensure callbacks are placed
  16. * at the end of the chain.
  17. *
  18. * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
  19. * own events and placing ourselves at the end of the chain.
  20. */
  21. triggerAndReturn: function (name, data) {
  22. var event = new $.Event(name);
  23. this.trigger(event, data);
  24. return event.result !== false;
  25. },
  26. /**
  27. * Handles execution of remote calls. Provides following callbacks:
  28. *
  29. * - ajax:before - is execute before the whole thing begings
  30. * - ajax:loading - is executed before firing ajax call
  31. * - ajax:success - is executed when status is success
  32. * - ajax:complete - is execute when status is complete
  33. * - ajax:failure - is execute in case of error
  34. * - ajax:after - is execute every single time at the end of ajax call
  35. */
  36. callRemote: function () {
  37. var el = this,
  38. method = el.attr('method') || el.attr('data-method') || 'GET',
  39. url = el.attr('action') || el.attr('href'),
  40. dataType = el.attr('data-type') || 'script';
  41. if (url === undefined) {
  42. throw "No URL specified for remote call (action or href must be present).";
  43. } else {
  44. if (el.triggerAndReturn('ajax:before')) {
  45. var data = el.is('form') ? el.serializeArray() : [];
  46. $.ajax({
  47. url: url,
  48. data: data,
  49. dataType: dataType,
  50. type: method.toUpperCase(),
  51. beforeSend: function (xhr) {
  52. el.trigger('ajax:loading', xhr);
  53. },
  54. success: function (data, status, xhr) {
  55. el.trigger('ajax:success', [data, status, xhr]);
  56. },
  57. complete: function (xhr) {
  58. el.trigger('ajax:complete', xhr);
  59. },
  60. error: function (xhr, status, error) {
  61. el.trigger('ajax:failure', [xhr, status, error]);
  62. }
  63. });
  64. }
  65. el.trigger('ajax:after');
  66. }
  67. }
  68. });
  69. /**
  70. * confirmation handler
  71. */
  72. $('body').delegate('a[data-confirm], button[data-confirm], input[data-confirm]', 'click', function () {
  73. var el = $(this);
  74. if (el.triggerAndReturn('confirm')) {
  75. if (!confirm(el.attr('data-confirm'))) {
  76. return false;
  77. }
  78. }
  79. });
  80. /**
  81. * remote handlers
  82. */
  83. $('form[data-remote]').live('submit', function (e) {
  84. $(this).callRemote();
  85. e.preventDefault();
  86. });
  87. $('a[data-remote],input[data-remote]').live('click', function (e) {
  88. $(this).callRemote();
  89. e.preventDefault();
  90. });
  91. $('a[data-method]:not([data-remote])').live('click', function (e){
  92. var link = $(this),
  93. href = link.attr('href'),
  94. method = link.attr('data-method'),
  95. form = $('<form method="post" action="'+href+'"></form>'),
  96. metadata_input = '<input name="_method" value="'+method+'" type="hidden" />';
  97. if (csrf_param != null && csrf_token != null) {
  98. metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />';
  99. }
  100. form.hide()
  101. .append(metadata_input)
  102. .appendTo('body');
  103. e.preventDefault();
  104. form.submit();
  105. });
  106. /**
  107. * disable-with handlers
  108. */
  109. var disable_with_input_selector = 'input[data-disable-with]',
  110. disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')',
  111. disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')';
  112. var disable_with_input_function = function () {
  113. $(this).find(disable_with_input_selector).each(function () {
  114. var input = $(this);
  115. input.data('enable-with', input.val())
  116. .attr('value', input.attr('data-disable-with'))
  117. .attr('disabled', 'disabled');
  118. });
  119. };
  120. $(disable_with_form_remote_selector).live('ajax:before', disable_with_input_function);
  121. $(disable_with_form_not_remote_selector).live('submit', disable_with_input_function);
  122. $(disable_with_form_remote_selector).live('ajax:complete', function () {
  123. $(this).find(disable_with_input_selector).each(function () {
  124. var input = $(this);
  125. input.removeAttr('disabled')
  126. .val(input.data('enable-with'));
  127. });
  128. });
  129. var jqueryVersion = $().jquery;
  130. if ( (jqueryVersion === '1.4') || (jqueryVersion === '1.4.1') || (jqueryVersion === '1.4.2') ){
  131. alert('This rails.js does not support the jQuery version you are using. Please read documentation.')
  132. }
  133. });