[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

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


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