text.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. (function ($) {
  2. /**
  3. * Auto-hide summary textarea if empty and show hide and unhide links.
  4. */
  5. Drupal.behaviors.textSummary = {
  6. attach: function (context, settings) {
  7. $('.text-summary', context).once('text-summary', function () {
  8. var $widget = $(this).closest('div.field-type-text-with-summary');
  9. var $summaries = $widget.find('div.text-summary-wrapper');
  10. $summaries.once('text-summary-wrapper').each(function(index) {
  11. var $summary = $(this);
  12. var $summaryLabel = $summary.find('label');
  13. var $full = $widget.find('.text-full').eq(index).closest('.form-item');
  14. var $fullLabel = $full.find('label');
  15. // Create a placeholder label when the field cardinality is
  16. // unlimited or greater than 1.
  17. if ($fullLabel.length == 0) {
  18. $fullLabel = $('<label></label>').prependTo($full);
  19. }
  20. // Setup the edit/hide summary link.
  21. var $link = $('<span class="field-edit-link">(<a class="link-edit-summary" href="#">' + Drupal.t('Hide summary') + '</a>)</span>').toggle(
  22. function () {
  23. $summary.hide();
  24. $(this).find('a').html(Drupal.t('Edit summary')).end().appendTo($fullLabel);
  25. return false;
  26. },
  27. function () {
  28. $summary.show();
  29. $(this).find('a').html(Drupal.t('Hide summary')).end().appendTo($summaryLabel);
  30. return false;
  31. }
  32. ).appendTo($summaryLabel);
  33. // If no summary is set, hide the summary field.
  34. if ($(this).find('.text-summary').val() == '') {
  35. $link.click();
  36. }
  37. return;
  38. });
  39. });
  40. }
  41. };
  42. })(jQuery);