-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
344 additions
and
200 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
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,55 @@ | ||
/* | ||
* 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 { BasePathSetup } from '../../base_path'; | ||
|
||
export interface NavLinkProperties { | ||
disabled: boolean; | ||
euiIconType?: string; | ||
hidden: boolean; | ||
icon?: string; | ||
id: string; | ||
linkToLastSubUrl: boolean; | ||
order: number; | ||
title: string; | ||
tooltip: string; | ||
url: string; | ||
} | ||
|
||
export class NavLink { | ||
public readonly id: string; | ||
public readonly properties: Readonly<NavLinkProperties>; | ||
|
||
constructor(properties: NavLinkProperties, private readonly basePath: BasePathSetup) { | ||
if (!properties || !properties.id) { | ||
throw new Error('`id` is required.'); | ||
} | ||
|
||
this.id = properties.id; | ||
this.properties = Object.freeze(properties); | ||
} | ||
|
||
public getUrl() { | ||
return this.basePath.addToPath(this.properties.url); | ||
} | ||
|
||
public update(newProps: Partial<NavLinkProperties>) { | ||
return new NavLink({ ...this.properties, ...newProps }, this.basePath); | ||
} | ||
} |
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,88 @@ | ||
/* | ||
* 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 { sortBy } from 'lodash'; | ||
import { BehaviorSubject, ReplaySubject } from 'rxjs'; | ||
import { map, takeUntil } from 'rxjs/operators'; | ||
import { BasePathSetup } from '../../base_path'; | ||
import { NavLink, NavLinkProperties } from './nav_link'; | ||
|
||
export class NavLinksService { | ||
private readonly stop$ = new ReplaySubject(1); | ||
|
||
public setup(basePath: BasePathSetup) { | ||
const navLinks$ = new BehaviorSubject<ReadonlyArray<NavLink>>([]); | ||
|
||
return { | ||
getNavLinks$: () => { | ||
return navLinks$.pipe( | ||
map(sortNavLinks), | ||
takeUntil(this.stop$) | ||
); | ||
}, | ||
|
||
add(navLink: NavLinkProperties) { | ||
navLinks$.next([...navLinks$.value, new NavLink(navLink, basePath)]); | ||
}, | ||
|
||
clear() { | ||
navLinks$.next([]); | ||
}, | ||
|
||
get(id: string) { | ||
const link = navLinks$.value.find(l => l.id === id); | ||
return link ? link.properties : undefined; | ||
}, | ||
|
||
getAll() { | ||
return sortNavLinks(navLinks$.value); | ||
}, | ||
|
||
exists(id: string) { | ||
return this.get(id) !== undefined; | ||
}, | ||
|
||
showOnly(id: string) { | ||
navLinks$.next(navLinks$.value.filter(link => link.id === id)); | ||
}, | ||
|
||
update(id: string, values: Partial<NavLinkProperties>) { | ||
if (!this.exists(id)) { | ||
return; | ||
} | ||
|
||
navLinks$.next( | ||
navLinks$.value.map(link => { | ||
return link.id === id ? link.update(values) : link; | ||
}) | ||
); | ||
|
||
return this.get(id); | ||
}, | ||
}; | ||
} | ||
|
||
public stop() { | ||
this.stop$.next(); | ||
} | ||
} | ||
|
||
function sortNavLinks(navLinks: ReadonlyArray<NavLink>) { | ||
return sortBy(navLinks.map(link => link.properties), 'order'); | ||
} |
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
Oops, something went wrong.