Sha256: f51c68adbaa157ccf4c9c47017484b25eacf06717ac2c30352c2e13548ed63ce

Contents?: true

Size: 842 Bytes

Versions: 5

Compression:

Stored size: 842 Bytes

Contents

import { Controller } from "@hotwired/stimulus"
import Sortable from "sortablejs"
import { patch } from '@rails/request.js'

export default class extends Controller {
  static values = {
    param: { type: String, default: 'postion' },
    handle: { type: String, default: null },
    animation: { type: Number, default: 150 },
  }

  connect() {
    this.sortable = Sortable.create(this.element, {
      onEnd: this.onEnd.bind(this),
      animation: this.animationValue,
      handle: this.handleValue,
    })
  }

  async onEnd({ item, newIndex }) {
    if (!item.dataset.sortableUrl) return

    const data = new FormData()
    data.append(this.paramValue, newIndex + 1)

    return await patch(item.dataset.sortableUrl, { body: data, responseKind: "js" })
  }

  disconnect() {
    this.sortable.destroy()
    this.sortable = null
  }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_admin-0.3.2 app/javascript/solidus_admin/controllers/sortable_controller.js
solidus_admin-0.3.1 app/javascript/solidus_admin/controllers/sortable_controller.js
solidus_admin-0.3.0 app/javascript/solidus_admin/controllers/sortable_controller.js
solidus_admin-0.2.0 app/javascript/solidus_admin/controllers/sortable_controller.js
solidus_admin-0.1.0 app/javascript/solidus_admin/controllers/sortable_controller.js