Sha256: 10d2db96d41c1e22dc932abd81bbda983209d700d1c37ce7674daa77634d3a88

Contents?: true

Size: 1.42 KB

Versions: 14

Compression:

Stored size: 1.42 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()

# 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(/,/g, '') || 0.00) * 100.00
  $(event.target).siblings("input[type='hidden']").first().val(value.toFixed(0))

# Format the value for display as currency (USD)
$(document).on 'change', "input[type='text'].effective_price", (event) ->
  value = parseInt($(event.target).siblings("input[type='hidden']").first().val() || 0)

  if isNaN(value) == false
    value = (value / 100.0) if value != 0

    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)

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
effective_form_inputs-0.8.17 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.16 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.15 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.14 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.13 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.12 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.11 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.10 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.9 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.8 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.7 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.6 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.5 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-0.8.4 app/assets/javascripts/effective_price/initialize.js.coffee