-
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 branch 'main' into oas-no-serverless
- Loading branch information
Showing
92 changed files
with
1,958 additions
and
711 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
94 changes: 94 additions & 0 deletions
94
...s/embeddable_examples/public/app/presentation_container_example/components/add_button.tsx
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,94 @@ | ||
/* | ||
* 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 React, { ReactElement, useEffect, useState } from 'react'; | ||
import { EuiButton, EuiContextMenuItem, EuiContextMenuPanel, EuiPopover } from '@elastic/eui'; | ||
import { ADD_PANEL_TRIGGER, UiActionsStart } from '@kbn/ui-actions-plugin/public'; | ||
import { ParentApi } from '../types'; | ||
|
||
export function AddButton({ | ||
parentApi, | ||
uiActions, | ||
}: { | ||
parentApi: ParentApi; | ||
uiActions: UiActionsStart; | ||
}) { | ||
const [isPopoverOpen, setIsPopoverOpen] = useState(false); | ||
const [items, setItems] = useState<ReactElement[]>([]); | ||
|
||
useEffect(() => { | ||
let cancelled = false; | ||
|
||
const actionContext = { | ||
embeddable: parentApi, | ||
trigger: { | ||
id: ADD_PANEL_TRIGGER, | ||
}, | ||
}; | ||
const actionsPromises = uiActions.getTriggerActions(ADD_PANEL_TRIGGER).map(async (action) => { | ||
return { | ||
isCompatible: await action.isCompatible(actionContext), | ||
action, | ||
}; | ||
}); | ||
|
||
Promise.all(actionsPromises).then((actions) => { | ||
if (cancelled) { | ||
return; | ||
} | ||
|
||
const nextItems = actions | ||
.filter( | ||
({ action, isCompatible }) => isCompatible && action.id !== 'ACTION_CREATE_ESQL_CHART' | ||
) | ||
.map(({ action }) => { | ||
return ( | ||
<EuiContextMenuItem | ||
key={action.id} | ||
icon="share" | ||
onClick={() => { | ||
action.execute(actionContext); | ||
setIsPopoverOpen(false); | ||
}} | ||
> | ||
{action.getDisplayName(actionContext)} | ||
</EuiContextMenuItem> | ||
); | ||
}); | ||
setItems(nextItems); | ||
}); | ||
|
||
return () => { | ||
cancelled = true; | ||
}; | ||
}, [parentApi, uiActions]); | ||
|
||
return ( | ||
<EuiPopover | ||
button={ | ||
<EuiButton | ||
iconType="arrowDown" | ||
iconSide="right" | ||
onClick={() => { | ||
setIsPopoverOpen(!isPopoverOpen); | ||
}} | ||
> | ||
Add | ||
</EuiButton> | ||
} | ||
isOpen={isPopoverOpen} | ||
closePopover={() => { | ||
setIsPopoverOpen(false); | ||
}} | ||
panelPaddingSize="none" | ||
anchorPosition="downLeft" | ||
> | ||
<EuiContextMenuPanel items={items} /> | ||
</EuiPopover> | ||
); | ||
} |
126 changes: 126 additions & 0 deletions
126
...s/public/app/presentation_container_example/components/presentation_container_example.tsx
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,126 @@ | ||
/* | ||
* 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 React, { useEffect, useMemo } from 'react'; | ||
import { | ||
EuiButtonEmpty, | ||
EuiCallOut, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiSpacer, | ||
EuiSuperDatePicker, | ||
} from '@elastic/eui'; | ||
import { useBatchedPublishingSubjects } from '@kbn/presentation-publishing'; | ||
import { ReactEmbeddableRenderer } from '@kbn/embeddable-plugin/public'; | ||
import { UiActionsStart } from '@kbn/ui-actions-plugin/public'; | ||
import { getParentApi } from '../parent_api'; | ||
import { AddButton } from './add_button'; | ||
import { TopNav } from './top_nav'; | ||
import { lastSavedStateSessionStorage } from '../session_storage/last_saved_state'; | ||
import { unsavedChangesSessionStorage } from '../session_storage/unsaved_changes'; | ||
|
||
export const PresentationContainerExample = ({ uiActions }: { uiActions: UiActionsStart }) => { | ||
const { cleanUp, componentApi, parentApi } = useMemo(() => { | ||
return getParentApi(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
return () => { | ||
cleanUp(); | ||
}; | ||
}, [cleanUp]); | ||
|
||
const [dataLoading, panels, timeRange] = useBatchedPublishingSubjects( | ||
parentApi.dataLoading, | ||
componentApi.panels$, | ||
parentApi.timeRange$ | ||
); | ||
|
||
return ( | ||
<div> | ||
<EuiCallOut title="Presentation Container interfaces"> | ||
<p> | ||
At times, you will need to render many embeddables and allow users to add, remove, and | ||
re-arrange embeddables. Use the <strong>PresentationContainer</strong> and{' '} | ||
<strong>CanAddNewPanel</strong> interfaces for this functionallity. | ||
</p> | ||
<p> | ||
Each embeddable manages its own state. The page is only responsible for persisting and | ||
providing the last persisted state to the embeddable. Implement{' '} | ||
<strong>HasSerializedChildState</strong> interface to provide an embeddable with last | ||
persisted state. Implement <strong>HasRuntimeChildState</strong> interface to provide an | ||
embeddable with a previous session's unsaved changes. | ||
</p> | ||
<p> | ||
This example uses session storage to persist saved state and unsaved changes while a | ||
production implementation may choose to persist state elsewhere. | ||
<EuiButtonEmpty | ||
color={'warning'} | ||
onClick={() => { | ||
lastSavedStateSessionStorage.clear(); | ||
unsavedChangesSessionStorage.clear(); | ||
window.location.reload(); | ||
}} | ||
> | ||
Reset | ||
</EuiButtonEmpty> | ||
</p> | ||
</EuiCallOut> | ||
|
||
<EuiSpacer size="m" /> | ||
|
||
<EuiFlexGroup justifyContent="spaceBetween"> | ||
<EuiFlexItem grow={false}> | ||
<EuiSuperDatePicker | ||
isLoading={dataLoading} | ||
start={timeRange?.from} | ||
end={timeRange?.to} | ||
onTimeChange={({ start, end }) => { | ||
componentApi.setTimeRange({ | ||
from: start, | ||
to: end, | ||
}); | ||
}} | ||
onRefresh={() => { | ||
componentApi.onReload(); | ||
}} | ||
/> | ||
</EuiFlexItem> | ||
|
||
<EuiFlexItem grow={false}> | ||
<TopNav | ||
onSave={componentApi.onSave} | ||
resetUnsavedChanges={parentApi.resetUnsavedChanges} | ||
unsavedChanges$={parentApi.unsavedChanges} | ||
/> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
|
||
<EuiSpacer size="m" /> | ||
|
||
{panels.map(({ id, type }) => { | ||
return ( | ||
<div key={id} style={{ height: '200' }}> | ||
<ReactEmbeddableRenderer | ||
type={type} | ||
maybeId={id} | ||
getParentApi={() => parentApi} | ||
hidePanelChrome={false} | ||
onApiAvailable={(api) => { | ||
componentApi.setChild(id, api); | ||
}} | ||
/> | ||
<EuiSpacer size="s" /> | ||
</div> | ||
); | ||
})} | ||
|
||
<AddButton parentApi={parentApi} uiActions={uiActions} /> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.