-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into task/avc-banner-pac…
…kage
- Loading branch information
Showing
5 changed files
with
253 additions
and
111 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
68 changes: 68 additions & 0 deletions
68
examples/controls_example/public/react_controls/control_group/init_controls_manager.test.ts
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,68 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { DefaultControlApi } from '../types'; | ||
import { initControlsManager } from './init_controls_manager'; | ||
|
||
jest.mock('uuid', () => ({ | ||
v4: jest.fn().mockReturnValue('delta'), | ||
})); | ||
|
||
describe('PresentationContainer api', () => { | ||
test('addNewPanel should add control at end of controls', async () => { | ||
const controlsManager = initControlsManager({ | ||
alpha: { type: 'whatever', order: 0 }, | ||
bravo: { type: 'whatever', order: 1 }, | ||
charlie: { type: 'whatever', order: 2 }, | ||
}); | ||
const addNewPanelPromise = controlsManager.api.addNewPanel({ | ||
panelType: 'whatever', | ||
initialState: {}, | ||
}); | ||
controlsManager.setControlApi('delta', {} as unknown as DefaultControlApi); | ||
await addNewPanelPromise; | ||
expect(controlsManager.controlsInOrder$.value.map((element) => element.id)).toEqual([ | ||
'alpha', | ||
'bravo', | ||
'charlie', | ||
'delta', | ||
]); | ||
}); | ||
|
||
test('removePanel should remove control', () => { | ||
const controlsManager = initControlsManager({ | ||
alpha: { type: 'whatever', order: 0 }, | ||
bravo: { type: 'whatever', order: 1 }, | ||
charlie: { type: 'whatever', order: 2 }, | ||
}); | ||
controlsManager.api.removePanel('bravo'); | ||
expect(controlsManager.controlsInOrder$.value.map((element) => element.id)).toEqual([ | ||
'alpha', | ||
'charlie', | ||
]); | ||
}); | ||
|
||
test('replacePanel should replace control', async () => { | ||
const controlsManager = initControlsManager({ | ||
alpha: { type: 'whatever', order: 0 }, | ||
bravo: { type: 'whatever', order: 1 }, | ||
charlie: { type: 'whatever', order: 2 }, | ||
}); | ||
const replacePanelPromise = controlsManager.api.replacePanel('bravo', { | ||
panelType: 'whatever', | ||
initialState: {}, | ||
}); | ||
controlsManager.setControlApi('delta', {} as unknown as DefaultControlApi); | ||
await replacePanelPromise; | ||
expect(controlsManager.controlsInOrder$.value.map((element) => element.id)).toEqual([ | ||
'alpha', | ||
'delta', | ||
'charlie', | ||
]); | ||
}); | ||
}); |
158 changes: 158 additions & 0 deletions
158
examples/controls_example/public/react_controls/control_group/init_controls_manager.ts
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,158 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { v4 as generateId } from 'uuid'; | ||
import { | ||
HasSerializedChildState, | ||
PanelPackage, | ||
PresentationContainer, | ||
} from '@kbn/presentation-containers'; | ||
import { Reference } from '@kbn/content-management-utils'; | ||
import { BehaviorSubject, merge } from 'rxjs'; | ||
import { PublishingSubject } from '@kbn/presentation-publishing'; | ||
import { omit } from 'lodash'; | ||
import { ControlPanelsState, ControlPanelState } from './types'; | ||
import { DefaultControlApi, DefaultControlState } from '../types'; | ||
|
||
export function initControlsManager(initialControlPanelsState: ControlPanelsState) { | ||
const children$ = new BehaviorSubject<{ [key: string]: DefaultControlApi }>({}); | ||
const controlsPanelState: { [panelId: string]: DefaultControlState } = { | ||
...initialControlPanelsState, | ||
}; | ||
const controlsInOrder$ = new BehaviorSubject<Array<{ id: string; type: string }>>( | ||
Object.keys(initialControlPanelsState) | ||
.map((key) => ({ | ||
id: key, | ||
order: initialControlPanelsState[key].order, | ||
type: initialControlPanelsState[key].type, | ||
})) | ||
.sort((a, b) => (a.order > b.order ? 1 : -1)) | ||
); | ||
|
||
function untilControlLoaded( | ||
id: string | ||
): DefaultControlApi | Promise<DefaultControlApi | undefined> { | ||
if (children$.value[id]) { | ||
return children$.value[id]; | ||
} | ||
|
||
return new Promise((resolve) => { | ||
const subscription = merge(children$, controlsInOrder$).subscribe(() => { | ||
if (children$.value[id]) { | ||
subscription.unsubscribe(); | ||
resolve(children$.value[id]); | ||
return; | ||
} | ||
|
||
// control removed before the control finished loading. | ||
const controlState = controlsInOrder$.value.find((element) => element.id === id); | ||
if (!controlState) { | ||
subscription.unsubscribe(); | ||
resolve(undefined); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function getControlApi(controlUuid: string) { | ||
return children$.value[controlUuid]; | ||
} | ||
|
||
async function addNewPanel( | ||
{ panelType, initialState }: PanelPackage<DefaultControlState>, | ||
index: number | ||
) { | ||
const id = generateId(); | ||
const nextControlsInOrder = [...controlsInOrder$.value]; | ||
nextControlsInOrder.splice(index, 0, { | ||
id, | ||
type: panelType, | ||
}); | ||
controlsInOrder$.next(nextControlsInOrder); | ||
controlsPanelState[id] = initialState ?? {}; | ||
return await untilControlLoaded(id); | ||
} | ||
|
||
function removePanel(panelId: string) { | ||
delete controlsPanelState[panelId]; | ||
controlsInOrder$.next(controlsInOrder$.value.filter(({ id }) => id !== panelId)); | ||
children$.next(omit(children$.value, panelId)); | ||
} | ||
|
||
return { | ||
controlsInOrder$: controlsInOrder$ as PublishingSubject<Array<{ id: string; type: string }>>, | ||
getControlApi, | ||
setControlApi: (uuid: string, controlApi: DefaultControlApi) => { | ||
children$.next({ | ||
...children$.getValue(), | ||
[uuid]: controlApi, | ||
}); | ||
}, | ||
serializeControls: () => { | ||
const references: Reference[] = []; | ||
const explicitInputPanels: { | ||
[panelId: string]: ControlPanelState & { explicitInput: object }; | ||
} = {}; | ||
|
||
controlsInOrder$.getValue().forEach(({ id }, index) => { | ||
const controlApi = getControlApi(id); | ||
if (!controlApi) { | ||
return; | ||
} | ||
|
||
const { | ||
rawState: { grow, width, ...rest }, | ||
references: controlReferences, | ||
} = controlApi.serializeState(); | ||
|
||
if (controlReferences && controlReferences.length > 0) { | ||
references.push(...controlReferences); | ||
} | ||
|
||
explicitInputPanels[id] = { | ||
grow, | ||
order: index, | ||
type: controlApi.type, | ||
width, | ||
/** Re-add the `explicitInput` layer on serialize so control group saved object retains shape */ | ||
explicitInput: rest, | ||
}; | ||
}); | ||
|
||
return { | ||
panelsJSON: JSON.stringify(explicitInputPanels), | ||
references, | ||
}; | ||
}, | ||
api: { | ||
getSerializedStateForChild: (childId: string) => { | ||
const controlPanelState = controlsPanelState[childId]; | ||
return controlPanelState ? { rawState: controlPanelState } : undefined; | ||
}, | ||
children$: children$ as PublishingSubject<{ | ||
[key: string]: DefaultControlApi; | ||
}>, | ||
getPanelCount: () => { | ||
return controlsInOrder$.value.length; | ||
}, | ||
addNewPanel: async (panel: PanelPackage<DefaultControlState>) => { | ||
return addNewPanel(panel, controlsInOrder$.value.length); | ||
}, | ||
removePanel, | ||
replacePanel: async (panelId, newPanel) => { | ||
const index = controlsInOrder$.value.findIndex(({ id }) => id === panelId); | ||
removePanel(panelId); | ||
const controlApi = await addNewPanel( | ||
newPanel, | ||
index >= 0 ? index : controlsInOrder$.value.length | ||
); | ||
return controlApi ? controlApi.uuid : ''; | ||
}, | ||
} as PresentationContainer & HasSerializedChildState<ControlPanelState>, | ||
}; | ||
} |
Oops, something went wrong.