Skip to content

Commit

Permalink
Merge pull request #23 from MorganDawe/DGIR-115
Browse files Browse the repository at this point in the history
DGIR-115 : Fix: behaviour attaching events/elements incorrectly in some occasions.
  • Loading branch information
nchiasson-dgi authored Apr 5, 2024
2 parents 781eb9a + 3722127 commit 7511278
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
2 changes: 2 additions & 0 deletions dgi_members.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ compound_parts:
js/compound_parts.js: {}
dependencies:
- core/once
- core/drupal
- core/jquery
57 changes: 30 additions & 27 deletions js/compound_parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,44 @@

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', 'body', 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) {
e.preventDefault();
$('.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",
);
});
});
}
};

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7511278

Please sign in to comment.