-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[Dashboard First] Unlink from Library Action With ReferenceOrValueEmbeddable #74905
Changes from all commits
b3c450d
77a7e94
171268b
14dcd6a
6ba38d0
d21daef
e8abb12
19b2bc0
98f3e56
230341d
3586923
ed0cb93
f01c5ac
f2727aa
0ac821c
957b7d3
73c1cdd
37cca10
5798261
21c7e03
1060914
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* 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 { isErrorEmbeddable, IContainer, ReferenceOrValueEmbeddable } from '../../embeddable_plugin'; | ||
import { DashboardContainer } from '../embeddable'; | ||
import { getSampleDashboardInput } from '../test_helpers'; | ||
import { | ||
CONTACT_CARD_EMBEDDABLE, | ||
ContactCardEmbeddableFactory, | ||
ContactCardEmbeddable, | ||
ContactCardEmbeddableInput, | ||
ContactCardEmbeddableOutput, | ||
} from '../../embeddable_plugin_test_samples'; | ||
import { coreMock } from '../../../../../core/public/mocks'; | ||
import { CoreStart } from 'kibana/public'; | ||
import { UnlinkFromLibraryAction } from '.'; | ||
import { embeddablePluginMock } from 'src/plugins/embeddable/public/mocks'; | ||
import { ViewMode } from '../../../../embeddable/public'; | ||
|
||
const { setup, doStart } = embeddablePluginMock.createInstance(); | ||
setup.registerEmbeddableFactory( | ||
CONTACT_CARD_EMBEDDABLE, | ||
new ContactCardEmbeddableFactory((() => null) as any, {} as any) | ||
); | ||
const start = doStart(); | ||
|
||
let container: DashboardContainer; | ||
let embeddable: ContactCardEmbeddable & ReferenceOrValueEmbeddable; | ||
let coreStart: CoreStart; | ||
beforeEach(async () => { | ||
coreStart = coreMock.createStart(); | ||
|
||
const containerOptions = { | ||
ExitFullScreenButton: () => null, | ||
SavedObjectFinder: () => null, | ||
application: {} as any, | ||
embeddable: start, | ||
inspector: {} as any, | ||
notifications: {} as any, | ||
overlays: coreStart.overlays, | ||
savedObjectMetaData: {} as any, | ||
uiActions: {} as any, | ||
}; | ||
|
||
container = new DashboardContainer(getSampleDashboardInput(), containerOptions); | ||
|
||
const contactCardEmbeddable = await container.addNewEmbeddable< | ||
ContactCardEmbeddableInput, | ||
ContactCardEmbeddableOutput, | ||
ContactCardEmbeddable | ||
>(CONTACT_CARD_EMBEDDABLE, { | ||
firstName: 'Kibanana', | ||
}); | ||
|
||
if (isErrorEmbeddable(contactCardEmbeddable)) { | ||
throw new Error('Failed to create embeddable'); | ||
} | ||
embeddable = embeddablePluginMock.mockRefOrValEmbeddable< | ||
ContactCardEmbeddable, | ||
ContactCardEmbeddableInput | ||
>(contactCardEmbeddable, { | ||
mockedByReferenceInput: { savedObjectId: 'testSavedObjectId', id: contactCardEmbeddable.id }, | ||
mockedByValueInput: { firstName: 'Kibanana', id: contactCardEmbeddable.id }, | ||
}); | ||
embeddable.updateInput({ viewMode: ViewMode.EDIT }); | ||
}); | ||
|
||
test('Unlink is compatible when embeddable on dashboard has reference type input', async () => { | ||
const action = new UnlinkFromLibraryAction(); | ||
embeddable.updateInput(await embeddable.getInputAsRefType()); | ||
expect(await action.isCompatible({ embeddable })).toBe(true); | ||
}); | ||
|
||
test('Unlink is not compatible when embeddable input is by value', async () => { | ||
const action = new UnlinkFromLibraryAction(); | ||
embeddable.updateInput(await embeddable.getInputAsValueType()); | ||
expect(await action.isCompatible({ embeddable })).toBe(false); | ||
}); | ||
|
||
test('Unlink is not compatible when view mode is set to view', async () => { | ||
const action = new UnlinkFromLibraryAction(); | ||
embeddable.updateInput(await embeddable.getInputAsRefType()); | ||
embeddable.updateInput({ viewMode: ViewMode.VIEW }); | ||
expect(await action.isCompatible({ embeddable })).toBe(false); | ||
}); | ||
|
||
test('Unlink is not compatible when embeddable is not in a dashboard container', async () => { | ||
let orphanContactCard = await container.addNewEmbeddable< | ||
ContactCardEmbeddableInput, | ||
ContactCardEmbeddableOutput, | ||
ContactCardEmbeddable | ||
>(CONTACT_CARD_EMBEDDABLE, { | ||
firstName: 'Orphan', | ||
}); | ||
orphanContactCard = embeddablePluginMock.mockRefOrValEmbeddable< | ||
ContactCardEmbeddable, | ||
ContactCardEmbeddableInput | ||
>(orphanContactCard, { | ||
mockedByReferenceInput: { savedObjectId: 'test', id: orphanContactCard.id }, | ||
mockedByValueInput: { firstName: 'Kibanana', id: orphanContactCard.id }, | ||
}); | ||
const action = new UnlinkFromLibraryAction(); | ||
expect(await action.isCompatible({ embeddable: orphanContactCard })).toBe(false); | ||
}); | ||
|
||
test('Unlink replaces embeddableId but retains panel count', async () => { | ||
const dashboard = embeddable.getRoot() as IContainer; | ||
const originalPanelCount = Object.keys(dashboard.getInput().panels).length; | ||
const originalPanelKeySet = new Set(Object.keys(dashboard.getInput().panels)); | ||
const action = new UnlinkFromLibraryAction(); | ||
await action.execute({ embeddable }); | ||
expect(Object.keys(container.getInput().panels).length).toEqual(originalPanelCount); | ||
|
||
const newPanelId = Object.keys(container.getInput().panels).find( | ||
(key) => !originalPanelKeySet.has(key) | ||
); | ||
expect(newPanelId).toBeDefined(); | ||
const newPanel = container.getInput().panels[newPanelId!]; | ||
expect(newPanel.type).toEqual(embeddable.type); | ||
}); | ||
|
||
test('Unlink unwraps all attributes from savedObject', async () => { | ||
const complicatedAttributes = { | ||
attribute1: 'The best attribute', | ||
attribute2: 22, | ||
attribute3: ['array', 'of', 'strings'], | ||
attribute4: { nestedattribute: 'hello from the nest' }, | ||
}; | ||
|
||
embeddable = embeddablePluginMock.mockRefOrValEmbeddable<ContactCardEmbeddable>(embeddable, { | ||
mockedByReferenceInput: { savedObjectId: 'testSavedObjectId', id: embeddable.id }, | ||
mockedByValueInput: { attributes: complicatedAttributes, id: embeddable.id }, | ||
}); | ||
const dashboard = embeddable.getRoot() as IContainer; | ||
const originalPanelKeySet = new Set(Object.keys(dashboard.getInput().panels)); | ||
const action = new UnlinkFromLibraryAction(); | ||
await action.execute({ embeddable }); | ||
const newPanelId = Object.keys(container.getInput().panels).find( | ||
(key) => !originalPanelKeySet.has(key) | ||
); | ||
expect(newPanelId).toBeDefined(); | ||
const newPanel = container.getInput().panels[newPanelId!]; | ||
expect(newPanel.type).toEqual(embeddable.type); | ||
expect(newPanel.explicitInput.attributes).toEqual(complicatedAttributes); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import _ from 'lodash'; | ||
import uuid from 'uuid'; | ||
import { ActionByType, IncompatibleActionError } from '../../ui_actions_plugin'; | ||
import { ViewMode, PanelState, IEmbeddable } from '../../embeddable_plugin'; | ||
import { | ||
PanelNotFoundError, | ||
EmbeddableInput, | ||
isReferenceOrValueEmbeddable, | ||
} from '../../../../embeddable/public'; | ||
import { DashboardPanelState, DASHBOARD_CONTAINER_TYPE, DashboardContainer } from '..'; | ||
|
||
export const ACTION_UNLINK_FROM_LIBRARY = 'unlinkFromLibrary'; | ||
|
||
export interface UnlinkFromLibraryActionContext { | ||
embeddable: IEmbeddable; | ||
} | ||
|
||
export class UnlinkFromLibraryAction implements ActionByType<typeof ACTION_UNLINK_FROM_LIBRARY> { | ||
public readonly type = ACTION_UNLINK_FROM_LIBRARY; | ||
public readonly id = ACTION_UNLINK_FROM_LIBRARY; | ||
public order = 15; | ||
|
||
constructor() {} | ||
|
||
public getDisplayName({ embeddable }: UnlinkFromLibraryActionContext) { | ||
if (!embeddable.getRoot() || !embeddable.getRoot().isContainer) { | ||
throw new IncompatibleActionError(); | ||
} | ||
return i18n.translate('dashboard.panel.unlinkFromLibrary', { | ||
defaultMessage: 'Unlink from library item', | ||
}); | ||
} | ||
|
||
public getIconType({ embeddable }: UnlinkFromLibraryActionContext) { | ||
if (!embeddable.getRoot() || !embeddable.getRoot().isContainer) { | ||
throw new IncompatibleActionError(); | ||
} | ||
return 'folderExclamation'; | ||
} | ||
|
||
public async isCompatible({ embeddable }: UnlinkFromLibraryActionContext) { | ||
return Boolean( | ||
embeddable.getInput()?.viewMode !== ViewMode.VIEW && | ||
embeddable.getRoot() && | ||
embeddable.getRoot().isContainer && | ||
embeddable.getRoot().type === DASHBOARD_CONTAINER_TYPE && | ||
isReferenceOrValueEmbeddable(embeddable) && | ||
embeddable.inputIsRefType(embeddable.getInput()) | ||
); | ||
} | ||
|
||
public async execute({ embeddable }: UnlinkFromLibraryActionContext) { | ||
if (!isReferenceOrValueEmbeddable(embeddable)) { | ||
throw new IncompatibleActionError(); | ||
} | ||
|
||
const newInput = await embeddable.getInputAsValueType(); | ||
embeddable.updateInput(newInput); | ||
|
||
const dashboard = embeddable.getRoot() as DashboardContainer; | ||
const panelToReplace = dashboard.getInput().panels[embeddable.id] as DashboardPanelState; | ||
if (!panelToReplace) { | ||
throw new PanelNotFoundError(); | ||
} | ||
|
||
const newPanel: PanelState<EmbeddableInput> = { | ||
type: embeddable.type, | ||
explicitInput: { ...newInput, id: uuid.v4() }, | ||
}; | ||
dashboard.replacePanel(panelToReplace, newPanel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not related to this PR, but really shows how much we need an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I absolutely agree, fingers crossed that this #74253 will handle it. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we simplify this statement a bit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that getting the
explicitInput
of an embeddable will be a common enough task.The reason this is needed is so that the 'unlink' or 'add' actions don't make inherited input into explicit input.
I added a helper to the attribute service in the add to library PR #75098.:
kibana/src/plugins/dashboard/public/attribute_service/attribute_service.tsx
Lines 126 to 133 in b41eae8
It can be simplified a little bit with optional chaining, and I will update it to reflect that over there.