From 33b797b5d939e9ac4de74cc6176cf10bda492614 Mon Sep 17 00:00:00 2001 From: David Vandorpe Date: Tue, 7 Jul 2015 21:47:20 +0200 Subject: [PATCH 1/2] Enforce stock max en 0 min for amount of products in orders Fixes #48 --- app/assets/javascripts/orders.js.coffee | 36 +++++++++++++++++----- app/views/order_items/_order_item.html.erb | 2 +- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/orders.js.coffee b/app/assets/javascripts/orders.js.coffee index 32d707f..d3786eb 100644 --- a/app/assets/javascripts/orders.js.coffee +++ b/app/assets/javascripts/orders.js.coffee @@ -9,16 +9,36 @@ ready = -> increment($(this), -1) $('.form_row').each((index, row) -> - disIfNec(row) - $(row).on('input', recalculate) + updateInput(row, false) + $(row).on('input', -> + updateInput(row) + ) ) recalculate() + + +# Validate input, and then update +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(); + + 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())) + $(row).find('.btn-inc').prop('disabled', counter == parseInt($(row).find('.row_counter').attr('max'))) recalculate = () -> value = ($(row).find('.row_counter').val() * $(row).find('.price').val() for row in $('.form_row')).reduce (a, b) -> a+b @@ -29,12 +49,12 @@ increment = (button, n) -> # Fix the counter counter = $(row).find('.row_counter') - counter.val(parseInt(counter.val()) + n) + value = parseInt(counter.val()) + # Apparently CoffeeScript doesn't support ?: + value = if isNaN(value) then 0 else value + counter.val(value + n) - # Disable buttons if necessary - disIfNec(row) - - recalculate() + updateInput(row[0]) $(document).ready(ready) $(document).on('page:load', ready) diff --git a/app/views/order_items/_order_item.html.erb b/app/views/order_items/_order_item.html.erb index 50334af..ddc2708 100644 --- a/app/views/order_items/_order_item.html.erb +++ b/app/views/order_items/_order_item.html.erb @@ -9,7 +9,7 @@ <%= content_tag :span, product.name %> <%= content_tag :small, euro(product.price) %> - <%= f.counter :count, max: 1000, skip_label: true, wrapper_class: "input-group", class: "row_counter" %> + <%= f.counter :count, max: product.stock, min:0, skip_label: true, wrapper_class: "input-group", class: "row_counter" %> <%= f.fields_for :product do |product| %> <%= product.hidden_field :price_cents, class: :price %> <%= product.hidden_field :stock, class: :stock %> From 24f9531192af16c1b7fbb4e3dae5fd703843c97b Mon Sep 17 00:00:00 2001 From: David Vandorpe Date: Wed, 8 Jul 2015 13:37:19 +0200 Subject: [PATCH 2/2] Senpai Silox Styling Suggestions --- app/assets/javascripts/orders.js.coffee | 6 +++--- app/views/order_items/_order_item.html.erb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/orders.js.coffee b/app/assets/javascripts/orders.js.coffee index d3786eb..64c204f 100644 --- a/app/assets/javascripts/orders.js.coffee +++ b/app/assets/javascripts/orders.js.coffee @@ -20,7 +20,7 @@ ready = -> # Validate input, and then update -updateInput = (row, useRecalculate = true)-> +updateInput = (row, useRecalculate = true) -> cell = row.querySelector("input") if ! cell.validity.valid if(parseInt(cell.value) > parseInt(cell.max)) @@ -50,8 +50,8 @@ increment = (button, n) -> # Fix the counter counter = $(row).find('.row_counter') value = parseInt(counter.val()) - # Apparently CoffeeScript doesn't support ?: - value = if isNaN(value) then 0 else value + if isNaN(value) + value = 0 counter.val(value + n) updateInput(row[0]) diff --git a/app/views/order_items/_order_item.html.erb b/app/views/order_items/_order_item.html.erb index ddc2708..1027ec4 100644 --- a/app/views/order_items/_order_item.html.erb +++ b/app/views/order_items/_order_item.html.erb @@ -9,7 +9,7 @@ <%= content_tag :span, product.name %> <%= content_tag :small, euro(product.price) %> - <%= f.counter :count, max: product.stock, min:0, skip_label: true, wrapper_class: "input-group", class: "row_counter" %> + <%= f.counter :count, min: 0, max: product.stock, skip_label: true, wrapper_class: "input-group", class: "row_counter" %> <%= f.fields_for :product do |product| %> <%= product.hidden_field :price_cents, class: :price %> <%= product.hidden_field :stock, class: :stock %>