From eec24ccd3537da25cdcb949ce2d108394d11fd96 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 21 Aug 2023 13:22:45 +0200 Subject: [PATCH] Fix quota links in files Signed-off-by: jld3103 --- js/quota-files-sidebar.js | 12 ------ lib/BeforeTemplateRenderedListener.php | 58 ++++++++++++++++---------- package-lock.json | 15 +++++++ package.json | 1 + src/quota-files-sidebar.js | 33 +++++++++++++++ webpack.config.js | 1 + 6 files changed, 87 insertions(+), 33 deletions(-) delete mode 100644 js/quota-files-sidebar.js create mode 100644 src/quota-files-sidebar.js diff --git a/js/quota-files-sidebar.js b/js/quota-files-sidebar.js deleted file mode 100644 index bfb00ade..00000000 --- a/js/quota-files-sidebar.js +++ /dev/null @@ -1,12 +0,0 @@ -$(document).ready(function () { - - var $p = document.createElement('p'), - $quotaLink = $('#external_quota_link'), - $quotaName = $('#external_quota_name'); - $p.innerText = $quotaName.val(); - $p.setAttribute('style', 'margin-top: -22px'); - $('li#quota').on('click', function() { - OC.redirect($quotaLink.val()); - }); - document.getElementById('quota').getElementsByTagName('a')[0].appendChild($p); -}); diff --git a/lib/BeforeTemplateRenderedListener.php b/lib/BeforeTemplateRenderedListener.php index 0989403b..00065193 100644 --- a/lib/BeforeTemplateRenderedListener.php +++ b/lib/BeforeTemplateRenderedListener.php @@ -25,6 +25,7 @@ use OCA\Files\Event\LoadAdditionalScriptsEvent; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; +use OCP\AppFramework\Services\IInitialState; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\INavigationManager; @@ -42,12 +43,19 @@ class BeforeTemplateRenderedListener implements IEventListener { /** @var IURLGenerator */ protected $urlGenerator; - public function __construct(SitesManager $sitesManager, + /** @var IInitialState */ + private $initialState; + + public function __construct( + SitesManager $sitesManager, INavigationManager $navigationManager, - IURLGenerator $urlGenerator) { + IURLGenerator $urlGenerator, + IInitialState $initialState, + ) { $this->sitesManager = $sitesManager; $this->navigationManager = $navigationManager; $this->urlGenerator = $urlGenerator; + $this->initialState = $initialState; } public function handle(Event $event): void { @@ -63,20 +71,36 @@ public function handle(Event $event): void { protected function loadQuotaInformationOnFilesApp(LoadAdditionalScriptsEvent $event): void { $sites = $this->sitesManager->getSitesToDisplay(); + $data = []; foreach ($sites as $site) { if ($site['type'] === SitesManager::TYPE_QUOTA) { - $link = $site['url']; - if (!$site['redirect']) { - $link = $this->urlGenerator->linkToRoute('external.site.showPage', ['id' => $site['id']]); - } - - $event->addHiddenField('external_quota_link', $link); - $event->addHiddenField('external_quota_name', $site['name']); + $image = $this->generateImageLink($site); + $href = $this->getHref($site); - Util::addScript('external', 'quota-files-sidebar'); - return; + $data[] = ['name' => $site['name'], 'href' => $href, 'image' => $image]; } } + + if (count($data) > 0) { + $this->initialState->provideInitialState('external-quota-sites', $data); + Util::addScript('external', 'dist/quota-files-sidebar'); + } + } + + protected function generateImageLink(array $site): string { + if ($site['icon'] !== '') { + return $this->urlGenerator->linkToRoute('external.icon.showIcon', ['icon' => $site['icon']]); + } + + return $this->urlGenerator->linkToRoute('external.icon.showIcon', ['icon' => 'external.svg']); + } + + protected function getHref(array $site): string { + if (!$site['redirect']) { + return $this->urlGenerator->linkToRoute('external.site.showPage', ['id' => $site['id']]); + } + + return $site['url']; } protected function generateNavigationLinks(): void { @@ -88,16 +112,8 @@ protected function generateNavigationLinks(): void { } $this->navigationManager->add(function () use ($site) { - if ($site['icon'] !== '') { - $image = $this->urlGenerator->linkToRoute('external.icon.showIcon', ['icon' => $site['icon']]); - } else { - $image = $this->urlGenerator->linkToRoute('external.icon.showIcon', ['icon' => 'external.svg']); - } - - $href = $site['url']; - if (!$site['redirect']) { - $href = $this->urlGenerator->linkToRoute('external.site.showPage', ['id' => $site['id']]); - } + $image = $this->generateImageLink($site); + $href = $this->getHref($site); return [ 'id' => 'external_index' . $site['id'], diff --git a/package-lock.json b/package-lock.json index 406b39a8..9a582be1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "4.2.0", "license": "AGPL-3.0-or-later", "dependencies": { + "@nextcloud/initial-state": "^2.1.0", "@nextcloud/router": "^2.1.2", "escape-html": "^1.0.3" }, @@ -2771,6 +2772,15 @@ "eslint": ">=7.0.0" } }, + "node_modules/@nextcloud/initial-state": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@nextcloud/initial-state/-/initial-state-2.1.0.tgz", + "integrity": "sha512-b92X/GvUPGQJpUQwauyG3D3dHsWowViVLnTtFPSMUc0rXtvYR5CvhkqJRfPC7O7W4VC7+V3q+FWeA+mQWMxN2Q==", + "engines": { + "node": "^20.0.0", + "npm": "^9.0.0" + } + }, "node_modules/@nextcloud/router": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/@nextcloud/router/-/router-2.1.2.tgz", @@ -12131,6 +12141,11 @@ "requireindex": "^1.2.0" } }, + "@nextcloud/initial-state": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@nextcloud/initial-state/-/initial-state-2.1.0.tgz", + "integrity": "sha512-b92X/GvUPGQJpUQwauyG3D3dHsWowViVLnTtFPSMUc0rXtvYR5CvhkqJRfPC7O7W4VC7+V3q+FWeA+mQWMxN2Q==" + }, "@nextcloud/router": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/@nextcloud/router/-/router-2.1.2.tgz", diff --git a/package.json b/package.json index ef7aa999..965a0aaf 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "watch": "NODE_ENV=development webpack --progress --watch --config webpack.config.js" }, "dependencies": { + "@nextcloud/initial-state": "^2.1.0", "@nextcloud/router": "^2.1.2", "escape-html": "^1.0.3" }, diff --git a/src/quota-files-sidebar.js b/src/quota-files-sidebar.js new file mode 100644 index 00000000..6b7295ee --- /dev/null +++ b/src/quota-files-sidebar.js @@ -0,0 +1,33 @@ +import {loadState} from '@nextcloud/initial-state' + +document.addEventListener('DOMContentLoaded', () => { + const state = loadState('external', 'external-quota-sites') + + for (const site of state) { + const image = document.createElement('img') + image.src = site.image + + const icon = document.createElement('div') + icon.classList.add('app-navigation-entry-icon') + icon.append(image) + + const name = document.createElement('span') + name.classList.add('app-navigation-entry__name') + name.innerText = site.name + + const a = document.createElement('a') + a.classList.add('app-navigation-entry-link') + a.href = site.href + a.append(icon) + a.append(name) + + const div = document.createElement('div') + div.classList.add('app-navigation-entry') + div.append(a) + + const li = document.createElement('li') + li.append(div) + + document.getElementsByClassName('app-navigation-entry__settings')[0].prepend(li) + } +}) diff --git a/webpack.config.js b/webpack.config.js index bd08ae20..24a29789 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -3,6 +3,7 @@ const path = require('path') module.exports = { entry: { admin: path.join(__dirname, 'src', 'admin.js'), + 'quota-files-sidebar': path.join(__dirname, 'src', 'quota-files-sidebar.js'), }, output: { path: path.join(__dirname, 'js', 'dist'),