2020-04-27 13:50:33 -05:00
|
|
|
import { DashboardModel, PanelModel } from '../../../state';
|
2021-02-25 04:26:28 -06:00
|
|
|
import { CoreEvents, ThunkResult } from 'app/types';
|
|
|
|
import { appEvents } from 'app/core/core';
|
|
|
|
import { SaveLibraryPanelModal } from 'app/features/library-panels/components/SaveLibraryPanelModal/SaveLibraryPanelModal';
|
2020-02-15 13:31:11 -06:00
|
|
|
import {
|
|
|
|
closeCompleted,
|
2020-04-27 13:50:33 -05:00
|
|
|
PANEL_EDITOR_UI_STATE_STORAGE_KEY,
|
2020-02-15 13:31:11 -06:00
|
|
|
PanelEditorUIState,
|
|
|
|
setPanelEditorUIState,
|
2020-04-27 13:50:33 -05:00
|
|
|
updateEditorInitState,
|
2020-02-15 13:31:11 -06:00
|
|
|
} from './reducers';
|
2021-02-25 04:26:28 -06:00
|
|
|
import { updateLocation } from 'app/core/actions';
|
2020-02-28 04:04:40 -06:00
|
|
|
import { cleanUpEditPanel, panelModelAndPluginReady } from '../../../state/reducers';
|
2020-11-25 04:24:06 -06:00
|
|
|
import store from 'app/core/store';
|
2021-02-25 04:26:28 -06:00
|
|
|
import pick from 'lodash/pick';
|
|
|
|
import omit from 'lodash/omit';
|
|
|
|
import isEqual from 'lodash/isEqual';
|
2020-02-11 07:57:16 -06:00
|
|
|
|
|
|
|
export function initPanelEditor(sourcePanel: PanelModel, dashboard: DashboardModel): ThunkResult<void> {
|
2021-01-20 00:59:48 -06:00
|
|
|
return (dispatch) => {
|
2020-04-10 09:37:26 -05:00
|
|
|
const panel = dashboard.initEditPanel(sourcePanel);
|
2020-02-11 07:57:16 -06:00
|
|
|
|
|
|
|
dispatch(
|
|
|
|
updateEditorInitState({
|
|
|
|
panel,
|
|
|
|
sourcePanel,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-25 04:26:28 -06:00
|
|
|
export function updateSourcePanel(sourcePanel: PanelModel): ThunkResult<void> {
|
|
|
|
return (dispatch, getStore) => {
|
|
|
|
const { getPanel } = getStore().panelEditor;
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
updateEditorInitState({
|
|
|
|
panel: getPanel(),
|
|
|
|
sourcePanel,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function exitPanelEditor(): ThunkResult<void> {
|
|
|
|
return (dispatch, getStore) => {
|
|
|
|
const dashboard = getStore().dashboard.getModel();
|
|
|
|
const { getPanel, getSourcePanel, shouldDiscardChanges } = getStore().panelEditor;
|
|
|
|
const onConfirm = () =>
|
|
|
|
dispatch(
|
|
|
|
updateLocation({
|
|
|
|
query: { editPanel: null, tab: null },
|
|
|
|
partial: true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const modifiedPanel = getPanel();
|
|
|
|
const modifiedSaveModel = modifiedPanel.getSaveModel();
|
|
|
|
const initialSaveModel = getSourcePanel().getSaveModel();
|
|
|
|
const panelChanged = !isEqual(omit(initialSaveModel, 'id'), omit(modifiedSaveModel, 'id'));
|
|
|
|
if (shouldDiscardChanges || !modifiedPanel.libraryPanel || !panelChanged) {
|
|
|
|
onConfirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
appEvents.emit(CoreEvents.showModalReact, {
|
|
|
|
component: SaveLibraryPanelModal,
|
|
|
|
props: {
|
|
|
|
panel: modifiedPanel,
|
|
|
|
folderId: dashboard!.meta.folderId,
|
|
|
|
isOpen: true,
|
|
|
|
onConfirm,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateDuplicateLibraryPanels(modifiedPanel: PanelModel, dashboard: DashboardModel, dispatch: any) {
|
|
|
|
if (modifiedPanel.libraryPanel?.uid === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const modifiedSaveModel = modifiedPanel.getSaveModel();
|
|
|
|
for (const panel of dashboard.panels) {
|
|
|
|
if (panel.libraryPanel?.uid !== modifiedPanel.libraryPanel!.uid) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
panel.restoreModel({
|
|
|
|
...modifiedSaveModel,
|
|
|
|
...pick(panel, 'gridPos', 'id'),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Loaded plugin is not included in the persisted properties
|
|
|
|
// So is not handled by restoreModel
|
|
|
|
panel.plugin = modifiedSaveModel.plugin;
|
|
|
|
|
|
|
|
if (panel.type !== modifiedPanel.type) {
|
|
|
|
dispatch(panelModelAndPluginReady({ panelId: panel.id, plugin: panel.plugin! }));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resend last query result on source panel query runner
|
|
|
|
// But do this after the panel edit editor exit process has completed
|
|
|
|
setTimeout(() => {
|
|
|
|
panel.getQueryRunner().useLastResultFrom(modifiedPanel.getQueryRunner());
|
|
|
|
}, 20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
export function panelEditorCleanUp(): ThunkResult<void> {
|
|
|
|
return (dispatch, getStore) => {
|
|
|
|
const dashboard = getStore().dashboard.getModel();
|
2020-05-25 07:05:43 -05:00
|
|
|
const { getPanel, getSourcePanel, shouldDiscardChanges } = getStore().panelEditor;
|
2020-03-24 09:44:13 -05:00
|
|
|
|
2020-02-11 07:57:16 -06:00
|
|
|
if (!shouldDiscardChanges) {
|
2020-02-13 09:06:45 -06:00
|
|
|
const panel = getPanel();
|
|
|
|
const modifiedSaveModel = panel.getSaveModel();
|
|
|
|
const sourcePanel = getSourcePanel();
|
2020-02-28 04:04:40 -06:00
|
|
|
const panelTypeChanged = sourcePanel.type !== panel.type;
|
2020-02-13 09:06:45 -06:00
|
|
|
|
2021-02-25 04:26:28 -06:00
|
|
|
updateDuplicateLibraryPanels(panel, dashboard!, dispatch);
|
|
|
|
|
2020-02-13 09:06:45 -06:00
|
|
|
// restore the source panel id before we update source panel
|
|
|
|
modifiedSaveModel.id = sourcePanel.id;
|
|
|
|
|
|
|
|
sourcePanel.restoreModel(modifiedSaveModel);
|
2020-02-26 06:39:23 -06:00
|
|
|
|
2020-05-01 07:14:52 -05:00
|
|
|
// Loaded plugin is not included in the persisted properties
|
|
|
|
// So is not handled by restoreModel
|
|
|
|
sourcePanel.plugin = panel.plugin;
|
|
|
|
|
2020-02-28 04:04:40 -06:00
|
|
|
if (panelTypeChanged) {
|
2020-03-24 09:44:13 -05:00
|
|
|
dispatch(panelModelAndPluginReady({ panelId: sourcePanel.id, plugin: panel.plugin! }));
|
2020-02-28 04:04:40 -06:00
|
|
|
}
|
|
|
|
|
2020-02-26 06:39:23 -06:00
|
|
|
// Resend last query result on source panel query runner
|
|
|
|
// But do this after the panel edit editor exit process has completed
|
|
|
|
setTimeout(() => {
|
2020-05-06 09:06:21 -05:00
|
|
|
sourcePanel.getQueryRunner().useLastResultFrom(panel.getQueryRunner());
|
2020-02-28 04:04:40 -06:00
|
|
|
}, 20);
|
2020-02-11 07:57:16 -06:00
|
|
|
}
|
|
|
|
|
2020-03-24 09:44:13 -05:00
|
|
|
if (dashboard) {
|
|
|
|
dashboard.exitPanelEditor();
|
|
|
|
}
|
|
|
|
|
2020-02-22 14:08:42 -06:00
|
|
|
dispatch(cleanUpEditPanel());
|
2020-02-12 11:36:32 -06:00
|
|
|
dispatch(closeCompleted());
|
2020-02-11 07:57:16 -06:00
|
|
|
};
|
|
|
|
}
|
2020-02-15 13:31:11 -06:00
|
|
|
|
|
|
|
export function updatePanelEditorUIState(uiState: Partial<PanelEditorUIState>): ThunkResult<void> {
|
|
|
|
return (dispatch, getStore) => {
|
2020-04-20 01:47:25 -05:00
|
|
|
const nextState = { ...getStore().panelEditor.ui, ...uiState };
|
2020-02-15 13:31:11 -06:00
|
|
|
dispatch(setPanelEditorUIState(nextState));
|
2020-04-20 08:13:02 -05:00
|
|
|
try {
|
|
|
|
store.setObject(PANEL_EDITOR_UI_STATE_STORAGE_KEY, nextState);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2020-02-15 13:31:11 -06:00
|
|
|
};
|
|
|
|
}
|