Keyboard shortcuts: Add method to retrieve info about a particular shortcut

This commit is contained in:
Molkobain
2021-04-13 11:25:40 +02:00
parent 0816d27456
commit 923a4048d3
3 changed files with 45 additions and 12 deletions

View File

@@ -2684,15 +2684,13 @@ HTML;
}
/**
* Return keyboard shortcuts config as an array
*
* @return array
* @return array All keyboard shortcuts config as an array
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MySQLException
* @since 3.0.0
*/
public static function GetKeyboardShortcutPref(): array
public static function GetAllKeyboardShortcutsPrefs(): array
{
$aResultPref = [];
$aShortcutPrefs = appUserPreferences::GetPref('keyboard_shortcuts', []);
@@ -2703,13 +2701,48 @@ HTML;
$sTriggeredElement = $cShortcutPlugin::GetShortcutTriggeredElementSelector();
foreach ($cShortcutPlugin::GetShortcutKeys() as $aShortcutKey) {
$sKey = isset($aShortcutPrefs[$aShortcutKey['id']]) ? $aShortcutPrefs[$aShortcutKey['id']] : $aShortcutKey['key'];
$aResultPref[$aShortcutKey['id']] = ['key' => $sKey, 'label' => $aShortcutKey['label'], 'event' => $aShortcutKey['event'], 'triggered_element_selector' => $sTriggeredElement];
// Format key for display
$aKeyParts = explode('+', $sKey);
$aFormattedKeyParts = [];
foreach ($aKeyParts as $sKeyPart) {
$aFormattedKeyParts[] = ucfirst(trim($sKeyPart));
}
$sFormattedKey = implode(' + ', $aFormattedKeyParts);
$aResultPref[$aShortcutKey['id']] = [
'key' => $sKey,
'key_for_display' => $sFormattedKey,
'label' => $aShortcutKey['label'],
'event' => $aShortcutKey['event'],
'triggered_element_selector' => $sTriggeredElement,
];
}
}
return $aResultPref;
}
/**
* @param string $sShortcutId
*
* @return array The properties of the $sShortcutId shorcut
* @throws \Exception
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MySQLException
* @since 3.0.0
*/
public static function GetKeyboardShortcutPref(string $sShortcutId): array
{
$aPrefs = static::GetAllKeyboardShortcutsPrefs();
if (false === array_key_exists($sShortcutId, $aPrefs)) {
throw new Exception('No shortcut identified as "'.$sShortcutId.'" is currently handled by the application.');
}
return $aPrefs[$sShortcutId];
}
//----------------------------------------------
// Environment helpers
//----------------------------------------------

View File

@@ -338,14 +338,14 @@ JS
JS
);
// For each existing shortcut keyboard existing in iTop
$aKeyboardShortcuts = utils::GetKeyboardShortcutPref();
$aKeyboardShortcuts = utils::GetAllKeyboardShortcutsPrefs();
$sKeyboardShortcutsInputHint = Dict::S('UI:Preferences:PersonalizeKeyboardShortcuts:Input:Hint');
$sKeyboardShortcutsButtonTooltip = Dict::S('UI:Preferences:PersonalizeKeyboardShortcuts:Button:Tooltip');
foreach($aKeyboardShortcuts as $sKeyboardShortcutId => $aKeyboardShortcut){
// Recording button
$oButton = ButtonUIBlockFactory::MakeForAlternativeSecondaryAction('');
$oButton->SetIconClass('fas fa-pen')->SetTooltip($sKeyboardShortcutsButtonTooltip)->SetOnClickJsCode(
<<<JS
foreach ($aKeyboardShortcuts as $sKeyboardShortcutId => $aKeyboardShortcut) {
// Recording button
$oButton = ButtonUIBlockFactory::MakeForAlternativeSecondaryAction('');
$oButton->SetIconClass('fas fa-pen')->SetTooltip($sKeyboardShortcutsButtonTooltip)->SetOnClickJsCode(
<<<JS
let oPanel = $(this).siblings('input');
var fCallback = function(sVal){
oPanel.removeClass('ibo-is-focus').val(sVal);

View File

@@ -185,7 +185,7 @@ class iTopWebPage extends NiceWebPage implements iTabbedPage
*/
protected function InitializeKeyboardShortcuts(): void
{
$aShortcuts = utils::GetKeyboardShortcutPref();
$aShortcuts = utils::GetAllKeyboardShortcutsPrefs();
$sShortcuts = json_encode($aShortcuts);
$this->add_script("aKeyboardShortcuts = $sShortcuts;");
}