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