766591993b6d8ecb3e6bfa84c632f8f16b3ad447
[yaffs-website] / js / text / text.js
1 /**
2  * @file
3  * Text behaviors.
4  */
5
6 (function ($) {
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 btn btn-default btn-xs pull-right" data-toggle="button" aria-pressed="false" autocomplete="off">' + 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             $fullLabel.before($link);
43           }
44           else {
45             $summary.show();
46             $button.html(Drupal.t('Hide summary'));
47             $summaryLabel.before($link);
48           }
49           e.preventDefault();
50           toggleClick = !toggleClick;
51         });
52         $summaryLabel.before($link);
53
54         // If no summary is set, hide the summary field.
55         if ($widget.find('.js-text-summary').val() === '') {
56           $link.trigger('click');
57         }
58         else {
59           $link.addClass('active');
60         }
61       });
62     }
63   };
64
65 })(jQuery);