[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2007 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\Language\Text;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Utility class for JavaScript behaviors
  21   *
  22   * @since  1.5
  23   */
  24  abstract class Behavior
  25  {
  26      /**
  27       * Array containing information for loaded files
  28       *
  29       * @var    array
  30       * @since  2.5
  31       */
  32      protected static $loaded = array();
  33  
  34      /**
  35       * Method to load core.js into the document head.
  36       *
  37       * Core.js defines the 'Joomla' namespace and contains functions which are used across extensions
  38       *
  39       * @return  void
  40       *
  41       * @since   3.3
  42       *
  43       * @deprecated 5.0  Use Joomla\CMS\WebAsset\WebAssetManager::enable();
  44       */
  45      public static function core()
  46      {
  47          Factory::getApplication()->getDocument()->getWebAssetManager()->useScript('core');
  48      }
  49  
  50      /**
  51       * Add unobtrusive JavaScript support for form validation.
  52       *
  53       * To enable form validation the form tag must have class="form-validate".
  54       * Each field that needs to be validated needs to have class="validate".
  55       * Additional handlers can be added to the handler for username, password,
  56       * numeric and email. To use these add class="validate-email" and so on.
  57       *
  58       * @return  void
  59       *
  60       * @since   3.4
  61       * @deprecated 5.0 Use the script directly
  62       */
  63      public static function formvalidator()
  64      {
  65          // Only load once
  66          if (isset(static::$loaded[__METHOD__])) {
  67              return;
  68          }
  69  
  70          Factory::getDocument()->getWebAssetManager()->useScript('form.validate');
  71  
  72          static::$loaded[__METHOD__] = true;
  73      }
  74  
  75      /**
  76       * Add unobtrusive JavaScript support for a combobox effect.
  77       *
  78       * Note that this control is only reliable in absolutely positioned elements.
  79       * Avoid using a combobox in a slider or dynamic pane.
  80       *
  81       * @return  void
  82       *
  83       * @since   1.5
  84       * @deprecated 5.0 Use the script directly
  85       */
  86      public static function combobox()
  87      {
  88          Factory::getDocument()->getWebAssetManager()->usePreset('awesomplete');
  89      }
  90  
  91      /**
  92       * JavaScript behavior to allow shift select in grids
  93       *
  94       * @param   string  $id  The id of the form for which a multiselect behaviour is to be applied.
  95       *
  96       * @return  void
  97       *
  98       * @since   1.7
  99       * @deprecated 5.0 Use the script directly
 100       */
 101      public static function multiselect($id = 'adminForm')
 102      {
 103          // Only load once
 104          if (isset(static::$loaded[__METHOD__][$id])) {
 105              return;
 106          }
 107  
 108          Factory::getDocument()->getWebAssetManager()->useScript('multiselect');
 109  
 110          // Pass the required options to the javascript
 111          Factory::getDocument()->addScriptOptions('js-multiselect', ['formName' => $id]);
 112  
 113          // Set static array
 114          static::$loaded[__METHOD__][$id] = true;
 115      }
 116  
 117      /**
 118       * Keep session alive, for example, while editing or creating an article.
 119       *
 120       * @return  void
 121       *
 122       * @since   1.5
 123       *
 124       * @deprecated 5.0  Use Joomla\CMS\WebAsset\WebAssetManager::enable();
 125       */
 126      public static function keepalive()
 127      {
 128          Factory::getApplication()->getDocument()->getWebAssetManager()->useScript('keepalive');
 129      }
 130  
 131      /**
 132       * Highlight some words via Javascript.
 133       *
 134       * @param   array   $terms      Array of words that should be highlighted.
 135       * @param   string  $start      ID of the element that marks the begin of the section in which words
 136       *                              should be highlighted. Note this element will be removed from the DOM.
 137       * @param   string  $end        ID of the element that end this section.
 138       *                              Note this element will be removed from the DOM.
 139       * @param   string  $className  Class name of the element highlights are wrapped in.
 140       * @param   string  $tag        Tag that will be used to wrap the highlighted words.
 141       *
 142       * @return  void
 143       *
 144       * @since   2.5
 145       *
 146       * @deprecated 5.0 Use the script directly
 147       */
 148      public static function highlighter(array $terms, $start = 'highlighter-start', $end = 'highlighter-end', $className = 'highlight', $tag = 'span')
 149      {
 150          $terms = array_filter($terms, 'strlen');
 151  
 152          if (!empty($terms)) {
 153              $doc = Factory::getDocument();
 154  
 155              $doc->getWebAssetManager()->useScript('highlight');
 156              $doc->addScriptOptions(
 157                  'highlight',
 158                  [[
 159                      'class'         => 'js-highlight',
 160                      'highLight'     => $terms,
 161                      'compatibility' => true,
 162                      'start'         => $start,
 163                      'end'           => $end,
 164                  ]]
 165              );
 166          }
 167      }
 168  
 169      /**
 170       * Add javascript polyfills.
 171       *
 172       * @param   string|array  $polyfillTypes       The polyfill type(s). Examples: event, array('event', 'classlist').
 173       * @param   string        $conditionalBrowser  An IE conditional expression. Example: lt IE 9 (lower than IE 9).
 174       *
 175       * @return  void
 176       *
 177       * @since   3.7.0
 178       */
 179      public static function polyfill($polyfillTypes = null, $conditionalBrowser = null)
 180      {
 181          if ($polyfillTypes === null) {
 182              return;
 183          }
 184  
 185          foreach ((array) $polyfillTypes as $polyfillType) {
 186              $sig = md5(serialize(array($polyfillType, $conditionalBrowser)));
 187  
 188              // Only load once
 189              if (isset(static::$loaded[__METHOD__][$sig])) {
 190                  continue;
 191              }
 192  
 193              // If include according to browser.
 194              $scriptOptions = array('version' => 'auto', 'relative' => true);
 195              $scriptOptions = $conditionalBrowser !== null ? array_replace($scriptOptions, array('conditional' => $conditionalBrowser)) : $scriptOptions;
 196  
 197              /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
 198              $wa = Factory::getApplication()->getDocument()->getWebAssetManager();
 199              $wa->registerAndUseScript('polyfill.' . $polyfillType, 'vendor/polyfills/polyfill-' . $polyfillType . '.js', $scriptOptions);
 200  
 201              // Set static array
 202              static::$loaded[__METHOD__][$sig] = true;
 203          }
 204      }
 205  
 206      /**
 207       * Internal method to translate the JavaScript Calendar
 208       *
 209       * @return  string  JavaScript that translates the object
 210       *
 211       * @since   1.5
 212       */
 213      protected static function calendartranslation()
 214      {
 215          static $jsscript = 0;
 216  
 217          // Guard clause, avoids unnecessary nesting
 218          if ($jsscript) {
 219              return false;
 220          }
 221  
 222          $jsscript = 1;
 223  
 224          // To keep the code simple here, run strings through Text::_() using array_map()
 225          $callback = array('Text', '_');
 226          $weekdays_full = array_map(
 227              $callback,
 228              array(
 229                  'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY',
 230              )
 231          );
 232          $weekdays_short = array_map(
 233              $callback,
 234              array(
 235                  'SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN',
 236              )
 237          );
 238          $months_long = array_map(
 239              $callback,
 240              array(
 241                  'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE',
 242                  'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER',
 243              )
 244          );
 245          $months_short = array_map(
 246              $callback,
 247              array(
 248                  'JANUARY_SHORT', 'FEBRUARY_SHORT', 'MARCH_SHORT', 'APRIL_SHORT', 'MAY_SHORT', 'JUNE_SHORT',
 249                  'JULY_SHORT', 'AUGUST_SHORT', 'SEPTEMBER_SHORT', 'OCTOBER_SHORT', 'NOVEMBER_SHORT', 'DECEMBER_SHORT',
 250              )
 251          );
 252  
 253          // This will become an object in Javascript but define it first in PHP for readability
 254          $today = " " . Text::_('JLIB_HTML_BEHAVIOR_TODAY') . " ";
 255          $text = array(
 256              'INFO'           => Text::_('JLIB_HTML_BEHAVIOR_ABOUT_THE_CALENDAR'),
 257              'ABOUT'          => "DHTML Date/Time Selector\n"
 258                  . "(c) dynarch.com 20022005 / Author: Mihai Bazon\n"
 259                  . "For latest version visit: http://www.dynarch.com/projects/calendar/\n"
 260                  . "Distributed under GNU LGPL.  See http://gnu.org/licenses/lgpl.html for details."
 261                  . "\n\n"
 262                  . Text::_('JLIB_HTML_BEHAVIOR_DATE_SELECTION')
 263                  . Text::_('JLIB_HTML_BEHAVIOR_YEAR_SELECT')
 264                  . Text::_('JLIB_HTML_BEHAVIOR_MONTH_SELECT')
 265                  . Text::_('JLIB_HTML_BEHAVIOR_HOLD_MOUSE'),
 266              'ABOUT_TIME'      => "\n\n"
 267                  . "Time selection:\n"
 268                  . " Click on any of the time parts to increase it\n"
 269                  . " or Shiftclick to decrease it\n"
 270                  . " or click and drag for faster selection.",
 271              'PREV_YEAR'       => Text::_('JLIB_HTML_BEHAVIOR_PREV_YEAR_HOLD_FOR_MENU'),
 272              'PREV_MONTH'      => Text::_('JLIB_HTML_BEHAVIOR_PREV_MONTH_HOLD_FOR_MENU'),
 273              'GO_TODAY'        => Text::_('JLIB_HTML_BEHAVIOR_GO_TODAY'),
 274              'NEXT_MONTH'      => Text::_('JLIB_HTML_BEHAVIOR_NEXT_MONTH_HOLD_FOR_MENU'),
 275              'SEL_DATE'        => Text::_('JLIB_HTML_BEHAVIOR_SELECT_DATE'),
 276              'DRAG_TO_MOVE'    => Text::_('JLIB_HTML_BEHAVIOR_DRAG_TO_MOVE'),
 277              'PART_TODAY'      => $today,
 278              'DAY_FIRST'       => Text::_('JLIB_HTML_BEHAVIOR_DISPLAY_S_FIRST'),
 279              'WEEKEND'         => Factory::getLanguage()->getWeekEnd(),
 280              'CLOSE'           => Text::_('JLIB_HTML_BEHAVIOR_CLOSE'),
 281              'TODAY'           => Text::_('JLIB_HTML_BEHAVIOR_TODAY'),
 282              'TIME_PART'       => Text::_('JLIB_HTML_BEHAVIOR_SHIFT_CLICK_OR_DRAG_TO_CHANGE_VALUE'),
 283              'DEF_DATE_FORMAT' => "%Y%m%d",
 284              'TT_DATE_FORMAT'  => Text::_('JLIB_HTML_BEHAVIOR_TT_DATE_FORMAT'),
 285              'WK'              => Text::_('JLIB_HTML_BEHAVIOR_WK'),
 286              'TIME'            => Text::_('JLIB_HTML_BEHAVIOR_TIME'),
 287          );
 288  
 289          return 'Calendar._DN = ' . json_encode($weekdays_full) . ';'
 290              . ' Calendar._SDN = ' . json_encode($weekdays_short) . ';'
 291              . ' Calendar._FD = 0;'
 292              . ' Calendar._MN = ' . json_encode($months_long) . ';'
 293              . ' Calendar._SMN = ' . json_encode($months_short) . ';'
 294              . ' Calendar._TT = ' . json_encode($text) . ';';
 295      }
 296  }


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