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