From 92031120e3250ea108447269e7fabdf7b7ffa82f Mon Sep 17 00:00:00 2001 From: Guido Oliveira Date: Sun, 6 Jun 2021 15:32:09 -0300 Subject: [PATCH] Create service for select2 autocomplete --- .../producer_autocomplete.js.coffee | 31 ++++++------------- .../directives/taxon_autocomplete.js.coffee | 31 ++++++------------- .../services/autocomplete_select2.js.coffee | 31 +++++++++++++++++++ app/assets/stylesheets/admin/disabled.scss | 8 +++-- app/services/products_renderer.rb | 4 +-- 5 files changed, 56 insertions(+), 49 deletions(-) create mode 100644 app/assets/javascripts/admin/utils/services/autocomplete_select2.js.coffee diff --git a/app/assets/javascripts/admin/enterprises/directives/producer_autocomplete.js.coffee b/app/assets/javascripts/admin/enterprises/directives/producer_autocomplete.js.coffee index 181b781903..e955be0a9d 100644 --- a/app/assets/javascripts/admin/enterprises/directives/producer_autocomplete.js.coffee +++ b/app/assets/javascripts/admin/enterprises/directives/producer_autocomplete.js.coffee @@ -1,4 +1,4 @@ -angular.module("admin.enterprises").directive "ofnProducerAutocomplete", (Enterprises, $sanitize) -> +angular.module("admin.enterprises").directive "ofnProducerAutocomplete", (Enterprises, AutocompleteSelect2) -> scope: true link: (scope,element,attrs) -> multiple = scope.$eval attrs.multipleSelection @@ -7,24 +7,11 @@ angular.module("admin.enterprises").directive "ofnProducerAutocomplete", (Enterp suppliers = scope.suppliers setTimeout -> - element.select2 - placeholder: placeholder - multiple: multiple - initSelection: (element, callback) -> - if multiple - callback Enterprises.findByIDs(initialSelection) - else - callback Enterprises.findByID(initialSelection) - query: (query) -> - query.callback { results: Enterprises.findByTerm(suppliers, query.term) } - formatResult: (producer) -> - $sanitize(producer.name) - formatSelection: (producer) -> - producer.name - - #Allows drag and drop - if multiple - element.select2("container").find("ul.select2-choices").sortable - containment: 'parent' - start: -> element.select2("onSortStart") - update: -> element.select2("onSortEnd") + AutocompleteSelect2.autocomplete( + multiple, + placeholder, + element, + (-> Enterprises.findByID(initialSelection)), + (-> Enterprises.findByIDs(initialSelection)), + ((term) -> Enterprises.findByTerm(suppliers, term)) + ) diff --git a/app/assets/javascripts/admin/taxons/directives/taxon_autocomplete.js.coffee b/app/assets/javascripts/admin/taxons/directives/taxon_autocomplete.js.coffee index 2c28be9c69..31f5a54cb6 100644 --- a/app/assets/javascripts/admin/taxons/directives/taxon_autocomplete.js.coffee +++ b/app/assets/javascripts/admin/taxons/directives/taxon_autocomplete.js.coffee @@ -1,4 +1,4 @@ -angular.module("admin.taxons").directive "ofnTaxonAutocomplete", (Taxons, $sanitize) -> +angular.module("admin.taxons").directive "ofnTaxonAutocomplete", (Taxons, AutocompleteSelect2) -> # Adapted from Spree's existing taxon autocompletion scope: true link: (scope,element,attrs) -> @@ -7,24 +7,11 @@ angular.module("admin.taxons").directive "ofnTaxonAutocomplete", (Taxons, $sanit initialSelection = scope.$eval attrs.ngModel setTimeout -> - element.select2 - placeholder: placeholder - multiple: multiple - initSelection: (element, callback) -> - if multiple - callback Taxons.findByIDs(initialSelection) - else - callback Taxons.findByID(initialSelection) - query: (query) -> - query.callback { results: Taxons.findByTerm(query.term) } - formatResult: (taxon) -> - $sanitize(taxon.name) - formatSelection: (taxon) -> - taxon.name - - #Allows drag and drop - if multiple - element.select2("container").find("ul.select2-choices").sortable - containment: 'parent' - start: -> element.select2("onSortStart") - update: -> element.select2("onSortEnd") + AutocompleteSelect2.autocomplete( + multiple, + placeholder, + element, + (-> Taxons.findByID(initialSelection)), + (-> Taxons.findByIDs(initialSelection)), + ((term) -> Taxons.findByTerm(term)) + ) diff --git a/app/assets/javascripts/admin/utils/services/autocomplete_select2.js.coffee b/app/assets/javascripts/admin/utils/services/autocomplete_select2.js.coffee new file mode 100644 index 0000000000..a17eee843a --- /dev/null +++ b/app/assets/javascripts/admin/utils/services/autocomplete_select2.js.coffee @@ -0,0 +1,31 @@ +angular.module("admin.utils").factory 'AutocompleteSelect2', ($sanitize) -> + scope: true + autocomplete: ( + multiple, + placeholder, + element, + findByID, + findByIDs, + findByTerm + ) -> + element.select2 + placeholder: placeholder + multiple: multiple + initSelection: (element, callback) -> + if multiple + callback findByIDs() + else + callback findByID() + query: (query) -> + query.callback { results: findByTerm(query.term) } + formatResult: (item) -> + $sanitize(item.name) + formatSelection: (item) -> + item.name + + #Allows drag and drop + if multiple + element.select2("container").find("ul.select2-choices").sortable + containment: 'parent' + start: -> element.select2("onSortStart") + update: -> element.select2("onSortEnd") diff --git a/app/assets/stylesheets/admin/disabled.scss b/app/assets/stylesheets/admin/disabled.scss index 5555e30d4e..e78c23e5c5 100644 --- a/app/assets/stylesheets/admin/disabled.scss +++ b/app/assets/stylesheets/admin/disabled.scss @@ -1,11 +1,13 @@ +$disabled-background: #c3c3c3; + label.disabled { - color: #c3c3c3; + color: $disabled-background; pointer-events: none; } input[type='button'], input[type='submit'] { &:disabled { - background-color: #c3c3c3; + background-color: $disabled-background; color: #ffffff; } } @@ -18,6 +20,6 @@ input[type='button'], input[type='submit'] { } .select2-search-choice { - background-color: #c3c3c3 !important; + background-color: $disabled-background !important; } } diff --git a/app/services/products_renderer.rb b/app/services/products_renderer.rb index 1f276937eb..8b7e621f80 100644 --- a/app/services/products_renderer.rb +++ b/app/services/products_renderer.rb @@ -32,7 +32,7 @@ class ProductsRenderer @products ||= begin results = distributed_products. products_relation. - order(Arel.sql(taxon_order)) + order(Arel.sql(products_order)) filter_and_paginate(results). each { |product| product_scoper.scope(product) } # Scope results with variant_overrides @@ -59,7 +59,7 @@ class ProductsRenderer OrderCycleDistributedProducts.new(distributor, order_cycle, customer) end - def taxon_order + def products_order if distributor.preferred_shopfront_product_sorting_method == "by_producer" && distributor.preferred_shopfront_producer_order.present? distributor .preferred_shopfront_producer_order