Sha256: 57e2cc1b4056e5ffe1c409cded3b822dee6a4ed27fdb12173abcd227471a105d

Contents?: true

Size: 1.41 KB

Versions: 31

Compression:

Stored size: 1.41 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": { "X-Spree-Token": 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
  });
};

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

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
solidus_backend-2.4.2 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.4.1 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.3.1 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.2.2 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.1.1 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.0.3 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-1.4.2 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.4.0 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.4.0.rc1 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.4.0.beta1 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.3.0 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.3.0.rc3 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.3.0.rc2 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.3.0.rc1 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.3.0.beta1 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.0.2 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-1.4.1 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.2.1 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.2.0 app/assets/javascripts/spree/backend/product_picker.js
solidus_backend-2.2.0.rc1 app/assets/javascripts/spree/backend/product_picker.js