From 3ebe05e1b6dcc01403e070b2b4a68a5ef9d9b537 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 12 May 2023 16:35:56 +0200 Subject: [PATCH] feat(settings): Migrate admin settings to vue Signed-off-by: Joas Schilling --- lib/Settings/Admin.php | 26 +++++--- src/AdminSettings.vue | 137 +++++++++++++++++++++++++++++++++++++++++ src/admin.js | 40 ++++++++---- templates/admin.php | 75 +--------------------- 4 files changed, 182 insertions(+), 96 deletions(-) create mode 100644 src/AdminSettings.vue diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index 4c3a9992..4de53ef7 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -26,31 +26,37 @@ namespace OCA\AnnouncementCenter\Settings; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Services\IInitialState; use OCP\IConfig; use OCP\Settings\ISettings; +use OCP\Util; class Admin implements ISettings { /** @var IConfig */ protected $config; + /** @var IInitialState */ + protected $initialState; - public function __construct(IConfig $config) { + public function __construct(IConfig $config, IInitialState $initialState) { $this->config = $config; + $this->initialState = $initialState; } /** * @return TemplateResponse */ public function getForm(): TemplateResponse { - $adminGroups = $this->config->getAppValue('announcementcenter', 'admin_groups', '["admin"]'); - $adminGroups = implode('|', json_decode($adminGroups, true)); - return new TemplateResponse('announcementcenter', 'admin', [ - 'adminGroups' => $adminGroups, - 'createActivities' => $this->config->getAppValue('announcementcenter', 'create_activities', 'yes') === 'yes', - 'createNotifications' => $this->config->getAppValue('announcementcenter', 'create_notifications', 'yes') === 'yes', - 'sendEmails' => $this->config->getAppValue('announcementcenter', 'send_emails', 'yes') === 'yes', - 'allowComments' => $this->config->getAppValue('announcementcenter', 'allow_comments', 'yes') === 'yes', - ], 'blank'); + $adminGroups = json_decode($this->config->getAppValue('announcementcenter', 'admin_groups', '["admin"]'), true); + + $this->initialState->provideInitialState('admin_groups', $adminGroups); + $this->initialState->provideInitialState('create_activities', $this->config->getAppValue('announcementcenter', 'create_activities', 'yes') === 'yes'); + $this->initialState->provideInitialState('create_notifications', $this->config->getAppValue('announcementcenter', 'create_notifications', 'yes') === 'yes'); + $this->initialState->provideInitialState('send_emails', $this->config->getAppValue('announcementcenter', 'send_emails', 'yes') === 'yes'); + $this->initialState->provideInitialState('allow_comments', $this->config->getAppValue('announcementcenter', 'allow_comments', 'yes') === 'yes'); + + Util::addScript('announcementcenter', 'announcementcenter-admin'); + return new TemplateResponse('announcementcenter', 'admin', [], TemplateResponse::RENDER_AS_BLANK); } /** diff --git a/src/AdminSettings.vue b/src/AdminSettings.vue new file mode 100644 index 00000000..cbbb8a73 --- /dev/null +++ b/src/AdminSettings.vue @@ -0,0 +1,137 @@ + + + + + + + diff --git a/src/admin.js b/src/admin.js index 413fd6b8..f5f9d8a6 100644 --- a/src/admin.js +++ b/src/admin.js @@ -20,18 +20,34 @@ * */ -$(document).ready(function() { - const $announcementCenter = $('#announcementcenter') - const $adminGroup = $announcementCenter.find('.admin_groups') +import Vue from 'vue' +import { generateFilePath } from '@nextcloud/router' +import { getRequestToken } from '@nextcloud/auth' +import { translate, translatePlural } from '@nextcloud/l10n' +import store from './store/index.js' +import AdminSettings from './AdminSettings.vue' +import Vuex from 'vuex' - OC.Settings.setupGroupsSelect($adminGroup) - $adminGroup.change(function(event) { - let groups = event.val || ['admin'] - groups = JSON.stringify(groups) - OCP.AppConfig.setValue('announcementcenter', 'admin_groups', groups) - }) +// Styles +import '@nextcloud/dialogs/dist/index.css' - $announcementCenter.find('.checkbox').change(function() { - OCP.AppConfig.setValue('announcementcenter', $(this).attr('name'), (this.checked ? 'yes' : 'no')) - }) +// eslint-disable-next-line +__webpack_nonce__ = btoa(getRequestToken()) + +// eslint-disable-next-line +__webpack_public_path__ = generateFilePath('announcementcenter', '', 'js/') + +Vue.use(Vuex) + +Vue.mixin({ + methods: { + t: translate, + n: translatePlural, + }, +}) + +export default new Vue({ + el: '#announcementcenter', + store, + render: h => h(AdminSettings), }) diff --git a/templates/admin.php b/templates/admin.php index 5138eb56..8a63b7ef 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -1,74 +1 @@ - - * - * @author Joas Schilling - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -script('announcementcenter', 'announcementcenter-admin'); - -/** @var array $_ */ -/** @var \OCP\IL10N $l */ -?> -
-

t('Announcements')); ?>

- -

- -
- t('These groups will be able to post announcements.')); ?> -

-
- -

- /> -
-

- -

- /> -
-

- -

- /> -
-

- -

- /> -
-

-
+