-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathchrome_service.tsx
420 lines (362 loc) · 13.4 KB
/
chrome_service.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* 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 React from 'react';
import { BehaviorSubject, Observable, ReplaySubject, combineLatest, of, merge } from 'rxjs';
import { flatMap, map, takeUntil } from 'rxjs/operators';
import { parse } from 'url';
import { i18n } from '@kbn/i18n';
import { IconType, Breadcrumb as EuiBreadcrumb } from '@elastic/eui';
import { InjectedMetadataStart } from '../injected_metadata';
import { NotificationsStart } from '../notifications';
import { InternalApplicationStart } from '../application';
import { HttpStart } from '../http';
import { ChromeNavLinks, NavLinksService } from './nav_links';
import { ChromeRecentlyAccessed, RecentlyAccessedService } from './recently_accessed';
import { NavControlsService, ChromeNavControls } from './nav_controls';
import { DocTitleService, ChromeDocTitle } from './doc_title';
import { LoadingIndicator, Header } from './ui';
import { DocLinksStart } from '../doc_links';
import { ChromeHelpExtensionMenuLink } from './ui/header/header_help_menu';
import { KIBANA_ASK_ELASTIC_LINK } from './constants';
import { IUiSettingsClient } from '../ui_settings';
export { ChromeNavControls, ChromeRecentlyAccessed, ChromeDocTitle };
const IS_LOCKED_KEY = 'core.chrome.isLocked';
/** @public */
export interface ChromeBadge {
text: string;
tooltip: string;
iconType?: IconType;
}
/** @public */
export interface ChromeBrand {
logo?: string;
smallLogo?: string;
}
/** @public */
export type ChromeBreadcrumb = EuiBreadcrumb;
/** @public */
export interface ChromeHelpExtension {
/**
* Provide your plugin's name to create a header for separation
*/
appName: string;
/**
* Creates unified links for sending users to documentation, GitHub, Discuss, or a custom link/button
*/
links?: ChromeHelpExtensionMenuLink[];
/**
* Custom content to occur below the list of links
*/
content?: (element: HTMLDivElement) => () => void;
}
interface ConstructorParams {
browserSupportsCsp: boolean;
}
interface StartDeps {
application: InternalApplicationStart;
docLinks: DocLinksStart;
http: HttpStart;
injectedMetadata: InjectedMetadataStart;
notifications: NotificationsStart;
uiSettings: IUiSettingsClient;
}
/** @internal */
export class ChromeService {
private isVisible$!: Observable<boolean>;
private appHidden$!: Observable<boolean>;
private toggleHidden$!: BehaviorSubject<boolean>;
private readonly stop$ = new ReplaySubject(1);
private readonly navControls = new NavControlsService();
private readonly navLinks = new NavLinksService();
private readonly recentlyAccessed = new RecentlyAccessedService();
private readonly docTitle = new DocTitleService();
constructor(private readonly params: ConstructorParams) {}
/**
* These observables allow consumers to toggle the chrome visibility via either:
* 1. Using setIsVisible() to trigger the next chromeHidden$
* 2. Setting `chromeless` when registering an application, which will
* reset the visibility whenever the next application is mounted
* 3. Having "embed" in the query string
*/
private initVisibility(application: StartDeps['application']) {
// Start off the chrome service hidden if "embed" is in the hash query string.
const isEmbedded = 'embed' in parse(location.hash.slice(1), true).query;
this.toggleHidden$ = new BehaviorSubject(isEmbedded);
this.appHidden$ = merge(
// Default the app being hidden to the same value initial value as the chrome visibility
// in case the application service has not emitted an app ID yet, since we want to trigger
// combineLatest below regardless of having an application value yet.
of(isEmbedded),
application.currentAppId$.pipe(
flatMap(appId =>
application.applications$.pipe(
map(applications => {
return !!appId && applications.has(appId) && !!applications.get(appId)!.chromeless;
})
)
)
)
);
this.isVisible$ = combineLatest([this.appHidden$, this.toggleHidden$]).pipe(
map(([appHidden, toggleHidden]) => !(appHidden || toggleHidden)),
takeUntil(this.stop$)
);
}
public async start({
application,
docLinks,
http,
injectedMetadata,
notifications,
uiSettings,
}: StartDeps): Promise<InternalChromeStart> {
this.initVisibility(application);
const appTitle$ = new BehaviorSubject<string>('Kibana');
const brand$ = new BehaviorSubject<ChromeBrand>({});
const applicationClasses$ = new BehaviorSubject<Set<string>>(new Set());
const helpExtension$ = new BehaviorSubject<ChromeHelpExtension | undefined>(undefined);
const breadcrumbs$ = new BehaviorSubject<ChromeBreadcrumb[]>([]);
const badge$ = new BehaviorSubject<ChromeBadge | undefined>(undefined);
const helpSupportUrl$ = new BehaviorSubject<string>(KIBANA_ASK_ELASTIC_LINK);
const isNavDrawerLocked$ = new BehaviorSubject(localStorage.getItem(IS_LOCKED_KEY) === 'true');
const navControls = this.navControls.start();
const navLinks = this.navLinks.start({ application, http });
const recentlyAccessed = await this.recentlyAccessed.start({ http });
const docTitle = this.docTitle.start({ document: window.document });
const setIsNavDrawerLocked = (isLocked: boolean) => {
isNavDrawerLocked$.next(isLocked);
localStorage.setItem(IS_LOCKED_KEY, `${isLocked}`);
};
const getIsNavDrawerLocked$ = isNavDrawerLocked$.pipe(takeUntil(this.stop$));
if (!this.params.browserSupportsCsp && injectedMetadata.getCspConfig().warnLegacyBrowsers) {
notifications.toasts.addWarning(
i18n.translate('core.chrome.legacyBrowserWarning', {
defaultMessage: 'Your browser does not meet the security requirements for Kibana.',
})
);
}
return {
navControls,
navLinks,
recentlyAccessed,
docTitle,
getHeaderComponent: () => (
<React.Fragment>
<LoadingIndicator loadingCount$={http.getLoadingCount$()} />
<Header
application={application}
appTitle$={appTitle$.pipe(takeUntil(this.stop$))}
badge$={badge$.pipe(takeUntil(this.stop$))}
basePath={http.basePath}
breadcrumbs$={breadcrumbs$.pipe(takeUntil(this.stop$))}
kibanaDocLink={docLinks.links.kibana}
forceAppSwitcherNavigation$={navLinks.getForceAppSwitcherNavigation$()}
helpExtension$={helpExtension$.pipe(takeUntil(this.stop$))}
helpSupportUrl$={helpSupportUrl$.pipe(takeUntil(this.stop$))}
homeHref={http.basePath.prepend('/app/kibana#/home')}
isVisible$={this.isVisible$}
kibanaVersion={injectedMetadata.getKibanaVersion()}
legacyMode={injectedMetadata.getLegacyMode()}
navLinks$={navLinks.getNavLinks$()}
recentlyAccessed$={recentlyAccessed.get$()}
navControlsLeft$={navControls.getLeft$()}
navControlsRight$={navControls.getRight$()}
onIsLockedUpdate={setIsNavDrawerLocked}
isLocked$={getIsNavDrawerLocked$}
/>
</React.Fragment>
),
setAppTitle: (appTitle: string) => appTitle$.next(appTitle),
getBrand$: () => brand$.pipe(takeUntil(this.stop$)),
setBrand: (brand: ChromeBrand) => {
brand$.next(
Object.freeze({
logo: brand.logo,
smallLogo: brand.smallLogo,
})
);
},
getIsVisible$: () => this.isVisible$,
setIsVisible: (isVisible: boolean) => this.toggleHidden$.next(!isVisible),
getApplicationClasses$: () =>
applicationClasses$.pipe(
map(set => [...set]),
takeUntil(this.stop$)
),
addApplicationClass: (className: string) => {
const update = new Set([...applicationClasses$.getValue()]);
update.add(className);
applicationClasses$.next(update);
},
removeApplicationClass: (className: string) => {
const update = new Set([...applicationClasses$.getValue()]);
update.delete(className);
applicationClasses$.next(update);
},
getBadge$: () => badge$.pipe(takeUntil(this.stop$)),
setBadge: (badge: ChromeBadge) => {
badge$.next(badge);
},
getBreadcrumbs$: () => breadcrumbs$.pipe(takeUntil(this.stop$)),
setBreadcrumbs: (newBreadcrumbs: ChromeBreadcrumb[]) => {
breadcrumbs$.next(newBreadcrumbs);
},
getHelpExtension$: () => helpExtension$.pipe(takeUntil(this.stop$)),
setHelpExtension: (helpExtension?: ChromeHelpExtension) => {
helpExtension$.next(helpExtension);
},
setHelpSupportUrl: (url: string) => helpSupportUrl$.next(url),
getIsNavDrawerLocked$: () => getIsNavDrawerLocked$,
};
}
public stop() {
this.navLinks.stop();
this.stop$.next();
}
}
/**
* ChromeStart allows plugins to customize the global chrome header UI and
* enrich the UX with additional information about the current location of the
* browser.
*
* @remarks
* While ChromeStart exposes many APIs, they should be used sparingly and the
* developer should understand how they affect other plugins and applications.
*
* @example
* How to add a recently accessed item to the sidebar:
* ```ts
* core.chrome.recentlyAccessed.add('/app/map/1234', 'Map 1234', '1234');
* ```
*
* @example
* How to set the help dropdown extension:
* ```tsx
* core.chrome.setHelpExtension(elem => {
* ReactDOM.render(<MyHelpComponent />, elem);
* return () => ReactDOM.unmountComponentAtNode(elem);
* });
* ```
*
* @public
*/
export interface ChromeStart {
/** {@inheritdoc ChromeNavLinks} */
navLinks: ChromeNavLinks;
/** {@inheritdoc ChromeNavControls} */
navControls: ChromeNavControls;
/** {@inheritdoc ChromeRecentlyAccessed} */
recentlyAccessed: ChromeRecentlyAccessed;
/** {@inheritdoc ChromeDocTitle} */
docTitle: ChromeDocTitle;
/**
* Sets the current app's title
*
* @internalRemarks
* This should be handled by the application service once it is in charge
* of mounting applications.
*/
setAppTitle(appTitle: string): void;
/**
* Get an observable of the current brand information.
*/
getBrand$(): Observable<ChromeBrand>;
/**
* Set the brand configuration.
*
* @remarks
* Normally the `logo` property will be rendered as the
* CSS background for the home link in the chrome navigation, but when the page is
* rendered in a small window the `smallLogo` will be used and rendered at about
* 45px wide.
*
* @example
* ```js
* chrome.setBrand({
* logo: 'url(/plugins/app/logo.png) center no-repeat'
* smallLogo: 'url(/plugins/app/logo-small.png) center no-repeat'
* })
* ```
*
*/
setBrand(brand: ChromeBrand): void;
/**
* Get an observable of the current visibility state of the chrome.
*/
getIsVisible$(): Observable<boolean>;
/**
* 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
* with an exit button.
*/
setIsVisible(isVisible: boolean): void;
/**
* Get the current set of classNames that will be set on the application container.
*/
getApplicationClasses$(): Observable<string[]>;
/**
* Add a className that should be set on the application container.
*/
addApplicationClass(className: string): void;
/**
* Remove a className added with `addApplicationClass()`. If className is unknown it is ignored.
*/
removeApplicationClass(className: string): void;
/**
* Get an observable of the current badge
*/
getBadge$(): Observable<ChromeBadge | undefined>;
/**
* Override the current badge
*/
setBadge(badge?: ChromeBadge): void;
/**
* Get an observable of the current list of breadcrumbs
*/
getBreadcrumbs$(): Observable<ChromeBreadcrumb[]>;
/**
* Override the current set of breadcrumbs
*/
setBreadcrumbs(newBreadcrumbs: ChromeBreadcrumb[]): void;
/**
* Get an observable of the current custom help conttent
*/
getHelpExtension$(): Observable<ChromeHelpExtension | undefined>;
/**
* Override the current set of custom help content
*/
setHelpExtension(helpExtension?: ChromeHelpExtension): void;
/**
* Override the default support URL shown in the help menu
* @param url The updated support URL
*/
setHelpSupportUrl(url: string): void;
/**
* Get an observable of the current locked state of the nav drawer.
*/
getIsNavDrawerLocked$(): Observable<boolean>;
}
/** @internal */
export interface InternalChromeStart extends ChromeStart {
/**
* Used only by MountingService to render the header UI
* @internal
*/
getHeaderComponent(): JSX.Element;
}