Files
openfoodnetwork/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee
luisramos0 7e6259da31 Replace admin/variants/search rabl template with existing admin variant serializer with a few added attributes
This commit fixes a bug as the image of the variatn was not being rendered, we need to fetch the image of the product, not the image of the variant.
2019-07-23 21:37:45 +01:00

34 lines
1.4 KiB
CoffeeScript

angular.module("admin.utils").directive "variantAutocomplete", ($timeout) ->
restrict: 'C'
link: (scope, element, attrs) ->
# Make variantAutocomplete do nothing because it is called
# from core/app/assets/javascripts/admin/orders/edit.js
$.fn.variantAutocomplete = angular.noop
$timeout ->
if $("#variant_autocomplete_template").length > 0
variantTemplate = Handlebars.compile($("#variant_autocomplete_template").text())
if Spree.routes
element.parent().children(".options_placeholder").attr "id", element.parent().data("index")
element.select2
placeholder: t('admin.orders.select_variant')
minimumInputLength: 3
quietMillis: 300
ajax:
url: Spree.routes.variants_search
datatype: "json"
data: (term, page) ->
q: term
distributor_id: scope.distributor_id
order_cycle_id: scope.order_cycle_id
eligible_for_subscriptions: scope.eligible_for_subscriptions
results: (data, page) ->
window.variants = data # this is how spree auto complete JS code picks up variants
results: data
formatResult: (variant) ->
variantTemplate variant: variant
formatSelection: (variant) ->
element.parent().children(".options_placeholder").html variant.options_text
variant.name