Sha256: 3e9ffa4583ef89ff04dcb1849dbc577587f04176876c8b0f755d16b6dd1e74c2

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

initialize = ->
  $('input.effective_price:not(.initialized)').each (i, element) ->
    element = $(element)
    options = element.data('input-js-options') || {}

    # We don't actually do anything
    element.addClass('initialized')

$ -> initialize()
$(document).on 'page:change', -> initialize()
$(document).on 'cocoon:after-insert', -> initialize()


# Prevent non-currency buttons from being pressed
$(document).on 'keydown', "input[type='text'].effective_price", (event) ->
  allowed = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', ',', '.']

  if event.key && event.key.length == 1 && event.metaKey == false && allowed.indexOf(event.key) == -1
    event.preventDefault()


# Format the value for display as currency (USD)
$(document).on 'change', "input[type='text'].effective_price", (event) ->
  value = parseFloat($(event.target).val().replace(',', ''))

  if isNaN(value) == false
    value = value.toFixed(2).toString()
    value = value.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2') while (/(\d+)(\d{3})/.test(value))
  else
    value = ''

  $(event.target).val(value)

# Assign the hidden input a value of 100x value
$(document).on 'change keyup', "input[type='text'].effective_price", (event) ->
  value = (parseFloat($(event.target).val().replace(',', '')) || 0.00) * 100.00
  $(event.target).siblings("input[type='hidden']").first().val(value.toFixed(0))

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
effective_form_inputs-0.8.3 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.2 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.1 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.0 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.7.7 app/assets/javascripts/effective_price/initialize.js.coffee