Sha256: 3a9e6f9a8d5526389dbb7e107824626f5176a11b8aec4a6cfc29695929a798c9

Contents?: true

Size: 1.98 KB

Versions: 11

Compression:

Stored size: 1.98 KB

Contents

class IuguUI.Money
  defaults:
    none: false
    hideInput: true

  el: undefined

  @fromCentsToMoney: ( cents, locale ) ->
    cents = (cents / 100).toFixed(2)
    numeral( cents ).format("$0,0.00")

  @fromMoneyToCents: ( currency, locale ) ->
    value = numeral().unformat( currency )
    (value * 100).toFixed(0)

  @configureLocale: ->
    numeral.language( i18n.locale.toLowerCase() )

  @load: ( context ) ->
    selector = "[data-type='iux.form.money_input']"
    if context
      elements = context.find(selector)
    else
      elements = $(selector)

    return if elements.length == 0
    
    elements.each ->
      return if $(@).data("iux.initialized") == true

      new IuguUI.Money
        el: @
        context: context

      $(@).data "iux.initialized", true

  constructor: ( options ) ->
    @initialize( options )
  
  initialize: ( options ) ->
    _.bindAll @
    @options = _.extend {}, @defaults, options

    return null unless @options.el

    @el = $(@options.el)

    @input_element = @el

    @decorator = $('<input>',
      type: "text"
      placeholder: @input_element.attr "placeholder"
      value: @input_element.attr "value"
      class: @input_element.attr "class"
    )

    @decorator.insertAfter( @el )

    that = @

    @decorator.bind "focus", ->
      $(this).val( IuguUI.Money.fromCentsToMoney( that.input_element.val()  ) )

    @decorator.bind "blur", ->
      that.input_element.val( IuguUI.Money.fromMoneyToCents( $(this).val() ) )
      that.input_element.trigger "change"
      $(this).val( IuguUI.Money.fromCentsToMoney( that.input_element.val()  ) )

    @decorator.val( IuguUI.Money.fromCentsToMoney( that.input_element.val()  ) )

    @input_element.hide()

  getInput: ->
    return $(@input_element) if @input_element
    false

  configureInputElementValue: ( value ) ->
    return unless @getInput()

    @getInput().val( value ) if @getInput()

    if (@getInput())
      @getInput().trigger("change")

@IuguUI.Money= IuguUI.Money

$ ->
  IuguUI.Money.load()

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
iugu-ux-1.0.11 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.10 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.9 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.8 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.7 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.6 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.5 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.4 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.3 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.2 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee
iugu-ux-1.0.1 vendor/assets/javascripts/iugu-ux/components/usecode/iugu-ui-currency-input.js.coffee