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

61 lines
1.5 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) ->
updateInput(row, false)
$(row).on('input', ->
updateInput(row)
)
)
2014-12-10 20:47:56 +01:00
2015-02-12 14:39:58 +01:00
recalculate()
# Validate input, and then update
2015-07-08 13:37:19 +02:00
updateInput = (row, useRecalculate = true) ->
cell = row.querySelector("input")
if ! cell.validity.valid
if(parseInt(cell.value) > parseInt(cell.max))
cell.value = parseInt(cell.max)
else
cell.value = 0
# Disable buttons if necessary
disIfNec(row)
if useRecalculate
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('.row_counter').attr('max')))
2015-02-04 16:10:47 +01:00
2015-02-12 14:39:58 +01:00
recalculate = () ->
value = ($(row).val() * $(row).data('price') for row in $('.row_counter')).reduce(((a, b) -> a+b), 0)
$('#order_price').html((value / 100.0).toFixed(2))
2015-02-12 14:39:58 +01:00
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')
value = parseInt(counter.val())
2015-07-08 13:37:19 +02:00
if isNaN(value)
value = 0
counter.val(value + n)
2014-12-09 09:43:21 +01:00
updateInput(row[0])
2014-12-04 17:02:08 +01:00
$(document).ready(ready)
$(document).on('page:load', ready)