mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.6 KiB
78 lines
1.6 KiB
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
|
|
*/
|
|
|
|
// tslint:disable: readonly-array
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
export class TitlesConfig {
|
|
constructor(
|
|
public readonly prefix?: string,
|
|
public readonly suffix?: string,
|
|
public readonly separator?: string
|
|
) {
|
|
}
|
|
}
|
|
|
|
export const TitleServiceFactory = (titles: TitlesConfig) => {
|
|
return new TitleService(titles);
|
|
};
|
|
|
|
@Injectable()
|
|
export class TitleService {
|
|
private readonly stack: any[] = [];
|
|
|
|
constructor(private readonly titles: TitlesConfig) {
|
|
this.updateTitle();
|
|
}
|
|
|
|
public push(value: any, previous?: any) {
|
|
if (value) {
|
|
const lastIndex = this.stack.length - 1;
|
|
|
|
if (previous && this.stack[lastIndex] === previous) {
|
|
this.stack[lastIndex] = value;
|
|
} else {
|
|
this.stack.push(value);
|
|
}
|
|
|
|
this.updateTitle();
|
|
}
|
|
}
|
|
|
|
public pop() {
|
|
this.stack.pop();
|
|
|
|
this.updateTitle();
|
|
}
|
|
|
|
private updateTitle() {
|
|
const { prefix, separator, suffix } = this.titles;
|
|
|
|
let title = '';
|
|
|
|
if (this.stack.length > 0) {
|
|
title = this.stack.join(separator || ' | ');
|
|
}
|
|
|
|
if (title) {
|
|
if (prefix) {
|
|
title = `${prefix} - ${title}`;
|
|
}
|
|
|
|
if (suffix) {
|
|
title = `${title} - ${suffix}`;
|
|
}
|
|
} else if (suffix) {
|
|
title = suffix;
|
|
} else if (prefix) {
|
|
title = prefix;
|
|
}
|
|
|
|
document.title = title;
|
|
}
|
|
}
|