Sha256: 05ce44e186fa49d237dfcaa40d3f7931922598342aa9bccb304248a6707814f1

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

import { Controller } from "@hotwired/stimulus"
import { Sortable } from "sortablejs"
import { patch } from "helpers"

export default class extends Controller {
  static values = { handle: String }

  connect() {
    const itemSortable = {
      ...this.options
    }

    let containers = null
    containers = this.element.querySelectorAll("[data-sortable-tree-parent-id-value]")

    for (let i = 0; i < containers.length; i++) {
      new Sortable(containers[i], itemSortable)
    }
  }

  async end({ item, newIndex, to }) {
    if (!item.dataset.sortableTreeUpdateUrlValue) return

    const data = {
      [item.dataset.sortableTreeResourceNameValue]: {
        new_parent_id: to.dataset.sortableTreeParentIdValue,
        new_position_idx: newIndex
      }
    }

    const response = await patch(item.dataset.sortableTreeUpdateUrlValue, { body: data })

    if (!response.ok) {
      show_flash("error", "This move could not be saved.")
    }
  }

  get options() {
    return {
      group: {
        name: "sortable-tree",
        pull: true,
        put: true
      },
      handle: this.handleValue || undefined,
      swapThreshold: 0.5,
      emptyInsertThreshold: 8,
      dragClass: "item-dragged",
      draggable: ".draggable",
      animation: 350,
      forceFallback: false,
      onEnd: this.end
    }
  }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_backend-4.8.4 app/javascript/spree/backend/controllers/sortable_tree_controller.js
spree_backend-4.8.3 app/javascript/spree/backend/controllers/sortable_tree_controller.js
spree_backend-4.8.2 app/javascript/spree/backend/controllers/sortable_tree_controller.js
spree_backend-4.8.1 app/javascript/spree/backend/controllers/sortable_tree_controller.js