Skip to content
Snippets Groups Projects
Commit 440a3c59 authored by Mingsong Hu's avatar Mingsong Hu
Browse files

Improve user experience on drag and drop

parent 59e19063
No related branches found
No related tags found
No related merge requests found
......@@ -37,11 +37,6 @@
treeContainer.jstree({
core: {
'check_callback' : function (operation, node, node_parent, node_position, more) {
// prevent any node with multiple parents from moving.
if (operation === 'move_node') {
return node.id.indexOf('_') === -1;
}
return true;
},
data: {
......@@ -57,6 +52,20 @@
themes: themes,
"multiple": false,
},
'dnd' : {
"is_draggable" : function(node) {
let can_drag = node[0].data.draggable;
if (can_drag) {
return true;
}
else {
let drupalMessages = new Drupal.Message();
drupalMessages.clear();
drupalMessages.add(Drupal.t("Can't drag this item. It may because it has multiple parents."), {type: 'warning'});
return false;
}
}
},
search: {
show_only_matches: true,
"search_callback": function(str, node) {
......
......@@ -175,28 +175,18 @@ class HmMenuController extends ControllerBase {
$parent = $request->get('parent');
$updated_links = $request->get('keys');
$all_siblings = [];
$links_uuid = [];
if (is_array($updated_links) && !empty($updated_links)) {
if (empty($parent)) {
// Root is the parent.
$parent = '';
// All children menu links (depth = 1).
$parent_links = $children = $this->loadMenuLinkObjs($mid, $parent, 1);
}
else {
$parent = $this->entityTypeManager->getStorage('menu_link_content')->load($parent);
if (!empty($parent)) {
$parent = $parent->getPluginId();
}
else {
$parent = '';
}
// All children menu links (depth = 1).
$parent_links = $this->loadMenuLinkObjs($mid, $parent, 1);
}
// In order to make room for menu links inserted,
// we need to move all children links forward,
// and work out the weight for links inserted.
if (empty($parent_links)) {
// The parent menu doesn't exist.
return new JsonResponse(['result' => 'fail']);
......@@ -213,30 +203,18 @@ class HmMenuController extends ControllerBase {
foreach ($children as $child) {
$link = $child->link;
$uuid = $link->getDerivativeId();
$link_id = $this->entityRepository->loadEntityByUuid('menu_link_content', $uuid)->id();
$links_uuid[$link_id] = $link->getPluginId();
$link_id = $link->getPluginId();
$all_siblings[$link_id] = $link->getWeight();
}
}
// In order to make room for menu links inserted,
// we need to move all children links forward,
// and work out the weight for links inserted.
$new_hierarchy = $this->hmPluginTypeManager->updateHierarchy($target_position, $all_siblings, $updated_links, $old_position);
foreach ($updated_links as $id) {
if (!isset($links_uuid[$id])) {
$entity = $this->entityTypeManager->getStorage('menu_link_content')->load($id);
if (!empty($entity)) {
$links_uuid[$id] = $entity->getPluginId();
}
else {
// The updated menu link doesn't exist.
return new JsonResponse(['result' => 'fail']);
}
}
}
// Update all links need to update.
foreach ($new_hierarchy as $link_id => $link_weight) {
$this->menuLinkManager->updateDefinition($links_uuid[$link_id], ['weight' => $link_weight, 'parent' => $parent]);
$this->menuLinkManager->updateDefinition($link_id, ['weight' => $link_weight, 'parent' => $parent]);
}
$result = [
......@@ -345,10 +323,7 @@ class HmMenuController extends ControllerBase {
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$link = $element->link;
if ($link) {
// The id consistes of plugin ID and link ID.
$uuid = $link->getDerivativeId();
$menu_entity = $this->entityRepository->loadEntityByUuid('menu_link_content', $uuid);
$id = $menu_entity->id();
$id = $link->getPluginId();
$this->overviewTree[$id]['id'] = $id;
$this->overviewTree[$id]['status'] = $link->isEnabled();
if (!$link->isEnabled()) {
......@@ -366,14 +341,7 @@ class HmMenuController extends ControllerBase {
$this->overviewTree[$id]['title'] = $link->getTitle();
}
$parent_uuid = explode(':', $link->getParent());
if (isset($parent_uuid[1])) {
$parent_id = $this->entityRepository->loadEntityByUuid($parent_uuid[0], $parent_uuid[1])->id();
}
else {
$parent_id = '';
}
$parent_id = $link->getParent();
$this->overviewTree[$id]['parent'] = $parent_id;
$this->overviewTree[$id]['weight'] = $link->getWeight();
// Build the edit url.
......
......@@ -112,10 +112,12 @@ class HmTaxonomyController extends ControllerBase {
$term_parent_index = $parents[$term->id()] = 0;
}
$term_id = $term->id() . '_' . $term_parent_index;
$draggable = false;
}
else {
$term_id = $term->id();
$term_parent_index = 0;
$draggable = true;
}
$term_array[] = $this->hmPluginTypeManager->buildHierarchyItem(
......@@ -124,7 +126,8 @@ class HmTaxonomyController extends ControllerBase {
$term->parents[$term_parent_index],
$url,
$term->isPublished(),
$term->getWeight()
$term->getWeight(),
$draggable
);
}
}
......
......@@ -118,6 +118,7 @@ class HmDisplayJstree extends HmDisplayPluginBase implements HmDisplayPluginInte
];
$jstree_node['data'] = [
'weight' => $tree_node['weight'],
'draggable' => $tree_node['draggable']
];
unset($jstree_node['edit_url']);
// Add this node into the data array.
......
......@@ -57,7 +57,7 @@ class PluginTypeManager {
* @return array
* The hierarchy item array.
*/
public function buildHierarchyItem($id, $label, $parent, $edit_url, $status = TRUE, $weight = 0) {
public function buildHierarchyItem($id, $label, $parent, $edit_url, $status = TRUE, $weight = 0, $draggable = TRUE) {
return
[
'id' => $id,
......@@ -65,7 +65,8 @@ class PluginTypeManager {
'parent' => $parent,
'edit_url' => $edit_url,
'status' => $status,
'weight' => $weight
'weight' => $weight,
'draggable' => $draggable
];
}
......@@ -136,7 +137,8 @@ class PluginTypeManager {
}
else {
$target_item = array_slice($all_siblings, $target_position, 1);
$weight = $target_item[0];
$target_weight = reset($target_item);
$weight = $target_weight;
$total_insert = count($updated_items);
// Figure out if the target element should move forward.
if ($num_new || ($old_position > $target_position)) {
......@@ -154,11 +156,11 @@ class PluginTypeManager {
if ($target_position - $num_new >= $total / 2) {
if ($move_forward) {
$moving_siblings = array_slice($all_siblings, $target_position, NULL, TRUE);
$weight = $target_item[0];
$weight = $target_weight;
}
else {
$moving_siblings = array_slice($all_siblings, $target_position + 1, NULL, TRUE);
$weight = $target_item[0] + 1;
$weight = $target_weight + 1;
}
$after = true;
$expected_weight = $weight + $total_insert;
......@@ -167,11 +169,11 @@ class PluginTypeManager {
else {
if ($move_forward) {
$moving_siblings = array_slice($all_siblings, 0, $target_position, TRUE);
$weight = $target_item[0] - 1;
$weight = $target_weight - 1;
}
else {
$moving_siblings = array_slice($all_siblings, 0, $target_position + 1, TRUE);
$weight = $target_item[0];
$weight = $target_weight;
}
$after = false;
$expected_weight = $weight - count($moving_siblings) - $total_insert + 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment