Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 1 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 2 | export interface IXosPageTitleService { |
| 3 | get(): string; |
| 4 | set(title: string): void; |
| 5 | formatStateName(stateName: string): string; |
| 6 | } |
| 7 | |
| 8 | export class PageTitle { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 9 | static $inject = ['$window', '$transitions', 'StyleConfig']; |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 10 | constructor( |
| 11 | private $window: angular.IWindowService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 12 | private $transitions: any, // missing definition |
| 13 | private StyleConfig: IXosStyleConfig |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 14 | ) { |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 15 | this.$transitions.onSuccess({ to: '**' }, (transtion) => { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 16 | this.set(transtion.$to().name); |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 17 | }); |
| 18 | } |
| 19 | |
| 20 | get() { |
| 21 | return this.$window.document.title; |
| 22 | } |
| 23 | |
| 24 | set(title: string) { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 25 | this.$window.document.title = `${this.StyleConfig.projectName} - ${this.formatStateName(title)}`; |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | private formatStateName(stateName: string): string { |
| 29 | // TODO pluralize and capitalize first letter only |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 30 | return stateName.replace('xos.', '').split('.').join(' > '); |
Matteo Scandolo | 5655bdc | 2016-12-16 08:32:15 -0800 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | |