[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/com_modules/js/ -> admin-module-search-es5.js (source)

   1  (function () {
   2    'use strict';
   3  
   4    /**
   5     * @copyright  (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
   6     * @license    GNU General Public License version 2 or later; see LICENSE.txt
   7     */
   8  
   9    /**
  10     * Add a keyboard event listener to the Select a Module Type search element.
  11     *
  12     * IMPORTANT! This script is meant to be loaded deferred. This means that a. it's non-blocking
  13     * (the browser can load it whenever) and b. it doesn't need an on DOMContentLoaded event handler
  14     * because the browser is guaranteed to execute it only after the DOM content has loaded, the
  15     * whole point of it being deferred.
  16     *
  17     * The search box has a keyboard handler that fires every time you press a keyboard button or send
  18     * a keypress with a touch / virtual keyboard. We then iterate all module type cards and check if
  19     * the plain text (HTML stripped out) representation of the module title or description partially
  20     * matches the text you entered in the search box. If it doesn't we add a Bootstrap class to hide
  21     * the module.
  22     *
  23     * This way we limit the displayed modules only to those searched.
  24     *
  25     * This feature follows progressive enhancement. The search box is hidden by default and only
  26     * displayed when this JavaScript here executes. Furthermore, session storage is only used if it
  27     * is available in the browser. That's a bit of a pain but makes sure things won't break in older
  28     * browsers.
  29     *
  30     * Furthermore and to facilitate the user experience we auto-focus the search element which has a
  31     * suitable title so that non-sighted users are not startled. This way we address both UX concerns
  32     * and accessibility.
  33     *
  34     * Finally, the search string is saved into session storage on the assumption that the user is
  35     * probably going to be creating multiple instances of the same module, one after another, as is
  36     * typical when building a new Joomla! site.
  37     */
  38    // Make sure the element exists i.e. a template override has not removed it.
  39    var elSearch = document.getElementById('comModulesSelectSearch');
  40    var elSearchContainer = document.getElementById('comModulesSelectSearchContainer');
  41    var elSearchHeader = document.getElementById('comModulesSelectTypeHeader');
  42    var elSearchResults = document.getElementById('comModulesSelectResultsContainer');
  43    var alertElement = document.querySelector('.modules-alert');
  44    var elCards = [].slice.call(document.querySelectorAll('.comModulesSelectCard'));
  45  
  46    if (elSearch && elSearchContainer) {
  47      // Add the keyboard event listener which performs the live search in the cards
  48      elSearch.addEventListener('keyup', function (event) {
  49        /** @type {KeyboardEvent} event */
  50        var partialSearch = event.target.value;
  51        var hasSearchResults = false; // Save the search string into session storage
  52  
  53        if (typeof sessionStorage !== 'undefined') {
  54          sessionStorage.setItem('Joomla.com_modules.new.search', partialSearch);
  55        } // Iterate all the module cards
  56  
  57  
  58        elCards.forEach(function (card) {
  59          // First remove the class which hide the module cards
  60          card.classList.remove('d-none'); // An empty search string means that we should show everything
  61  
  62          if (!partialSearch) {
  63            return;
  64          }
  65  
  66          var cardHeader = card.querySelector('.new-module-title');
  67          var cardBody = card.querySelector('.card-body');
  68          var title = cardHeader ? cardHeader.textContent : '';
  69          var description = cardBody ? cardBody.textContent : ''; // If the module title and description don’t match add a class to hide it.
  70  
  71          if (title && !title.toLowerCase().includes(partialSearch.toLowerCase()) && description && !description.toLowerCase().includes(partialSearch.toLowerCase())) {
  72            card.classList.add('d-none');
  73          } else {
  74            hasSearchResults = true;
  75          }
  76        });
  77  
  78        if (hasSearchResults || !partialSearch) {
  79          alertElement.classList.add('d-none');
  80          elSearchHeader.classList.remove('d-none');
  81          elSearchResults.classList.remove('d-none');
  82        } else {
  83          alertElement.classList.remove('d-none');
  84          elSearchHeader.classList.add('d-none');
  85          elSearchResults.classList.add('d-none');
  86        }
  87      }); // For reasons of progressive enhancement the search box is hidden by default.
  88  
  89      elSearchContainer.classList.remove('d-none'); // Focus the just show element
  90  
  91      elSearch.focus();
  92  
  93      try {
  94        if (typeof sessionStorage !== 'undefined') {
  95          // Load the search string from session storage
  96          elSearch.value = sessionStorage.getItem('Joomla.com_modules.new.search') || ''; // Trigger the keyboard handler event manually to initiate the search
  97  
  98          elSearch.dispatchEvent(new KeyboardEvent('keyup'));
  99        }
 100      } catch (e) {// This is probably Internet Explorer which doesn't support the KeyboardEvent constructor :(
 101      }
 102    }
 103  
 104  })();


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