3 * Menu UI admin behaviors.
6 (function ($, Drupal) {
12 * @type {Drupal~behavior}
14 Drupal.behaviors.menuUiChangeParentItems = {
15 attach: function (context, settings) {
16 var $menu = $('#edit-menu').once('menu-parent');
18 // Update the list of available parent menu items to match the initial
20 Drupal.menuUiUpdateParentList();
22 // Update list of available parent menu items.
23 $menu.on('change', 'input', Drupal.menuUiUpdateParentList);
29 * Function to set the options of the menu parent item dropdown.
31 Drupal.menuUiUpdateParentList = function () {
32 var $menu = $('#edit-menu');
35 $menu.find('input:checked').each(function () {
36 // Get the names of all checked menus.
37 values.push(Drupal.checkPlain($.trim($(this).val())));
41 url: location.protocol + '//' + location.host + Drupal.url('admin/structure/menu/parents'),
43 data: {'menus[]': values},
45 success: function (options) {
46 var $select = $('#edit-menu-parent');
47 // Save key of last selected element.
48 var selected = $select.val();
49 // Remove all existing options from dropdown.
50 $select.children().remove();
51 // Add new options to dropdown. Keep a count of options for testing later.
53 for (var machineName in options) {
54 if (options.hasOwnProperty(machineName)) {
56 $('<option ' + (machineName === selected ? ' selected="selected"' : '') + '></option>').val(machineName).text(options[machineName])
62 // Hide the parent options if there are no options for it.
63 $select.closest('div').toggle(totalOptions > 0).attr('hidden', totalOptions === 0);