Sha256: 89a602ce6e3b2cb522e60e23e74b717930984cd211326f020efcb6fdcc40467f

Contents?: true

Size: 1.64 KB

Versions: 12

Compression:

Stored size: 1.64 KB

Contents

$.fn.optionValueAutocomplete = function (options) {
  'use strict'

  // Default options
  options = options || {}
  var multiple = typeof (options.multiple) !== 'undefined' ? options.multiple : true
  var productSelect = options.productSelect
  var productId = options.productId
  var values = options.values
  var clearSelection = options.clearSelection

  function formatOptionValueList(values) {
    return values.map(function(obj) {
      return { id: obj.id, text: obj.name }
    })
  }

  function addOptions(select, productId, values) {
    $.ajax({
      type: 'GET',
      url: Spree.routes.option_values_api,
      dataType: 'json',
      data: {
        token: Spree.api_key,
        q: {
          id_in: values,
          variants_product_id_eq: productId
        }
      }
    }).then(function (data) {
      select.addSelect2Options(data)
    })
  }

  this.select2({
    multiple: multiple,
    minimumInputLength: 1,
    ajax: {
      url: Spree.routes.option_values_api,
      dataType: 'json',
      data: function (params) {
        var selectedProductId = typeof (productSelect) !== 'undefined' ? productSelect.val() : null

        var query = {
          q: {
            name_cont: params.term,
            variants_product_id_eq: selectedProductId
          },
          token: Spree.api_key
        }

        return query;
      },
      processResults: function(data) {
        var results = formatOptionValueList(data)

        return {
          results: results
        }
      }
    }
  })

  if (values && productId && !clearSelection) {
    addOptions(this, productId, values)
  }

  if (clearSelection) {
    this.val(null).trigger('change')
  }
}

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
spree_backend-4.2.7 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.6 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.5 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.4 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.3.1 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.3 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.2 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.1 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.0 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.0.rc5 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.0.rc4 app/assets/javascripts/spree/backend/option_value_picker.js
spree_backend-4.2.0.rc3 app/assets/javascripts/spree/backend/option_value_picker.js