Add pageTitle service

Later on we will need to update the page title accordingly with the
runMetadataKey selected by the user. In order to centralize the logic
that does it, I am extracting this portion of code into a reusable service.

Change-Id: Id008b3a1f9485aa080ebd143bac06621040df6c4
This commit is contained in:
Glauco Oliveira 2015-11-18 10:35:26 -02:00
parent 982b9ff12d
commit 1adafc84e0
3 changed files with 48 additions and 9 deletions

View File

@ -3,18 +3,11 @@
/**
* @ngInject
*/
function OnRun ($rootScope, AppSettings) {
function OnRun ($rootScope, pageTitleService) {
// change page title based on state
var disable = $rootScope.$on('$stateChangeSuccess', function(event, toState) {
$rootScope.pageTitle = '';
if (toState.title) {
$rootScope.pageTitle += toState.title;
$rootScope.pageTitle += ' \u2014 ';
}
$rootScope.pageTitle += AppSettings.appTitle;
pageTitleService.update(toState.title);
});
$rootScope.$on('$destroy', disable);

View File

@ -0,0 +1,25 @@
'use strict';
var servicesModule = require('./_index.js');
/**
* @ngInject
*/
var pageTitleService = function($rootScope, AppSettings) {
var titlelize = function(title) {
return title.charAt(0).toUpperCase() + title.slice(1);
};
this.update = function(title) {
$rootScope.pageTitle = '';
if (title) {
$rootScope.pageTitle += titlelize(title);
$rootScope.pageTitle += ' \u2014 ';
}
$rootScope.pageTitle += AppSettings.appTitle;
};
};
servicesModule.service('pageTitleService', pageTitleService);

View File

@ -0,0 +1,21 @@
describe('PageTitleService', function() {
beforeEach(function() {
module('app');
module('app.services');
});
var $rootScope, pageTitleService;
beforeEach(inject(function(_$rootScope_, _pageTitleService_) {
$rootScope = _$rootScope_;
pageTitleService = _pageTitleService_;
}));
it('should update page title', function() {
var newPageTitle = 'new page title';
pageTitleService.update(newPageTitle);
var expectedPageTitle = 'New page title — OpenStack Health';
expect($rootScope.pageTitle).toEqual(expectedPageTitle);
});
});