Chris@0: /**
Chris@0: * @file
Chris@0: * Text behaviors.
Chris@0: */
Chris@0:
Chris@0: (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@0: $(context).find('.js-text-summary').once('text-summary').each(function () {
Chris@0: const $widget = $(this).closest('.js-text-format-wrapper');
Chris@0:
Chris@0: const $summary = $widget.find('.js-text-summary-wrapper');
Chris@0: const $summaryLabel = $summary.find('label').eq(0);
Chris@0: const $full = $widget.children('.js-form-type-textarea');
Chris@0: let $fullLabel = $full.find('label').eq(0);
Chris@0:
Chris@0: // Create a placeholder label when the field cardinality is greater
Chris@0: // than 1.
Chris@0: if ($fullLabel.length === 0) {
Chris@0: $fullLabel = $('').prependTo($full);
Chris@0: }
Chris@0:
Chris@0: // Set up the edit/hide summary link.
Chris@0: const $link = $(` ()`);
Chris@0: const $button = $link.find('button');
Chris@0: let toggleClick = true;
Chris@0: $link.on('click', (e) => {
Chris@0: if (toggleClick) {
Chris@0: $summary.hide();
Chris@0: $button.html(Drupal.t('Edit summary'));
Chris@0: $link.appendTo($fullLabel);
Chris@0: }
Chris@0: else {
Chris@0: $summary.show();
Chris@0: $button.html(Drupal.t('Hide summary'));
Chris@0: $link.appendTo($summaryLabel);
Chris@0: }
Chris@0: e.preventDefault();
Chris@0: toggleClick = !toggleClick;
Chris@0: }).appendTo($summaryLabel);
Chris@0:
Chris@0: // If no summary is set, hide the summary field.
Chris@0: if ($widget.find('.js-text-summary').val() === '') {
Chris@0: $link.trigger('click');
Chris@0: }
Chris@0: });
Chris@0: },
Chris@0: };
Chris@0: }(jQuery, Drupal));