From 93e6801ad884499f73e5cad862c8a92acb2a7366 Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Wed, 7 Feb 2024 14:52:49 -0400 Subject: [PATCH 1/2] Fix: behaviour attaching events/elements correctly in some occasions. --- dgi_members.libraries.yml | 2 ++ js/compound_parts.js | 57 ++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/dgi_members.libraries.yml b/dgi_members.libraries.yml index 8df75f4..d052023 100644 --- a/dgi_members.libraries.yml +++ b/dgi_members.libraries.yml @@ -3,3 +3,5 @@ compound_parts: js/compound_parts.js: {} dependencies: - core/once + - core/drupal + - core/jquery diff --git a/js/compound_parts.js b/js/compound_parts.js index 94c7d5c..740b68b 100644 --- a/js/compound_parts.js +++ b/js/compound_parts.js @@ -8,10 +8,34 @@ Drupal.behaviors.dgi_members_compound_parts = { attach: function (context) { - once('dgi_members-compound_controller', 'body').forEach(() => { - Drupal.dgi_members.compound_members.ajaxBegin(); - Drupal.dgi_members.compound_members.appendLabels(); - Drupal.dgi_members.compound_members.updateActiveMetadataDisplay(); + once('dgi_members-compound_controller', '.compound-member-metadata, .compound-object-metadata', context).forEach(() => { + $(document).ready(function () { + Drupal.dgi_members.compound_members.ajaxBegin(); + Drupal.dgi_members.compound_members.appendLabels(); + Drupal.dgi_members.compound_members.updateActiveMetadataDisplay(); + + once('dgi_members-compound_controller_metadata-element', '.object-metadata.element-compound', context).forEach((element) => { + $(element).on('click', function (e) { + _click( + e, + '.object-metadata.element-compound', + ".compound-member-metadata", + ".compound-object-metadata" + ); + }); + }); + + once('dgi_members-compound_controller_metadata-part', '.object-metadata.part-metadata', context).forEach((element) => { + $(element).on('click', function (e) { + _click( + e, + '.object-metadata.part-metadata', + ".compound-object-metadata", + ".compound-member-metadata" + ); + }); + }); + }); }); function _click(e, active_selector, hide_selector, show_selector) { @@ -19,30 +43,9 @@ $('.object-metadata').removeClass('element-active'); $(active_selector).addClass('element-active'); - $(hide_selector).addClass('hidden') + $(hide_selector).addClass('hidden'); $(show_selector).removeClass('hidden'); } - - once('dgi_members-compound_controller_metadata-element', '.object-metadata.element-compound', context).forEach((element) => { - $(element).on('click', function (e) { - _click( - e, - '.object-metadata.element-compound', - ".compound-member-metadata", - ".compound-object-metadata", - ); - }); - }); - once('dgi_members-compound_controller_metadata-part', '.object-metadata.part-metadata', context).forEach((element) => { - $(element).on('click', function (e) { - _click( - e, - '.object-metadata.part-metadata', - ".compound-object-metadata", - ".compound-member-metadata", - ); - }); - }); } }; @@ -72,7 +75,7 @@ * Append the metadata labels to each panel. */ appendLabels: function () { - $(".compound-object-metadata, .compound-member-metadata").find('.panel-heading').append( + $(".compound-object-metadata, .compound-member-metadata").find('.panel-heading:first').append( // XXX: Drupal's/Squiz coding standards do not presently appears to be // aware of the possibility of template strings in Javascript. // phpcs:disable Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore,Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter From b529456fe66dd4fe3e96b4b57757fece3473822d Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Tue, 13 Feb 2024 13:20:36 -0400 Subject: [PATCH 2/2] Updating js selector. --- js/compound_parts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/compound_parts.js b/js/compound_parts.js index 740b68b..eb23000 100644 --- a/js/compound_parts.js +++ b/js/compound_parts.js @@ -8,7 +8,7 @@ Drupal.behaviors.dgi_members_compound_parts = { attach: function (context) { - once('dgi_members-compound_controller', '.compound-member-metadata, .compound-object-metadata', context).forEach(() => { + once('dgi_members-compound_controller', 'body', context).forEach(() => { $(document).ready(function () { Drupal.dgi_members.compound_members.ajaxBegin(); Drupal.dgi_members.compound_members.appendLabels();