Chris@0: /** Chris@0: * @file Chris@0: * Text behaviors. Chris@0: */ Chris@0: Chris@17: (function($, Drupal) { Chris@0: /** Chris@0: * Auto-hide summary textarea if empty and show hide and unhide links. Chris@0: * Chris@0: * @type {Drupal~behavior} Chris@0: * Chris@0: * @prop {Drupal~behaviorAttach} attach Chris@0: * Attaches auto-hide behavior on `text-summary` events. Chris@0: */ Chris@0: Drupal.behaviors.textSummary = { Chris@0: attach(context, settings) { Chris@17: $(context) Chris@17: .find('.js-text-summary') Chris@17: .once('text-summary') Chris@17: .each(function() { Chris@17: const $widget = $(this).closest('.js-text-format-wrapper'); Chris@0: Chris@17: const $summary = $widget.find('.js-text-summary-wrapper'); Chris@17: const $summaryLabel = $summary.find('label').eq(0); Chris@17: const $full = $widget.children('.js-form-type-textarea'); Chris@17: let $fullLabel = $full.find('label').eq(0); Chris@0: Chris@17: // Create a placeholder label when the field cardinality is greater Chris@17: // than 1. Chris@17: if ($fullLabel.length === 0) { Chris@17: $fullLabel = $('').prependTo($full); Chris@17: } Chris@0: Chris@17: // Set up the edit/hide summary link. Chris@17: const $link = $( Chris@17: ` ()`, Chris@17: ); Chris@17: const $button = $link.find('button'); Chris@17: let toggleClick = true; Chris@17: $link Chris@17: .on('click', e => { Chris@17: if (toggleClick) { Chris@17: $summary.hide(); Chris@17: $button.html(Drupal.t('Edit summary')); Chris@17: $link.appendTo($fullLabel); Chris@17: } else { Chris@17: $summary.show(); Chris@17: $button.html(Drupal.t('Hide summary')); Chris@17: $link.appendTo($summaryLabel); Chris@17: } Chris@17: e.preventDefault(); Chris@17: toggleClick = !toggleClick; Chris@17: }) Chris@17: .appendTo($summaryLabel); Chris@17: Chris@17: // If no summary is set, hide the summary field. Chris@17: if ($widget.find('.js-text-summary').val() === '') { Chris@17: $link.trigger('click'); Chris@0: } Chris@17: }); Chris@0: }, Chris@0: }; Chris@17: })(jQuery, Drupal);