annotate core/modules/text/text.es6.js @ 5:12f9dff5fda9 tip

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