Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add navControlsPrimaryHeaderRight slot to header #9223

2 changes: 2 additions & 0 deletions changelogs/fragments/9223.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Add navControlsPrimaryHeaderRight slot to header ([#9223](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9223))
2 changes: 2 additions & 0 deletions src/core/public/chrome/chrome_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ const createStartContractMock = () => {
registerCenter: jest.fn(),
registerRight: jest.fn(),
registerLeftBottom: jest.fn(),
registerPrimaryHeaderRight: jest.fn(),
getLeft$: jest.fn(),
getCenter$: jest.fn(),
getRight$: jest.fn(),
getLeftBottom$: jest.fn(),
getPrimaryHeaderRight$: jest.fn(),
},
navGroup: {
getNavGroupsMap$: jest.fn(() => new BehaviorSubject({})),
Expand Down
1 change: 1 addition & 0 deletions src/core/public/chrome/chrome_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ export class ChromeService {
navControlsExpandedCenter$={navControls.getExpandedCenter$()}
navControlsExpandedRight$={navControls.getExpandedRight$()}
navControlsLeftBottom$={navControls.getLeftBottom$()}
navControlsPrimaryHeaderRight$={navControls.getPrimaryHeaderRight$()}
onIsLockedUpdate={setIsNavDrawerLocked}
isLocked$={getIsNavDrawerLocked$}
branding={injectedMetadata.getBranding()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,28 @@ describe('RecentlyAccessed#start()', () => {
expect(await navControls.getLeftBottom$().pipe(take(1)).toPromise()).toEqual([nc2, nc1, nc3]);
});
});

describe('new primary header right controls', () => {
it('allows registration', async () => {
const navControls = getStart();
const nc = { mount: jest.fn() };
navControls.registerPrimaryHeaderRight(nc);
expect(await navControls.getPrimaryHeaderRight$().pipe(take(1)).toPromise()).toEqual([nc]);
});

it('sorts controls by order property', async () => {
const navControls = getStart();
const nc1 = { mount: jest.fn(), order: 10 };
const nc2 = { mount: jest.fn(), order: 0 };
const nc3 = { mount: jest.fn(), order: 20 };
navControls.registerPrimaryHeaderRight(nc1);
navControls.registerPrimaryHeaderRight(nc2);
navControls.registerPrimaryHeaderRight(nc3);
expect(await navControls.getPrimaryHeaderRight$().pipe(take(1)).toPromise()).toEqual([
nc2,
nc1,
nc3,
]);
});
});
});
17 changes: 17 additions & 0 deletions src/core/public/chrome/nav_controls/nav_controls_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export interface ChromeNavControls {
registerCenter(navControl: ChromeNavControl): void;
/** Register a nav control to be presented on the left-bottom side of the left navigation. */
registerLeftBottom(navControl: ChromeNavControl): void;
/** Register a nav control to be presented on the right side of the primary chrome header. */
registerPrimaryHeaderRight(navControl: ChromeNavControl): void;
/** @internal */
getLeft$(): Observable<ChromeNavControl[]>;
/** @internal */
Expand All @@ -72,6 +74,8 @@ export interface ChromeNavControls {
getCenter$(): Observable<ChromeNavControl[]>;
/** @internal */
getLeftBottom$(): Observable<ChromeNavControl[]>;
/** @internal */
getPrimaryHeaderRight$(): Observable<ChromeNavControl[]>;
}

/** @internal */
Expand All @@ -87,6 +91,9 @@ export class NavControlsService {
new Set()
);
const navControlsLeftBottom$ = new BehaviorSubject<ReadonlySet<ChromeNavControl>>(new Set());
const navControlsPrimaryHeaderRight$ = new BehaviorSubject<ReadonlySet<ChromeNavControl>>(
new Set()
);

return {
// In the future, registration should be moved to the setup phase. This
Expand Down Expand Up @@ -115,6 +122,11 @@ export class NavControlsService {
new Set([...navControlsLeftBottom$.value.values(), navControl])
),

registerPrimaryHeaderRight: (navControl: ChromeNavControl) =>
navControlsPrimaryHeaderRight$.next(
new Set([...navControlsPrimaryHeaderRight$.value.values(), navControl])
),

getLeft$: () =>
navControlsLeft$.pipe(
map((controls) => sortBy([...controls.values()], 'order')),
Expand Down Expand Up @@ -145,6 +157,11 @@ export class NavControlsService {
map((controls) => sortBy([...controls.values()], 'order')),
takeUntil(this.stop$)
),
getPrimaryHeaderRight$: () =>
navControlsPrimaryHeaderRight$.pipe(
map((controls) => sortBy([...controls.values()], 'order')),
takeUntil(this.stop$)
),
};
}

Expand Down
Loading
Loading