From 0ca420c9499f9a0a24afc9fb9f516db009b7c794 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 20 Nov 2021 04:46:45 -0500 Subject: [PATCH] [frio] Make addon settings panels keyboard actionable - Keep the addon panel open after form was submitted --- doc/Addons.md | 52 ++++++++++++++++++- mod/settings.php | 25 +++++++-- view/templates/settings/addon/link.tpl | 1 + view/templates/settings/addon/panel.tpl | 33 ++++++++++++ view/templates/settings/addons.tpl | 4 +- .../frio/templates/settings/addon/link.tpl | 1 + .../frio/templates/settings/addon/panel.tpl | 36 +++++++++++++ view/theme/frio/templates/settings/addons.tpl | 18 +++---- 8 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 view/templates/settings/addon/link.tpl create mode 100644 view/templates/settings/addon/panel.tpl create mode 100644 view/theme/frio/templates/settings/addon/link.tpl create mode 100644 view/theme/frio/templates/settings/addon/panel.tpl diff --git a/doc/Addons.md b/doc/Addons.md index 708a2e0f0..cf4fbdab1 100644 --- a/doc/Addons.md +++ b/doc/Addons.md @@ -242,7 +242,57 @@ Called when the Settings pages are submitted. ### addon_settings Called when generating the HTML for the addon settings page. -`$b` is the (string) HTML of the addon settings page before the final `` tag. +`$data` is an array containing: + +- **addon** (output): Required. The addon folder name. +- **title** (output): Required. The addon settings panel title. +- **href** (output): Optional. If set, will reduce the panel to a link pointing to this URL, can be relative. Incompatible with the following keys. +- **html** (output): Optional. Raw HTML of the addon form elements. Both the `
` tags and the submit buttons are taken care of elsewhere. +- **submit** (output): Optional. If unset, a default submit button with `name="-submit"` will be generated. + Can take different value types: + - **string**: The label to replace the default one. + - **associative array**: A list of submit button, the key is the value of the `name` attribute, the value is the displayed label. + The first submit button in this list is considered the main one and themes might emphasize its display. + +#### Examples + +##### With link +```php +$data = [ + 'addon' => 'advancedcontentfilter', + 'title' => DI::l10n()->t('Advanced Content Filter'), + 'href' => 'advancedcontentfilter', +]; +``` +##### With default submit button +```php +$data = [ + 'addon' => 'fromapp', + 'title' => DI::l10n()->t('FromApp Settings'), + 'html' => $html, +]; +``` +##### With no HTML, just a submit button +```php +$data = [ + 'addon' => 'opmlexport', + 'title' => DI::l10n()->t('OPML Export'), + 'submit' => DI::l10n()->t('Export RSS/Atom contacts'), +]; +``` +##### With multiple submit buttons +```php +$data = [ + 'addon' => 'catavar', + 'title' => DI::l10n()->t('Cat Avatar Settings'), + 'html' => $html, + 'submit' => [ + 'catavatar-usecat' => DI::l10n()->t('Use Cat as Avatar'), + 'catavatar-morecat' => DI::l10n()->t('Another random Cat!'), + 'catavatar-emailcat' => DI::pConfig()->get(local_user(), 'catavatar', 'seed', false) ? DI::l10n()->t('Reset to email Cat') : null, + ], +]; +``` ### addon_settings_post Called when the Addon Settings pages are submitted. diff --git a/mod/settings.php b/mod/settings.php index 1643b19fc..54484d8a9 100644 --- a/mod/settings.php +++ b/mod/settings.php @@ -62,9 +62,10 @@ function settings_post(App $a) } if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] == 'addon')) { - BaseModule::checkFormSecurityTokenRedirectOnError('/settings/addon', 'settings_addon'); + BaseModule::checkFormSecurityTokenRedirectOnError(DI::args()->getQueryString(), 'settings_addon'); Hook::callAll('addon_settings_post', $_POST); + DI::baseUrl()->redirect(DI::args()->getQueryString()); return; } @@ -438,11 +439,27 @@ function settings_content(App $a) if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] === 'addon')) { $addon_settings_forms = []; - foreach (DI::dba()->selectToArray('hook', ['file', 'function'], ['hook' => 'addon_settings']) as $hook) { - $data = ''; + $data = []; Hook::callSingle(DI::app(), 'addon_settings', [$hook['file'], $hook['function']], $data); - $addon_settings_forms[] = $data; + + if (!empty($data['href'])) { + $tpl = Renderer::getMarkupTemplate('settings/addon/link.tpl'); + $addon_settings_forms[] = Renderer::replaceMacros($tpl, [ + '$addon' => $data['addon'], + '$title' => $data['title'], + '$href' => $data['href'], + ]); + } elseif(!empty($data['addon'])) { + $tpl = Renderer::getMarkupTemplate('settings/addon/panel.tpl'); + $addon_settings_forms[$data['addon']] = Renderer::replaceMacros($tpl, [ + '$addon' => $data['addon'], + '$title' => $data['title'], + '$open' => (DI::args()->getArgv()[2] ?? '') === $data['addon'], + '$html' => $data['html'] ?? '', + '$submit' => $data['submit'] ?? DI::l10n()->t('Save Settings'), + ]); + } } $tpl = Renderer::getMarkupTemplate('settings/addons.tpl'); diff --git a/view/templates/settings/addon/link.tpl b/view/templates/settings/addon/link.tpl new file mode 100644 index 000000000..4fb8178cc --- /dev/null +++ b/view/templates/settings/addon/link.tpl @@ -0,0 +1 @@ +

{{$title}}

\ No newline at end of file diff --git a/view/templates/settings/addon/panel.tpl b/view/templates/settings/addon/panel.tpl new file mode 100644 index 000000000..f401f0123 --- /dev/null +++ b/view/templates/settings/addon/panel.tpl @@ -0,0 +1,33 @@ + +

{{$title}}

+
+
+ +

{{$title}}

+
+ {{$html nofilter}} +{{if $submit}} + +{{/if}} +
diff --git a/view/templates/settings/addons.tpl b/view/templates/settings/addons.tpl index e30732d17..03313c924 100644 --- a/view/templates/settings/addons.tpl +++ b/view/templates/settings/addons.tpl @@ -1,8 +1,8 @@

{{$title}}

-{{foreach $addon_settings_forms as $addon_settings_form}} +{{foreach $addon_settings_forms as $addon => $addon_settings_form}} - + {{$addon_settings_form nofilter}} diff --git a/view/theme/frio/templates/settings/addon/link.tpl b/view/theme/frio/templates/settings/addon/link.tpl new file mode 100644 index 000000000..bf430db46 --- /dev/null +++ b/view/theme/frio/templates/settings/addon/link.tpl @@ -0,0 +1 @@ + diff --git a/view/theme/frio/templates/settings/addon/panel.tpl b/view/theme/frio/templates/settings/addon/panel.tpl new file mode 100644 index 000000000..561d0fc9f --- /dev/null +++ b/view/theme/frio/templates/settings/addon/panel.tpl @@ -0,0 +1,36 @@ + +
+
+ {{$html nofilter}} +
+ +
diff --git a/view/theme/frio/templates/settings/addons.tpl b/view/theme/frio/templates/settings/addons.tpl index f6e2d6108..f150303ea 100644 --- a/view/theme/frio/templates/settings/addons.tpl +++ b/view/theme/frio/templates/settings/addons.tpl @@ -2,17 +2,15 @@ {{* include the title template for the settings title *}} {{include file="section_title.tpl" title=$title}} -{{foreach $addon_settings_forms as $addon_settings_form}} - -
- - {{$addon_settings_form nofilter}} -
- +
+{{foreach $addon_settings_forms as $addon => $addon_settings_form}} +
+ + {{$addon_settings_form nofilter}} +
{{foreachelse}} - - - + {{/foreach}} +
\ No newline at end of file