Sha256: cef05b8d25dcca7a78cdcef06ef0dfdf64d34c824535dc3e8f175b40ed520ce2

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

$(document).ready ->
  window.productTemplate = Handlebars.compile($('#product_template_sortable').text());

  productListTemplate = (products) ->
    products.map(productTemplate).join('') ||
    "<h4>#{Spree.translations.no_results}</h4>"

  raiseDraggable = (draggable) ->
    draggable.prev().insertAfter(draggable)
    sortupdate(draggable)

  lowerDraggable = (draggable) ->
    draggable.next().insertBefore(draggable)
    sortupdate(draggable)

  focusDraggable = (e) ->
    $(e.srcElement).focus()

  moveDraggable = (e) ->
    if e.keyCode == $.ui.keyCode.UP
      raiseDraggable $(e.currentTarget)
    else if e.keyCode == $.ui.keyCode.DOWN
      lowerDraggable $(e.currentTarget)

  saveSort = (event, ui) ->
   Spree.ajax
     url: Spree.routes.classifications_api,
     method: 'PUT',
     data:
       product_id: ui.item.data('product-id'),
       taxon_id: $('#taxon_id').val(),
       position: ui.item.index()

  sortable = $('#taxon_products').sortable()
    .on
      sortstart: focusDraggable
      sortstop: focusDraggable
      sortupdate: saveSort
    .on
      click: focusDraggable
      keydown: moveDraggable
    , '.sort_item'

  sortupdate = _.debounce (draggable) ->
    sortable.trigger('sortupdate', item: draggable)
  , 250

  formatTaxon = (taxon) ->
    Select2.util.escapeMarkup(taxon.pretty_name)

  $('#taxon_id').select2
    dropdownCssClass: "taxon_select_box",
    placeholder: Spree.translations.find_a_taxon,
    ajax:
      url: Spree.routes.taxons_search,
      params: { "headers": { "X-Spree-Token": Spree.api_key } },
      data: (term, page) ->
        per_page: 50,
        page: page,
        q:
          name_cont: term
      results: (data) ->
        results: data['taxons'],
        more: data.current_page < data.pages
    formatResult: formatTaxon,
    formatSelection: formatTaxon

  $('#taxon_id').on "change", (e) ->
    Spree.ajax
      url: Spree.routes.taxon_products_api,
      data: { id: e.val }
      success: (data) ->
        sortable.html productListTemplate(data.products)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_backend-1.0.7 app/assets/javascripts/spree/backend/taxons.js.coffee
solidus_backend-1.0.6 app/assets/javascripts/spree/backend/taxons.js.coffee
solidus_backend-1.0.5 app/assets/javascripts/spree/backend/taxons.js.coffee