Skip to content

Commit

Permalink
d13n docTitle (elastic#39162)
Browse files Browse the repository at this point in the history
* refactor doc title so it can be used outside of angular service
  • Loading branch information
mattkime authored Jun 19, 2019
1 parent 7e4e8fe commit 80eb1e1
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';

import { applyResizeCheckerToEditors } from '../sense_editor_resize';
import $ from 'jquery';
Expand All @@ -36,7 +36,6 @@ module.run(function ($rootScope) {
});

module.controller('SenseController', function SenseController(Private, $scope, $timeout, $location, kbnUiAceKeyboardModeService) {
const docTitle = Private(DocTitleProvider);
docTitle.change('Console');

$scope.topNavController = Private(SenseTopNavController);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { ConfirmationButtonTypes } from 'ui/modals/confirm_modal';
import { FilterBarQueryFilterProvider } from 'ui/filter_manager/query_filter';

// @ts-ignore
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';

// @ts-ignore
import { showSaveModal } from 'ui/saved_objects/show_saved_object_save_modal';
Expand Down Expand Up @@ -215,7 +215,6 @@ class DashboardAppController {
}) {
const filterManager = Private(FilterManagerProvider);
const queryFilter = Private(FilterBarQueryFilterProvider);
const docTitle = Private<{ change: (title: string) => void }>(DocTitleProvider);
const embeddableFactories = Private(
EmbeddableFactoriesRegistryProvider
) as EmbeddableFactoryRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { hasSearchStategyForIndexPattern, isDefaultTypeIndexPattern } from 'ui/c
import { toastNotifications } from 'ui/notify';
import { VisProvider } from 'ui/vis';
import { vislibSeriesResponseHandlerProvider } from 'ui/vis/response_handlers/vislib';
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';
import { FilterBarQueryFilterProvider } from 'ui/filter_manager/query_filter';
import { intervalOptions } from 'ui/agg_types/buckets/_interval_options';
import { stateMonitorFactory } from 'ui/state_management/state_monitor_factory';
Expand Down Expand Up @@ -196,7 +196,6 @@ function discoverController(
const visualizeLoader = Private(VisualizeLoaderProvider);
let visualizeHandler;
const Vis = Private(VisProvider);
const docTitle = Private(DocTitleProvider);
const queryFilter = Private(FilterBarQueryFilterProvider);
const responseHandler = vislibSeriesResponseHandlerProvider().handler;
const filterManager = Private(FilterManagerProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { Field } from 'ui/index_patterns/_field';
import { RegistryFieldFormatEditorsProvider } from 'ui/registry/field_format_editors';
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';
import { KbnUrlProvider } from 'ui/url';
import uiRoutes from 'ui/routes';
import { toastNotifications } from 'ui/notify';
Expand Down Expand Up @@ -104,7 +104,6 @@ uiRoutes
controllerAs: 'fieldSettings',
controller: function FieldEditorPageController($scope, $route, $timeout, $http, Private, config) {
const getConfig = (...args) => config.get(...args);
const docTitle = Private(DocTitleProvider);
const fieldFormatEditors = Private(RegistryFieldFormatEditorsProvider);
const kbnUrl = Private(KbnUrlProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import _ from 'lodash';
import './index_header';
import './create_edit_field';
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';
import { KbnUrlProvider } from 'ui/url';
import { IndicesEditSectionsProvider } from './edit_sections';
import { fatalError, toastNotifications } from 'ui/notify';
Expand Down Expand Up @@ -182,7 +182,6 @@ uiModules.get('apps/management')
$scope.indexPatternListProvider = indexPatternListProvider;
$scope.indexPattern.tags = indexPatternListProvider.getIndexPatternTags($scope.indexPattern);
$scope.getFieldInfo = indexPatternListProvider.getFieldInfo;
const docTitle = Private(DocTitleProvider);
docTitle.change($scope.indexPattern.title);

const otherPatterns = _.filter($route.current.locals.indexPatterns, pattern => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import angular from 'angular';
import { FormattedMessage } from '@kbn/i18n/react';
import { toastNotifications } from 'ui/notify';
import { VisTypesRegistryProvider } from 'ui/registry/vis_types';
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';
import { FilterBarQueryFilterProvider } from 'ui/filter_manager/query_filter';
import { stateMonitorFactory } from 'ui/state_management/state_monitor_factory';
import { migrateAppState } from './lib';
Expand Down Expand Up @@ -132,7 +132,6 @@ function VisEditor(
kbnBaseUrl,
localStorage
) {
const docTitle = Private(DocTitleProvider);
const queryFilter = Private(FilterBarQueryFilterProvider);
const getUnhashableStates = Private(getUnhashableStatesProvider);
const shareContextMenuExtensions = Private(ShareContextMenuExtensionsRegistryProvider);
Expand Down
3 changes: 1 addition & 2 deletions src/legacy/core_plugins/timelion/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import _ from 'lodash';
import { i18n } from '@kbn/i18n';

import { capabilities } from 'ui/capabilities';
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';
import { SavedObjectRegistryProvider } from 'ui/saved_objects/saved_object_registry';
import { notify, fatalError, toastNotifications } from 'ui/notify';
import { timezoneProvider } from 'ui/vis/lib/timezone';
Expand Down Expand Up @@ -138,7 +138,6 @@ app.controller('timelion', function (

const savedVisualizations = Private(SavedObjectRegistryProvider).byLoaderPropertiesName.visualizations;
const timezone = Private(timezoneProvider)();
const docTitle = Private(DocTitleProvider);

const defaultExpression = '.es(*)';
const savedSheet = $route.current.locals.savedSheet;
Expand Down
16 changes: 4 additions & 12 deletions src/legacy/ui/public/doc_title/__tests__/doc_title.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,31 @@
* under the License.
*/

import _ from 'lodash';
import sinon from 'sinon';
import expect from '@kbn/expect';
import ngMock from 'ng_mock';
import { DocTitleProvider } from '..';
import { setBaseTitle, docTitle } from '../doc_title';

describe('docTitle Service', function () {
let initialDocTitle;
const MAIN_TITLE = 'Kibana 4';

let docTitle;
let $rootScope;

beforeEach(function () {
initialDocTitle = document.title;
document.title = MAIN_TITLE;
setBaseTitle(MAIN_TITLE);
});
afterEach(function () {
document.title = initialDocTitle;
setBaseTitle(initialDocTitle);
});

beforeEach(ngMock.module('kibana', function ($provide) {
$provide.decorator('docTitle', decorateWithSpy('update'));
$provide.decorator('$rootScope', decorateWithSpy('$on'));
}));

beforeEach(ngMock.inject(function ($injector, Private) {
if (_.random(0, 1)) {
docTitle = $injector.get('docTitle');
} else {
docTitle = Private(DocTitleProvider);
}

beforeEach(ngMock.inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
}));

Expand Down
67 changes: 36 additions & 31 deletions src/legacy/ui/public/doc_title/doc_title.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,50 @@
import _ from 'lodash';
import { uiModules } from '../modules';

uiModules.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);
})
.service('docTitle', function () {
const baseTitle = document.title;
const self = this;
let baseTitle = document.title;

let lastChange;
// for karma test
export function setBaseTitle(str) {
baseTitle = str;
}

function render() {
lastChange = lastChange || [];
let lastChange;

const parts = [lastChange[0]];
function render() {
lastChange = lastChange || [];

if (!lastChange[1]) parts.push(baseTitle);
const parts = [lastChange[0]];

return _(parts).flattenDeep().compact().join(' - ');
}
if (!lastChange[1]) parts.push(baseTitle);

self.change = function (title, complete) {
lastChange = [title, complete];
self.update();
};
return _(parts).flattenDeep().compact().join(' - ');
}

self.reset = function () {
lastChange = null;
};
function change(title, complete) {
lastChange = [title, complete];
update();
}

self.update = function () {
document.title = render();
};
});
function reset() {
lastChange = null;
}

// return a "private module" so that it can be used both ways
export function DocTitleProvider(docTitle) {
return docTitle;
function update() {
document.title = render();
}

export const docTitle = {
render,
change,
reset,
update,
};

uiModules.get('kibana')
.run(function ($rootScope) {
// always bind to the route events
$rootScope.$on('$routeChangeStart', docTitle.reset);
$rootScope.$on('$routeChangeError', docTitle.update);
$rootScope.$on('$routeChangeSuccess', docTitle.update);
});

2 changes: 1 addition & 1 deletion src/legacy/ui/public/doc_title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

import './doc_title';

export { DocTitleProvider } from './doc_title';
export { docTitle } from './doc_title';
5 changes: 2 additions & 3 deletions x-pack/plugins/maps/public/angular/map_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
import { getQueryableUniqueIndexPatternIds } from '../selectors/map_selectors';
import { getInspectorAdapters } from '../store/non_serializable_instances';
import { Inspector } from 'ui/inspector';
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';
import { indexPatternService } from '../kibana_services';
import { SavedObjectSaveModal } from 'ui/saved_objects/components/saved_object_save_modal';
import { showSaveModal } from 'ui/saved_objects/show_saved_object_save_modal';
Expand All @@ -55,7 +55,7 @@ const REACT_ANCHOR_DOM_ELEMENT_ID = 'react-maps-root';

const app = uiModules.get('app/maps', []);

app.controller('GisMapController', ($scope, $route, config, kbnUrl, localStorage, AppState, globalState, Private) => {
app.controller('GisMapController', ($scope, $route, config, kbnUrl, localStorage, AppState, globalState) => {

const savedMap = $route.current.locals.map;
let unsubscribe;
Expand Down Expand Up @@ -235,7 +235,6 @@ app.controller('GisMapController', ($scope, $route, config, kbnUrl, localStorage
async function doSave(saveOptions) {
await store.dispatch(clearTransientLayerStateAndCloseFlyout());
savedMap.syncWithStore(store.getState());
const docTitle = Private(DocTitleProvider);
let id;

try {
Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/maps/public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import chrome from 'ui/chrome';
import routes from 'ui/routes';
import 'ui/kbn_top_nav';
import { uiModules } from 'ui/modules';
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';
import 'ui/autoload/styles';
import 'ui/autoload/all';
import 'react-vis/dist/style.css';
Expand Down Expand Up @@ -103,10 +103,8 @@ routes
template: mapTemplate,
controller: 'GisMapController',
resolve: {
map: function (gisMapSavedObjectLoader, redirectWhenMissing, $route,
Private) {
map: function (gisMapSavedObjectLoader, redirectWhenMissing, $route) {
const id = $route.current.params.id;
const docTitle = Private(DocTitleProvider);
return gisMapSavedObjectLoader.get(id)
.then((savedMap) => {
recentlyAccessed.add(savedMap.getFullPath(), savedMap.title, id);
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/monitoring/public/services/title.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { uiModules } from 'ui/modules';
import { DocTitleProvider } from 'ui/doc_title';
import { docTitle } from 'ui/doc_title';

const uiModule = uiModules.get('monitoring/title', []);
uiModule.service('title', Private => {
const docTitle = Private(DocTitleProvider);
uiModule.service('title', () => {
return function changeTitle(cluster, suffix) {
let clusterName = _.get(cluster, 'cluster_name');
clusterName = (clusterName) ? `- ${clusterName}` : '';
Expand Down

0 comments on commit 80eb1e1

Please sign in to comment.