(function (Drupal, once) {
'use strict';
const DATA_COPY_NAME = 'data-pr-set';
const DATA_SOURCE_CLASS = 'pr-data-source';
const DATA_NAME_STARTS_WITH = 'data-pr-name-starts-with';
const handleCopy = (context, value, name_starts_with) => {
context.querySelectorAll(`[name^="${name_starts_with}"][${DATA_COPY_NAME}]`).forEach((element) => {
const values = JSON.parse(element.getAttribute(DATA_COPY_NAME));
if (values.hasOwnProperty(value)) {
setElementValue(element, values[value]);
}
else {
setElementValue(element);
}
});
}
const setElementValue = (element, value) => {
if (element.classList.contains('mask-money--apply')) {
// element.value = value;
Drupal.behaviors.autoCalculateFormulaBehaviour.setValue(element.getAttribute('name'), value === undefined ? 0 : parseFloat(value));
}
else {
element.value = value === undefined ? '' : value;
const event = new Event('change');
element.dispatchEvent(event);
}
}
Drupal.behaviors.copyItemValues = {
attach: (context) => {
once('copy-item-values', `.${DATA_SOURCE_CLASS}`, context).forEach((element) => {
element.addEventListener('change', (event) => {
handleCopy(context, event.target.value, event.target.getAttribute(DATA_NAME_STARTS_WITH));
});
});
}
}
})(Drupal, once);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter