[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/com_associations/js/ -> associations-edit.js (source)

   1  /**
   2   * @copyright   (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   3   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   4   */
   5  Joomla = window.Joomla || {};
   6  
   7  ((Joomla, document) => {
   8  
   9    Joomla.hideAssociation = (formControl, languageCode) => {
  10      const controlGroup = [].slice.call(document.querySelectorAll('#associations .control-group'));
  11      controlGroup.forEach(element => {
  12        // Current selected language. Hide it
  13        const el = element.querySelector('.control-label label');
  14  
  15        if (el) {
  16          const attribute = el.getAttribute('for');
  17  
  18          if (attribute.replace(/_name$/, '') === `$formControl}_associations_$languageCode.replace('-', '_')}`) {
  19            element.classList.add('hidden');
  20          }
  21        }
  22      });
  23    };
  24  
  25    Joomla.showAssociationMessage = () => {
  26      const controlGroup = [].slice.call(document.querySelectorAll('#associations .control-group'));
  27      const associations = document.getElementById('associations');
  28  
  29      if (associations) {
  30        const html = document.createElement('joomla-alert');
  31        html.innerText = Joomla.Text._('JGLOBAL_ASSOC_NOT_POSSIBLE');
  32        associations.insertAdjacentElement('afterbegin', html);
  33      }
  34  
  35      controlGroup.forEach(element => {
  36        element.classList.add('hidden');
  37      });
  38    };
  39    /**
  40     * Inject associations into other association fields
  41     *
  42     * This function is called whenever the Ajax request within propagateAssociation() completes
  43     * successfully.
  44     * Its purpose is to inject the associations which have been returned in the Ajax response into
  45     * the other association fields in the form.
  46     * It does this by invoking the various callback functions of those association fields (i.e. the
  47     * function which gets called whenever the administrator selects an association via the modal),
  48     * and passing the appropriate associated record details.
  49     *
  50     * @param   result                  The response from the Ajax request.
  51     *                                  Its structure is that generated by the JResponseJson class,
  52     *                                  with the data field containing the associations
  53     * @param   callbackFunctionPrefix  The name of the callback function which the modal window uses
  54     *                                  to set the selected item as the association, but minus the
  55     *                                  language tag at the end
  56     *
  57     * @return  boolean
  58     *
  59     * @since   3.9.0
  60     */
  61  
  62  
  63    Joomla.injectAssociations = (result, callbackFunctionPrefix) => {
  64      let functionName;
  65  
  66      if (result.success) {
  67        if (result.data.length !== 0) {
  68          [].slice.call(Object.keys(result.data)).forEach(lang => {
  69            functionName = callbackFunctionPrefix + lang.replace('-', '_');
  70            window[functionName](result.data[lang].id, result.data[lang].title, result.data[lang].catid, null, null, lang);
  71          });
  72        }
  73  
  74        if (result.message) {
  75          Joomla.renderMessages({
  76            notice: [result.message]
  77          });
  78        }
  79      } else {
  80        Joomla.renderMessages({
  81          warning: [Joomla.Text._('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED')]
  82        });
  83      }
  84    };
  85    /**
  86     * Propagate associations from this field into other association fields
  87     *
  88     * This function is called whenever an administrator populates an association (in the association
  89     * modal field) and then clicks on the Propagate button.
  90     * The purpose of this function is to find what other records (if any) are associated with the
  91     * one which the administrator has selected, and populate the other association fields with these
  92     * records. (Otherwise, if the administrator just clicks on Save without clicking on Propagate,
  93     * those other associations will be deleted). It does this by finding the id of the selected
  94     * associated record (from a hidden field) and makes an Ajax call to the server to find the other
  95     * associations, also passing up the language of the record currently being edited, as it should
  96     * be excluded. Once it has received from the server the other associations it calls
  97     * injectAssociations to inject them into the other association fields within the form.
  98     *
  99     * @param   fieldPrefix             The stem of the html ids for the elements comprising the
 100     *                                  modal field
 101     * @param   callbackFunctionPrefix  The name of the callback function which the modal window uses
 102     *                                  to set the selected item as the association, but minus the
 103     *                                  language tag at the end
 104     *
 105     * @return  boolean
 106     *
 107     * @since   3.9.0
 108     */
 109  
 110  
 111    Joomla.propagateAssociation = (fieldPrefix, callbackFunctionPrefix) => {
 112      // Find the id of the record which has been set as an association
 113      const assocId = document.getElementById(`$fieldPrefix}_id`).value; // Find the language of the record being edited
 114  
 115      const languageField = document.getElementById('jform_language');
 116      const currentLang = languageField.options[languageField.selectedIndex].value;
 117      const data = {
 118        task: 'ajax.fetchAssociations',
 119        format: 'json',
 120        assocId,
 121        excludeLang: currentLang
 122      };
 123      data[Joomla.getOptions('csrf.token', '')] = 1;
 124      const queryString = Object.keys(data).reduce((a, k) => {
 125        a.push(`$k}=$encodeURIComponent(data[k])}`);
 126        return a;
 127      }, []).join('&');
 128      const url = `$document.forms.adminForm.action}&$queryString}`;
 129      Joomla.request({
 130        // Find the action url associated with the form - we need to add the token to this
 131        url,
 132        method: 'GET',
 133        data: JSON.stringify(data),
 134        headers: {
 135          'Content-Type': 'application/json'
 136        },
 137        onSuccess: response => {
 138          Joomla.injectAssociations(JSON.parse(response), callbackFunctionPrefix);
 139        },
 140        onError: () => {
 141          Joomla.renderMessages({
 142            warning: [Joomla.Text._('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED')]
 143          });
 144        }
 145      });
 146      return false;
 147    };
 148  
 149    document.addEventListener('DOMContentLoaded', () => {
 150      const associationsEditOptions = Joomla.getOptions('system.associations.edit');
 151      const formControl = associationsEditOptions.formControl || 'jform';
 152      const formControlLanguage = document.getElementById(`$formControl}_language`); // Hide the associations tab if needed
 153  
 154      if (parseInt(associationsEditOptions.hidden, 10) === 1) {
 155        Joomla.showAssociationMessage();
 156      } else if (formControlLanguage) {
 157        // Hide only the associations for the current language
 158        Joomla.hideAssociation(formControl, formControlLanguage.value);
 159      } // When changing the language
 160  
 161  
 162      if (formControlLanguage) {
 163        formControlLanguage.addEventListener('change', ({
 164          target
 165        }) => {
 166          // Remove message if any
 167          Joomla.removeMessages();
 168          let existsAssociations = false;
 169          /** For each language, remove the associations, ie,
 170           *  empty the associations fields and reset the buttons to Select/Create
 171           */
 172  
 173          const controlGroup = [].slice.call(document.querySelectorAll('#associations .control-group'));
 174          controlGroup.forEach(element => {
 175            const attribute = element.querySelector('.control-label label').getAttribute('for');
 176            const languageCode = attribute.replace('_name', '').replace('jform_associations_', ''); // Show the association fields
 177  
 178            element.classList.remove('hidden'); // Check if there was an association selected for this language
 179  
 180            if (!existsAssociations && document.getElementById(`$formControl}_associations_$languageCode}_id`).value !== '') {
 181              existsAssociations = true;
 182            } // Call the modal clear button
 183  
 184  
 185            const clear = document.getElementById(`$formControl}_associations_$languageCode}_clear`);
 186  
 187            if (clear.onclick) {
 188              clear.onclick();
 189            } else if (clear.click) {
 190              clear.click();
 191            }
 192          }); // If associations existed, send a warning to the user
 193  
 194          if (existsAssociations) {
 195            Joomla.renderMessages({
 196              warning: [Joomla.Text._('JGLOBAL_ASSOCIATIONS_RESET_WARNING')]
 197            });
 198          } // If the selected language is All hide the fields and add a message
 199  
 200  
 201          const selectedLanguage = target.value;
 202  
 203          if (selectedLanguage === '*') {
 204            Joomla.showAssociationMessage();
 205          } else {
 206            // Else show the associations fields/buttons and hide the current selected language
 207            Joomla.hideAssociation(formControl, selectedLanguage);
 208          }
 209        });
 210      }
 211    });
 212  })(Joomla, document);


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer