Skip to content

Commit

Permalink
[core/public/chrome] migrate ui/chrome/theme apis
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Sep 13, 2018
1 parent 07987cb commit 3486219
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 103 deletions.
20 changes: 20 additions & 0 deletions src/core/public/chrome/chrome_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,34 @@ function isEmbedParamInHash() {
return Boolean(query.embed);
}

export interface Brand {
logo?: string;
smallLogo?: string;
}

export class ChromeService {
private readonly brand$ = new Rx.BehaviorSubject<Brand>({});
private readonly visibility$ = new Rx.BehaviorSubject(true);
private readonly applicationClasses$ = new Rx.BehaviorSubject<Set<string>>(new Set());

public start() {
const VISIBLE_BY_DEFAULT = !isEmbedParamInHash();

return {
/**
* Set the
*/
setBrand: (brand: Brand) => {
this.brand$.next(
Object.freeze({
logo: brand.logo,
smallLogo: brand.logo,
})
);
},

getBrand$: () => this.brand$.asObservable(),

/**
* Set the temporary visibility for the chrome. This does nothing if the chrome is hidden
* by default and should be used to hide the chrome for things like full-screen modes
Expand Down
2 changes: 1 addition & 1 deletion src/core/public/chrome/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* under the License.
*/

export { ChromeService, ChromeStartContract } from './chrome_service';
export { ChromeService, ChromeStartContract, Brand } from './chrome_service';
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Array [
"ui/chrome/api/base_path",
"ui/chrome/api/ui_settings",
"ui/chrome/api/controls",
"ui/chrome/api/theme",
"ui/chrome",
"legacy files",
]
Expand All @@ -23,6 +24,7 @@ Array [
"ui/chrome/api/base_path",
"ui/chrome/api/ui_settings",
"ui/chrome/api/controls",
"ui/chrome/api/theme",
"ui/test_harness",
"legacy files",
]
Expand Down
40 changes: 40 additions & 0 deletions src/core/public/legacy_platform/legacy_platform_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ jest.mock('ui/chrome/api/ui_settings', () => {
};
});

const mockChromeControlsInit = jest.fn();
jest.mock('ui/chrome/api/controls', () => {
mockLoadOrder.push('ui/chrome/api/controls');
return {
__newPlatformInit__: mockChromeControlsInit,
};
});

const mockChromeThemeInit = jest.fn();
jest.mock('ui/chrome/api/theme', () => {
mockLoadOrder.push('ui/chrome/api/theme');
return {
__newPlatformInit__: mockChromeThemeInit,
};
});

import { LegacyPlatformService } from './legacy_platform_service';

const fatalErrorsStartContract = {} as any;
Expand All @@ -110,6 +126,7 @@ const basePathStartContract = {
};

const uiSettingsStartContract: any = {};
const chromeStartContract: any = {};

const defaultParams = {
targetDomElement: document.createElement('div'),
Expand All @@ -125,6 +142,7 @@ const defaultStartDeps = {
loadingCount: loadingCountStartContract,
basePath: basePathStartContract,
uiSettings: uiSettingsStartContract,
chrome: chromeStartContract,
};

afterEach(() => {
Expand Down Expand Up @@ -205,6 +223,28 @@ describe('#start()', () => {
expect(mockUiSettingsInit).toHaveBeenCalledWith(uiSettingsStartContract);
});

it('passes chrome service to ui/chrome/api/controls', () => {
const legacyPlatform = new LegacyPlatformService({
...defaultParams,
});

legacyPlatform.start(defaultStartDeps);

expect(mockChromeControlsInit).toHaveBeenCalledTimes(1);
expect(mockChromeControlsInit).toHaveBeenCalledWith(chromeStartContract);
});

it('passes chrome service to ui/chrome/api/theme', () => {
const legacyPlatform = new LegacyPlatformService({
...defaultParams,
});

legacyPlatform.start(defaultStartDeps);

expect(mockChromeThemeInit).toHaveBeenCalledTimes(1);
expect(mockChromeThemeInit).toHaveBeenCalledWith(chromeStartContract);
});

describe('useLegacyTestHarness = false', () => {
it('passes the targetDomElement to ui/chrome', () => {
const legacyPlatform = new LegacyPlatformService({
Expand Down
1 change: 1 addition & 0 deletions src/core/public/legacy_platform/legacy_platform_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class LegacyPlatformService {
require('ui/chrome/api/base_path').__newPlatformInit__(basePath);
require('ui/chrome/api/ui_settings').__newPlatformInit__(uiSettings);
require('ui/chrome/api/controls').__newPlatformInit__(chrome);
require('ui/chrome/api/theme').__newPlatformInit__(chrome);

// Load the bootstrap module before loading the legacy platform files so that
// the bootstrap module can modify the environment a bit first
Expand Down
100 changes: 0 additions & 100 deletions src/ui/public/chrome/api/theme.js

This file was deleted.

63 changes: 63 additions & 0 deletions src/ui/public/chrome/api/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import * as Rx from 'rxjs';

import { Brand, ChromeStartContract } from '../../../../core/public/chrome';

let newPlatformChrome: ChromeStartContract;

export function __newPlatformInit__(instance: ChromeStartContract) {
if (newPlatformChrome) {
throw new Error('ui/chrome/api/theme is already initialized');
}

newPlatformChrome = instance;
}

export function initChromeThemeApi(chrome: { [key: string]: any }) {
const brandCache$ = new Rx.BehaviorSubject<Brand>({});
newPlatformChrome.getBrand$().subscribe(brandCache$);

const applicationClassesCache$ = new Rx.BehaviorSubject<string[]>([]);
newPlatformChrome.getApplicationClasses$().subscribe(applicationClassesCache$);

chrome.setBrand = (brand: Brand) => {
newPlatformChrome.setBrand(brand);
return chrome;
};

chrome.getBrand = (key: keyof Brand) => {
return brandCache$.getValue()[key];
};

chrome.addApplicationClass = (className: string) => {
newPlatformChrome.addApplicationClass(className);
return chrome;
};

chrome.removeApplicationClass = (className: string) => {
newPlatformChrome.removeApplicationClass(className);
return chrome;
};

chrome.getApplicationClasses = () => {
return applicationClassesCache$.getValue().join(' ');
};
}
4 changes: 2 additions & 2 deletions src/ui/public/chrome/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import appsApi from './api/apps';
import { initChromeControlsApi } from './api/controls';
import { initChromeNavApi } from './api/nav';
import templateApi from './api/template';
import themeApi from './api/theme';
import { initChromeThemeApi } from './api/theme';
import translationsApi from './api/translations';
import { initChromeXsrfApi } from './api/xsrf';
import { initUiSettingsApi } from './api/ui_settings';
Expand Down Expand Up @@ -70,7 +70,7 @@ initLoadingCountApi(chrome, internals);
initAngularApi(chrome, internals);
initChromeControlsApi(chrome);
templateApi(chrome, internals);
themeApi(chrome, internals);
initChromeThemeApi(chrome, internals);
translationsApi(chrome, internals);

const waitForBootstrap = new Promise(resolve => {
Expand Down

0 comments on commit 3486219

Please sign in to comment.