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

61 lines
1.5 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) ->
updateInput(row, false)
$(row).on('input', ->
updateInput(row)
)
)
2014-12-10 19:47:56 +00:00
2015-02-12 13:39:58 +00:00
recalculate()
# Validate input, and then update
2015-07-08 11:37:19 +00: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 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('.row_counter').attr('max')))
2015-02-04 15:10:47 +00:00
2015-02-12 13:39:58 +00: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 13:39:58 +00:00
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')
value = parseInt(counter.val())
2015-07-08 11:37:19 +00:00
if isNaN(value)
value = 0
counter.val(value + n)
2014-12-09 08:43:21 +00:00
updateInput(row[0])
2014-12-04 16:02:08 +00:00
$(document).ready(ready)
$(document).on('page:load', ready)