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