Sha256: 2a2f4a6993d99d5f9b68a88cddfdad23a8d5cf2581dc8db192326e0bac44c8a0

Contents?: true

Size: 1.42 KB

Versions: 21

Compression:

Stored size: 1.42 KB

Contents

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

  // Default options
  options = options || {}
  var multiple = typeof(options['multiple']) !== 'undefined' ? options['multiple'] : true

  function formatProduct(product) {
    return Select2.util.escapeMarkup(product.name);
  }

  this.select2({
    minimumInputLength: 3,
    multiple: multiple,
    initSelection: function (element, callback) {
      $.get(Spree.routes.admin_product_search, {
        ids: element.val().split(','),
        token: Spree.api_key
      }, function (data) {
        callback(multiple ? data.products : data.products[0]);
      });
    },
    ajax: {
      url: Spree.routes.admin_product_search,
      datatype: 'json',
      params: { "headers": {  'Authorization': 'Bearer ' + Spree.api_key } },
      data: function (term, page) {
        return {
          q: {
            name_cont: term,
            variants_including_master_sku_start: term,
            m: 'or'
          },
          token: Spree.api_key,
          page: page
        };
      },
      results: function (data, page) {
        var products = data.products ? data.products : [];
        return {
          results: products,
          more: (data.current_page * data.per_page) < data.total_count
        };
      }
    },
    formatResult: formatProduct,
    formatSelection: formatProduct
  });
};

Spree.ready(function () {
  $('.product_picker').productAutocomplete();
});

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
solidus_backend-2.8.0 app/assets/javascripts/spree/backend/product_picker.js