Skip to content

Commit

Permalink
Added palette arg to embeddable function
Browse files Browse the repository at this point in the history
Fixed ts errors
  • Loading branch information
cqliu1 committed Oct 26, 2021
1 parent dc6a861 commit 3abb10f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { PaletteOutput } from 'src/plugins/charts/common';
import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
import { ExpressionValueFilter, EmbeddableInput } from '../../../types';
import { EmbeddableExpressionType, EmbeddableExpression } from '../../expression_types';
Expand All @@ -17,6 +18,7 @@ import { InitializeArguments } from '.';
export interface Arguments {
config: string;
type: string;
palette?: PaletteOutput;
}

const defaultTimeRange = {
Expand Down Expand Up @@ -55,6 +57,11 @@ export function embeddableFunctionFactory({
required: true,
help: argHelp.config,
},
palette: {
types: ['palette'],
help: argHelp.palette,
required: false,
},
type: {
types: ['string'],
required: true,
Expand All @@ -76,6 +83,7 @@ export function embeddableFunctionFactory({
...baseEmbeddableInput,
...embeddableInput,
filters: getQueryFilters(filters),
palette: args.palette,
},
generatedAt: Date.now(),
embeddableType: args.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function embeddableInputToExpression(
useGenericEmbeddable?: boolean
): string | undefined {
if (useGenericEmbeddable) {
return genericToExpression(input, embeddableType);
return genericToExpression(input, embeddableType, palettes);
}

if (inputToExpressionTypeMap[embeddableType]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
* 2.0.
*/

import { toExpression } from './embeddable';
import { EmbeddableInput } from '../../../../types';
import { decode } from '../../../../common/lib/embeddable_dataurl';
import { fromExpression } from '@kbn/interpreter/common';
import { chartPluginMock } from 'src/plugins/charts/public/mocks';
import { decode } from '../../../../common/lib/embeddable_dataurl';
import { EmbeddableInput } from '../../../../types';
import { toExpression } from './embeddable';

describe('toExpression', () => {
describe('by-reference embeddable input', () => {
Expand All @@ -21,7 +22,11 @@ describe('toExpression', () => {
it('converts to an embeddable expression', () => {
const input: EmbeddableInput = baseEmbeddableInput;

const expression = toExpression(input, 'visualization');
const expression = toExpression(
input,
'visualization',
chartPluginMock.createPaletteRegistry()
);
const ast = fromExpression(expression);

expect(ast.type).toBe('expression');
Expand All @@ -43,7 +48,11 @@ describe('toExpression', () => {
},
};

const expression = toExpression(input, 'visualization');
const expression = toExpression(
input,
'visualization',
chartPluginMock.createPaletteRegistry()
);
const ast = fromExpression(expression);

const config = decode(ast.chain[0].arguments.config[0] as string);
Expand All @@ -60,7 +69,11 @@ describe('toExpression', () => {
title: '',
};

const expression = toExpression(input, 'visualization');
const expression = toExpression(
input,
'visualization',
chartPluginMock.createPaletteRegistry()
);
const ast = fromExpression(expression);

const config = decode(ast.chain[0].arguments.config[0] as string);
Expand All @@ -78,12 +91,12 @@ describe('toExpression', () => {
it('converts to an embeddable expression', () => {
const input: EmbeddableInput = baseEmbeddableInput;

const expression = toExpression(input, 'visualization');
const expression = toExpression(input, 'lens', chartPluginMock.createPaletteRegistry());
const ast = fromExpression(expression);

expect(ast.type).toBe('expression');
expect(ast.chain[0].function).toBe('embeddable');
expect(ast.chain[0].arguments.type[0]).toBe('visualization');
expect(ast.chain[0].arguments.type[0]).toBe('lens');

const config = decode(ast.chain[0].arguments.config[0] as string);
expect(config.filters).toStrictEqual(input.filters);
Expand All @@ -100,7 +113,7 @@ describe('toExpression', () => {
},
};

const expression = toExpression(input, 'visualization');
const expression = toExpression(input, 'lens', chartPluginMock.createPaletteRegistry());
const ast = fromExpression(expression);

const config = decode(ast.chain[0].arguments.config[0] as string);
Expand All @@ -117,7 +130,7 @@ describe('toExpression', () => {
title: '',
};

const expression = toExpression(input, 'visualization');
const expression = toExpression(input, 'lens', chartPluginMock.createPaletteRegistry());
const ast = fromExpression(expression);

const config = decode(ast.chain[0].arguments.config[0] as string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,34 @@
* 2.0.
*/

import { toExpression as toExpressionString } from '@kbn/interpreter/common';
import { PaletteRegistry } from 'src/plugins/charts/public';
import { encode } from '../../../../common/lib/embeddable_dataurl';
import { EmbeddableInput } from '../../../expression_types';

export function toExpression(input: EmbeddableInput, embeddableType: string): string {
return `embeddable config="${encode(input)}" type="${embeddableType}"`;
/*
Take the input from an embeddable and the type of embeddable and convert it into an expression
*/
export function toExpression(
input: EmbeddableInput,
embeddableType: string,
palettes: PaletteRegistry
): string {
const expressionParts = [] as string[];

expressionParts.push('embeddable');

expressionParts.push(`config="${encode(input)}"`);

expressionParts.push(`type="${embeddableType}"`);

if (input.palette) {
expressionParts.push(
`palette={${toExpressionString(
palettes.get(input.palette.name).toExpression(input.palette.params)
)}}`
);
}

return expressionParts.join(' ');
}
3 changes: 3 additions & 0 deletions x-pack/plugins/canvas/i18n/functions/dict/embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const help: FunctionHelp<FunctionFactory<ReturnType<typeof embeddableFunc
config: i18n.translate('xpack.canvas.functions.embeddable.args.idHelpText', {
defaultMessage: `The base64 encoded embeddable input object`,
}),
palette: i18n.translate('xpack.canvas.functions.embeddable.args.paletteHelpText', {
defaultMessage: `The color palette to use for the embeddable. Only compatible with the Lens visualization`,
}),
type: i18n.translate('xpack.canvas.functions.embeddable.args.typeHelpText', {
defaultMessage: `The embeddable type`,
}),
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/canvas/types/embeddables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import { TimeRange } from 'src/plugins/data/public';
import { Filter } from '@kbn/es-query';
import { PaletteOutput } from 'src/plugins/charts/common';
import { EmbeddableInput as Input } from '../../../../src/plugins/embeddable/common/';

export type EmbeddableInput = Input & {
timeRange?: TimeRange;
filters?: Filter[];
savedObjectId?: string;
palette?: PaletteOutput;
};

0 comments on commit 3abb10f

Please sign in to comment.