annotate core/modules/text/text.es6.js @ 0:c75dbcec494b

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