Skip to content

Commit

Permalink
service: add new service to personalized title and meta html
Browse files Browse the repository at this point in the history
* Adds service for title and meta html
* Adds new parameter prefixWidow on config
* Implements this service on ng-core-tester

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Dec 13, 2019
1 parent 1eaa9e9 commit fd9eff4
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 6 deletions.
1 change: 1 addition & 0 deletions projects/ng-core-tester/src/app/app-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class AppConfigService extends CoreConfigService {
constructor() {
super();
this.production = false;
this.prefixWindow = environment.prefixWindow;
this.apiBaseUrl = 'https://localhost:5000';
this.schemaFormEndpoint = '/api/schemaform';
this.$refPrefix = environment.$refPrefix;
Expand Down
1 change: 1 addition & 0 deletions projects/ng-core-tester/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const routes: Routes = [
key: 'documents',
label: 'Documents',
component: DocumentComponent,
detailComponent: DetailComponent,
aggregationsOrder: aggrDocumentOrder,
aggregationsExpand: aggrDocumentExpand,
aggregationsBucketSize: aggrBucketSize,
Expand Down
11 changes: 9 additions & 2 deletions projects/ng-core-tester/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { CoreConfigService } from '@rero/ng-core';
import { CoreConfigService, TitleMetaService } from '@rero/ng-core';


@Component({
Expand Down Expand Up @@ -82,7 +82,8 @@ export class AppComponent implements OnInit {

constructor(
private translateService: TranslateService,
private configService: CoreConfigService
private configService: CoreConfigService,
private titleMetaService: TitleMetaService
) {
}

Expand All @@ -97,6 +98,12 @@ export class AppComponent implements OnInit {
}
this.languagesMenu.entries.push(data);
}
// Set default title window when application start
const prefix = this.configService.prefixWindow;
if (prefix) {
this.titleMetaService.setPrefix(prefix);
}
this.titleMetaService.setTitle('Welcome');
}

changeLang(item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';

import { TitleMetaService } from '@rero/ng-core';
import { DetailRecord } from '@rero/ng-core/lib/record/detail/view/detail-record';

@Component({
Expand All @@ -33,7 +34,10 @@ export class DetailComponent implements DetailRecord, OnInit {
/** Record data */
record: any;

constructor(private titleMetaService: TitleMetaService) { }

ngOnInit(): void {
this.titleMetaService.setTitle('Detail of ' + this.type);
this.record$.subscribe((record) => {
this.record = record;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
<!--
 Invenio angular core
 Copyright (C) 2019 RERO

 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, version 3 of the License.

 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 <http://www.gnu.org/licenses/>.
-->
<h5>
<ng-container *ngIf="detailUrl; else titleWithoutLink">
<a [routerLink]="detailUrl.link" *ngIf="detailUrl.external === false; else hrefLink">
Expand All @@ -16,4 +32,4 @@ <h5>
<p>
<span class="badge badge-primary mr-1" *ngFor="let author of record.metadata.authors">{{ author.name }}</span>
</p>
<div *ngIf="record.metadata.abstracts">{{ record.metadata.abstracts[0].value | truncateText:30 }}</div>
<div *ngIf="record.metadata.abstracts">{{ record.metadata.abstracts[0].value | truncateText:30 }}</div>
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
/*
* Invenio angular core
* Copyright (C) 2019 RERO
*
* 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, version 3 of the License.
*
* 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 <http://www.gnu.org/licenses/>.
*/
import { Component, OnInit, Input } from '@angular/core';
import { ResultItem } from '@rero/ng-core';
import { ResultItem, TitleMetaService } from '@rero/ng-core';

@Component({
templateUrl: './document.component.html'
})
export class DocumentComponent implements ResultItem {

export class DocumentComponent implements OnInit, ResultItem {

@Input()
record: any;

Expand All @@ -13,4 +31,17 @@ export class DocumentComponent implements ResultItem {

@Input()
detailUrl: { link: string, external: boolean };

/**
* Constructor
* @param titleMetaService - TitleMetaService
*/
constructor(private titleMetaService: TitleMetaService) { }

/**
* On Init
*/
ngOnInit(): void {
this.titleMetaService.setTitle(this.type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
export const environment = {
production: true,
prefixWindow: 'NG CORE TESTER',
apiBaseUrl: 'https://localhost:5000',
$refPrefix: 'https://sonar.ch',
languages: ['fr', 'de', 'it', 'en'],
Expand Down
1 change: 1 addition & 0 deletions projects/ng-core-tester/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

export const environment = {
production: false,
prefixWindow: 'NG CORE TESTER',
apiBaseUrl: 'https://localhost:5000',
$refPrefix: 'https://sonar.ch',
languages: ['fr', 'de', 'it', 'en'],
Expand Down
2 changes: 2 additions & 0 deletions projects/rero/ng-core/src/lib/core-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Injectable } from '@angular/core';

export interface Config {
production?: boolean;
prefixWindow?: string;
apiBaseUrl?: string;
apiEndpointPrefix?: string;
$refPrefix: string;
Expand All @@ -37,6 +38,7 @@ export interface Config {
})
export class CoreConfigService implements Config {
production = false;
prefixWindow = undefined;
apiBaseUrl = '';
apiEndpointPrefix = '/api';
schemaFormEndpoint = '/api/schemaform';
Expand Down
2 changes: 1 addition & 1 deletion projects/rero/ng-core/src/lib/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ import { TranslateLanguagePipe } from './translate/translate-language.pipe';
],
entryComponents: [DialogComponent]
})
export class CoreModule {}
export class CoreModule { }
53 changes: 53 additions & 0 deletions projects/rero/ng-core/src/lib/service/title-meta.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Invenio angular core
* Copyright (C) 2019 RERO
*
* 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, version 3 of the License.
*
* 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 <http://www.gnu.org/licenses/>.
*/
import { TestBed } from '@angular/core/testing';

import { TitleMetaService } from './title-meta.service';
import { TranslateModule, TranslateLoader, TranslateFakeLoader } from '@ngx-translate/core';

describe('TitleMetaService', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot({
loader: { provide: TranslateLoader, useClass: TranslateFakeLoader }
})
]
}));

it('should be created', () => {
const service: TitleMetaService = TestBed.get(TitleMetaService);
expect(service).toBeTruthy();
});

it('should be set title', () => {
const service: TitleMetaService = TestBed.get(TitleMetaService);
service.setTitle('my title');
expect(service.getTitle()).toEqual('my title');
});

it('should be set title with prefix', () => {
const service: TitleMetaService = TestBed.get(TitleMetaService);
service.setPrefix('my app').setTitle('my title');
expect(service.getTitle()).toEqual('my app: my title');
});

it('should be set title', () => {
const service: TitleMetaService = TestBed.get(TitleMetaService);
service.setMeta('description', 'my description');
expect(service.getMeta('name=description').content).toEqual('my description');
});
});
95 changes: 95 additions & 0 deletions projects/rero/ng-core/src/lib/service/title-meta.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Invenio angular core
* Copyright (C) 2019 RERO
*
* 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, version 3 of the License.
*
* 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 <http://www.gnu.org/licenses/>.
*/
import { Injectable } from '@angular/core';
import { Title, Meta } from '@angular/platform-browser';
import { TranslateService } from '@ngx-translate/core';

@Injectable({
providedIn: 'root'
})
export class TitleMetaService {

private _prefix = null;

/**
* Constructor
* @param titleService - Title
* @param metaService - Meta
* @param translateService - TranslateService
*/
constructor(
private titleService: Title,
private metaService: Meta,
private translateService: TranslateService
) { }

/**
* Prefix of title window
* @param prefix - string
* @return this
*/
setPrefix(prefix?: string) {
this._prefix = this.translateService.instant(prefix);

return this;
}

/**
* Title window <head>
* @param title - sting
* @return this
*/
setTitle(title: string) {
let pageTitle = '';
if (this._prefix !== null) {
pageTitle += this._prefix + ': ';
}
pageTitle += this.translateService.instant(title);
this.titleService.setTitle(pageTitle);

return this;
}

/**
* Get Title
* @return string
*/
getTitle() {
return this.titleService.getTitle();
}

/**
* Meta window <head>
* @param name - string
* @param content - string
* @return this
*/
setMeta(name: string, content: string) {
this.metaService.updateTag({name, content});

return this;
}

/**
* Get Meta with its name
* @param name - string
* @return string
*/
getMeta(name: string) {
return this.metaService.getTag(name);
}
}
1 change: 1 addition & 0 deletions projects/rero/ng-core/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export * from './lib/translate/translate-language.service';
export * from './lib/record/action-status';
export * from './lib/record/autocomplete/autocomplete.component';
export * from './lib/record/editor/bootstrap4-framework/custombootstrap4-framework';
export * from './lib/service/title-meta.service';

0 comments on commit fd9eff4

Please sign in to comment.