Skip to content

Commit

Permalink
Moved addOrUpdateEmbeddable and its usages from elastic#70272
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomThomson committed Jul 8, 2020
1 parent c815c96 commit f5a3135
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,17 @@ export class DashboardAppController {
.getIncomingEmbeddablePackage();
if (incomingState) {
if ('id' in incomingState) {
container.addNewEmbeddable<EmbeddableInput>(incomingState.type, {
container.addOrUpdateEmbeddable<SavedObjectEmbeddableInput>(incomingState.type, {
savedObjectId: incomingState.id,
id: incomingState.embeddableIdToReplace,
});
} else if ('input' in incomingState) {
const input = incomingState.input;
delete input.id;
const explicitInput = {
savedVis: input,
};
container.addNewEmbeddable<EmbeddableInput>(incomingState.type, explicitInput);
container.addOrUpdateEmbeddable<EmbeddableInput>(incomingState.type, explicitInput);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
} from '../../../../kibana_react/public';
import { PLACEHOLDER_EMBEDDABLE } from './placeholder';
import { PanelPlacementMethod, IPanelPlacementArgs } from './panel/dashboard_panel_placement';
import { EmbeddableStateTransfer } from '../../../../embeddable/public';
import { EmbeddableStateTransfer, EmbeddableOutput } from '../../../../embeddable/public';

export interface DashboardContainerInput extends ContainerInput {
viewMode: ViewMode;
Expand Down Expand Up @@ -159,29 +159,55 @@ export class DashboardContainer extends Container<InheritedChildInput, Dashboard
[placeholderPanelState.explicitInput.id]: placeholderPanelState,
},
});
newStateComplete.then((newPanelState: Partial<PanelState>) => {
const finalPanels = { ...this.input.panels };
delete finalPanels[placeholderPanelState.explicitInput.id];
const newPanelId = newPanelState.explicitInput?.id
? newPanelState.explicitInput.id
: uuid.v4();
finalPanels[newPanelId] = {
...placeholderPanelState,
...newPanelState,
gridData: {
...placeholderPanelState.gridData,
i: newPanelId,
},
newStateComplete.then((newPanelState: Partial<PanelState>) =>
this.replacePanel(placeholderPanelState, newPanelState)
);
}

public replacePanel(
previousPanelState: DashboardPanelState<EmbeddableInput>,
newPanelState: Partial<PanelState>
) {
// TODO: In the current infrastructure, embeddables in a container do not react properly to
// changes. Removing the existing embeddable, and adding a new one is a temporary workaround
// until the container logic is fixed.
const finalPanels = { ...this.input.panels };
delete finalPanels[previousPanelState.explicitInput.id];
const newPanelId = newPanelState.explicitInput?.id ? newPanelState.explicitInput.id : uuid.v4();
finalPanels[newPanelId] = {
...previousPanelState,
...newPanelState,
gridData: {
...previousPanelState.gridData,
i: newPanelId,
},
explicitInput: {
...newPanelState.explicitInput,
id: newPanelId,
},
};
this.updateInput({
panels: finalPanels,
lastReloadRequestTime: new Date().getTime(),
});
}

public async addOrUpdateEmbeddable<
EEI extends EmbeddableInput = EmbeddableInput,
EEO extends EmbeddableOutput = EmbeddableOutput,
E extends IEmbeddable<EEI, EEO> = IEmbeddable<EEI, EEO>
>(type: string, explicitInput: Partial<EEI>) {
if (explicitInput.id && this.input.panels[explicitInput.id]) {
this.replacePanel(this.input.panels[explicitInput.id], {
type,
explicitInput: {
...newPanelState.explicitInput,
id: newPanelId,
...explicitInput,
id: uuid.v4(),
},
};
this.updateInput({
panels: finalPanels,
lastReloadRequestTime: new Date().getTime(),
});
});
} else {
this.addNewEmbeddable<EEI, EEO, E>(type, explicitInput);
}
}

public render(dom: HTMLElement) {
Expand Down

0 comments on commit f5a3135

Please sign in to comment.