6 (function ($, Drupal) {
8 * Auto-hide summary textarea if empty and show hide and unhide links.
10 * @type {Drupal~behavior}
12 * @prop {Drupal~behaviorAttach} attach
13 * Attaches auto-hide behavior on `text-summary` events.
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');
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);
25 // Create a placeholder label when the field cardinality is greater
27 if ($fullLabel.length === 0) {
28 $fullLabel = $('<label></label>').prependTo($full);
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) => {
38 $button.html(Drupal.t('Edit summary'));
39 $link.appendTo($fullLabel);
43 $button.html(Drupal.t('Hide summary'));
44 $link.appendTo($summaryLabel);
47 toggleClick = !toggleClick;
48 }).appendTo($summaryLabel);
50 // If no summary is set, hide the summary field.
51 if ($widget.find('.js-text-summary').val() === '') {
52 $link.trigger('click');