mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-13 04:00:21 +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")
|
||||
Reference in New Issue
Block a user