diff --git a/app/assets/javascripts/repositories/stock.js b/app/assets/javascripts/repositories/stock.js new file mode 100644 index 000000000..ea31742e5 --- /dev/null +++ b/app/assets/javascripts/repositories/stock.js @@ -0,0 +1,94 @@ +/* global dropdownSelector */ + +var RepositoryStockValues = (function() { + const UNIT_SELECTOR = '#repository-stock-value-units'; + + function initManageAction() { + $('.repository-show').on('click', '.manage-repository-stock-value-link', function() { + $.ajax({ + url: $(this).closest('tr').data('manage-stock-url'), + type: 'GET', + dataType: 'json', + success: (result) => { + var $manageModal = $('#manage-repository-stock-value-modal'); + $manageModal.find('.modal-content').html(result.html); + + dropdownSelector.init(UNIT_SELECTOR, { + singleSelect: true, + closeOnSelect: true, + selectAppearance: 'simple', + onChange: function() { + $('.stock-final-container .units').text(dropdownSelector.getValues(UNIT_SELECTOR)); + } + }); + + $manageModal.find('form').on('ajax:success', function() { + var dataTable = $('.dataTable').DataTable(); + $manageModal.modal('hide'); + dataTable.ajax.reload(); + }); + + $('.stock-operator-option').click(function() { + var $stockInput = $('#stock-input-amount'); + $('.stock-operator-option').removeClass('btn-primary').addClass('btn-secondary'); + $(this).removeClass('btn-secondary').addClass('btn-primary'); + $stockInput.data('operator', $(this).data('operator')); + + dropdownSelector.selectValues(UNIT_SELECTOR, $('#initial_units').val()); + switch ($(this).data('operator')) { + case 'set': + $stockInput.val($stockInput.data('currentAmount')); + dropdownSelector.enableSelector(UNIT_SELECTOR); + break; + case 'add': + $stockInput.val(''); + dropdownSelector.disableSelector(UNIT_SELECTOR); + break; + case 'remove': + $stockInput.val(''); + dropdownSelector.disableSelector(UNIT_SELECTOR); + break; + default: + break; + } + }); + + $('#stock-input-amount').on('input', function() { + var currentAmount = parseFloat($(this).data('currentAmount')); + var inputAmount = parseFloat($(this).val()); + var newAmount; + + if (!inputAmount) return; + + switch ($(this).data('operator')) { + case 'set': + newAmount = inputAmount; + break; + case 'add': + newAmount = currentAmount + inputAmount; + break; + case 'remove': + newAmount = currentAmount - inputAmount; + break; + default: + newAmount = currentAmount; + break; + } + $('#repository_stock_value_amount').val(newAmount); + $('.stock-final-container .value').text(newAmount); + }); + + $manageModal.modal('show'); + } + }); + }); + } + + return { + init: () => { + initManageAction(); + } + }; +}()); + +RepositoryStockValues.init(); diff --git a/app/assets/javascripts/repository_columns/index.js b/app/assets/javascripts/repository_columns/index.js index 6b8a19892..545dc0e87 100644 --- a/app/assets/javascripts/repository_columns/index.js +++ b/app/assets/javascripts/repository_columns/index.js @@ -425,90 +425,8 @@ var RepositoryColumns = (function() { }; }()); -var RepositoryStockValues = (function() { - function initManageAction() { - $(document).on('click', '.manage-repository-stock-value-link', function() { - $.ajax({ - url: $(this).closest('tr').data('manage-stock-url'), - type: 'GET', - dataType: 'json', - success: (result) => { - var $manageModal = $('#manage-repository-stock-value-modal'); - $manageModal.find('.modal-content').html(result.html); - dropdownSelector.init('#repository-stock-value-units', { - noEmptyOption: true, - singleSelect: true, - closeOnSelect: true, - selectAppearance: 'simple' - }); - - $manageModal.find('form').on('ajax:success', function() { - var dataTable = $('.dataTable').DataTable(); - $manageModal.modal('hide'); - dataTable.ajax.reload(); - }); - - $('.stock-operator-option').click(function() { - var $stockInput = $('#stock-input-amount'); - $('.stock-operator-option').removeClass('btn-primary').addClass('btn-secondary'); - $(this).removeClass('btn-secondary').addClass('btn-primary'); - $stockInput.data('operator', $(this).data('operator')); - - switch ($(this).data('operator')) { - case 'set': - $stockInput.val($stockInput.data('currentAmount')); - break; - case 'add': - $stockInput.val(''); - break; - case 'remove': - $stockInput.val(''); - break; - default: - - break; - } - }); - - $('#stock-input-amount').on('input', function() { - var currentAmount = parseFloat($(this).data('currentAmount')); - var inputAmount = parseFloat($(this).val()); - var newAmount; - - if (!inputAmount) return; - - switch ($(this).data('operator')) { - case 'set': - newAmount = inputAmount; - break; - case 'add': - newAmount = currentAmount + inputAmount; - break; - case 'remove': - newAmount = currentAmount - inputAmount; - break; - default: - newAmount = currentAmount; - break; - } - $('#repository_stock_value_amount').val(newAmount); - }); - - $manageModal.modal('show'); - } - }); - }); - } - - return { - init: () => { - initManageAction(); - } - }; -}()); $(document).on('turbolinks:load', function() { RepositoryColumns.init(); - RepositoryStockValues.init(); }); diff --git a/app/assets/stylesheets/repository/stock_modal.scss b/app/assets/stylesheets/repository/stock_modal.scss new file mode 100644 index 000000000..85da0a14c --- /dev/null +++ b/app/assets/stylesheets/repository/stock_modal.scss @@ -0,0 +1,38 @@ +// scss-lint:disable SelectorDepth +// scss-lint:disable NestingDepth + +.repository-stock-modal { + .stock-update-view { + align-items: center; + display: grid; + grid-template-columns: 1fr auto 1fr; + + .stock-initial-container, + .stock-final-container { + align-items: center; + background: $color-concrete; + display: flex; + flex-direction: column; + flex-grow: 1; + padding: .5em 0; + + .value { + @include font-h1; + } + + .subtitle { + @include font-small; + color: $color-silver-chalice; + } + + .units { + @include font-small; + } + } + + .stock-arrow { + @include font-h1; + padding: 1em; + } + } +} diff --git a/app/views/repository_columns/_manage_column_modal_content.html.erb b/app/views/repository_columns/_manage_column_modal_content.html.erb index 1994a4d7d..fadf8e3e1 100644 --- a/app/views/repository_columns/_manage_column_modal_content.html.erb +++ b/app/views/repository_columns/_manage_column_modal_content.html.erb @@ -79,10 +79,12 @@ diff --git a/app/views/repository_stock_values/_manage_modal.html.erb b/app/views/repository_stock_values/_manage_modal.html.erb index 3ab53660c..8cc0be146 100644 --- a/app/views/repository_stock_values/_manage_modal.html.erb +++ b/app/views/repository_stock_values/_manage_modal.html.erb @@ -1,4 +1,4 @@ - +<%= javascript_include_tag 'repositories/stock' %> diff --git a/app/views/repository_stock_values/_manage_modal_content.erb b/app/views/repository_stock_values/_manage_modal_content.erb index c79f6e7e3..d5c077635 100644 --- a/app/views/repository_stock_values/_manage_modal_content.erb +++ b/app/views/repository_stock_values/_manage_modal_content.erb @@ -9,6 +9,7 @@ + <% unless repository_stock_value.new_record? %> +
+
+
+
+ <%= t('repository_stock_values.manage_modal.current_stock') %> + <%= repository_stock_value.amount %> + <%= repository_stock_value.units %> +
+
+ +
+
+ <%= t('repository_stock_values.manage_modal.new_stock') %> + <%= repository_stock_value.amount %> + <%= repository_stock_value.units %> +
+
+
+
+ <% end %>