[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/HTML/Helpers/ -> FormBehavior.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2012 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\HTML\Helpers;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\HTML\HTMLHelper;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\Registry\Registry;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Utility class for form related behaviors
  23   *
  24   * @since       3.0
  25   *
  26   * @deprecated  5.0  Without replacement
  27   */
  28  abstract class FormBehavior
  29  {
  30      /**
  31       * @var    array  Array containing information for loaded files
  32       * @since  3.0
  33       */
  34      protected static $loaded = array();
  35  
  36      /**
  37       * Method to load the Chosen JavaScript framework and supporting CSS into the document head
  38       *
  39       * If debugging mode is on an uncompressed version of Chosen is included for easier debugging.
  40       *
  41       * @param   string  $selector  Class for Chosen elements.
  42       * @param   mixed   $debug     Is debugging mode on? [optional]
  43       * @param   array   $options   the possible Chosen options as name => value [optional]
  44       *
  45       * @return  void
  46       *
  47       * @since   3.0
  48       */
  49      public static function chosen($selector = '.advancedSelect', $debug = null, $options = array())
  50      {
  51          if (isset(static::$loaded[__METHOD__][$selector])) {
  52              return;
  53          }
  54  
  55          // If no debugging value is set, use the configuration setting
  56          if ($debug === null) {
  57              $debug = JDEBUG;
  58          }
  59  
  60          // Default settings
  61          if (!isset($options['disable_search_threshold'])) {
  62              $options['disable_search_threshold'] = 10;
  63          }
  64  
  65          // Allow searching contains space in query
  66          if (!isset($options['search_contains'])) {
  67              $options['search_contains'] = true;
  68          }
  69  
  70          if (!isset($options['allow_single_deselect'])) {
  71              $options['allow_single_deselect'] = true;
  72          }
  73  
  74          if (!isset($options['placeholder_text_multiple'])) {
  75              $options['placeholder_text_multiple'] = Text::_('JGLOBAL_TYPE_OR_SELECT_SOME_OPTIONS');
  76          }
  77  
  78          if (!isset($options['placeholder_text_single'])) {
  79              $options['placeholder_text_single'] = Text::_('JGLOBAL_SELECT_AN_OPTION');
  80          }
  81  
  82          if (!isset($options['no_results_text'])) {
  83              $options['no_results_text'] = Text::_('JGLOBAL_SELECT_NO_RESULTS_MATCH');
  84          }
  85  
  86          // Options array to json options string
  87          $options_str = \json_encode($options, ($debug && \defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false));
  88  
  89          // Add chosen.js assets
  90  
  91          /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
  92          $wa = Factory::getApplication()->getDocument()->getWebAssetManager();
  93          $wa->usePreset('chosen')
  94              ->registerAndUseScript('joomla-chosen', 'legacy/joomla-chosen.min.js', [], [], ['chosen'])
  95              ->addInlineScript(
  96                  "
  97          jQuery(document).ready(function (){
  98              jQuery('" . $selector . "').jchosen(" . $options_str . ");
  99          });
 100      "
 101              );
 102  
 103          static::$loaded[__METHOD__][$selector] = true;
 104      }
 105  
 106      /**
 107       * Method to load the AJAX Chosen library
 108       *
 109       * If debugging mode is on an uncompressed version of AJAX Chosen is included for easier debugging.
 110       *
 111       * @param   Registry  $options  Options in a Registry object
 112       * @param   mixed     $debug    Is debugging mode on? [optional]
 113       *
 114       * @return  void
 115       *
 116       * @since   3.0
 117       */
 118      public static function ajaxchosen(Registry $options, $debug = null)
 119      {
 120          // Retrieve options/defaults
 121          $selector       = $options->get('selector', '.tagfield');
 122          $type           = $options->get('type', 'GET');
 123          $url            = $options->get('url', null);
 124          $dataType       = $options->get('dataType', 'json');
 125          $jsonTermKey    = $options->get('jsonTermKey', 'term');
 126          $afterTypeDelay = $options->get('afterTypeDelay', '500');
 127          $minTermLength  = $options->get('minTermLength', '3');
 128  
 129          // Ajax URL is mandatory
 130          if (!empty($url)) {
 131              if (isset(static::$loaded[__METHOD__][$selector])) {
 132                  return;
 133              }
 134  
 135              // Requires chosen to work
 136              static::chosen($selector, $debug);
 137  
 138              Text::script('JGLOBAL_KEEP_TYPING');
 139              Text::script('JGLOBAL_LOOKING_FOR');
 140  
 141              // Include scripts
 142              HTMLHelper::_('behavior.core');
 143              HTMLHelper::_('jquery.framework');
 144              HTMLHelper::_('script', 'legacy/ajax-chosen.min.js', ['version' => 'auto', 'relative' => true, 'detectDebug' => $debug]);
 145  
 146              Factory::getDocument()->addScriptOptions(
 147                  'ajax-chosen',
 148                  array(
 149                      'url'            => $url,
 150                      'debug'          => $debug,
 151                      'options'        => $options,
 152                      'selector'       => $selector,
 153                      'type'           => $type,
 154                      'dataType'       => $dataType,
 155                      'jsonTermKey'    => $jsonTermKey,
 156                      'afterTypeDelay' => $afterTypeDelay,
 157                      'minTermLength'  => $minTermLength,
 158                  )
 159              );
 160  
 161              static::$loaded[__METHOD__][$selector] = true;
 162          }
 163      }
 164  }


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