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