Skip to content

Commit

Permalink
Implementof Target_Blank option for External links
Browse files Browse the repository at this point in the history
Signed-off-by: christophe canovas <[email protected]>
  • Loading branch information
ChrisCano66 committed Jun 30, 2021
1 parent ad4c1bd commit e1dab82
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 10 deletions.
4 changes: 4 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
padding: 8px 0;
}

.site-target-box {
padding: 8px 0;
}

#external .icon-more {
width: 16px;
height: 16px;
Expand Down
10 changes: 10 additions & 0 deletions js/templates/site.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@
</label>
</div>

<div class="site-target-box">
<label>
<span>{{targetTXT}}</span>
<input type="checkbox" id="site_target_{{id}}" name="site_target_{{id}}"
value="1" class="site-target checkbox trigger-save" {{#if target}} checked="checked"{{/if}} />
<label for="site_target_{{id}}">{{OpenTabTXT}}</label>
</label>
</div>


<div class="button delete-button">{{removeSiteTXT}}</div>
</div>
</li>
3 changes: 2 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function registerSites(
}

$href = $site['url'];
if (!$site['redirect']) {
if (!$site['redirect'] && !$site['target']) {
$href = $url->linkToRoute('external.site.showPage', ['id'=> $site['id']]);
}

Expand All @@ -93,6 +93,7 @@ public function registerSites(
'icon' => $image,
'type' => $site['type'],
'name' => $site['name'],
'target' => $site['target'],
];
});
}
Expand Down
5 changes: 3 additions & 2 deletions lib/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function loadQuotaInformationOnFilesApp(LoadAdditionalScriptsEvent $ev
foreach ($sites as $site) {
if ($site['type'] === SitesManager::TYPE_QUOTA) {
$link = $site['url'];
if (!$site['redirect']) {
if (!$site['redirect'] && !$site['target']) {
$link = $this->urlGenerator->linkToRoute('external.site.showPage', ['id'=> $site['id']]);
}

Expand Down Expand Up @@ -93,7 +93,7 @@ protected function generateNavigationLinks(): void {
}

$href = $site['url'];
if (!$site['redirect']) {
if (!$site['redirect'] && !$site['target']) {
$href = $this->urlGenerator->linkToRoute('external.site.showPage', ['id'=> $site['id']]);
}

Expand All @@ -104,6 +104,7 @@ protected function generateNavigationLinks(): void {
'icon' => $image,
'type' => $site['type'],
'name' => $site['name'],
'target' => $site['target'],
];
});
}
Expand Down
1 change: 1 addition & 0 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function getCapabilities() {
'device',
'groups',
'redirect',
'target'
],
],
];
Expand Down
13 changes: 9 additions & 4 deletions lib/Controller/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public function get() {

$site['redirect'] = (int) $site['redirect'];

$site['target'] = (int) $site['target'];

unset($site['lang'], $site['device'], $site['groups']);
$sites[] = $site;
}
Expand Down Expand Up @@ -152,11 +154,13 @@ public function getAdmin() {
* @param string $icon
* @param string[] $groups
* @param int $redirect
* @param int $target
* @return DataResponse
*/
public function add($name, $url, $lang, $type, $device, $icon, array $groups, $redirect) {
public function add($name, $url, $lang, $type, $device, $icon, array $groups, $redirect, $target) {

try {
return new DataResponse($this->sitesManager->addSite($name, $url, $lang, $type, $device, $icon, $groups, (bool) $redirect));
return new DataResponse($this->sitesManager->addSite($name, $url, $lang, $type, $device, $icon, $groups, (bool) $redirect, (bool) $target));
} catch (InvalidNameException $e) {
return new DataResponse(['error' => $this->l->t('The given label is invalid'), 'field' => 'name'], Http::STATUS_BAD_REQUEST);
} catch (InvalidURLException $e) {
Expand Down Expand Up @@ -184,11 +188,12 @@ public function add($name, $url, $lang, $type, $device, $icon, array $groups, $r
* @param string $icon
* @param string[] $groups
* @param int $redirect
* @param int $target
* @return DataResponse
*/
public function update($id, $name, $url, $lang, $type, $device, $icon, array $groups, $redirect) {
public function update($id, $name, $url, $lang, $type, $device, $icon, array $groups, $redirect, $target) {
try {
return new DataResponse($this->sitesManager->updateSite($id, $name, $url, $lang, $type, $device, $icon, $groups, (bool) $redirect));
return new DataResponse($this->sitesManager->updateSite($id, $name, $url, $lang, $type, $device, $icon, $groups, (bool) $redirect, (bool) $target));
} catch (SiteNotFoundException $e) {
return new DataResponse(['error' => $this->l->t('The site does not exist'), 'field' => 'site'], Http::STATUS_NOT_FOUND);
} catch (InvalidNameException $e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getForm() {
}

$url = $quotaLink['url'];
if (!$quotaLink['redirect']) {
if (!$quotaLink['redirect'] && !$quotaLink['target']) {
$url = $this->url->linkToRoute('external.site.showPage', ['id'=> $quotaLink['id']]);
}

Expand Down
11 changes: 9 additions & 2 deletions lib/SitesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ protected function fillSiteArray(array $site) {
'device' => self::DEVICE_ALL,
'groups' => [],
'redirect' => false,
'target' => false,
],
$site
);
Expand All @@ -198,6 +199,7 @@ protected function fillSiteArray(array $site) {
* @param string $icon
* @param string[] $groups
* @param bool $redirect
* @param bool $target
* @return array
* @throws InvalidNameException
* @throws InvalidURLException
Expand All @@ -207,7 +209,7 @@ protected function fillSiteArray(array $site) {
* @throws GroupNotFoundException
* @throws IconNotFoundException
*/
public function addSite($name, $url, $lang, $type, $device, $icon, array $groups, $redirect) {
public function addSite($name, $url, $lang, $type, $device, $icon, array $groups, $redirect, $target) {
$id = 1 + (int) $this->config->getAppValue('external', 'max_site', 0);

if ($name === '') {
Expand Down Expand Up @@ -259,6 +261,7 @@ public function addSite($name, $url, $lang, $type, $device, $icon, array $groups

if ($type === self::TYPE_LOGIN) {
$redirect = true;
$target = true;
}

$sites = $this->getSites();
Expand All @@ -272,6 +275,7 @@ public function addSite($name, $url, $lang, $type, $device, $icon, array $groups
'icon' => $icon,
'groups' => $groups,
'redirect' => $redirect,
'target' => $target
];
$this->config->setAppValue('external', 'sites', json_encode($sites));
$this->config->setAppValue('external', 'max_site', $id);
Expand All @@ -289,6 +293,7 @@ public function addSite($name, $url, $lang, $type, $device, $icon, array $groups
* @param string $icon
* @param string[] $groups
* @param bool $redirect
* @param bool $target
* @return array
* @throws SiteNotFoundException
* @throws InvalidNameException
Expand All @@ -299,7 +304,7 @@ public function addSite($name, $url, $lang, $type, $device, $icon, array $groups
* @throws GroupNotFoundException
* @throws IconNotFoundException
*/
public function updateSite($id, $name, $url, $lang, $type, $device, $icon, array $groups, $redirect) {
public function updateSite($id, $name, $url, $lang, $type, $device, $icon, array $groups, $redirect, $target) {
$sites = $this->getSites();
if (!isset($sites[$id])) {
throw new SiteNotFoundException();
Expand Down Expand Up @@ -354,6 +359,7 @@ public function updateSite($id, $name, $url, $lang, $type, $device, $icon, array

if ($type === self::TYPE_LOGIN) {
$redirect = true;
$target = true;
}

$sites[$id] = [
Expand All @@ -366,6 +372,7 @@ public function updateSite($id, $name, $url, $lang, $type, $device, $icon, array
'icon' => $icon,
'groups' => $groups,
'redirect' => $redirect,
'target' => $target
];
$this->config->setAppValue('external', 'sites', json_encode($sites));

Expand Down
6 changes: 6 additions & 0 deletions src/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ import { generateUrl, imagePath, generateOcsUrl } from '@nextcloud/router';
data.iconTXT = t('external', 'Icon')
data.positionTXT = t('external', 'Position')
data.redirectTXT = t('external', 'Redirect')
data.targetTXT = t('external', 'New Tab')
data.OpenTabTXT = t('external', 'External links open in a new tab')
data.removeSiteTXT = t('external', 'Remove site')
data.noEmbedTXT = t('external', 'This site does not allow embedding')
data.deleteIMG = imagePath('core', 'actions/delete.svg')
Expand Down Expand Up @@ -173,6 +175,7 @@ import { generateUrl, imagePath, generateOcsUrl } from '@nextcloud/router';
self._attachEvents($el)
if (site.attributes.type === 'guest') {
$el.find('.site-redirect-box').hide()
$el.find('.site-target-box').hide()
}
self.$list.append($el)
})
Expand Down Expand Up @@ -231,6 +234,7 @@ import { generateUrl, imagePath, generateOcsUrl } from '@nextcloud/router';
type: $site.find('.site-type').val(),
device: $site.find('.site-device').val(),
redirect: $site.find('.site-redirect').prop('checked') ? 1 : 0,
target: $site.find('.site-target').prop('checked') ? 1 : 0,
groups: groups === '' ? [] : groups.split('|'),
icon: $site.find('.site-icon').val(),
}
Expand All @@ -239,8 +243,10 @@ import { generateUrl, imagePath, generateOcsUrl } from '@nextcloud/router';
$site.find('.invalid-value').removeClass('invalid-value')
if (data.type === 'guest') {
$site.find('.site-redirect-box').hide()
$site.find('.site-target-box').hide()
} else {
$site.find('.site-redirect-box').show()
$site.find('.site-target-box').show()
}

if (!_.isUndefined(site)) {
Expand Down

0 comments on commit e1dab82

Please sign in to comment.