ad89bd8ad1619b656d7992fd5c84382b9a532096
[yaffs-website] / web / core / modules / text / text.js
1 /**
2  * @file
3  * Text behaviors.
4  */
5
6 (function ($, Drupal) {
7
8   'use strict';
9
10   /**
11    * Auto-hide summary textarea if empty and show hide and unhide links.
12    *
13    * @type {Drupal~behavior}
14    *
15    * @prop {Drupal~behaviorAttach} attach
16    *   Attaches auto-hide behavior on `text-summary` events.
17    */
18   Drupal.behaviors.textSummary = {
19     attach: function (context, settings) {
20       $(context).find('.js-text-summary').once('text-summary').each(function () {
21         var $widget = $(this).closest('.js-text-format-wrapper');
22
23         var $summary = $widget.find('.js-text-summary-wrapper');
24         var $summaryLabel = $summary.find('label').eq(0);
25         var $full = $widget.find('.js-text-full').closest('.js-form-item');
26         var $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         var $link = $('<span class="field-edit-link"> (<button type="button" class="link link-edit-summary">' + Drupal.t('Hide summary') + '</button>)</span>');
36         var $button = $link.find('button');
37         var toggleClick = true;
38         $link.on('click', function (e) {
39           if (toggleClick) {
40             $summary.hide();
41             $button.html(Drupal.t('Edit summary'));
42             $link.appendTo($fullLabel);
43           }
44           else {
45             $summary.show();
46             $button.html(Drupal.t('Hide summary'));
47             $link.appendTo($summaryLabel);
48           }
49           e.preventDefault();
50           toggleClick = !toggleClick;
51         }).appendTo($summaryLabel);
52
53         // If no summary is set, hide the summary field.
54         if ($widget.find('.js-text-summary').val() === '') {
55           $link.trigger('click');
56         }
57       });
58     }
59   };
60
61 })(jQuery, Drupal);