[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/com_languages/js/ -> overrider.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, document) => {
   6    class Overrider {
   7      constructor() {
   8        this.states = {
   9          refreshing: false,
  10          refreshed: false,
  11          counter: 0,
  12          searchString: '',
  13          searchType: 'value'
  14        };
  15        this.spinner = document.getElementById('overrider-spinner');
  16        this.spinnerBtn = document.getElementById('overrider-spinner-btn');
  17        this.moreResults = document.getElementById('more-results');
  18        this.moreResultsButton = document.getElementById('more-results-button');
  19        this.resultsContainer = document.getElementById('results-container');
  20        this.refreshStatus = document.getElementById('refresh-status');
  21      }
  22      /**
  23       * Method for refreshing the database cache of known language strings via Ajax
  24       *
  25       * @return  void
  26       *
  27       * @since   2.5
  28       */
  29  
  30  
  31      refreshCache() {
  32        this.states.refreshing = true;
  33        this.refreshStatus.classList.add('show');
  34        Joomla.request({
  35          url: 'index.php?option=com_languages&task=strings.refresh&format=json',
  36          method: 'POST',
  37          headers: {
  38            'Content-Type': 'application/json'
  39          },
  40          onSuccess: response => {
  41            if (response.error && response.message) {
  42              alert(response.message);
  43            }
  44  
  45            if (response.messages) {
  46              Joomla.renderMessages(response.messages);
  47            }
  48  
  49            this.refreshStatus.classList.remove('show');
  50            this.states.refreshing = false;
  51          },
  52          onError: () => {
  53            alert(Joomla.Text._('COM_LANGUAGES_VIEW_OVERRIDE_REQUEST_ERROR'));
  54            this.refreshStatus.classList.remove('show');
  55          }
  56        });
  57      }
  58      /**
  59       * Method for searching known language strings via Ajax
  60       *
  61       * @param   more  Determines the limit start of the results
  62       *
  63       * @return  void
  64       *
  65       * @since   2.5
  66       */
  67  
  68  
  69      searchStrings(more) {
  70        // Prevent searching if the cache is refreshed at the moment
  71        if (this.states.refreshing) {
  72          return;
  73        }
  74  
  75        const formSearchString = document.getElementById('jform_searchstring');
  76        const formSearchType = document.getElementById('jform_searchtype'); // Only update the used searchstring and searchtype if the search button
  77        // was used to start the search (that will be the case if 'more' is null)
  78  
  79        if (!more) {
  80          this.states.searchString = formSearchString.value;
  81          this.states.searchType = formSearchType.value || 'value'; // Remove the old results
  82  
  83          const oldResults = [].slice.call(document.querySelectorAll('.language-results'));
  84          oldResults.forEach(result => {
  85            result.parentNode.removeChild(result);
  86          });
  87        }
  88  
  89        if (!this.states.searchString) {
  90          formSearchString.classList.add('invalid');
  91          return;
  92        }
  93  
  94        if (more) {
  95          // If 'more' is greater than 0 we have already displayed some results for
  96          // the current searchstring, so display the spinner at the more link
  97          this.spinnerBtn.classList.add('show');
  98        } else {
  99          // Otherwise it is a new searchstring and we have to remove all previous results first
 100          this.moreResults.classList.remove('show');
 101          const childs = [].slice.call(document.querySelectorAll('#results-container div.language-results'));
 102          childs.forEach(child => {
 103            child.parentNode.removeChild(child);
 104          });
 105          this.resultsContainer.classList.add('show');
 106          this.spinner.classList.add('show');
 107        }
 108  
 109        Joomla.request({
 110          url: `index.php?option=com_languages&task=strings.search&format=json&searchstring=$this.states.searchString}&searchtype=$this.states.searchType}&more=$more}`,
 111          method: 'POST',
 112          headers: {
 113            'Content-Type': 'application/json'
 114          },
 115          onSuccess: resp => {
 116            const response = JSON.parse(resp);
 117  
 118            if (response.error && response.message) {
 119              alert(response.message);
 120            }
 121  
 122            if (response.messages) {
 123              Joomla.renderMessages(response.messages);
 124            }
 125  
 126            if (response.data) {
 127              if (response.data.results) {
 128                Joomla.overrider.insertResults(response.data.results);
 129              }
 130  
 131              if (response.data.more) {
 132                // If there are more results than the sent ones
 133                // display the more link
 134                this.states.more = response.data.more;
 135                this.moreResultsButton.disabled = false;
 136                this.moreResults.classList.add('show');
 137              } else {
 138                this.moreResultsButton.disabled = true;
 139                this.moreResults.classList.remove('show');
 140              }
 141            }
 142  
 143            this.spinnerBtn.classList.remove('show');
 144            this.spinner.classList.remove('show');
 145          },
 146          onError: () => {
 147            alert(Joomla.Text._('COM_LANGUAGES_VIEW_OVERRIDE_REQUEST_ERROR'));
 148            this.moreResultsButton.disabled = true;
 149            this.moreResults.classList.remove('show');
 150            this.resultsContainer.classList.remove('show');
 151          }
 152        });
 153      }
 154      /**
 155       * Method inserting the received results into the results container
 156       *
 157       * @param   results  An array of search result objects
 158       *
 159       * @return  void
 160       *
 161       * @since   2.5
 162       */
 163  
 164  
 165      insertResults(results) {
 166        // For creating an individual ID for each result we use a counter
 167        this.states.counter += 1; // Create a container into which all the results will be inserted
 168  
 169        const resultsDiv = document.createElement('div');
 170        resultsDiv.setAttribute('id', `language-results$this.states.counter}`);
 171        resultsDiv.classList.add('language-results');
 172        resultsDiv.classList.add('list-group');
 173        resultsDiv.classList.add('mb-2');
 174        resultsDiv.classList.add('show'); // Create some elements for each result and insert it into the container
 175  
 176        results.forEach((item, index) => {
 177          const a = document.createElement('a');
 178          a.setAttribute('onclick', `Joomla.overrider.selectString($this.states.counter}$index});`);
 179          a.setAttribute('href', '#');
 180          a.classList.add('list-group-item');
 181          a.classList.add('list-group-item-action');
 182          a.classList.add('flex-column');
 183          a.classList.add('align-items-start');
 184          const key = document.createElement('div');
 185          key.setAttribute('id', `override_key$this.states.counter}$index}`);
 186          key.setAttribute('title', item.file);
 187          key.classList.add('result-key');
 188          key.innerHTML = Joomla.sanitizeHtml(item.constant);
 189          const string = document.createElement('div');
 190          string.setAttribute('id', `override_string$this.states.counter}$index}`);
 191          string.classList.add('result-string');
 192          string.innerHTML = Joomla.sanitizeHtml(item.string);
 193          a.appendChild(key);
 194          a.appendChild(string);
 195          resultsDiv.appendChild(a);
 196        }); // If there aren't any results display an appropriate message
 197  
 198        if (!results.length) {
 199          const noresult = document.createElement('div');
 200          noresult.innerText = Joomla.Text._('COM_LANGUAGES_VIEW_OVERRIDE_NO_RESULTS');
 201          resultsDiv.appendChild(noresult);
 202        }
 203  
 204        if (this.moreResults) {
 205          this.moreResults.parentNode.insertBefore(resultsDiv, this.moreResults);
 206        }
 207      }
 208      /**
 209       * Inserts a specific constant/value pair into the form and scrolls the page back to the top
 210       *
 211       * @param   id  The ID of the element which was selected for insertion
 212       *
 213       * @return  void
 214       *
 215       * @since   2.5
 216       */
 217      // eslint-disable-next-line class-methods-use-this
 218  
 219  
 220      selectString(id) {
 221        document.getElementById('jform_key').value = document.getElementById(`override_key$id}`).innerHTML;
 222        document.getElementById('jform_override').value = document.getElementById(`override_string$id}`).innerHTML;
 223      }
 224  
 225    }
 226  
 227    document.addEventListener('DOMContentLoaded', () => {
 228      Joomla.overrider = new Overrider();
 229    });
 230  })(Joomla, document);


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