Add comments to JavaScript to make it more readable.

This commit is contained in:
Michael Stenta 2021-12-01 08:08:47 -05:00
parent 662f7801e0
commit 91c61c6f40
1 changed files with 21 additions and 0 deletions

View File

@ -12,12 +12,15 @@
Drupal.behaviors.locationsDragAndDrop = {
attach: function (context, settings) {
// Function for toggling drag and drop.
var toggleDragAndDrop = function() {
$('input#edit-save').attr('disabled', !dragAndDropEnabled)
$('input#edit-reset').attr('disabled', !dragAndDropEnabled)
domTree.config.dragAndDrop.enabled = dragAndDropEnabled
}
// Enable Inspire Tree.
var tree = new InspireTree({
data: settings.asset_tree,
})
@ -27,8 +30,11 @@
})
tree.nodes().expand()
// Start with drag and drop disabled.
var dragAndDropEnabled = false
toggleDragAndDrop()
// Toggle drag and drop when the button is clicked.
$('input#edit-toggle').on('click', function(event) {
event.preventDefault()
dragAndDropEnabled = !dragAndDropEnabled
@ -37,9 +43,17 @@
domTree.attach($('.locations-tree'));
})
// Maintain a list of hierarchy changes as items are moved.
var changes = {}
tree.on('node.drop', function(event, source, target, index) {
// Determine the new parent. If target is null, then it means that the
// child was moved to the root context, which either means the child
// will no longer have a parent, or if we are in the context of a
// specific asset's children, that asset will become the new parent.
var new_parent = (target === null) ? settings.asset_parent : target.asset_id
// Create a change record, if one doesn't already exist.
if (!changes.hasOwnProperty(source.id)) {
if (source.original_parent !== new_parent) {
changes[source.id] = {
@ -49,6 +63,10 @@
}
}
}
// Otherwise, if a change record already exists, we will either update
// it, or delete it (if the child is changing back to its original
// parent, in which case a change is no longer necessary).
else {
if (changes[source.id].original_parent !== new_parent) {
changes[source.id].new_parent = new_parent
@ -57,9 +75,12 @@
delete changes[source.id]
}
}
// Save the change records as a JSON string in the hidden input field.
$('input[name=changes]').val(JSON.stringify(changes))
})
// Link to locations when drag and drop is disabled.
tree.on('node.click', function(event, node) {
event.preventDefault()
if (node.url && !dragAndDropEnabled) {