1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
/**
* @file
* Menu UI admin behaviors.
*/
(function ($, Drupal) {
/**
*
* @type {Drupal~behavior}
*/
Drupal.behaviors.menuUiChangeParentItems = {
attach(context, settings) {
const menu = once('menu-parent', '#edit-menu');
if (menu.length) {
const $menu = $(menu);
// Update the list of available parent menu items to match the initial
// available menus.
Drupal.menuUiUpdateParentList();
// Update list of available parent menu items.
$menu.on('change', 'input', Drupal.menuUiUpdateParentList);
}
},
};
/**
* Function to set the options of the menu parent item dropdown.
*/
Drupal.menuUiUpdateParentList = function () {
const $menu = $('#edit-menu');
const values = [];
$menu.find('input:checked').each(function () {
// Get the names of all checked menus.
values.push(Drupal.checkPlain(this.value));
});
$.ajax({
url: `${window.location.protocol}//${window.location.host}${Drupal.url(
'admin/structure/menu/parents',
)}`,
type: 'POST',
data: { 'menus[]': values },
dataType: 'json',
success(options) {
const $select = $('#edit-menu-parent');
// Save key of last selected element.
const selected = $select[0].value;
// Remove all existing options from dropdown.
$select.children().remove();
// Add new options to dropdown. Keep a count of options for testing later.
let totalOptions = 0;
Object.keys(options || {}).forEach((machineName) => {
const selectContents = document.createElement('option');
selectContents.selected = machineName === selected;
selectContents.value = machineName;
selectContents.textContent = options[machineName];
$select.append(selectContents);
totalOptions++;
});
// Hide the parent options if there are no options for it.
$select
.closest('div')
.toggle(totalOptions > 0)
.attr('hidden', totalOptions === 0);
},
});
};
})(jQuery, Drupal);
|