Sha256: 5ebfa1cf50ae4deecbaae9e0735a9ff51468d672039b54a3c81ccb813c50c2a2

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

(() => {
  const application = Stimulus.Application.start()

  application.register("repeater-form", class extends Stimulus.Controller {
    static get targets() {
      return ["list", "listItem", "content", "pane"]
    }

    connect() {
      Sortable.create(this.listTarget, {
        handle: '.sortable-handle',
        onUpdate: function(event) {
          let order_of_ids = [...this.listTarget.children].map(function(item) {
            return parseInt(item.dataset.partId)
          })

          // Sort the DOM elements containing the repeater fields
          let array = [...this.contentTarget.children]
          array.sort(function(a, b) {
            return order_of_ids.indexOf(parseInt(a.dataset.partId)) > order_of_ids.indexOf(parseInt(b.dataset.partId))
          }).map(node => this.contentTarget.appendChild(node))
        }.bind(this)
      })
    }

    delete(event) {
      event.preventDefault()
      let part_id = event.currentTarget.dataset.partId

      let pane = this.element.querySelector(`.structure-form-pane[data-part-id="${part_id}"]`)
      let listItem = this.element.querySelector(`.structure-form-menu li[data-part-id="${part_id}"]`)

      pane.parentElement.removeChild(pane)
      listItem.parentElement.removeChild(listItem)
    }
    
  })
})()

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spina-2.0.0 app/assets/javascripts/spina/admin/controllers/repeater_form_controller.js
spina-2.0.0.beta app/assets/javascripts/spina/admin/controllers/repeater_form_controller.js
spina-2.0.0.alpha app/assets/javascripts/spina/admin/controllers/repeater_form_controller.js