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) ->
|
2015-07-07 21:47:20 +02:00
|
|
|
updateInput(row, false)
|
|
|
|
$(row).on('input', ->
|
|
|
|
updateInput(row)
|
|
|
|
)
|
2015-01-15 00:39:34 +01:00
|
|
|
)
|
2014-12-10 20:47:56 +01:00
|
|
|
|
2015-02-12 14:39:58 +01:00
|
|
|
recalculate()
|
|
|
|
|
2015-07-07 21:47:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Validate input, and then update
|
2015-07-08 13:37:19 +02:00
|
|
|
updateInput = (row, useRecalculate = true) ->
|
2015-07-07 21:47:20 +02:00
|
|
|
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);
|
2015-07-07 21:47:20 +02:00
|
|
|
$(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 = () ->
|
2015-09-02 14:35:29 +02:00
|
|
|
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')
|
2015-07-07 21:47:20 +02:00
|
|
|
value = parseInt(counter.val())
|
2015-07-08 13:37:19 +02:00
|
|
|
if isNaN(value)
|
|
|
|
value = 0
|
2015-07-07 21:47:20 +02:00
|
|
|
counter.val(value + n)
|
2014-12-09 09:43:21 +01:00
|
|
|
|
2015-07-07 21:47:20 +02:00
|
|
|
updateInput(row[0])
|
2014-12-04 17:02:08 +01:00
|
|
|
|
|
|
|
$(document).ready(ready)
|
|
|
|
$(document).on('page:load', ready)
|