-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1942 from spenceralger/doc-title
Doc title
- Loading branch information
Showing
11 changed files
with
181 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
define(function (require) { | ||
|
||
require('modules').get('kibana') | ||
.run(function ($rootScope, docTitle) { | ||
// always bind to the route events | ||
$rootScope.$on('$routeChangeStart', docTitle.reset); | ||
$rootScope.$on('$routeChangeError', docTitle.update); | ||
$rootScope.$on('$routeChangeSuccess', docTitle.update); | ||
$rootScope.$watch('activeApp', docTitle.update); | ||
}) | ||
.service('docTitle', function ($rootScope) { | ||
var baseTitle = document.title; | ||
var self = this; | ||
|
||
var lastChange; | ||
|
||
function render() { | ||
lastChange = lastChange || []; | ||
|
||
var parts = [lastChange[0]]; | ||
|
||
if ($rootScope.activeApp) { | ||
parts.push($rootScope.activeApp.name); | ||
} | ||
|
||
if (!lastChange[1]) { | ||
parts.push(baseTitle); | ||
} | ||
|
||
return parts.filter(Boolean).join(' - '); | ||
} | ||
|
||
self.change = function (title, complete) { | ||
lastChange = [title, complete]; | ||
self.update(); | ||
}; | ||
|
||
self.reset = function () { | ||
lastChange = null; | ||
}; | ||
|
||
self.update = function () { | ||
document.title = render(); | ||
}; | ||
}); | ||
|
||
// return a "private module" so that it can be used both ways | ||
return function DoctitleProvider(docTitle) { | ||
return docTitle; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
define(function (require) { | ||
describe('docTitle Service', function () { | ||
var _ = require('lodash'); | ||
var sinon = require('test_utils/auto_release_sinon'); | ||
var initialDocTitle; | ||
var MAIN_TITLE = 'Kibana 4'; | ||
var fakeApp = { name: 'fancy pants' }; | ||
|
||
var docTitle; | ||
var $rootScope; | ||
|
||
beforeEach(function () { | ||
initialDocTitle = document.title; | ||
document.title = MAIN_TITLE; | ||
}); | ||
afterEach(function () { | ||
document.title = initialDocTitle; | ||
}); | ||
|
||
beforeEach(module('kibana', function ($provide) { | ||
$provide.decorator('docTitle', sinon.decorateWithSpy('update')); | ||
$provide.decorator('$rootScope', sinon.decorateWithSpy('$on')); | ||
})); | ||
|
||
beforeEach(inject(function ($injector, Private) { | ||
if (_.random(0, 1)) { | ||
docTitle = $injector.get('docTitle'); | ||
} else { | ||
docTitle = Private(require('components/doc_title/doc_title')); | ||
} | ||
|
||
$rootScope = $injector.get('$rootScope'); | ||
})); | ||
|
||
describe('setup', function () { | ||
it('resets the title when a route change begins', function () { | ||
var call; | ||
var spy = $rootScope.$on; | ||
for (var i = 0; i < spy.callCount; i++) { | ||
if (spy.args[i][0] === '$routeChangeStart') { | ||
call = spy.getCall(i); | ||
break; | ||
} | ||
} | ||
|
||
if (!call) throw new Error('$rootScope.$on not called'); | ||
|
||
expect(call.args[0]).to.be('$routeChangeStart'); | ||
expect(call.args[1]).to.be(docTitle.reset); | ||
}); | ||
}); | ||
|
||
describe('#reset', function () { | ||
it('clears the internal state, next update() will write the default', function () { | ||
docTitle.change('some title'); | ||
docTitle.update(); | ||
expect(document.title).to.be('some title - ' + MAIN_TITLE); | ||
|
||
docTitle.reset(); | ||
docTitle.update(); | ||
expect(document.title).to.be(MAIN_TITLE); | ||
}); | ||
}); | ||
|
||
describe('#change', function () { | ||
it('writes the first param to as the first part of the doc name', function () { | ||
expect(document.title).to.be(MAIN_TITLE); | ||
docTitle.change('some secondary title'); | ||
expect(document.title).to.be('some secondary title - ' + MAIN_TITLE); | ||
}); | ||
|
||
it('includes the name of the active app if available', function () { | ||
expect(document.title).to.be(MAIN_TITLE); | ||
$rootScope.activeApp = fakeApp; | ||
docTitle.change('some secondary title'); | ||
expect(document.title).to.be('some secondary title - ' + fakeApp.name + ' - ' + MAIN_TITLE); | ||
}); | ||
|
||
it('will write just the first param if the second param is true', function () { | ||
expect(document.title).to.be(MAIN_TITLE); | ||
docTitle.change('entire name', true); | ||
expect(document.title).to.be('entire name'); | ||
}); | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters