From bf2c1a0c1deffa84f3e33cd1bbab6ac795df630c Mon Sep 17 00:00:00 2001 From: Hugo Daniel Date: Fri, 9 Aug 2019 14:34:23 +0200 Subject: [PATCH] Add ng directive for setting on demand --- .../set_variant_on_demand.js.coffee | 21 +++++++ .../spree/admin/variants/_form.html.haml | 59 ++++--------------- 2 files changed, 32 insertions(+), 48 deletions(-) create mode 100644 app/assets/javascripts/admin/products/directives/set_variant_on_demand.js.coffee diff --git a/app/assets/javascripts/admin/products/directives/set_variant_on_demand.js.coffee b/app/assets/javascripts/admin/products/directives/set_variant_on_demand.js.coffee new file mode 100644 index 0000000000..8cbd03b4d0 --- /dev/null +++ b/app/assets/javascripts/admin/products/directives/set_variant_on_demand.js.coffee @@ -0,0 +1,21 @@ +angular.module("admin.products").directive "setOnDemand", -> + link: (scope, element, attr) -> + onHand = element.context.querySelector("#variant_on_hand") + onDemand = element.context.querySelector("#variant_on_demand") + + if onDemand.checked + onHand.disabled = 'disabled' + onHand.dataStock = onHand.value + onHand.value = t('admin.products.variants.infinity') + + onDemand.addEventListener 'change', (event) -> + if onDemand.checked + onHand.disabled = 'disabled' + onHand.dataStock = onHand.value + onHand.value = t('admin.products.variants.infinity') + else + onHand.removeAttribute('disabled') + onHand.value = onHand.dataStock + + + diff --git a/app/views/spree/admin/variants/_form.html.haml b/app/views/spree/admin/variants/_form.html.haml index 6f96acb020..00c4b01bfe 100644 --- a/app/views/spree/admin/variants/_form.html.haml +++ b/app/views/spree/admin/variants/_form.html.haml @@ -39,16 +39,17 @@ = f.text_field :cost_price, value: number_to_currency(@variant.cost_price, unit: ''), class: 'fullwidth' - if Spree::Config[:track_inventory_levels] - .field.checkbox - %label - = f.check_box :on_demand - = t(:on_demand) - %div{'ofn-with-tip' => t('admin.products.variants.to_order_tip')} - %a= t('admin.whats_this') - .field - = f.label :on_hand, t(:on_hand) - .fullwidth - = f.text_field :on_hand + %div{ 'set-on-demand' => '' } + .field.checkbox + %label + = f.check_box :on_demand + = t(:on_demand) + %div{'ofn-with-tip' => t('admin.products.variants.to_order_tip')} + %a= t('admin.whats_this') + .field + = f.label :on_hand, t(:on_hand) + .fullwidth + = f.text_field :on_hand .right.six.columns.omega.label-block - if @product.variant_unit != 'weight' @@ -64,41 +65,3 @@ = f.text_field field, value: value, class: 'fullwidth' .clear - - -:javascript - angular.element(document.getElementById("new_variant")).ready(function() { - angular.bootstrap(document.getElementById("new_variant"), ['admin.products']); - }); - - $(document).ready(function() { - - var on_demand = $('input#variant_on_demand'); - var on_hand = $('input#variant_on_hand'); - - disableOnHandIfOnDemand = function() { - on_demand_checked = on_demand.attr('checked') - if ( on_demand_checked == undefined ) - on_demand_checked = false; - - on_hand.attr('disabled', on_demand_checked); - if(on_demand_checked) { - on_hand.attr('data-stock', on_hand.val()); - on_hand.val(t('admin.products.variants.infinity')); - } - } - - disableOnHandIfOnDemand(); - - on_demand.change(function(){ - disableOnHandIfOnDemand(); - if(!this.checked) { - if(on_hand.attr('data-stock') !== undefined) { - on_hand.val(on_hand.attr('data-stock')); - } else { - on_hand.val("0"); - } - } - }); - }); -