2017-05-19 22:12:53 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Text behaviors.
|
|
|
|
*/
|
|
|
|
|
2020-10-21 10:41:33 +00:00
|
|
|
(function ($, Drupal) {
|
2017-05-19 22:12:53 +00:00
|
|
|
/**
|
|
|
|
* Auto-hide summary textarea if empty and show hide and unhide links.
|
|
|
|
*
|
|
|
|
* @type {Drupal~behavior}
|
|
|
|
*
|
|
|
|
* @prop {Drupal~behaviorAttach} attach
|
|
|
|
* Attaches auto-hide behavior on `text-summary` events.
|
|
|
|
*/
|
|
|
|
Drupal.behaviors.textSummary = {
|
2017-07-06 06:21:40 +00:00
|
|
|
attach(context, settings) {
|
2021-08-10 10:05:02 +00:00
|
|
|
once('text-summary', '.js-text-summary', context).forEach((summary) => {
|
|
|
|
const $widget = $(summary).closest('.js-text-format-wrapper');
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2021-08-10 10:05:02 +00:00
|
|
|
const $summary = $widget.find('.js-text-summary-wrapper');
|
|
|
|
const $summaryLabel = $summary.find('label').eq(0);
|
|
|
|
const $full = $widget.children('.js-form-type-textarea');
|
|
|
|
let $fullLabel = $full.find('label').eq(0);
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2021-08-10 10:05:02 +00:00
|
|
|
// Create a placeholder label when the field cardinality is greater
|
|
|
|
// than 1.
|
|
|
|
if ($fullLabel.length === 0) {
|
|
|
|
$fullLabel = $('<label></label>').prependTo($full);
|
|
|
|
}
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2021-10-01 12:21:36 +00:00
|
|
|
// To ensure the summary toggle is shown in case the label is hidden
|
|
|
|
// (in multivalue fields in particular), show the label but hide
|
|
|
|
// the original text of the label.
|
|
|
|
if ($fullLabel.hasClass('visually-hidden')) {
|
|
|
|
$fullLabel.html(
|
|
|
|
(index, oldHtml) =>
|
|
|
|
`<span class="visually-hidden">${oldHtml}</span>`,
|
|
|
|
);
|
|
|
|
$fullLabel.removeClass('visually-hidden');
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:05:02 +00:00
|
|
|
// Set up the edit/hide summary link.
|
|
|
|
const $link = $(
|
|
|
|
`<span class="field-edit-link"> (<button type="button" class="link link-edit-summary">${Drupal.t(
|
|
|
|
'Hide summary',
|
|
|
|
)}</button>)</span>`,
|
|
|
|
);
|
|
|
|
const $button = $link.find('button');
|
|
|
|
let toggleClick = true;
|
|
|
|
$link
|
|
|
|
.on('click', (e) => {
|
|
|
|
if (toggleClick) {
|
|
|
|
$summary.hide();
|
|
|
|
$button.html(Drupal.t('Edit summary'));
|
|
|
|
$link.appendTo($fullLabel);
|
|
|
|
} else {
|
|
|
|
$summary.show();
|
|
|
|
$button.html(Drupal.t('Hide summary'));
|
|
|
|
$link.appendTo($summaryLabel);
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
toggleClick = !toggleClick;
|
|
|
|
})
|
|
|
|
.appendTo($summaryLabel);
|
2018-08-09 15:49:18 +00:00
|
|
|
|
2021-08-10 10:05:02 +00:00
|
|
|
// If no summary is set, hide the summary field.
|
|
|
|
if ($widget.find('.js-text-summary').val() === '') {
|
|
|
|
$link.trigger('click');
|
|
|
|
}
|
|
|
|
});
|
2017-07-06 06:21:40 +00:00
|
|
|
},
|
2017-05-19 22:12:53 +00:00
|
|
|
};
|
2018-08-09 15:49:18 +00:00
|
|
|
})(jQuery, Drupal);
|