Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring code in public/src/admin/manage/categories.js #135

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions public/src/admin/manage/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,12 @@ define('admin/manage/categories', [
function itemDragDidEnd(e) {
const isCategoryUpdate = parseInt(newCategoryId, 10) !== -1;

// Added Variables
const positionChanged = e.newIndex != null && parseInt(e.oldIndex, 10) !== parseInt(e.newIndex, 10);
const needUpdate = positionChanged || isCategoryUpdate;

// Update needed?
if ((e.newIndex != null && parseInt(e.oldIndex, 10) !== parseInt(e.newIndex, 10)) || isCategoryUpdate) {
if (needUpdate) {
const cid = e.item.dataset.cid;
const modified = {};
// on page 1 baseIndex is 0, on page n baseIndex is (n - 1) * ajaxify.data.categoriesPerPage
Expand All @@ -220,18 +224,13 @@ define('admin/manage/categories', [
// Show/hide expand buttons after drag completion
const oldParentCid = parseInt(e.from.getAttribute('data-cid'), 10);
const newParentCid = parseInt(e.to.getAttribute('data-cid'), 10);

if (oldParentCid !== newParentCid) {
const toggle = document.querySelector(`.categories li[data-cid="${newParentCid}"] .toggle`);
if (toggle) {
toggle.classList.toggle('invisible', false);
}
toggleVisibility(newParentCid, false);

const children = document.querySelectorAll(`.categories li[data-cid="${oldParentCid}"] ul[data-cid] li[data-cid]`);
if (!children.length) {
const toggle = document.querySelector(`.categories li[data-cid="${oldParentCid}"] .toggle`);
if (toggle) {
toggle.classList.toggle('invisible', true);
}
toggleVisibility(oldParentCid, true);
}

e.item.dataset.parentCid = newParentCid;
Expand All @@ -243,6 +242,14 @@ define('admin/manage/categories', [
}
}

// Added Helper Function toggle visibility of category
function toggleVisibility(parentCid, visibility) {
const toggle = document.querySelector(`.categories li[data-cid="${parentCid}"] .toggle`);
if (toggle) {
toggle.classList.toggle('invisible', visibility);
}
}

/**
* Render categories - recursively
*
Expand Down
Empty file added use strict
Empty file.
Loading