2017-03-30 14:41:17 -04:00
|
|
|
{
|
|
|
|
fire, delegate
|
|
|
|
getData, $
|
|
|
|
refreshCSRFTokens, CSRFProtection
|
|
|
|
enableElement, disableElement, handleDisabledElement
|
2018-11-29 19:26:22 -05:00
|
|
|
handleConfirm, preventInsignificantClick
|
|
|
|
handleRemote, formSubmitButtonClick,
|
2017-03-30 14:41:17 -04:00
|
|
|
handleMethod
|
|
|
|
} = Rails
|
|
|
|
|
|
|
|
# For backward compatibility
|
2018-07-31 09:16:02 -04:00
|
|
|
if jQuery? and jQuery.ajax?
|
|
|
|
throw new Error('If you load both jquery_ujs and rails-ujs, use rails-ujs only.') if jQuery.rails
|
2017-03-30 14:41:17 -04:00
|
|
|
jQuery.rails = Rails
|
|
|
|
jQuery.ajaxPrefilter (options, originalOptions, xhr) ->
|
|
|
|
CSRFProtection(xhr) unless options.crossDomain
|
|
|
|
|
|
|
|
Rails.start = ->
|
|
|
|
# Cut down on the number of issues from people inadvertently including
|
|
|
|
# rails-ujs twice by detecting and raising an error when it happens.
|
|
|
|
throw new Error('rails-ujs has already been loaded!') if window._rails_loaded
|
|
|
|
|
|
|
|
# This event works the same as the load event, except that it fires every
|
|
|
|
# time the page is loaded.
|
|
|
|
# See https://github.com/rails/jquery-ujs/issues/357
|
|
|
|
# See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching
|
|
|
|
window.addEventListener 'pageshow', ->
|
|
|
|
$(Rails.formEnableSelector).forEach (el) ->
|
|
|
|
enableElement(el) if getData(el, 'ujs:disabled')
|
|
|
|
$(Rails.linkDisableSelector).forEach (el) ->
|
|
|
|
enableElement(el) if getData(el, 'ujs:disabled')
|
|
|
|
|
|
|
|
delegate document, Rails.linkDisableSelector, 'ajax:complete', enableElement
|
|
|
|
delegate document, Rails.linkDisableSelector, 'ajax:stopped', enableElement
|
|
|
|
delegate document, Rails.buttonDisableSelector, 'ajax:complete', enableElement
|
|
|
|
delegate document, Rails.buttonDisableSelector, 'ajax:stopped', enableElement
|
|
|
|
|
2018-11-29 19:26:22 -05:00
|
|
|
delegate document, Rails.linkClickSelector, 'click', preventInsignificantClick
|
2017-03-30 14:41:17 -04:00
|
|
|
delegate document, Rails.linkClickSelector, 'click', handleDisabledElement
|
|
|
|
delegate document, Rails.linkClickSelector, 'click', handleConfirm
|
|
|
|
delegate document, Rails.linkClickSelector, 'click', disableElement
|
|
|
|
delegate document, Rails.linkClickSelector, 'click', handleRemote
|
|
|
|
delegate document, Rails.linkClickSelector, 'click', handleMethod
|
|
|
|
|
2018-11-29 19:26:22 -05:00
|
|
|
delegate document, Rails.buttonClickSelector, 'click', preventInsignificantClick
|
2017-03-30 14:41:17 -04:00
|
|
|
delegate document, Rails.buttonClickSelector, 'click', handleDisabledElement
|
|
|
|
delegate document, Rails.buttonClickSelector, 'click', handleConfirm
|
|
|
|
delegate document, Rails.buttonClickSelector, 'click', disableElement
|
|
|
|
delegate document, Rails.buttonClickSelector, 'click', handleRemote
|
|
|
|
|
|
|
|
delegate document, Rails.inputChangeSelector, 'change', handleDisabledElement
|
|
|
|
delegate document, Rails.inputChangeSelector, 'change', handleConfirm
|
|
|
|
delegate document, Rails.inputChangeSelector, 'change', handleRemote
|
|
|
|
|
|
|
|
delegate document, Rails.formSubmitSelector, 'submit', handleDisabledElement
|
|
|
|
delegate document, Rails.formSubmitSelector, 'submit', handleConfirm
|
|
|
|
delegate document, Rails.formSubmitSelector, 'submit', handleRemote
|
|
|
|
# Normal mode submit
|
|
|
|
# Slight timeout so that the submit button gets properly serialized
|
|
|
|
delegate document, Rails.formSubmitSelector, 'submit', (e) -> setTimeout((-> disableElement(e)), 13)
|
|
|
|
delegate document, Rails.formSubmitSelector, 'ajax:send', disableElement
|
|
|
|
delegate document, Rails.formSubmitSelector, 'ajax:complete', enableElement
|
|
|
|
|
2018-11-29 19:26:22 -05:00
|
|
|
delegate document, Rails.formInputClickSelector, 'click', preventInsignificantClick
|
2017-03-30 14:41:17 -04:00
|
|
|
delegate document, Rails.formInputClickSelector, 'click', handleDisabledElement
|
|
|
|
delegate document, Rails.formInputClickSelector, 'click', handleConfirm
|
|
|
|
delegate document, Rails.formInputClickSelector, 'click', formSubmitButtonClick
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', refreshCSRFTokens)
|
|
|
|
window._rails_loaded = true
|
|
|
|
|
|
|
|
if window.Rails is Rails and fire(document, 'rails:attachBindings')
|
|
|
|
Rails.start()
|