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

41 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-11-06 18:56:00 +01: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 17:02:08 +01:00
ready = ->
$('.btn-inc').on 'click', ->
2014-12-10 00:38:48 +01:00
increment($(this), 1)
2014-12-04 17:02:08 +01:00
$('.btn-dec').on 'click', ->
2014-12-10 00:38:48 +01:00
increment($(this), -1)
2015-02-04 16:10:47 +01:00
$('.form_row').each((index, row) ->
disIfNec(row)
2015-02-12 14:39:58 +01:00
$(row).on('input', recalculate)
)
2014-12-10 20:47:56 +01:00
2015-02-12 14:39:58 +01:00
recalculate()
2015-02-04 16:10:47 +01: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 14:39:58 +01: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 09:43:21 +01:00
increment = (button, n) ->
2015-02-04 16:10:47 +01:00
row = $(button).closest('.form_row')
2014-12-10 00:38:48 +01:00
# Fix the counter
2015-02-04 16:10:47 +01:00
counter = $(row).find('.row_counter')
2014-12-10 00:38:48 +01:00
counter.val(parseInt(counter.val()) + n)
2015-02-04 16:10:47 +01:00
# Disable buttons if necessary
disIfNec(row)
2014-12-09 09:43:21 +01:00
2015-02-12 14:39:58 +01:00
recalculate()
2014-12-04 17:02:08 +01:00
$(document).ready(ready)
$(document).on('page:load', ready)