Sha256: de3e4a5db34f5c915e28cfbd4ed77e2baa4e46636b516377cb9777242cf82933

Contents?: true

Size: 1.64 KB

Versions: 35

Compression:

Stored size: 1.64 KB

Contents

import Sortable from "sortablejs"
import { patch } from "./utils/ajax"

function onFinishDragging(evt) {
  const pageId = evt.item.dataset.pageId
  const url = Alchemy.routes.move_admin_page_path(pageId)
  const data = {
    target_parent_id: evt.to.dataset.parentId,
    new_position: evt.newIndex
  }

  patch(url, data)
    .then(async (response) => {
      const pageData = await response.data
      const pageEl = document.getElementById(`page_${pageId}`)
      const urlPathEl = pageEl.querySelector(".sitemap_url")

      Alchemy.growl(Alchemy.t("Successfully moved page"))
      urlPathEl.textContent = pageData.url_path
      displayPageFolders()
    })
    .catch((error) => {
      Alchemy.growl(error.message || error, "error")
    })
}

export function displayPageFolders() {
  document.querySelectorAll("li.sitemap-item").forEach((el) => {
    const pageFolderEl = el.querySelector(".page_folder")
    const list = el.querySelector(".children")
    const page = {
      folded: el.dataset.folded === "true",
      id: el.dataset.pageId,
      type: el.dataset.type
    }

    if (list.children.length > 0 || page.folded) {
      pageFolderEl.outerHTML = HandlebarsTemplates.page_folder({ page })
    } else {
      pageFolderEl.innerHTML = ""
    }
  })
}

export function createSortables(sortables) {
  sortables.forEach((el) => {
    new Sortable(el, {
      group: "pages",
      animation: 150,
      fallbackOnBody: true,
      swapThreshold: 0.65,
      handle: ".handle",
      onEnd: onFinishDragging
    })
  })
}

export default function () {
  const sortables = document.querySelectorAll("ul.children")
  displayPageFolders()
  createSortables(sortables)
}

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
alchemy_cms-6.1.10 package/src/page_sorter.js
alchemy_cms-6.1.9 package/src/page_sorter.js
alchemy_cms-6.1.8 package/src/page_sorter.js
alchemy_cms-6.1.7 package/src/page_sorter.js
alchemy_cms-6.1.6 package/src/page_sorter.js
alchemy_cms-7.0.0.pre.b package/src/page_sorter.js
alchemy_cms-6.1.5 package/src/page_sorter.js
alchemy_cms-6.0.14 package/src/page_sorter.js
alchemy_cms-6.1.4 package/src/page_sorter.js
alchemy_cms-7.0.0.pre.a package/src/page_sorter.js
alchemy_cms-6.0.13 package/src/page_sorter.js
alchemy_cms-6.1.3 package/src/page_sorter.js
alchemy_cms-6.1.2 package/src/page_sorter.js
alchemy_cms-6.1.1 package/src/page_sorter.js
alchemy_cms-6.1.0 package/src/page_sorter.js
alchemy_cms-6.0.12 package/src/page_sorter.js
alchemy_cms-6.0.11 package/src/page_sorter.js
alchemy_cms-6.0.10 package/src/page_sorter.js
alchemy_cms-5.3.8 package/src/page_sorter.js
alchemy_cms-6.0.9 package/src/page_sorter.js