annotate core/modules/text/text.es6.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * Text behaviors.
Chris@0 4 */
Chris@0 5
Chris@17 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@17 17 $(context)
Chris@17 18 .find('.js-text-summary')
Chris@17 19 .once('text-summary')
Chris@17 20 .each(function() {
Chris@17 21 const $widget = $(this).closest('.js-text-format-wrapper');
Chris@0 22
Chris@17 23 const $summary = $widget.find('.js-text-summary-wrapper');
Chris@17 24 const $summaryLabel = $summary.find('label').eq(0);
Chris@17 25 const $full = $widget.children('.js-form-type-textarea');
Chris@17 26 let $fullLabel = $full.find('label').eq(0);
Chris@0 27
Chris@17 28 // Create a placeholder label when the field cardinality is greater
Chris@17 29 // than 1.
Chris@17 30 if ($fullLabel.length === 0) {
Chris@17 31 $fullLabel = $('<label></label>').prependTo($full);
Chris@17 32 }
Chris@0 33
Chris@17 34 // Set up the edit/hide summary link.
Chris@17 35 const $link = $(
Chris@17 36 `<span class="field-edit-link"> (<button type="button" class="link link-edit-summary">${Drupal.t(
Chris@17 37 'Hide summary',
Chris@17 38 )}</button>)</span>`,
Chris@17 39 );
Chris@17 40 const $button = $link.find('button');
Chris@17 41 let toggleClick = true;
Chris@17 42 $link
Chris@17 43 .on('click', e => {
Chris@17 44 if (toggleClick) {
Chris@17 45 $summary.hide();
Chris@17 46 $button.html(Drupal.t('Edit summary'));
Chris@17 47 $link.appendTo($fullLabel);
Chris@17 48 } else {
Chris@17 49 $summary.show();
Chris@17 50 $button.html(Drupal.t('Hide summary'));
Chris@17 51 $link.appendTo($summaryLabel);
Chris@17 52 }
Chris@17 53 e.preventDefault();
Chris@17 54 toggleClick = !toggleClick;
Chris@17 55 })
Chris@17 56 .appendTo($summaryLabel);
Chris@17 57
Chris@17 58 // If no summary is set, hide the summary field.
Chris@17 59 if ($widget.find('.js-text-summary').val() === '') {
Chris@17 60 $link.trigger('click');
Chris@0 61 }
Chris@17 62 });
Chris@0 63 },
Chris@0 64 };
Chris@17 65 })(jQuery, Drupal);