Create service for select2 autocomplete

This commit is contained in:
Guido Oliveira
2021-06-06 15:32:09 -03:00
parent baca89c575
commit 92031120e3
5 changed files with 56 additions and 49 deletions

View File

@@ -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))
)

View File

@@ -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))
)

View File

@@ -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")

View File

@@ -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;
}
}

View File

@@ -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