text.es6.js 1.9 KB

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