[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/system/shortcut/src/Extension/ -> Shortcut.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  System.shortcut
   6   *
   7   * @copyright   (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Plugin\System\Shortcut\Extension;
  12  
  13  use Joomla\CMS\Event\GenericEvent;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\Plugin\CMSPlugin;
  16  use Joomla\CMS\Router\Route;
  17  use Joomla\CMS\Uri\Uri;
  18  use Joomla\Event\Event;
  19  use Joomla\Event\SubscriberInterface;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Shortcut plugin to add accessible keyboard shortcuts to the administrator templates.
  27   *
  28   * @since  4.2.0
  29   */
  30  final class Shortcut extends CMSPlugin implements SubscriberInterface
  31  {
  32      /**
  33       * Load the language file on instantiation.
  34       *
  35       * @var    boolean
  36       * @since  4.2.0
  37       */
  38      protected $autoloadLanguage = true;
  39  
  40      /**
  41       * Returns an array of events this subscriber will listen to.
  42       *
  43       * The array keys are event names and the value can be:
  44       *
  45       *  - The method name to call (priority defaults to 0)
  46       *  - An array composed of the method name to call and the priority
  47       *
  48       * For instance:
  49       *
  50       *  * array('eventName' => 'methodName')
  51       *  * array('eventName' => array('methodName', $priority))
  52       *
  53       * @return  array
  54       *
  55       * @since   4.2.0
  56       */
  57      public static function getSubscribedEvents(): array
  58      {
  59          return [
  60              'onBeforeCompileHead' => 'initialize',
  61              'onLoadShortcuts'     => 'addShortcuts',
  62          ];
  63      }
  64  
  65      /**
  66       * Add the javascript for the shortcuts
  67       *
  68       * @return  void
  69       *
  70       * @since   4.2.0
  71       */
  72      public function initialize()
  73      {
  74          if (!$this->getApplication()->isClient('administrator')) {
  75              return;
  76          }
  77  
  78          $context = $this->getApplication()->input->get('option') . '.' . $this->getApplication()->input->get('view');
  79  
  80          $shortcuts = [];
  81  
  82          $event = new GenericEvent(
  83              'onLoadShortcuts',
  84              [
  85                  'context'   => $context,
  86                  'shortcuts' => $shortcuts,
  87              ]
  88          );
  89  
  90          $this->getDispatcher()->dispatch('onLoadShortcuts', $event);
  91  
  92          $shortcuts = $event->getArgument('shortcuts');
  93  
  94          Text::script('PLG_SYSTEM_SHORTCUT_OVERVIEW_HINT');
  95          Text::script('PLG_SYSTEM_SHORTCUT_OVERVIEW_TITLE');
  96          Text::script('PLG_SYSTEM_SHORTCUT_OVERVIEW_DESC');
  97          Text::script('JCLOSE');
  98  
  99          $document = $this->getApplication()->getDocument();
 100          $wa       = $document->getWebAssetManager();
 101          $wa->useScript('bootstrap.modal');
 102          $wa->registerAndUseScript('script', 'plg_system_shortcut/shortcut.min.js', ['dependencies' => ['hotkeysjs']]);
 103  
 104          $timeout = $this->params->get('timeout', 2000);
 105  
 106          $document->addScriptOptions('plg_system_shortcut.shortcuts', $shortcuts);
 107          $document->addScriptOptions('plg_system_shortcut.timeout', $timeout);
 108      }
 109  
 110      /**
 111       * Add default shortcuts to the document
 112       *
 113       * @param   Event  $event  The event
 114       *
 115       * @return  void
 116       *
 117       * @since   4.2.0
 118       */
 119      public function addShortcuts(Event $event)
 120      {
 121          $shortcuts = $event->getArgument('shortcuts', []);
 122  
 123          $shortcuts = array_merge(
 124              $shortcuts,
 125              [
 126                  'applyKey'   => (object) ['selector' => 'joomla-toolbar-button .button-apply', 'shortcut' => 'A', 'title' => Text::_('JAPPLY')],
 127                  'saveKey'    => (object) ['selector' => 'joomla-toolbar-button .button-save', 'shortcut' => 'S', 'title' => Text::_('JTOOLBAR_SAVE')],
 128                  'cancelKey'  => (object) ['selector' => 'joomla-toolbar-button .button-cancel', 'shortcut' => 'Q', 'title' => Text::_('JCANCEL')],
 129                  'newKey'     => (object) ['selector' => 'joomla-toolbar-button .button-new', 'shortcut' => 'N', 'title' => Text::_('JTOOLBAR_NEW')],
 130                  'searchKey'  => (object) ['selector' => 'input[placeholder=' . Text::_('JSEARCH_FILTER') . ']', 'shortcut' => 'F', 'title' => Text::_('JSEARCH_FILTER')],
 131                  'optionKey'  => (object) ['selector' => 'joomla-toolbar-button .button-options', 'shortcut' => 'O', 'title' => Text::_('JOPTIONS')],
 132                  'helpKey'    => (object) ['selector' => 'joomla-toolbar-button .button-help', 'shortcut' => 'H', 'title' => Text::_('JHELP')],
 133                  'toggleMenu' => (object) ['selector' => '#menu-collapse', 'shortcut' => 'M', 'title' => Text::_('JTOGGLE_SIDEBAR_MENU')],
 134                  'dashboard'  => (object) ['selector' => (string) new Uri(Route::_('index.php?')), 'shortcut' => 'D', 'title' => Text::_('JHOMEDASHBOARD')],
 135              ]
 136          );
 137  
 138          $event->setArgument('shortcuts', $shortcuts);
 139      }
 140  }


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