Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Update Product/OptionValues picker for Select2 4.x
Damian Legawiecaa64c7ea42f4
diff --git a/backend/app/assets/javascripts/spree/backend/option_value_picker.js b/backend/app/assets/javascripts/spree/backend/option_value_picker.jsindex abca5fa877..d358b477b9 100644--- a/backend/app/assets/javascripts/spree/backend/option_value_picker.js+++ b/backend/app/assets/javascripts/spree/backend/option_value_picker.js@@ -5,31 +5,47 @@ $.fn.optionValueAutocomplete = function (options) {options = options || {}var multiple = typeof (options.multiple) !== 'undefined' ? options.multiple : truevar productSelect = options.productSelect+ var productId = options.productId+ var values = options.values+ var clearSelection = options.clearSelectionfunction formatOptionValueList(values) {- var formatted_data = $.map(values, function (obj) {- var item = { id: obj.id, text: obj.name }-- return item- });+ return values.map(function(obj) {+ return { id: obj.id, text: obj.name }+ })+ }- return formatted_data+ function addOptions(select, productId, values) {+ $.ajax({+ type: 'GET',+ url: Spree.routes.option_values_api,+ dataType: 'json',+ data: {+ token: Spree.api_key,+ q: {+ id_in: values,+ variants_product_id_eq: productId+ }+ }+ }).then(function (data) {+ select.addSelect2Options(data)+ })}this.select2({multiple: multiple,- minimumInputLength: 2,+ minimumInputLength: 1,allowClear: true,ajax: {url: Spree.routes.option_values_api,dataType: 'json',data: function (params) {- var productId = typeof (productSelect) !== 'undefined' ? $(productSelect).select2('data').id : null+ var selectedProductId = typeof (productSelect) !== 'undefined' ? productSelect.val() : nullvar query = {q: {name_cont: params.term,- variants_product_id_eq: productId+ variants_product_id_eq: selectedProductId},token: Spree.api_key}@@ -37,16 +53,20 @@ $.fn.optionValueAutocomplete = function (options) {return query;},processResults: function(data) {- console.log('process results', data)var results = formatOptionValueList(data)return {results: results}- },- templateSelection: function(option_value) {- console.log('template selection', option_value)}}})++ if (values && productId && !clearSelection) {+ addOptions(this, productId, values)+ }++ if (clearSelection) {+ this.val(null).trigger('change')+ }}diff --git a/backend/app/assets/javascripts/spree/backend/product_picker.js b/backend/app/assets/javascripts/spree/backend/product_picker.jsindex 0e4c06284e..971ac07a80 100644--- a/backend/app/assets/javascripts/spree/backend/product_picker.js+++ b/backend/app/assets/javascripts/spree/backend/product_picker.js@@ -4,18 +4,27 @@ $.fn.productAutocomplete = function (options) {// Default optionsoptions = options || {}var multiple = typeof (options.multiple) !== 'undefined' ? options.multiple : true-- function formatProduct (product) {- return Select2.util.escapeMarkup(product.name)- }+ var values = typeof (options.values) !== 'undefined' ? options.values : nullfunction formatProductList(products) {- var formatted_data = $.map(products, function (obj) {- var item = { id: obj.id, text: obj.name }- return item- });+ return products.map(function(obj) {+ return { id: obj.id, text: obj.name }+ })+ }- return formatted_data+ function addOptions(select, values) {+ $.ajax({+ url: Spree.routes.products_api,+ dataType: 'json',+ data: {+ q: {+ id_in: values+ },+ token: Spree.api_key+ }+ }).then(function (data) {+ select.addSelect2Options(data.products)+ })}this.select2({@@ -42,10 +51,14 @@ $.fn.productAutocomplete = function (options) {}}},- templateSelection: function(data, container) {+ templateSelection: function(data, _container) {return data.text}})++ if (values) {+ addOptions(this, values)+ }}$(document).ready(function () {diff --git a/backend/app/assets/javascripts/spree/backend/promotions.js b/backend/app/assets/javascripts/spree/backend/promotions.jsindex aa71f84e98..444b3ab754 100644--- a/backend/app/assets/javascripts/spree/backend/promotions.js+++ b/backend/app/assets/javascripts/spree/backend/promotions.js@@ -39,20 +39,36 @@ function initProductActions () {if ($('#promo-rule-option-value-template').length) {var optionValueSelectNameTemplate = Handlebars.compile($('#promo-rule-option-value-option-values-select-name-template').html())var optionValueTemplate = Handlebars.compile($('#promo-rule-option-value-template').html())+ var optionValuesList = $('.js-promo-rule-option-values')- var addOptionValue = function (product, values) {- $('.js-promo-rule-option-values').append(optionValueTemplate({- productSelect: { value: product },- optionValuesSelect: { value: values }- }))- var optionValue = $('.js-promo-rule-option-values .promo-rule-option-value').last()- optionValue.find('.js-promo-rule-option-value-product-select').productAutocomplete({ multiple: false })- optionValue.find('.js-promo-rule-option-value-option-values-select').optionValueAutocomplete({- productSelect: '.js-promo-rule-option-value-product-select'+ var addOptionValue = function (productId, values) {+ var template = optionValueTemplate({+ productId: productId})- if (product === null) {- optionValue.find('.js-promo-rule-option-value-option-values-select').prop('disabled', true)++ optionValuesList.append(template)++ var optionValueId = '#promo-rule-option-value-'+ if (productId) {+ optionValueId += productId.toString()}+ var optionValue = optionValuesList.find(optionValueId)++ var productSelect = optionValue.find('.js-promo-rule-option-value-product-select')+ var valuesSelect = optionValue.find('.js-promo-rule-option-value-option-values-select')++ productSelect.productAutocomplete({ multiple: false, values: productId })+ productSelect.on('select2:select', function(e) {+ valuesSelect.attr('disabled', false).removeClass('d-none').addClass('d-block')+ valuesSelect.attr('name', optionValueSelectNameTemplate({ productId: productSelect.val() }).trim())+ valuesSelect.optionValueAutocomplete({+ productId: productId,+ productSelect: productSelect,+ multiple: true,+ values: values,+ clearSelection: productId != productSelect.val()+ })+ })}var originalOptionValues = $('.js-original-promo-rule-option-values').data('original-option-values')@@ -73,12 +89,6 @@ function initProductActions () {$(document).on('click', '.js-remove-promo-rule-option-value', function () {$(this).parents('.promo-rule-option-value').remove()})-- $(document).on('change', '.js-promo-rule-option-value-product-select', function () {- var optionValueSelect = $(this).parents('.promo-rule-option-value').find('.js-promo-rule-option-value-option-values-select')- optionValueSelect.attr('name', optionValueSelectNameTemplate({ productId: $(this).val() }).trim())- optionValueSelect.prop('disabled', $(this).val() === '').select2('val', '')- })}//diff --git a/backend/app/assets/javascripts/spree/backend/spree-select2.js b/backend/app/assets/javascripts/spree/backend/spree-select2.jsindex eee8713728..6caffa26be 100644--- a/backend/app/assets/javascripts/spree/backend/spree-select2.js+++ b/backend/app/assets/javascripts/spree/backend/spree-select2.js@@ -24,3 +24,26 @@ document.addEventListener('DOMContentLoaded', function() {}})})++$.fn.addSelect2Options = function (data) {+ var select = this++ function appendOption(select, data) {+ var option = new Option(data.name, data.id, true, true)+ select.append(option).trigger('change')+ }++ if (Array.isArray(data)) {+ data.map(function(row) {+ appendOption(select, row)+ })+ } else {+ appendOption(select, data)+ }+ select.trigger({+ type: 'select2:select',+ params: {+ data: data+ }+ })+}diff --git a/backend/app/views/spree/admin/promotions/rules/_option_value.html.erb b/backend/app/views/spree/admin/promotions/rules/_option_value.html.erbindex 9b2eedcf60..164fe69409 100644--- a/backend/app/views/spree/admin/promotions/rules/_option_value.html.erb+++ b/backend/app/views/spree/admin/promotions/rules/_option_value.html.erb@@ -12,11 +12,11 @@data: { :'original-option-values' => promotion_rule.preferred_eligible_values } %><script type="text/x-handlebars-template" id="promo-rule-option-value-option-values-select-name-template">- <%= param_prefix %>[preferred_eligible_values][{{productId}}]+ <%= param_prefix %>[preferred_eligible_values][{{productId}}][]</script><script type="text/x-handlebars-template" id="promo-rule-option-value-template">- <div class="promo-rule-option-value panel card mb-3">+ <div class="promo-rule-option-value panel card mb-3" id="promo-rule-option-value-{{productId}}"><div class="card-header d-flex justify-content-between"><label><%= Spree.t(:product) %> <%= Spree.t(:option_values) %></label><a class="js-remove-promo-rule-option-value delete pull-right">@@ -25,15 +25,12 @@</div><div class="card-body row p-3"><div class="col-12 col-md-6 product-select">- <!-- TODO -->- <%= select_tag "product", nil, class: 'js-promo-rule-option-value-product-select d-block w-100' %>+ <%= select_tag "product", nil, class: 'js-promo-rule-option-value-product-select d-block w-100', id: "promo-rule-option-value-product-select-{{productId}}" %></div><div class="col-12 col-md-6 option-value-select">- <%#= select_tag "#{param_prefix}[preferred_eligible_values][{{ productSelect.value }}]", nil, class: 'js-promo-rule-option-value-option-values-select d-block w-100' %>- <%= select "#{param_prefix}","[preferred_eligible_values][]", {}, { include_hidden: true }, class: 'js-promo-rule-option-value-option-values-select d-block w-100' %>+ <%= select_tag "#{param_prefix}[preferred_eligible_values][{{ productId }}][]", nil, multiple: true, disabled: true, class: 'js-promo-rule-option-value-option-values-select d-none w-100', id: "promo-rule-option-value-option-values-select-{{productId}}" %></div></div></div></script>-