CoreFiles by mySites.guru
associations-edit.es6.js108 lines, 3,965 bytes, from Joomla_4.0.0-alpha5-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha3
md5
a196b2eb26130d37caa244e7f3b4fb68
sha1
9fea4bf0e7fb6becc84c77f4b5715cb1aa9604e5
sha256
e53f728181e8b484ca3902d2a3965e1bf4b9b3bc2d3bdb24d0f609a648c57cff
/**
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
Joomla = window.Joomla || {};

((Joomla, document) => {
  'use strict';

  Joomla.hideAssociation = (formControl, languageCode) => {
    const controlGroup = [].slice.call(document.querySelectorAll('#associations .control-group'));

    controlGroup.forEach((element) => {
      // Current selected language. Hide it
      const el = element.querySelector('.control-label label');

      if (el) {
        const attribute = el.getAttribute('for');

        if (attribute.replace('_id', '') === `${formControl}_associations_${languageCode.replace('-', '_')}`) {
          element.style.display = 'none';
        }
      }
    });
  };

  Joomla.showAssociationMessage = () => {
    const controlGroup = [].slice.call(document.querySelectorAll('#associations .control-group'));
    const associations = document.getElementById('associations');

    if (associations) {
      const html = document.createElement('joomla-alert');
      html.innerHTML = Joomla.JText._('JGLOBAL_ASSOC_NOT_POSSIBLE');

      associations.insertAdjacentElement('afterbegin', html);
    }

    controlGroup.forEach((element) => {
      element.style.display = 'none';
    });
  };

  document.addEventListener('DOMContentLoaded', () => {
    const associationsEditOptions = Joomla.getOptions('system.associations.edit');
    const formControl = associationsEditOptions.formControl || 'jform';
    const formControlLanguage = document.getElementById(`${formControl}_language`);

    // Hide the associations tab if needed
    if (parseInt(associationsEditOptions.hidden, 10) === 1) {
      Joomla.showAssociationMessage();
    } else if (formControlLanguage) {
      // Hide only the associations for the current language
      Joomla.hideAssociation(formControl, formControlLanguage.value);
    }

    // When changing the language
    if (formControlLanguage) {
      formControlLanguage.addEventListener('change', (event) => {
        // Remove message if any
        Joomla.removeMessages();

        let existsAssociations = false;

        /** For each language, remove the associations, ie,
         *  empty the associations fields and reset the buttons to Select/Create
         */
        const controlGroup = [].slice.call(document.querySelectorAll('#associations .control-group'));

        controlGroup.forEach((element) => {
          const attribute = element.querySelector('.control-label label').getAttribute('for');
          const languageCode = attribute.replace('_id', '').replace('jform_associations_', '');

          // Show the association fields
          element.style.display = 'block';

          // Check if there was an association selected for this language
          if (!existsAssociations && document.getElementById(`${formControl}_associations_${languageCode}_id`).value !== '') {
            existsAssociations = true;
          }

          // Call the modal clear button
          const clear = document.getElementById(`${formControl}_associations_${languageCode}_clear`);

          if (clear.onclick) {
            clear.onclick();
          } else if (clear.click) {
            clear.click();
          }
        });

        // If associations existed, send a warning to the user
        if (existsAssociations) {
          Joomla.renderMessages({ warning: [Joomla.JText._('JGLOBAL_ASSOCIATIONS_RESET_WARNING')] });
        }

        // If the selected language is All hide the fields and add a message
        const selectedLanguage = event.target.value;

        if (selectedLanguage === '*') {
          Joomla.showAssociationMessage();
        } else {
          // Else show the associations fields/buttons and hide the current selected language
          Joomla.hideAssociation(formControl, selectedLanguage);
        }
      });
    }
  });
})(Joomla, document);

The same content is in 2 releases, Joomla 4.0.0-alpha5 to 4.0.0-alpha6. See them all, or every version of this file.