diff --git a/core/core.libraries.yml b/core/core.libraries.yml index 13c19005ce..0b3b12855f 100644 --- a/core/core.libraries.yml +++ b/core/core.libraries.yml @@ -122,7 +122,7 @@ drupal.checkbox: drupal.collapse: version: VERSION js: - misc/details.js: {} + misc/details-content-synopsis.js: {} misc/details-aria.js: {} misc/collapse.js: {} dependencies: diff --git a/core/misc/details-content-synopsis.es6.js b/core/misc/details-content-synopsis.es6.js new file mode 100644 index 0000000000..b724a279e0 --- /dev/null +++ b/core/misc/details-content-synopsis.es6.js @@ -0,0 +1,104 @@ +/** + * @file + * Adds a synopsis of a details element's contents to its summary. + */ +(($, Drupal) => { + /** + * The DetailsContentSynopsis object represents a single details element. + * + * @constructor Drupal.DetailsContentSynopsis + * + * @param {HTMLElement} node + * A details element, the summary of which may have supplemental text. + * The supplemental text is a synopsis of the details element's contents. + */ + function DetailsContentSynopsis(node) { + this.$node = $(node); + this.setupSummary(); + } + + $.extend( + DetailsContentSynopsis, + /** @lends Drupal.DetailsContentSynopsis */ { + /** + * Holds references to instantiated DetailsContentSynopsis objects. + * + * @type {Array.} + */ + instances: [], + }, + ); + + $.extend( + DetailsContentSynopsis.prototype, + /** @lends Drupal.DetailsContentSynopsis# */ { + /** + * Initialize and setup summary events and markup. + * + * @fires event:summaryUpdated + * + * @listens event:summaryUpdated + */ + setupSummary() { + this.$synopsizedDetailsContentWrapper = $( + Drupal.theme('synopsizedDetailsContentWrapper'), + ); + this.$node + .on('summaryUpdated', $.proxy(this.onSummaryUpdated, this)) + .trigger('summaryUpdated') + .find('> summary') + .append(this.$synopsizedDetailsContentWrapper); + }, + + /** + * Update summary. + */ + onSummaryUpdated() { + const text = $.trim(this.$node.drupalGetSummary()); + this.$synopsizedDetailsContentWrapper.html( + Drupal.theme('synopsizedDetailsContent', text), + ); + }, + }, + ); + + /** + * Adds a synopsis of a details element's contents to its summary. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior for the details element. + */ + Drupal.behaviors.detailsSummary = { + attach(context) { + const $detailsElements = $(context) + .find('details') + .once('details'); + $detailsElements.map( + (index, details) => + // eslint-disable-next-line no-new + new DetailsContentSynopsis(details), + ); + }, + }; + + /** + * The element containing the synopsis of the detail element's contents. + * + * @return {string} + * The markup for the element that will contain the synopsis text. + */ + Drupal.theme.synopsizedDetailsContentWrapper = () => + ``; + + /** + * Formats a summary's synopsis text. + * + * @param {string|null} [text] + * (optional) The synopsis text displayed by the summary. + * @return {string} + * The formatted synopsis text. + */ + Drupal.theme.synopsizedDetailsContent = text => (text ? ` (${text})` : ''); +})(jQuery, Drupal); diff --git a/core/misc/details.js b/core/misc/details-content-synopsis.js similarity index 52% rename from core/misc/details.js rename to core/misc/details-content-synopsis.js index a080141a4c..f3b60768c3 100644 --- a/core/misc/details.js +++ b/core/misc/details-content-synopsis.js @@ -6,43 +6,38 @@ **/ (function ($, Drupal) { - function DetailsEnhanced(node) { + function DetailsContentSynopsis(node) { this.$node = $(node); this.setupSummary(); } - $.extend(DetailsEnhanced, { + $.extend(DetailsContentSynopsis, { instances: [] }); - $.extend(DetailsEnhanced.prototype, { + $.extend(DetailsContentSynopsis.prototype, { setupSummary: function setupSummary() { - this.$summarySupplementalContent = $(Drupal.theme('summarySupplementalContent')); - this.$node.on('summaryUpdated', $.proxy(this.onSummaryUpdated, this)).trigger('summaryUpdated').find('> summary').append(this.$summarySupplementalContent); + this.$synopsizedDetailsContentWrapper = $(Drupal.theme('synopsizedDetailsContentWrapper')); + this.$node.on('summaryUpdated', $.proxy(this.onSummaryUpdated, this)).trigger('summaryUpdated').find('> summary').append(this.$synopsizedDetailsContentWrapper); }, onSummaryUpdated: function onSummaryUpdated() { var text = $.trim(this.$node.drupalGetSummary()); - this.$summarySupplementalContent.html(Drupal.theme('summarySupplementalContentText', text)); + this.$synopsizedDetailsContentWrapper.html(Drupal.theme('synopsizedDetailsContent', text)); } }); Drupal.behaviors.detailsSummary = { attach: function attach(context) { var $detailsElements = $(context).find('details').once('details'); - - if ($detailsElements.length) { - for (var i = 0; i < $detailsElements.length; i++) { - Drupal.EnhancedDetailsElements.instances.push(new DetailsEnhanced($detailsElements[i])); - } - } + $detailsElements.map(function (index, details) { + return new DetailsContentSynopsis(details); + }); } }; - Drupal.theme.summarySupplementalContent = function () { + Drupal.theme.synopsizedDetailsContentWrapper = function () { return ""; }; - Drupal.theme.summarySupplementalContentText = function (text) { + Drupal.theme.synopsizedDetailsContent = function (text) { return text ? " (".concat(text, ")") : ''; }; - - Drupal.EnhancedDetailsElements = DetailsEnhanced; })(jQuery, Drupal); \ No newline at end of file diff --git a/core/misc/details.es6.js b/core/misc/details.es6.js deleted file mode 100644 index 3dafad664c..0000000000 --- a/core/misc/details.es6.js +++ /dev/null @@ -1,109 +0,0 @@ -/** - * @file - * Additional functionality for details elements. - */ -(($, Drupal) => { - /** - * The DetailsEnhanced object represents a single details element. - * - * @constructor Drupal.DetailsEnhanced - * - * @param {HTMLElement} node - * The details element with enhanced functionality. - */ - function DetailsEnhanced(node) { - this.$node = $(node); - this.setupSummary(); - } - - $.extend( - DetailsEnhanced, - /** @lends Drupal.DetailsEnhanced */ { - /** - * Holds references to instantiated DetailsEnhanced objects. - * - * @type {Array.} - */ - instances: [], - }, - ); - - $.extend( - DetailsEnhanced.prototype, - /** @lends Drupal.DetailsEnhanced# */ { - /** - * Initialize and setup summary events and markup. - * - * @fires event:summaryUpdated - * - * @listens event:summaryUpdated - */ - setupSummary() { - this.$summarySupplementalContent = $( - Drupal.theme('summarySupplementalContent'), - ); - this.$node - .on('summaryUpdated', $.proxy(this.onSummaryUpdated, this)) - .trigger('summaryUpdated') - .find('> summary') - .append(this.$summarySupplementalContent); - }, - - /** - * Update summary. - */ - onSummaryUpdated() { - const text = $.trim(this.$node.drupalGetSummary()); - this.$summarySupplementalContent.html( - Drupal.theme('summarySupplementalContentText', text), - ); - }, - }, - ); - - /** - * Extends functionality of a details element. - * - * @type {Drupal~behavior} - * - * @prop {Drupal~behaviorAttach} attach - * Attaches behavior for the details element. - */ - Drupal.behaviors.detailsSummary = { - attach(context) { - const $detailsElements = $(context) - .find('details') - .once('details'); - if ($detailsElements.length) { - for (let i = 0; i < $detailsElements.length; i++) { - Drupal.EnhancedDetailsElements.instances.push( - new DetailsEnhanced($detailsElements[i]), - ); - } - } - }, - }; - - /** - * The element containing a summary's supplemental text. - * - * @return {string} - * The markup for the element that will contain supplemental summary text. - */ - Drupal.theme.summarySupplementalContent = () => - ``; - - /** - * Formats a summary's supplemental text. - * - * @param {string|null} [text] - * (optional) The additional text displayed by the summary. - * @return {string} - * The formatted text that will be appended to the summary element. - */ - Drupal.theme.summarySupplementalContentText = text => - text ? ` (${text})` : ''; - - // Expose constructor in the public space. - Drupal.EnhancedDetailsElements = DetailsEnhanced; -})(jQuery, Drupal);