pr_item_autopopulate.js

PHOTO EMBED

Sun Oct 30 2022 16:19:19 GMT+0000 (Coordinated Universal Time)

Saved by @igor #drupal #mysql

(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);
content_copyCOPY