Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / menu_ui / menu_ui.es6.js
1 /**
2  * @file
3  * Menu UI behaviors.
4  */
5
6 (function ($, Drupal) {
7   /**
8    * Set a summary on the menu link form.
9    *
10    * @type {Drupal~behavior}
11    *
12    * @prop {Drupal~behaviorAttach} attach
13    *   Find the form and call `drupalSetSummary` on it.
14    */
15   Drupal.behaviors.menuUiDetailsSummaries = {
16     attach(context) {
17       $(context).find('.menu-link-form').drupalSetSummary((context) => {
18         const $context = $(context);
19         if ($context.find('.js-form-item-menu-enabled input').is(':checked')) {
20           return Drupal.checkPlain($context.find('.js-form-item-menu-title input').val());
21         }
22
23         return Drupal.t('Not in menu');
24       });
25     },
26   };
27
28   /**
29    * Automatically fill in a menu link title, if possible.
30    *
31    * @type {Drupal~behavior}
32    *
33    * @prop {Drupal~behaviorAttach} attach
34    *   Attaches change and keyup behavior for automatically filling out menu
35    *   link titles.
36    */
37   Drupal.behaviors.menuUiLinkAutomaticTitle = {
38     attach(context) {
39       const $context = $(context);
40       $context.find('.menu-link-form').each(function () {
41         const $this = $(this);
42         // Try to find menu settings widget elements as well as a 'title' field
43         // in the form, but play nicely with user permissions and form
44         // alterations.
45         const $checkbox = $this.find('.js-form-item-menu-enabled input');
46         const $link_title = $context.find('.js-form-item-menu-title input');
47         const $title = $this.closest('form').find('.js-form-item-title-0-value input');
48         // Bail out if we do not have all required fields.
49         if (!($checkbox.length && $link_title.length && $title.length)) {
50           return;
51         }
52         // If there is a link title already, mark it as overridden. The user
53         // expects that toggling the checkbox twice will take over the node's
54         // title.
55         if ($checkbox.is(':checked') && $link_title.val().length) {
56           $link_title.data('menuLinkAutomaticTitleOverridden', true);
57         }
58         // Whenever the value is changed manually, disable this behavior.
59         $link_title.on('keyup', () => {
60           $link_title.data('menuLinkAutomaticTitleOverridden', true);
61         });
62         // Global trigger on checkbox (do not fill-in a value when disabled).
63         $checkbox.on('change', () => {
64           if ($checkbox.is(':checked')) {
65             if (!$link_title.data('menuLinkAutomaticTitleOverridden')) {
66               $link_title.val($title.val());
67             }
68           }
69           else {
70             $link_title.val('');
71             $link_title.removeData('menuLinkAutomaticTitleOverridden');
72           }
73           $checkbox.closest('.vertical-tabs-pane').trigger('summaryUpdated');
74           $checkbox.trigger('formUpdated');
75         });
76         // Take over any title change.
77         $title.on('keyup', () => {
78           if (!$link_title.data('menuLinkAutomaticTitleOverridden') && $checkbox.is(':checked')) {
79             $link_title.val($title.val());
80             $link_title.val($title.val()).trigger('formUpdated');
81           }
82         });
83       });
84     },
85   };
86 }(jQuery, Drupal));