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