Sha256: 6daef0644af47b583d301fe7ad70257492b1b8a9242e3657de41b7a00f3fb410

Contents?: true

Size: 1.83 KB

Versions: 17

Compression:

Stored size: 1.83 KB

Contents

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

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

$ -> initialize()
$(document).on 'page:change', -> initialize()
$(document).on 'turbolinks:load', -> initialize()
$(document).on 'turbolinks:render', -> initialize()
$(document).on 'cocoon:after-insert', -> initialize()
$(document).on 'effective-form-inputs:initialize', (event) -> initialize(event.currentTarget)

# 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) ->
  $input = $(event.target)
  value = $input.val().replace(/,/g, '')

  unless $input.data('include-blank') && value == ''
    value = (parseFloat(value || 0.00) * 100.00).toFixed(0)

  $input.siblings("input[type='hidden']").first().val(value)

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

  unless $input.data('include-blank') && value == ''
    value = parseInt(value || 0)

  if isNaN(value) == false && value != ''
    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 = ''

  $input.val(value)

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
effective_form_inputs-1.5.0 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.4.1 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.4.0 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.3.0 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.9 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.8 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.7 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.6 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.5 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.4 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.3 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.2 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.1 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.2.0 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.1.15 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.1.14 app/assets/javascripts/effective_price/initialize.js.coffee
effective_form_inputs-1.1.13 app/assets/javascripts/effective_price/initialize.js.coffee