-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit allows sorting of menus. We instantiate for each element in the tree one instance of Sortable.js, but - through the `group` option - allow dragging and dropping between each element. For this to work, all `ul` elements need to be present (otherwise we cannot drop a node below a list that does not have nodes yet). A little tricky was the correct behaviour of the `folded` button: It should only be displayed if there are any pages to fold. This is now accomplished through rendering the `+` and `-` buttons in Javascript, depending on how many elements are in each list after sorting.
- Loading branch information
Showing
4 changed files
with
70 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Alchemy.DisplayNodeFolders = function() { | ||
var generate_link = function(node_id, folded) { | ||
var icon = folded === "true" ? 'plus' : 'minus'; | ||
return '<a class="node_folder" data-node-id="' + node_id + '"><i class="far fa-' + icon + '-square fa-fw"></i></a>' | ||
} | ||
document.querySelectorAll('li.menu-item').forEach(function (el) { | ||
var leftIconArea = el.querySelector('.nodes_tree-left_images') | ||
if (el.dataset.childrenCount > 0) { | ||
leftIconArea.innerHTML = generate_link(el.dataset.nodeId, el.dataset.folded) | ||
} else { | ||
leftIconArea.innerHTML = ' ' | ||
} | ||
}); | ||
}; | ||
|
||
Alchemy.NodeSorter = function() { | ||
Alchemy.DisplayNodeFolders(); | ||
document.querySelectorAll('#archive_all ul.children').forEach(function (el) { | ||
new Sortable( | ||
el, | ||
{ | ||
group: 'nodes', | ||
animation: 150, | ||
fallbackOnBody: true, | ||
swapThreshold: 0.65, | ||
onEnd: function (/**Event*/evt) { | ||
var itemEl = evt.item; // dragged HTMLElement | ||
var url = '/api/nodes/' + evt.item.dataset.id + '/move.json' | ||
var xhr = new XMLHttpRequest() | ||
var token = document.querySelector('meta[name="csrf-token"]').attributes.content.textContent | ||
var data = { | ||
target_parent_id: evt.to.dataset.nodeId, | ||
new_position: evt.newIndex | ||
}; | ||
var json = JSON.stringify(data) | ||
|
||
xhr.open("PATCH", url); | ||
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8'); | ||
xhr.setRequestHeader('X-CSRF-Token', token) | ||
|
||
xhr.onload = function () { | ||
response_json = JSON.parse(xhr.responseText) | ||
if (xhr.readyState == 4 && xhr.status == "200") { | ||
// Update DOM element for list we remove elements from | ||
var list = evt.from.parentElement | ||
evt.from.parentElement.dataset.childrenCount = evt.from.children.length | ||
evt.to.parentElement.dataset.childrenCount = evt.to.children.length | ||
evt.item.dataset.left = response_json.lft | ||
evt.item.dataset.right = response_json.rgt | ||
Alchemy.DisplayNodeFolders() | ||
} else { | ||
Alchemy.growl(response_json.error, 'error'); | ||
} | ||
} | ||
xhr.send(json) | ||
}, | ||
} | ||
) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters