Files
openfoodnetwork/app/assets/javascripts/admin/utils/directives/variant_autocomplete.js.coffee
Cillian O'Ruanaidh 2462d71ab5 When creating backoffice orders hide variants with no stock by default
But let people include out of stock variants by checking a checkbox if they want.

Note, we only apply the variants in stock scope if a distributor is
present. I think this is because this search method is also used when
setting up subscriptions so I don't think we want to change the
behaviour there.

Co-authored-by: Maikel Linke <maikel@email.org.au>
2021-10-08 13:13:32 +01:00

33 lines
1.4 KiB
CoffeeScript

angular.module("admin.utils").directive "variantAutocomplete", ($timeout) ->
restrict: 'C'
link: (scope, element, attrs) ->
$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
include_out_of_stock: scope.include_out_of_stock
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
element.on "select2-opening", ->
scope.include_out_of_stock = if $('#include_out_of_stock').is(':checked') then "1" else ""