From 7c1bd4664234e3fcc21a2b49bdc922ab001bd103 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Mon, 2 Oct 2017 12:17:28 +0100 Subject: [PATCH] page title service (#2403) --- demo-shell-ng2/app.config-dev.json | 2 +- demo-shell-ng2/app.config-prod.json | 2 +- demo-shell-ng2/app/app.component.ts | 6 +- ng2-components/ng2-alfresco-core/index.ts | 3 + .../src/services/app-config.service.ts | 3 + .../src/services/page-title.service.spec.ts | 99 +++++++++++++++++++ .../src/services/page-title.service.ts | 35 +++++++ 7 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 ng2-components/ng2-alfresco-core/src/services/page-title.service.spec.ts create mode 100644 ng2-components/ng2-alfresco-core/src/services/page-title.service.ts diff --git a/demo-shell-ng2/app.config-dev.json b/demo-shell-ng2/app.config-dev.json index d8dd5d886e..e62d118d6e 100644 --- a/demo-shell-ng2/app.config-dev.json +++ b/demo-shell-ng2/app.config-dev.json @@ -2,7 +2,7 @@ "ecmHost": "http://{hostname}:{port}/ecm", "bpmHost": "http://{hostname}:{port}/bpm", "application": { - "name": "Alfresco" + "name": "Alfresco ADF Appplication" }, "pagination": { "size": 25 diff --git a/demo-shell-ng2/app.config-prod.json b/demo-shell-ng2/app.config-prod.json index 053fac90ef..5cb0efa9dd 100644 --- a/demo-shell-ng2/app.config-prod.json +++ b/demo-shell-ng2/app.config-prod.json @@ -2,7 +2,7 @@ "ecmHost": "http://{hostname}", "bpmHost": "http://{hostname}", "application": { - "name": "Alfresco" + "name": "Alfresco ADF Appplication" }, "pagination": { "size": 25 diff --git a/demo-shell-ng2/app/app.component.ts b/demo-shell-ng2/app/app.component.ts index a7327baeb4..3ad93d556f 100644 --- a/demo-shell-ng2/app/app.component.ts +++ b/demo-shell-ng2/app/app.component.ts @@ -16,7 +16,7 @@ */ import { Component, ViewEncapsulation } from '@angular/core'; -import { AlfrescoSettingsService, AlfrescoTranslationService, StorageService } from 'ng2-alfresco-core'; +import { AlfrescoSettingsService, AlfrescoTranslationService, PageTitle, StorageService } from 'ng2-alfresco-core'; @Component({ selector: 'adf-app', @@ -29,8 +29,10 @@ export class AppComponent { constructor(private settingsService: AlfrescoSettingsService, private translateService: AlfrescoTranslationService, - private storage: StorageService) { + private storage: StorageService, + pageTitle: PageTitle) { this.setProvider(); + pageTitle.setTitle(); } isAPageWithHeaderBar(): boolean { diff --git a/ng2-components/ng2-alfresco-core/index.ts b/ng2-components/ng2-alfresco-core/index.ts index 0f7fd317cf..a20331f76f 100644 --- a/ng2-components/ng2-alfresco-core/index.ts +++ b/ng2-components/ng2-alfresco-core/index.ts @@ -45,6 +45,7 @@ import { CookieService } from './src/services/cookie.service'; import { LogService } from './src/services/log.service'; import { LogServiceMock } from './src/services/log.service'; import { NotificationService } from './src/services/notification.service'; +import { PageTitle } from './src/services/page-title.service'; import { RenditionsService } from './src/services/renditions.service'; import { StorageService } from './src/services/storage.service'; import { ThumbnailService } from './src/services/thumbnail.service'; @@ -72,6 +73,7 @@ import { MomentDateAdapter } from './src/utils/momentDateAdapter'; export { CreateFolderDialogComponent } from './src/dialogs/create-folder.dialog'; export { DownloadZipDialogComponent } from './src/dialogs/download-zip.dialog'; +export { PageTitle } from './src/services/page-title.service'; export { ContentService } from './src/services/content.service'; export { StorageService } from './src/services/storage.service'; export { CookieService } from './src/services/cookie.service'; @@ -164,6 +166,7 @@ export * from './src/services/search.service'; export function providers() { return [ + PageTitle, UserPreferencesService, NotificationService, LogService, diff --git a/ng2-components/ng2-alfresco-core/src/services/app-config.service.ts b/ng2-components/ng2-alfresco-core/src/services/app-config.service.ts index 18cde51f98..02a441a299 100644 --- a/ng2-components/ng2-alfresco-core/src/services/app-config.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/app-config.service.ts @@ -23,6 +23,9 @@ import { ObjectUtils } from '../utils/object-utils'; export class AppConfigService { config: any = { + application: { + name: 'Alfresco ADF Application' + }, ecmHost: 'http://{hostname}:{port}/ecm', bpmHost: 'http://{hostname}:{port}/bpm' }; diff --git a/ng2-components/ng2-alfresco-core/src/services/page-title.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/page-title.service.spec.ts new file mode 100644 index 0000000000..8205dbb731 --- /dev/null +++ b/ng2-components/ng2-alfresco-core/src/services/page-title.service.spec.ts @@ -0,0 +1,99 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { inject, TestBed } from '@angular/core/testing'; +import { Title } from '@angular/platform-browser'; + +import { AppConfigService } from './app-config.service'; +import { PageTitle } from './page-title.service'; + +class TestConfig { + private setup: any = { + applicationName: undefined + }; + + titleService: Title = null; + appTitleService: PageTitle = null; + + constructor(setup: any = {}) { + Object.assign(this.setup, setup); + + const titleServiceProvider = { + provide: Title, + useValue: { + setTitle: jasmine.createSpy('setTitleSpy') + } + }; + + const appConfigProvider = { + provide: AppConfigService, + useValue: { + config: { + application: { + name: this.setup.applicationName + } + }, + get: () => this.setup.applicationName + } + }; + + TestBed.configureTestingModule({ + providers: [ + titleServiceProvider, + appConfigProvider, + PageTitle + ] + }); + + inject([ Title, PageTitle ], (titleService, appTitleService) => { + this.titleService = titleService; + this.appTitleService = appTitleService; + })(); + } +} + +describe('AppTitle service', () => { + it('sets default application name', () => { + const { appTitleService, titleService } = new TestConfig({ + applicationName: undefined + }); + + appTitleService.setTitle(); + expect(titleService.setTitle) + .toHaveBeenCalledWith('Alfresco ADF Application'); + }); + + it('sets only the application name', () => { + const { appTitleService, titleService } = new TestConfig({ + applicationName: 'My application' + }); + + appTitleService.setTitle(); + expect(titleService.setTitle) + .toHaveBeenCalledWith('My application'); + }); + + it('appends application name to the title', () => { + const { appTitleService, titleService } = new TestConfig({ + applicationName: 'My application' + }); + + appTitleService.setTitle('My page'); + expect(titleService.setTitle) + .toHaveBeenCalledWith('My page - My application'); + }); +}); diff --git a/ng2-components/ng2-alfresco-core/src/services/page-title.service.ts b/ng2-components/ng2-alfresco-core/src/services/page-title.service.ts new file mode 100644 index 0000000000..d2345b169a --- /dev/null +++ b/ng2-components/ng2-alfresco-core/src/services/page-title.service.ts @@ -0,0 +1,35 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Injectable } from '@angular/core'; +import { Title } from '@angular/platform-browser'; +import { AppConfigService } from './app-config.service'; + +@Injectable() +export class PageTitle { + + constructor( + private titleService: Title, + private appConfig: AppConfigService) {} + + setTitle(value: string = '') { + const name = this.appConfig.get('application.name') || 'Alfresco ADF Application'; + const title = value ? `${value} - ${name}` : `${name}`; + + this.titleService.setTitle(title); + } +}