text.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @file
  3. * Text behaviors.
  4. */
  5. (function ($, Drupal) {
  6. 'use strict';
  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: function (context, settings) {
  17. $(context).find('.js-text-summary').once('text-summary').each(function () {
  18. var $widget = $(this).closest('.js-text-format-wrapper');
  19. var $summary = $widget.find('.js-text-summary-wrapper');
  20. var $summaryLabel = $summary.find('label').eq(0);
  21. var $full = $widget.find('.js-text-full').closest('.js-form-item');
  22. var $fullLabel = $full.find('label').eq(0);
  23. // Create a placeholder label when the field cardinality is greater
  24. // than 1.
  25. if ($fullLabel.length === 0) {
  26. $fullLabel = $('<label></label>').prependTo($full);
  27. }
  28. // Set up the edit/hide summary link.
  29. var $link = $('<span class="field-edit-link"> (<button type="button" class="link link-edit-summary">' + Drupal.t('Hide summary') + '</button>)</span>');
  30. var $button = $link.find('button');
  31. var toggleClick = true;
  32. $link.on('click', function (e) {
  33. if (toggleClick) {
  34. $summary.hide();
  35. $button.html(Drupal.t('Edit summary'));
  36. $link.appendTo($fullLabel);
  37. }
  38. else {
  39. $summary.show();
  40. $button.html(Drupal.t('Hide summary'));
  41. $link.appendTo($summaryLabel);
  42. }
  43. e.preventDefault();
  44. toggleClick = !toggleClick;
  45. }).appendTo($summaryLabel);
  46. // If no summary is set, hide the summary field.
  47. if ($widget.find('.js-text-summary').val() === '') {
  48. $link.trigger('click');
  49. }
  50. });
  51. }
  52. };
  53. })(jQuery, Drupal);