commerce_shipping-8.x-2.0-rc2/components/commerce-shipment-panes/commerce-shipment-panes.js
components/commerce-shipment-panes/commerce-shipment-panes.js
/**
* @file
* Commerce Shipment Panes component script.
*/
((Drupal, once) => {
Drupal.behaviors.shipmentPanes = {
attach: function attach(context, settings) {
once(
'shipment-panes',
'.commerce-shipment-pane-navigation button',
context,
).forEach((tab) => {
tab.addEventListener('click', (e) => {
// Show the targeted pane.
const panes = e.currentTarget.closest('.commerce-shipment-panes');
const paneItems = panes.getElementsByClassName(
'commerce-shipment-pane-item',
);
Array.prototype.forEach.call(paneItems, function (pane) {
pane.classList.remove('active');
});
const target = e.currentTarget.dataset.shipmentPaneTarget;
const targetPane = panes.querySelector(
`.commerce-shipment-pane-item[data-pane-index="${target}"]`,
);
targetPane.classList.add('active');
// Replace link in the card header if possible.
const card = panes.closest('.card__content-wrapper');
if (typeof card === 'undefined') {
return;
}
const editLink = card.querySelector(
'.card__header .commerce-edit-link',
);
if (typeof editLink === 'undefined') {
return;
}
const newEditLink = editLink.cloneNode(true);
newEditLink.setAttribute(
'href',
e.currentTarget.dataset.shipmentEditUrl,
);
// Update the "data-once" attribute to reapply ajax to links.
const onceAttribute = editLink.dataset.once;
if (typeof onceAttribute === 'string') {
let onceItems = onceAttribute.split(/\s+/);
onceItems = onceItems.filter((item) => item !== 'ajax');
newEditLink.setAttribute('data-once', onceItems.join(' '));
}
editLink.replaceWith(newEditLink);
Drupal.ajax.bindAjaxLinks(card);
});
});
},
};
})(Drupal, once);
