[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/HTML/Helpers/ -> Dropdown.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\CMS\Router\Route;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * HTML utility class for building a dropdown menu
  23   *
  24   * @since  3.0
  25   */
  26  abstract class Dropdown
  27  {
  28      /**
  29       * @var    array  Array containing information for loaded files
  30       * @since  3.0
  31       */
  32      protected static $loaded = array();
  33  
  34      /**
  35       * @var    string  HTML markup for the dropdown list
  36       * @since  3.0
  37       */
  38      protected static $dropDownList = null;
  39  
  40      /**
  41       * Method to inject needed script
  42       *
  43       * @return  void
  44       *
  45       * @since   3.0
  46       */
  47      public static function init()
  48      {
  49          // Only load once
  50          if (isset(static::$loaded[__METHOD__])) {
  51              return;
  52          }
  53  
  54          // Depends on Bootstrap
  55          HTMLHelper::_('bootstrap.framework');
  56  
  57          Factory::getDocument()->addScriptDeclaration("
  58              (function($){
  59                  $(document).ready(function (){
  60                      $('.has-context')
  61                      .mouseenter(function (){
  62                          $('.btn-group',$(this)).show();
  63                      })
  64                      .mouseleave(function (){
  65                          $('.btn-group',$(this)).hide();
  66                          $('.btn-group',$(this)).removeClass('open');
  67                      });
  68  
  69                      contextAction =function (cbId, task)
  70                      {
  71                          $('input[name=\"cid[]\"]').removeAttr('checked');
  72                          $('#' + cbId).attr('checked','checked');
  73                          Joomla.submitbutton(task);
  74                      }
  75                  });
  76              })(jQuery);
  77              ");
  78  
  79          // Set static array
  80          static::$loaded[__METHOD__] = true;
  81      }
  82  
  83      /**
  84       * Method to start a new dropdown menu
  85       *
  86       * @return  void
  87       *
  88       * @since   3.0
  89       */
  90      public static function start()
  91      {
  92          // Only start once
  93          if (isset(static::$loaded[__METHOD__]) && static::$loaded[__METHOD__] == true) {
  94              return;
  95          }
  96  
  97          $dropDownList = '<div class="btn-group" style="margin-left:6px;display:none">
  98                              <a href="#" data-bs-toggle="dropdown" class="dropdown-toggle btn btn-secondary btn-sm">
  99                                  <span class="caret"></span>
 100                              </a>
 101                              <ul class="dropdown-menu">';
 102          static::$dropDownList = $dropDownList;
 103          static::$loaded[__METHOD__] = true;
 104      }
 105  
 106      /**
 107       * Method to render current dropdown menu
 108       *
 109       * @return  string  HTML markup for the dropdown list
 110       *
 111       * @since   3.0
 112       */
 113      public static function render()
 114      {
 115          $dropDownList  = static::$dropDownList;
 116          $dropDownList .= '</ul></div>';
 117  
 118          static::$dropDownList = null;
 119          static::$loaded[__CLASS__ . '::start'] = false;
 120  
 121          return $dropDownList;
 122      }
 123  
 124      /**
 125       * Append an edit item to the current dropdown menu
 126       *
 127       * @param   integer  $id          Record ID
 128       * @param   string   $prefix      Task prefix
 129       * @param   string   $customLink  The custom link if dont use default Joomla action format
 130       *
 131       * @return  void
 132       *
 133       * @since   3.0
 134       */
 135      public static function edit($id, $prefix = '', $customLink = '')
 136      {
 137          static::start();
 138  
 139          if (!$customLink) {
 140              $option = Factory::getApplication()->input->getCmd('option');
 141              $link = 'index.php?option=' . $option;
 142          } else {
 143              $link = $customLink;
 144          }
 145  
 146          $link .= '&task=' . $prefix . 'edit&id=' . $id;
 147          $link = Route::_($link);
 148  
 149          static::addCustomItem(Text::_('JACTION_EDIT'), $link);
 150      }
 151  
 152      /**
 153       * Append a publish item to the current dropdown menu
 154       *
 155       * @param   string  $checkboxId  ID of corresponding checkbox of the record
 156       * @param   string  $prefix      The task prefix
 157       *
 158       * @return  void
 159       *
 160       * @since   3.0
 161       */
 162      public static function publish($checkboxId, $prefix = '')
 163      {
 164          $task = $prefix . 'publish';
 165          static::addCustomItem(Text::_('JTOOLBAR_PUBLISH'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
 166      }
 167  
 168      /**
 169       * Append an unpublish item to the current dropdown menu
 170       *
 171       * @param   string  $checkboxId  ID of corresponding checkbox of the record
 172       * @param   string  $prefix      The task prefix
 173       *
 174       * @return  void
 175       *
 176       * @since   3.0
 177       */
 178      public static function unpublish($checkboxId, $prefix = '')
 179      {
 180          $task = $prefix . 'unpublish';
 181          static::addCustomItem(Text::_('JTOOLBAR_UNPUBLISH'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
 182      }
 183  
 184      /**
 185       * Append a featured item to the current dropdown menu
 186       *
 187       * @param   string  $checkboxId  ID of corresponding checkbox of the record
 188       * @param   string  $prefix      The task prefix
 189       *
 190       * @return  void
 191       *
 192       * @since   3.0
 193       */
 194      public static function featured($checkboxId, $prefix = '')
 195      {
 196          $task = $prefix . 'featured';
 197          static::addCustomItem(Text::_('JFEATURED'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
 198      }
 199  
 200      /**
 201       * Append an unfeatured item to the current dropdown menu
 202       *
 203       * @param   string  $checkboxId  ID of corresponding checkbox of the record
 204       * @param   string  $prefix      The task prefix
 205       *
 206       * @return  void
 207       *
 208       * @since   3.0
 209       */
 210      public static function unfeatured($checkboxId, $prefix = '')
 211      {
 212          $task = $prefix . 'unfeatured';
 213          static::addCustomItem(Text::_('JUNFEATURED'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
 214      }
 215  
 216      /**
 217       * Append an archive item to the current dropdown menu
 218       *
 219       * @param   string  $checkboxId  ID of corresponding checkbox of the record
 220       * @param   string  $prefix      The task prefix
 221       *
 222       * @return  void
 223       *
 224       * @since   3.0
 225       */
 226      public static function archive($checkboxId, $prefix = '')
 227      {
 228          $task = $prefix . 'archive';
 229          static::addCustomItem(Text::_('JTOOLBAR_ARCHIVE'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
 230      }
 231  
 232      /**
 233       * Append an unarchive item to the current dropdown menu
 234       *
 235       * @param   string  $checkboxId  ID of corresponding checkbox of the record
 236       * @param   string  $prefix      The task prefix
 237       *
 238       * @return  void
 239       *
 240       * @since   3.0
 241       */
 242      public static function unarchive($checkboxId, $prefix = '')
 243      {
 244          $task = $prefix . 'unpublish';
 245          static::addCustomItem(Text::_('JTOOLBAR_UNARCHIVE'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
 246      }
 247  
 248      /**
 249       * Append a trash item to the current dropdown menu
 250       *
 251       * @param   string  $checkboxId  ID of corresponding checkbox of the record
 252       * @param   string  $prefix      The task prefix
 253       *
 254       * @return  void
 255       *
 256       * @since   3.0
 257       */
 258      public static function trash($checkboxId, $prefix = '')
 259      {
 260          $task = $prefix . 'trash';
 261          static::addCustomItem(Text::_('JTOOLBAR_TRASH'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
 262      }
 263  
 264      /**
 265       * Append an untrash item to the current dropdown menu
 266       *
 267       * @param   string  $checkboxId  ID of corresponding checkbox of the record
 268       * @param   string  $prefix      The task prefix
 269       *
 270       * @return  void
 271       *
 272       * @since   3.0
 273       */
 274      public static function untrash($checkboxId, $prefix = '')
 275      {
 276          $task = $prefix . 'publish';
 277          static::addCustomItem(Text::_('JTOOLBAR_UNTRASH'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
 278      }
 279  
 280      /**
 281       * Append a checkin item to the current dropdown menu
 282       *
 283       * @param   string  $checkboxId  ID of corresponding checkbox of the record
 284       * @param   string  $prefix      The task prefix
 285       *
 286       * @return  void
 287       *
 288       * @since   3.0
 289       */
 290      public static function checkin($checkboxId, $prefix = '')
 291      {
 292          $task = $prefix . 'checkin';
 293          static::addCustomItem(Text::_('JTOOLBAR_CHECKIN'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
 294      }
 295  
 296      /**
 297       * Writes a divider between dropdown items
 298       *
 299       * @return  void
 300       *
 301       * @since   3.0
 302       */
 303      public static function divider()
 304      {
 305          static::$dropDownList .= '<li class="divider"></li>';
 306      }
 307  
 308      /**
 309       * Append a custom item to current dropdown menu
 310       *
 311       * @param   string   $label           The label of item
 312       * @param   string   $link            The link of item
 313       * @param   string   $linkAttributes  Custom link attributes
 314       * @param   string   $className       Class name of item
 315       * @param   boolean  $ajaxLoad        True if using ajax load when item clicked
 316       * @param   string   $jsCallBackFunc  Javascript function name, called when ajax load successfully
 317       *
 318       * @return  void
 319       *
 320       * @since   3.0
 321       */
 322      public static function addCustomItem(
 323          $label,
 324          $link = 'javascript:void(0)',
 325          $linkAttributes = '',
 326          $className = '',
 327          $ajaxLoad = false,
 328          $jsCallBackFunc = null
 329      ) {
 330          static::start();
 331  
 332          if ($ajaxLoad) {
 333              $href = ' href = "javascript:void(0)" onclick="loadAjax(\'' . $link . '\', \'' . $jsCallBackFunc . '\')"';
 334          } else {
 335              $href = ' href = "' . $link . '" ';
 336          }
 337  
 338          $dropDownList = static::$dropDownList;
 339          $dropDownList .= '<li class="' . $className . '"><a ' . $linkAttributes . $href . ' >';
 340          $dropDownList .= $label;
 341          $dropDownList .= '</a></li>';
 342          static::$dropDownList = $dropDownList;
 343      }
 344  }


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