mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Create service for select2 autocomplete
This commit is contained in:
@@ -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))
|
||||
)
|
||||
|
||||
@@ -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))
|
||||
)
|
||||
|
||||
@@ -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")
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user