Sha256: 7b0bca0aeee0e677896c367dc32e5424b8fc5672abe80aaf563454834fbba2d6

Contents?: true

Size: 1.86 KB

Versions: 54

Compression:

Stored size: 1.86 KB

Contents

/* global variantTemplate */
// variant autocompletion
$(function () {
  var variantAutocompleteTemplate = $('#variant_autocomplete_template')
  if (variantAutocompleteTemplate.length > 0) {
    window.variantTemplate = Handlebars.compile(variantAutocompleteTemplate.text())
    window.variantStockTemplate = Handlebars.compile($('#variant_autocomplete_stock_template').text())
    window.variantLineItemTemplate = Handlebars.compile($('#variant_line_items_autocomplete_stock_template').text())
  }
})

function formatVariantResult (variant) {
  if (variant['images'][0] !== undefined && variant['images'][0].mini_url !== undefined) {
    variant.image = variant.images[0].mini_url
  }
  return variantTemplate({
    variant: variant
  })
}

$.fn.variantAutocomplete = function () {
  return this.select2({
    placeholder: Spree.translations.variant_placeholder,
    minimumInputLength: 3,
    initSelection: function (element, callback) {
      return $.get(Spree.routes.variants_api + '/' + element.val(), {
        token: Spree.api_key
      }).done(function (data) {
        return callback(data)
      })
    },
    ajax: {
      url: Spree.url(Spree.routes.variants_api),
      quietMillis: 200,
      datatype: 'json',
      data: function (term) {
        return {
          q: {
            product_name_or_sku_cont: term
          },
          token: Spree.api_key
        }
      },
      results: function (data) {
        window.variants = data['variants']
        return {
          results: data['variants']
        }
      }
    },
    formatResult: formatVariantResult,
    formatSelection: function (variant) {
      // eslint-disable-next-line no-extra-boolean-cast
      if (!!variant.options_text) {
        return Select2.util.escapeMarkup(variant.name + '(' + variant.options_text + ')')
      } else {
        return Select2.util.escapeMarkup(variant.name)
      }
    }
  })
}

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
spree_backend-4.0.9 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.1.15 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.1.14 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.0.8 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-3.7.14.1 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.1.13.1 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.0.7.1 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.0.7 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-3.7.14 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.1.13 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.0.6 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-3.7.13 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.0.5 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.1.12 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-3.7.12 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.0.4 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-3.7.11 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.1.11 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.1.10 app/assets/javascripts/spree/backend/variant_autocomplete.js
spree_backend-4.1.9 app/assets/javascripts/spree/backend/variant_autocomplete.js