text.es6.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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)
  17. .find('.js-text-summary')
  18. .once('text-summary')
  19. .each(function() {
  20. const $widget = $(this).closest('.js-text-format-wrapper');
  21. const $summary = $widget.find('.js-text-summary-wrapper');
  22. const $summaryLabel = $summary.find('label').eq(0);
  23. const $full = $widget.children('.js-form-type-textarea');
  24. let $fullLabel = $full.find('label').eq(0);
  25. // Create a placeholder label when the field cardinality is greater
  26. // than 1.
  27. if ($fullLabel.length === 0) {
  28. $fullLabel = $('<label></label>').prependTo($full);
  29. }
  30. // Set up the edit/hide summary link.
  31. const $link = $(
  32. `<span class="field-edit-link"> (<button type="button" class="link link-edit-summary">${Drupal.t(
  33. 'Hide summary',
  34. )}</button>)</span>`,
  35. );
  36. const $button = $link.find('button');
  37. let toggleClick = true;
  38. $link
  39. .on('click', e => {
  40. if (toggleClick) {
  41. $summary.hide();
  42. $button.html(Drupal.t('Edit summary'));
  43. $link.appendTo($fullLabel);
  44. } else {
  45. $summary.show();
  46. $button.html(Drupal.t('Hide summary'));
  47. $link.appendTo($summaryLabel);
  48. }
  49. e.preventDefault();
  50. toggleClick = !toggleClick;
  51. })
  52. .appendTo($summaryLabel);
  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. })(jQuery, Drupal);