Sha256: aba0b4073658338bbbf4ab1354610ecefab3cd66cf61d9d27a251407279a3347

Contents?: true

Size: 1.91 KB

Versions: 16

Compression:

Stored size: 1.91 KB

Contents

import Sortable from "sortablejs"
import { growl } from "alchemy_admin/growler"
import { patch } from "alchemy_admin/utils/ajax"
import pleaseWaitOverlay from "alchemy_admin/please_wait_overlay"

function onSort(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
  }

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

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

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",
      onSort
    })
  })
}

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
alchemy_cms-7.3.6 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.2.9 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.3.5 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.2.8 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.3.4 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.3.3 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.3.2 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.2.7 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.3.1 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.3.0 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.2.6 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.2.5 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.2.4 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.2.3 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.2.2 app/javascript/alchemy_admin/page_sorter.js
alchemy_cms-7.2.1 app/javascript/alchemy_admin/page_sorter.js