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