Sha256: 51528cacfbd1f63db738b00b46dc049cb2ecb06e872a4fd3a20ec97fc17692c4

Contents?: true

Size: 1.81 KB

Versions: 32

Compression:

Stored size: 1.81 KB

Contents

if (typeof window.$ == 'undefined' ) {
  var $ = window.jQuery
}

$(function() {
  function warningAlert(message) {
    var div = $('<div>')
      .addClass('alert alert-danger alert-dismissible fade show')
      .text(message)

    return div.append(
      '<button type="button" class="close" data-dismiss="alert" aria-label="Close">' +
        '<span aria-hidden="true">&times;</span>' +
      '</button>'
    )
  }

  function handleResponse($form, event, status, xhr) {
    // Handle backwards compat for [rails/jquery]-ujs ajax callbacks
    var json
    try {
      if (event.detail) {
        json = JSON.parse(event.detail[2].response)
        status = event.detail[1]
      } else {
        json = xhr.responseJSON
      }
    } catch(ex) {
      status = 'error'
      json = {}
    }

    if (status == 'OK' || status == 'success') {
      $form.append(
        $('<span>').text(json.message).delay(2000).fadeOut(2000, function() { $(this).remove() })
      )
      $('input:visible, textarea', $form).val('')
      $('.alert', $form).remove()
      if (typeof window.grecaptcha != 'undefined') {
        window.grecaptcha.reset()
      }
    } else if (json.message) {
      $form.append(warningAlert(json.message))
    } else {
      $form.append(warningAlert('Sorry, something went wrong.'))
    }
  }

  $('[data-contact-form]').each(function(_, input) {
    var $form = $(input)

    $form.on('ajax:success', function(event, data, status, xhr) {
      try {
        handleResponse($form, event, status, xhr)
      } catch(ex) {
        alert('Sorry, something went wrong.')
        throw ex
      }
    })

    $form.on('ajax:error', function(event, xhr, status) {
      try {
        handleResponse($form, event, status, xhr)
      } catch(ex) {
        alert('Sorry, something went wrong.')
        throw ex
      }
    })
  })
})

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
wcc-contentful-app-1.7.1 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.7.0 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.6.2 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.6.1 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.6.0 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.5.1 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.5.0 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.5.0.rc1 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.4.0 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.4.0.rc3 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.4.0.rc2 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.3.2 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.4.0.rc1 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.3.1 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.3.0 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.2.1 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.2.0 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.1.2 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.1.1 app/assets/javascripts/wcc/contentful/app/contact-form.js
wcc-contentful-app-1.1.0 app/assets/javascripts/wcc/contentful/app/contact-form.js