diff options
author | Alex Pott <alex.a.pott@googlemail.com> | 2024-09-13 18:59:47 +0100 |
---|---|---|
committer | Alex Pott <alex.a.pott@googlemail.com> | 2024-09-13 18:59:47 +0100 |
commit | e9a2e9abfd3455cc54beb0054b4d7e21929e60c7 (patch) | |
tree | 7172167c7235928e6924fd1a4bea22d3dd884f18 /core/modules/text/js | |
parent | f296e346716d4a133362c5e4b581c91565967028 (diff) | |
download | drupal-e9a2e9abfd3455cc54beb0054b4d7e21929e60c7.tar.gz drupal-e9a2e9abfd3455cc54beb0054b4d7e21929e60c7.zip |
Issue #2484623 by quietone, anavarre, nod_, Manuel Garcia, rteijeiro, kostyashupenko, ankithashetty, smustgrave, jeroent, xjm, droplet, cilefen, travis-bradbury, catch: Move all JS in modules to a js/ folder
Diffstat (limited to 'core/modules/text/js')
-rw-r--r-- | core/modules/text/js/text.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/core/modules/text/js/text.js b/core/modules/text/js/text.js new file mode 100644 index 00000000000..c584f2dbd13 --- /dev/null +++ b/core/modules/text/js/text.js @@ -0,0 +1,73 @@ +/** + * @file + * Text behaviors. + */ + +(function ($, Drupal) { + /** + * Auto-hide summary textarea if empty and show hide and unhide links. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches auto-hide behavior on `text-summary` events. + */ + Drupal.behaviors.textSummary = { + attach(context, settings) { + once('text-summary', '.js-text-summary', context).forEach((summary) => { + const $widget = $(summary).closest('.js-text-format-wrapper'); + + const $summary = $widget.find('.js-text-summary-wrapper'); + const $summaryLabel = $summary.find('label').eq(0); + const $full = $widget.children('.js-form-type-textarea'); + let $fullLabel = $full.find('label').eq(0); + + // Create a placeholder label when the field cardinality is greater + // than 1. + if ($fullLabel.length === 0) { + $fullLabel = $('<label></label>').prependTo($full); + } + + // To ensure the summary toggle is shown in case the label is hidden + // (in multivalue fields in particular), show the label but hide + // the original text of the label. + if ($fullLabel.hasClass('visually-hidden')) { + $fullLabel.html( + (index, oldHtml) => + `<span class="visually-hidden">${oldHtml}</span>`, + ); + $fullLabel.removeClass('visually-hidden'); + } + + // Set up the edit/hide summary link. + const $link = $( + `<span class="field-edit-link"> (<button type="button" class="link link-edit-summary">${Drupal.t( + 'Hide summary', + )}</button>)</span>`, + ); + const $button = $link.find('button'); + let toggleClick = true; + $link + .on('click', (e) => { + if (toggleClick) { + $summary.hide(); + $button.html(Drupal.t('Edit summary')); + $link.appendTo($fullLabel); + } else { + $summary.show(); + $button.html(Drupal.t('Hide summary')); + $link.appendTo($summaryLabel); + } + e.preventDefault(); + toggleClick = !toggleClick; + }) + .appendTo($summaryLabel); + + // If no summary is set, hide the summary field. + if (summary.value === '') { + $link.trigger('click'); + } + }); + }, + }; +})(jQuery, Drupal); |