Skip to content

Commit 40e3e85

Browse files
ufal/fe-fix-static-page-redirect (DSpace#421)
* Fixed static page redirects. * Refactored processing links.
1 parent 8637755 commit 40e3e85

2 files changed

Lines changed: 71 additions & 19 deletions

File tree

src/app/static-page/static-page-routing-paths.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
*/
44
export const STATIC_PAGE_PATH = 'static';
55
export const STATIC_FILES_PROJECT_PATH = 'static-files';
6+
export const HTML_SUFFIX = '.html';
67
export const STATIC_FILES_DEFAULT_ERROR_PAGE_PATH = STATIC_FILES_PROJECT_PATH + '/' + 'error.html';

src/app/static-page/static-page.component.ts

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { BehaviorSubject, firstValueFrom } from 'rxjs';
44
import { Router } from '@angular/router';
55
import { isEmpty, isNotEmpty } from '../shared/empty.util';
66
import { LocaleService } from '../core/locale/locale.service';
7-
import { STATIC_FILES_DEFAULT_ERROR_PAGE_PATH, STATIC_FILES_PROJECT_PATH } from './static-page-routing-paths';
7+
import {
8+
HTML_SUFFIX,
9+
STATIC_FILES_DEFAULT_ERROR_PAGE_PATH,
10+
STATIC_FILES_PROJECT_PATH, STATIC_PAGE_PATH
11+
} from './static-page-routing-paths';
812
import { APP_CONFIG, AppConfig } from '../../config/app-config.interface';
913

1014
/**
@@ -39,6 +43,8 @@ export class StaticPageComponent implements OnInit {
3943
// Compose url
4044
url = STATIC_FILES_PROJECT_PATH;
4145
url += isEmpty(language) ? '/' + this.htmlFileName : '/' + language + '/' + this.htmlFileName;
46+
// Add `.html` suffix to get the current html file
47+
url = url.endsWith(HTML_SUFFIX) ? url : url + HTML_SUFFIX;
4248
let potentialContent = await firstValueFrom(this.htmlContentService.fetchHtmlContent(url));
4349
if (isNotEmpty(potentialContent)) {
4450
this.htmlContent.next(potentialContent);
@@ -57,25 +63,70 @@ export class StaticPageComponent implements OnInit {
5763
await this.loadErrorPage();
5864
}
5965

60-
processLinks(e) {
61-
const element: HTMLElement = e.target;
62-
if (element.nodeName === 'A') {
63-
e.preventDefault();
64-
const href = element.getAttribute('href')?.replace('/', '');
65-
let redirectUrl = window.location.origin + this.appConfig.ui.nameSpace + '/static/';
66-
// Start with `#` - redirect to the fragment
67-
if (href.startsWith('#')) {
68-
redirectUrl += this.htmlFileName + href;
69-
} else if (href.startsWith('.')) {
70-
// Redirect using namespace e.g. `./test.html` -> `<UI_PATH>/namespace/static/test.html`
71-
redirectUrl += href.replace('.', '') + '.html';
72-
} else {
73-
// Redirect without using namespace e.g. `/test.html` -> `<UI_PATH>/test.html`
74-
redirectUrl = redirectUrl.replace(this.appConfig.ui.nameSpace, '') + href;
75-
}
76-
// Call redirect
77-
window.location.href = redirectUrl;
66+
/**
67+
* Handle click on links in the static page.
68+
* @param event
69+
*/
70+
processLinks(event: Event): void {
71+
const targetElement = event.target as HTMLElement;
72+
73+
if (targetElement.nodeName !== 'A') {
74+
return;
7875
}
76+
77+
event.preventDefault();
78+
79+
const href = targetElement.getAttribute('href');
80+
const { nameSpace } = this.appConfig.ui;
81+
const namespacePrefix = nameSpace === '/' ? '' : nameSpace;
82+
83+
const redirectUrl = this.composeRedirectUrl(href, namespacePrefix);
84+
85+
if (this.isFragmentLink(href)) {
86+
this.redirectToFragment(redirectUrl, href);
87+
} else if (this.isRelativeLink(href)) {
88+
this.redirectToRelativeLink(redirectUrl, href);
89+
} else if (this.isExternalLink(href)) {
90+
this.redirectToExternalLink(href);
91+
} else {
92+
this.redirectToAbsoluteLink(redirectUrl, href, namespacePrefix);
93+
}
94+
}
95+
96+
private composeRedirectUrl(href: string | null, namespacePrefix: string): string {
97+
const staticPagePath = STATIC_PAGE_PATH;
98+
const baseUrl = new URL(window.location.origin);
99+
baseUrl.pathname = `${namespacePrefix}/${staticPagePath}/`;
100+
return baseUrl.href;
101+
}
102+
103+
private isFragmentLink(href: string | null): boolean {
104+
return href?.startsWith('#') ?? false;
105+
}
106+
107+
private redirectToFragment(redirectUrl: string, href: string | null): void {
108+
window.location.href = `${redirectUrl}${this.htmlFileName}${href}`;
109+
}
110+
111+
private isRelativeLink(href: string | null): boolean {
112+
return href?.startsWith('.') ?? false;
113+
}
114+
115+
private redirectToRelativeLink(redirectUrl: string, href: string | null): void {
116+
window.location.href = new URL(href, redirectUrl).href;
117+
}
118+
119+
private isExternalLink(href: string | null): boolean {
120+
return (href?.startsWith('http') || href?.startsWith('www')) ?? false;
121+
}
122+
123+
private redirectToExternalLink(href: string | null): void {
124+
window.location.replace(href);
125+
}
126+
127+
private redirectToAbsoluteLink(redirectUrl: string, href: string | null, namespacePrefix: string): void {
128+
const absoluteUrl = new URL(href, redirectUrl.replace(namespacePrefix, ''));
129+
window.location.href = absoluteUrl.href;
79130
}
80131

81132
/**

0 commit comments

Comments
 (0)