Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / text / text.es6.js
1 /**
2  * @file
3  * Text behaviors.
4  */
5
6 (function ($, Drupal) {
7   /**
8    * Auto-hide summary textarea if empty and show hide and unhide links.
9    *
10    * @type {Drupal~behavior}
11    *
12    * @prop {Drupal~behaviorAttach} attach
13    *   Attaches auto-hide behavior on `text-summary` events.
14    */
15   Drupal.behaviors.textSummary = {
16     attach(context, settings) {
17       $(context).find('.js-text-summary').once('text-summary').each(function () {
18         const $widget = $(this).closest('.js-text-format-wrapper');
19
20         const $summary = $widget.find('.js-text-summary-wrapper');
21         const $summaryLabel = $summary.find('label').eq(0);
22         const $full = $widget.find('.js-text-full').closest('.js-form-item');
23         let $fullLabel = $full.find('label').eq(0);
24
25         // Create a placeholder label when the field cardinality is greater
26         // than 1.
27         if ($fullLabel.length === 0) {
28           $fullLabel = $('<label></label>').prependTo($full);
29         }
30
31         // Set up the edit/hide summary link.
32         const $link = $(`<span class="field-edit-link"> (<button type="button" class="link link-edit-summary">${Drupal.t('Hide summary')}</button>)</span>`);
33         const $button = $link.find('button');
34         let toggleClick = true;
35         $link.on('click', (e) => {
36           if (toggleClick) {
37             $summary.hide();
38             $button.html(Drupal.t('Edit summary'));
39             $link.appendTo($fullLabel);
40           }
41           else {
42             $summary.show();
43             $button.html(Drupal.t('Hide summary'));
44             $link.appendTo($summaryLabel);
45           }
46           e.preventDefault();
47           toggleClick = !toggleClick;
48         }).appendTo($summaryLabel);
49
50         // If no summary is set, hide the summary field.
51         if ($widget.find('.js-text-summary').val() === '') {
52           $link.trigger('click');
53         }
54       });
55     },
56   };
57 }(jQuery, Drupal));