annotate core/modules/menu_ui/menu_ui.admin.es6.js @ 2:5311817fb629

Theme updates
author Chris Cannam
date Tue, 10 Jul 2018 13:19:18 +0000
parents c75dbcec494b
children a9cd425dd02b
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * Menu UI admin behaviors.
Chris@0 4 */
Chris@0 5
Chris@0 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@0 28 Drupal.menuUiUpdateParentList = function () {
Chris@0 29 const $menu = $('#edit-menu');
Chris@0 30 const values = [];
Chris@0 31
Chris@0 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@0 38 url: `${location.protocol}//${location.host}${Drupal.url('admin/structure/menu/parents')}`,
Chris@0 39 type: 'POST',
Chris@0 40 data: { 'menus[]': values },
Chris@0 41 dataType: 'json',
Chris@0 42 success(options) {
Chris@0 43 const $select = $('#edit-menu-parent');
Chris@0 44 // Save key of last selected element.
Chris@0 45 const selected = $select.val();
Chris@0 46 // Remove all existing options from dropdown.
Chris@0 47 $select.children().remove();
Chris@0 48 // Add new options to dropdown. Keep a count of options for testing later.
Chris@0 49 let totalOptions = 0;
Chris@0 50 Object.keys(options || {}).forEach((machineName) => {
Chris@0 51 $select.append(
Chris@0 52 $(`<option ${machineName === selected ? ' selected="selected"' : ''}></option>`).val(machineName).text(options[machineName]),
Chris@0 53 );
Chris@0 54 totalOptions++;
Chris@0 55 });
Chris@0 56
Chris@0 57 // Hide the parent options if there are no options for it.
Chris@0 58 $select.closest('div').toggle(totalOptions > 0).attr('hidden', totalOptions === 0);
Chris@0 59 },
Chris@0 60 });
Chris@0 61 };
Chris@0 62 }(jQuery, Drupal));