Skip to content

Commit

Permalink
🏷️ Add palettes to the minimal set of frameAPI to load
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Feb 10, 2021
1 parent a4f0edf commit 34ec35d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { SavedObjectReference } from 'kibana/public';
import { Ast } from '@kbn/interpreter/common';
import { PaletteRegistry } from 'src/plugins/charts/public';
import {
Datasource,
DatasourcePublicAPI,
Expand Down Expand Up @@ -74,7 +75,8 @@ export function createDatasourceLayers(
export async function persistedStateToExpression(
datasources: Record<string, Datasource>,
visualizations: Record<string, Visualization>,
doc: Document
doc: Document,
palettes: PaletteRegistry
): Promise<{ ast: Ast | null; errors: ErrorMessage[] | undefined }> {
const {
state: { visualization: visualizationState, datasourceStates: persistedDatasourceStates },
Expand Down Expand Up @@ -117,7 +119,7 @@ export async function persistedStateToExpression(
datasourceStates[datasourceId].state,
visualization,
visualizationState,
{ datasourceLayers }
{ datasourceLayers, availablePalettes: palettes }
);

return {
Expand All @@ -139,7 +141,7 @@ export const validateDatasourceAndVisualization = (
currentDatasourceState: unknown | null,
currentVisualization: Visualization | null,
currentVisualizationState: unknown | undefined,
frameAPI: Pick<FramePublicAPI, 'datasourceLayers'>
frameAPI: Pick<FramePublicAPI, 'datasourceLayers' | 'activeData' | 'availablePalettes'>
): ErrorMessage[] | undefined => {
const layersGroups = currentVisualizationState
? currentVisualization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { AttributeService, ViewMode } from '../../../../../../src/plugins/embedd
import { LensAttributeService } from '../../lens_attribute_service';
import { OnSaveProps } from '../../../../../../src/plugins/saved_objects/public/save_modal';
import { act } from 'react-dom/test-utils';
import { chartPluginMock } from 'src/plugins/charts/public/mocks';

jest.mock('../../../../../../src/plugins/inspector/public/', () => ({
isAvailable: false,
Expand Down Expand Up @@ -125,6 +126,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
{} as LensEmbeddableInput
);
Expand Down Expand Up @@ -157,6 +159,7 @@ describe('embeddable', () => {
},
errors: [{ shortMessage: '', longMessage: 'my validation error' }],
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
{} as LensEmbeddableInput
);
Expand Down Expand Up @@ -197,6 +200,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
{} as LensEmbeddableInput
);
Expand Down Expand Up @@ -232,6 +236,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
{ id: '123' } as LensEmbeddableInput
);
Expand Down Expand Up @@ -273,6 +278,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
{ id: '123' } as LensEmbeddableInput
);
Expand Down Expand Up @@ -309,6 +315,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
{ id: '123' } as LensEmbeddableInput
);
Expand Down Expand Up @@ -359,6 +366,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
input
);
Expand Down Expand Up @@ -409,6 +417,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
input
);
Expand Down Expand Up @@ -458,6 +467,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
input
);
Expand Down Expand Up @@ -496,6 +506,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
{ id: '123' } as LensEmbeddableInput
);
Expand Down Expand Up @@ -534,6 +545,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
{ id: '123' } as LensEmbeddableInput
);
Expand Down Expand Up @@ -572,6 +584,7 @@ describe('embeddable', () => {
},
errors: undefined,
}),
palettes: chartPluginMock.createPaletteRegistry(),
},
{ id: '123', timeRange, query, filters } as LensEmbeddableInput
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
TimeRange,
IndexPattern,
} from 'src/plugins/data/public';
import { PaletteOutput } from 'src/plugins/charts/public';
import { PaletteOutput, PaletteRegistry } from 'src/plugins/charts/public';

import { Subscription } from 'rxjs';
import { toExpression, Ast } from '@kbn/interpreter/common';
Expand Down Expand Up @@ -78,8 +78,10 @@ export interface LensEmbeddableOutput extends EmbeddableOutput {

export interface LensEmbeddableDeps {
attributeService: LensAttributeService;
palettes: PaletteRegistry;
documentToExpression: (
doc: Document
doc: Document,
palettes: PaletteRegistry
) => Promise<{ ast: Ast | null; errors: ErrorMessage[] | undefined }>;
editable: boolean;
indexPatternService: IndexPatternsContract;
Expand Down Expand Up @@ -229,7 +231,7 @@ export class Embeddable
type: this.type,
savedObjectId: (input as LensByReferenceInput)?.savedObjectId,
};
const { ast, errors } = await this.deps.documentToExpression(this.savedVis);
const { ast, errors } = await this.deps.documentToExpression(this.savedVis, this.deps.palettes);
this.errors = errors;
this.expression = ast ? toExpression(ast) : null;
await this.initializeOutput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Capabilities, HttpSetup } from 'kibana/public';
import { i18n } from '@kbn/i18n';
import { RecursiveReadonly } from '@kbn/utility-types';
import { Ast } from '@kbn/interpreter/target/common';
import { PaletteRegistry } from 'src/plugins/charts/public';
import {
IndexPatternsContract,
TimefilterContract,
Expand All @@ -34,8 +35,10 @@ export interface LensEmbeddableStartServices {
indexPatternService: IndexPatternsContract;
uiActions?: UiActionsStart;
documentToExpression: (
doc: Document
doc: Document,
palettes: PaletteRegistry
) => Promise<{ ast: Ast | null; errors: ErrorMessage[] | undefined }>;
palettes: PaletteRegistry;
}

export class EmbeddableFactory implements EmbeddableFactoryDefinition {
Expand Down Expand Up @@ -85,6 +88,7 @@ export class EmbeddableFactory implements EmbeddableFactoryDefinition {
coreHttp,
attributeService,
indexPatternService,
palettes,
} = await this.getStartServices();

const { Embeddable } = await import('../../async_services');
Expand All @@ -100,6 +104,7 @@ export class EmbeddableFactory implements EmbeddableFactoryDefinition {
getTrigger: uiActions?.getTrigger,
getTriggerCompatibleActions: uiActions?.getTriggerCompatibleActions,
documentToExpression,
palettes,
},
input,
parent
Expand Down
12 changes: 9 additions & 3 deletions x-pack/plugins/lens/public/editor_frame_service/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Document } from '../persistence/saved_object_store';
import { mergeTables } from './merge_tables';
import { EmbeddableFactory, LensEmbeddableStartServices } from './embeddable/embeddable_factory';
import { UiActionsStart } from '../../../../../src/plugins/ui_actions/public';
import { ChartsPluginSetup } from '../../../../../src/plugins/charts/public';
import { ChartsPluginSetup, PaletteRegistry } from '../../../../../src/plugins/charts/public';
import { DashboardStart } from '../../../../../src/plugins/dashboard/public';
import { LensAttributeService } from '../lens_attribute_service';

Expand Down Expand Up @@ -72,15 +72,20 @@ export class EditorFrameService {
* This is an asynchronous process and should only be triggered once for a saved object.
* @param doc parsed Lens saved object
*/
private documentToExpression = async (doc: Document) => {
private documentToExpression = async (doc: Document, palettes: PaletteRegistry) => {
const [resolvedDatasources, resolvedVisualizations] = await Promise.all([
collectAsyncDefinitions(this.datasources),
collectAsyncDefinitions(this.visualizations),
]);

const { persistedStateToExpression } = await import('../async_services');

return await persistedStateToExpression(resolvedDatasources, resolvedVisualizations, doc);
return await persistedStateToExpression(
resolvedDatasources,
resolvedVisualizations,
doc,
palettes
);
};

public setup(
Expand All @@ -101,6 +106,7 @@ export class EditorFrameService {
documentToExpression: this.documentToExpression,
indexPatternService: deps.data.indexPatterns,
uiActions: deps.uiActions,
palettes: await plugins.charts.palettes.getPalettes(),
};
};

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export interface LensMultiTable {

export interface VisualizationConfigProps<T = unknown> {
layerId: string;
frame: Pick<FramePublicAPI, 'datasourceLayers' | 'activeData'>;
frame: Pick<FramePublicAPI, 'datasourceLayers' | 'activeData' | 'availablePalettes'>;
state: T;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function getColorAssignments(

export function getAccessorColorConfig(
colorAssignments: ColorAssignments,
frame: FramePublicAPI,
frame: Pick<FramePublicAPI, 'datasourceLayers'>,
layer: XYLayerConfig,
paletteService: PaletteRegistry
): AccessorConfig[] {
Expand Down

0 comments on commit 34ec35d

Please sign in to comment.