From a2fe2908d3829fa23cecfbc5bbc92843bb8e192e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kr=C3=B6ger?= Date: Wed, 13 Mar 2024 12:09:51 +0100 Subject: [PATCH] Add help from Servershell plugins defined in JavaScript --- .../static/js/servershell/command.js | 32 +++++++++++++++++-- serveradmin/servershell/utils.py | 4 +-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/serveradmin/servershell/static/js/servershell/command.js b/serveradmin/servershell/static/js/servershell/command.js index fc36aa26..e9140379 100644 --- a/serveradmin/servershell/static/js/servershell/command.js +++ b/serveradmin/servershell/static/js/servershell/command.js @@ -172,6 +172,18 @@ function validate_selected(min=1, max=-1) { return true; } +// Serveradmin Servershell plugins can provide help for custom commands by +// extending the servershell.commands_help array like in the example below in +// their static/js/$app.servershell.plugin.js +servershell.commands_help = [ + // Example: + // { + // "command": "foo", + // "arguments": "attr_name", + // "description": "A foo that bars", + // } +] + servershell.commands = { pin: function() { let selected_ids = servershell.get_selected(); @@ -770,7 +782,23 @@ function execute_command() { } $(document).ready(function() { - $('#command_form').submit(function(event) { + + // Gather description for custom commands from Servershell plugins and add + // them to the help modal. + servershell.commands_help.forEach(function(command_help) { + let row = document.createElement("tr"); + row.id = `cmd-${command_help["command"]}`; + + for (let index of ["command", "arguments", "description"]) { + let cell = document.createElement("td"); + cell.innerText = command_help[index]; + row.appendChild(cell); + } + + $("#help_command").append(row); + }); + + $('#command_form').submit(function(event) { event.preventDefault(); // Workaround: @@ -786,5 +814,5 @@ $(document).ready(function() { if (execute_command()) { servershell.command = ''; } - }) + }); }); diff --git a/serveradmin/servershell/utils.py b/serveradmin/servershell/utils.py index a860d55f..114b1a79 100644 --- a/serveradmin/servershell/utils.py +++ b/serveradmin/servershell/utils.py @@ -6,8 +6,8 @@ def servershell_plugins(): """Find $app.servershell.plugin.js files from Serveradmin apps Scans all apps beginning with serveradmin_ for files that follow the pattern - $app.servershell.plugin.js and return them. This allows apps to add - commands to the Servershell. + $app.servershell.plugin.js and return them. This allows Serveradmin apps to + extend the Servershell for example with custom commands. @return: """