2017-05-19 22:12:53 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Menu UI admin behaviors.
|
|
|
|
*/
|
|
|
|
|
2020-10-21 10:41:33 +00:00
|
|
|
(function ($, Drupal) {
|
2017-05-19 22:12:53 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @type {Drupal~behavior}
|
|
|
|
*/
|
|
|
|
Drupal.behaviors.menuUiChangeParentItems = {
|
2017-07-06 06:21:40 +00:00
|
|
|
attach(context, settings) {
|
|
|
|
const $menu = $('#edit-menu').once('menu-parent');
|
2017-05-19 22:12:53 +00:00
|
|
|
if ($menu.length) {
|
|
|
|
// 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);
|
|
|
|
}
|
2017-07-06 06:21:40 +00:00
|
|
|
},
|
2017-05-19 22:12:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to set the options of the menu parent item dropdown.
|
|
|
|
*/
|
2020-10-21 10:41:33 +00:00
|
|
|
Drupal.menuUiUpdateParentList = function () {
|
2017-07-06 06:21:40 +00:00
|
|
|
const $menu = $('#edit-menu');
|
|
|
|
const values = [];
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2020-10-21 10:41:33 +00:00
|
|
|
$menu.find('input:checked').each(function () {
|
2017-05-19 22:12:53 +00:00
|
|
|
// Get the names of all checked menus.
|
|
|
|
values.push(Drupal.checkPlain($.trim($(this).val())));
|
|
|
|
});
|
|
|
|
|
|
|
|
$.ajax({
|
2018-08-09 15:49:18 +00:00
|
|
|
url: `${window.location.protocol}//${window.location.host}${Drupal.url(
|
|
|
|
'admin/structure/menu/parents',
|
|
|
|
)}`,
|
2017-05-19 22:12:53 +00:00
|
|
|
type: 'POST',
|
2017-07-06 06:21:40 +00:00
|
|
|
data: { 'menus[]': values },
|
2017-05-19 22:12:53 +00:00
|
|
|
dataType: 'json',
|
2017-07-06 06:21:40 +00:00
|
|
|
success(options) {
|
|
|
|
const $select = $('#edit-menu-parent');
|
2017-05-19 22:12:53 +00:00
|
|
|
// Save key of last selected element.
|
2017-07-06 06:21:40 +00:00
|
|
|
const selected = $select.val();
|
2017-05-19 22:12:53 +00:00
|
|
|
// Remove all existing options from dropdown.
|
|
|
|
$select.children().remove();
|
|
|
|
// Add new options to dropdown. Keep a count of options for testing later.
|
2017-07-06 06:21:40 +00:00
|
|
|
let totalOptions = 0;
|
2020-10-21 10:41:33 +00:00
|
|
|
Object.keys(options || {}).forEach((machineName) => {
|
2018-01-03 23:25:46 +00:00
|
|
|
$select.append(
|
2018-08-09 15:49:18 +00:00
|
|
|
$(
|
|
|
|
`<option ${
|
|
|
|
machineName === selected ? ' selected="selected"' : ''
|
|
|
|
}></option>`,
|
|
|
|
)
|
|
|
|
.val(machineName)
|
|
|
|
.text(options[machineName]),
|
2018-01-03 23:25:46 +00:00
|
|
|
);
|
|
|
|
totalOptions++;
|
|
|
|
});
|
2017-05-19 22:12:53 +00:00
|
|
|
|
|
|
|
// Hide the parent options if there are no options for it.
|
2018-08-09 15:49:18 +00:00
|
|
|
$select
|
|
|
|
.closest('div')
|
|
|
|
.toggle(totalOptions > 0)
|
|
|
|
.attr('hidden', totalOptions === 0);
|
2017-07-06 06:21:40 +00:00
|
|
|
},
|
2017-05-19 22:12:53 +00:00
|
|
|
});
|
|
|
|
};
|
2018-08-09 15:49:18 +00:00
|
|
|
})(jQuery, Drupal);
|