tap/app/assets/javascripts/orders.js.coffee

41 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-11-06 17:56:00 +00:00
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
2014-12-04 16:02:08 +00:00
ready = ->
$('.btn-inc').on 'click', ->
2014-12-09 23:38:48 +00:00
increment($(this), 1)
2014-12-04 16:02:08 +00:00
$('.btn-dec').on 'click', ->
2014-12-09 23:38:48 +00:00
increment($(this), -1)
2015-02-04 15:10:47 +00:00
$('.form_row').each((index, row) ->
disIfNec(row)
2015-02-12 13:39:58 +00:00
$(row).on('input', recalculate)
)
2014-12-10 19:47:56 +00:00
2015-02-12 13:39:58 +00:00
recalculate()
2015-02-04 15:10:47 +00:00
disIfNec = (row) ->
counter = parseInt($(row).find('.row_counter').val())
$(row).find('.btn-dec').prop('disabled', counter == 0);
$(row).find('.btn-inc').prop('disabled', counter == parseInt($(row).find('.stock').val()))
2015-02-12 13:39:58 +00:00
recalculate = () ->
value = ($(row).find('.row_counter').val() * $(row).find('.price').val() for row in $('.form_row')).reduce (a, b) -> a+b
$('#order_total_price').val((value / 100.0).toFixed(2))
2014-12-09 08:43:21 +00:00
increment = (button, n) ->
2015-02-04 15:10:47 +00:00
row = $(button).closest('.form_row')
2014-12-09 23:38:48 +00:00
# Fix the counter
2015-02-04 15:10:47 +00:00
counter = $(row).find('.row_counter')
2014-12-09 23:38:48 +00:00
counter.val(parseInt(counter.val()) + n)
2015-02-04 15:10:47 +00:00
# Disable buttons if necessary
disIfNec(row)
2014-12-09 08:43:21 +00:00
2015-02-12 13:39:58 +00:00
recalculate()
2014-12-04 16:02:08 +00:00
$(document).ready(ready)
$(document).on('page:load', ready)