|
| 1 | +# |
| 2 | +# Unobtrusive JavaScript |
| 3 | +# https://github.com/rails/rails-ujs |
| 4 | +# |
| 5 | +# Released under the MIT license |
| 6 | +# |
| 7 | +#= require ./config |
| 8 | +#= require_tree ./utils |
| 9 | +#= require_tree ./features |
| 10 | + |
| 11 | +{ |
| 12 | + fire, delegate |
| 13 | + getData, $ |
| 14 | + refreshCSRFTokens, CSRFProtection |
| 15 | + enableElement, disableElement |
| 16 | + handleConfirm |
| 17 | + handleRemote, validateForm, formSubmitButtonClick, handleMetaClick |
| 18 | + handleMethod |
| 19 | +} = Rails |
| 20 | + |
| 21 | +# For backward compatibility |
| 22 | +if jQuery? and not jQuery.rails |
| 23 | + jQuery.rails = Rails |
| 24 | + jQuery.ajaxPrefilter (options, originalOptions, xhr) -> |
| 25 | + CSRFProtection(xhr) unless options.crossDomain |
| 26 | + |
| 27 | +Rails.start = -> |
| 28 | + # Cut down on the number of issues from people inadvertently including jquery_ujs twice |
| 29 | + # by detecting and raising an error when it happens. |
| 30 | + throw new Error('jquery-ujs has already been loaded!') if window._rails_loaded |
| 31 | + |
| 32 | + # This event works the same as the load event, except that it fires every |
| 33 | + # time the page is loaded. |
| 34 | + # See https://github.com/rails/jquery-ujs/issues/357 |
| 35 | + # See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching |
| 36 | + window.addEventListener 'pageshow', -> |
| 37 | + $(Rails.formEnableSelector).forEach (el) -> |
| 38 | + enableElement(el) if getData(el, 'ujs:disabled') |
| 39 | + $(Rails.linkDisableSelector).forEach (el) -> |
| 40 | + enableElement(el) if getData(el, 'ujs:disabled') |
| 41 | + |
| 42 | + delegate document, Rails.linkDisableSelector, 'ajax:complete', enableElement |
| 43 | + delegate document, Rails.linkDisableSelector, 'ajax:stopped', enableElement |
| 44 | + delegate document, Rails.buttonDisableSelector, 'ajax:complete', enableElement |
| 45 | + delegate document, Rails.buttonDisableSelector, 'ajax:stopped', enableElement |
| 46 | + |
| 47 | + delegate document, Rails.linkClickSelector, 'click', handleConfirm |
| 48 | + delegate document, Rails.linkClickSelector, 'click', handleMetaClick |
| 49 | + delegate document, Rails.linkClickSelector, 'click', disableElement |
| 50 | + delegate document, Rails.linkClickSelector, 'click', handleRemote |
| 51 | + delegate document, Rails.linkClickSelector, 'click', handleMethod |
| 52 | + |
| 53 | + delegate document, Rails.buttonClickSelector, 'click', handleConfirm |
| 54 | + delegate document, Rails.buttonClickSelector, 'click', disableElement |
| 55 | + delegate document, Rails.buttonClickSelector, 'click', handleRemote |
| 56 | + |
| 57 | + delegate document, Rails.inputChangeSelector, 'change', handleConfirm |
| 58 | + delegate document, Rails.inputChangeSelector, 'change', handleRemote |
| 59 | + |
| 60 | + delegate document, Rails.formSubmitSelector, 'submit', handleConfirm |
| 61 | + delegate document, Rails.formSubmitSelector, 'submit', validateForm |
| 62 | + delegate document, Rails.formSubmitSelector, 'submit', handleRemote |
| 63 | + # Normal mode submit |
| 64 | + # Slight timeout so that the submit button gets properly serialized |
| 65 | + delegate document, Rails.formSubmitSelector, 'submit', (e) -> setTimeout((-> disableElement(e)), 13) |
| 66 | + delegate document, Rails.formSubmitSelector, 'ajax:send', disableElement |
| 67 | + delegate document, Rails.formSubmitSelector, 'ajax:complete', enableElement |
| 68 | + |
| 69 | + delegate document, Rails.formInputClickSelector, 'click', handleConfirm |
| 70 | + delegate document, Rails.formInputClickSelector, 'click', formSubmitButtonClick |
| 71 | + |
| 72 | + document.addEventListener('DOMContentLoaded', refreshCSRFTokens) |
| 73 | + window._rails_loaded = true |
| 74 | + |
| 75 | +if window.Rails is Rails and fire(document, 'rails:attachBindings') |
| 76 | + Rails.start() |
0 commit comments