Sha256: debebcbec29619ae7d9437187bfd81816b70758f680687cf880c94afb1b9869b

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

class @DynamicOrder
  @refresh_trigger_attributes = ['price', 'quantity', 'payment_fee', 'shipment_fee']

  constructor: (@options = {}) ->
    @options['order_item_selector'] = '.comable-order-items' unless @options['order_item_selector']
    @listen_events()

  listen_events: ->
    self = this
    $('input').on('change', ->
      attribute_name = $(this).attr('data-name')
      return unless jQuery.inArray(attribute_name, self.refresh_trigger_attributes)
      self.refresh_order_item_prices_for(this)
    )

  refresh_order_item_prices_for: (element) ->
    $group = $(element).closest(@options['order_item_selector'])

    $price = $group.find('[data-name="price"]')
    $quantity = $group.find('[data-name="quantity"]')
    $subtotal_price = $group.find('[data-name="subtotal_price"]')

    price = Number($price.val())
    quantity = Number($quantity.val())
    $subtotal_price.val(price * quantity)

    @refresh_order_prices()

  refresh_order_prices: ->
    item_total_price = 0
    $('[data-name="subtotal_price"]').each( ->
      item_total_price += Number($(this).val())
    )

    $item_total_price = $('[data-name="item_total_price"]')
    $payment_fee = $('[data-name="payment_fee"]')
    $shipment_fee = $('[data-name="shipment_fee"]')
    $total_price = $('[data-name="total_price"]')

    payment_fee = Number($payment_fee.val())
    shipment_fee = Number($shipment_fee.val())

    $item_total_price.val(item_total_price)
    $total_price.val(item_total_price + payment_fee + shipment_fee)

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
comable-backend-0.7.0.beta2 app/assets/javascripts/comable/admin/orders.coffee
comable-backend-0.7.0.beta1 app/assets/javascripts/comable/admin/orders.coffee
comable-backend-0.6.0 app/assets/javascripts/comable/admin/orders.coffee
comable_backend-0.5.0 app/assets/javascripts/comable/admin/orders.coffee