Problem/Motivation
The native JS is good, but, according to the drupal js cashing and another standatrs points I suggest to move it to the drupal standarts of Js. I really respect this approach, but it makes a mess in the future.
Proposed resolution
I did locally some updates
(function ($, Drupal, drupalSettings) {
'use strict';
Drupal.behaviors.menuTree = {
attach: function (context, settings) {
/**
* Add functionality for expanding and collapsing menu tree.
*/
$(once('menu-tree-toggle', '#edit-menu', context)).on('click', 'span.toggle', function(event) {
$($(event.currentTarget)).toggleClass("show");
});
/**
* Set default value of menu tree and expand active branches.
*/
$(once('menu-tree-state', '#edit-menu input[name="menu[menu_parent]"]', context)).each(function (index, element) {
let $item = $('#edit-menu input[name="menu_tree"][value="' + element.value + '"]', context).get(0);
$item.checked = true;
while ($item) {
if ($item.matches('li') && $item.firstElementChild.matches('.toggle')) {
$($item.firstElementChild).addClass("show");
}
$item = $item.parentElement;
}
});
/**
* React on radio button changes and set value of actual menu parent element.
*/
$(once('menu-tree-change', '#edit-menu input[type="radio"][name="menu_tree"]', context)).on('change', function(event) {
$('#edit-menu input[name="menu[menu_parent]"]', context).get(0).value = $(event.currentTarget).val();
});
}
};
})(jQuery, Drupal, drupalSettings);
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | menu_tree-js-update-3375050.patch | 3.11 KB | p.kasianov |
Comments
Comment #2
p.kasianov commentedAlso, take a look - I use 'once' as it was added in the D10. It means that need update menu_tree.libraries.yml file as well..
As it on my local now.
I did add version and dependicies.
Thanks.
Comment #3
p.kasianov commentedThe patch
Comment #4
p.kasianov commentedPlease, take a look for review.
Comment #5
heykarthikwithuComment #6
heykarthikwithuComment #7
peter törnstrand commentedThank, will have a look asap.
Comment #8
peter törnstrand commentedIf you can refactor this to not use jQuery I will commit this.
Comment #9
peter törnstrand commentedComment #10
peter törnstrand commentedComment #12
peter törnstrand commentedComment #13
peter törnstrand commented