Sha256: af834838424d6e039480b27f3d10d627d2171c038350786018c25814950b298a

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

import Sortable from "sortablejs"

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
  }

  fetch(url, {
    method: "PATCH",
    headers: {
      "Content-Type": "application/json",
      Accept: "application/json"
    },
    body: JSON.stringify(data)
  })
    .then(async (response) => {
      const pageData = await response.json()
      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

1 entries across 1 versions & 1 rubygems

Version Path
alchemy_cms-5.3.1 package/src/page_sorter.js