text.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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').first();
  13. var $full = $widget.find('.text-full').eq(index).closest('.form-item');
  14. var $fullLabel = $full.find('label').first();
  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>');
  22. var $a = $link.find('a');
  23. var toggleClick = true;
  24. $link.bind('click', function (e) {
  25. if (toggleClick) {
  26. $summary.hide();
  27. $a.html(Drupal.t('Edit summary'));
  28. $link.appendTo($fullLabel);
  29. }
  30. else {
  31. $summary.show();
  32. $a.html(Drupal.t('Hide summary'));
  33. $link.appendTo($summaryLabel);
  34. }
  35. toggleClick = !toggleClick;
  36. return false;
  37. }).appendTo($summaryLabel);
  38. // If no summary is set, hide the summary field.
  39. if ($(this).find('.text-summary').val() == '') {
  40. $link.click();
  41. }
  42. });
  43. });
  44. }
  45. };
  46. })(jQuery);