Skip to content

Commit

Permalink
Merge branch 'master' into asyncSearchTest
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored Jun 24, 2020
2 parents 169c36e + 9e00da3 commit c238e60
Show file tree
Hide file tree
Showing 33 changed files with 934 additions and 412 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/canvas/.storybook/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ function loadStories() {
// Find all files ending in *.examples.ts
const req = require.context('./..', true, /.(stories|examples).tsx$/);
req.keys().forEach(filename => req(filename));

// Import Canvas CSS
require('../public/style/index.scss')
}

// Set up the Storybook environment with custom settings.
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/canvas/.storybook/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ module.exports = async ({ config }) => {
config.resolve.alias['ui/url/absolute_to_parsed_url'] = path.resolve(__dirname, '../tasks/mocks/uiAbsoluteToParsedUrl');
config.resolve.alias['ui/chrome'] = path.resolve(__dirname, '../tasks/mocks/uiChrome');
config.resolve.alias.ui = path.resolve(KIBANA_ROOT, 'src/legacy/ui/public');
config.resolve.alias['src/legacy/ui/public/styles/styling_constants'] = path.resolve(KIBANA_ROOT, 'src/legacy/ui/public/styles/_styling_constants.scss');
config.resolve.alias.ng_mock$ = path.resolve(KIBANA_ROOT, 'src/test_utils/public/ng_mock');

return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { functionWrapper } from '../../../__tests__/helpers/function_wrapper';
import { palettes } from '../../../common/lib/palettes';
import { paulTor14 } from '../../../common/lib/palettes';
import { palette } from './palette';

describe('palette', () => {
Expand All @@ -25,7 +25,7 @@ describe('palette', () => {

it('defaults to pault_tor_14 colors', () => {
const result = fn(null);
expect(result.colors).toEqual(palettes.paul_tor_14.colors);
expect(result.colors).toEqual(paulTor14.colors);
});
});

Expand All @@ -47,17 +47,17 @@ describe('palette', () => {
describe('reverse', () => {
it('reverses order of the colors', () => {
const result = fn(null, { reverse: true });
expect(result.colors).toEqual(palettes.paul_tor_14.colors.reverse());
expect(result.colors).toEqual(paulTor14.colors.reverse());
});

it('keeps the original order of the colors', () => {
const result = fn(null, { reverse: false });
expect(result.colors).toEqual(palettes.paul_tor_14.colors);
expect(result.colors).toEqual(paulTor14.colors);
});

it(`defaults to 'false`, () => {
const result = fn(null);
expect(result.colors).toEqual(palettes.paul_tor_14.colors);
expect(result.colors).toEqual(paulTor14.colors);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/

import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
// @ts-expect-error untyped local
import { palettes } from '../../../common/lib/palettes';
import { paulTor14 } from '../../../common/lib/palettes';
import { getFunctionHelp } from '../../../i18n';

interface Arguments {
Expand Down Expand Up @@ -52,7 +51,7 @@ export function palette(): ExpressionFunctionDefinition<'palette', null, Argumen
},
fn: (input, args) => {
const { color, reverse, gradient } = args;
const colors = ([] as string[]).concat(color || palettes.paul_tor_14.colors);
const colors = ([] as string[]).concat(color || paulTor14.colors);

return {
type: 'palette',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { storiesOf } from '@storybook/react';
import React from 'react';
import { action } from '@storybook/addon-actions';
import { PaletteArgInput } from '../palette';
import { paulTor14 } from '../../../../common/lib/palettes';

storiesOf('arguments/Palette', module).add('default', () => (
<div className="canvasContainerWrapper" style={{ width: '200px' }}>
<PaletteArgInput
argValue={{
type: 'expression',
chain: [
{
arguments: {
_: paulTor14.colors,
gradient: [paulTor14.gradient],
},
function: 'palette',
type: 'function',
},
],
}}
onValueChange={action('onValueChange')}
renderError={action('renderError')}
/>
</div>
));
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { imageUpload } from './image_upload';
// @ts-expect-error untyped local
import { number } from './number';
import { numberFormatInitializer } from './number_format';
// @ts-expect-error untyped local
import { palette } from './palette';
// @ts-expect-error untyped local
import { percentage } from './percentage';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,63 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { FC } from 'react';
import PropTypes from 'prop-types';
import { get } from 'lodash';
import { getType } from '@kbn/interpreter/common';
import { ExpressionAstFunction, ExpressionAstExpression } from 'src/plugins/expressions';
import { PalettePicker } from '../../../public/components/palette_picker';
import { templateFromReactComponent } from '../../../public/lib/template_from_react_component';
import { ArgumentStrings } from '../../../i18n';
import { identifyPalette, ColorPalette } from '../../../common/lib';

const { Palette: strings } = ArgumentStrings;

const PaletteArgInput = ({ onValueChange, argValue, renderError }) => {
// Why is this neccesary? Does the dialog really need to know what parameter it is setting?

const throwNotParsed = () => renderError();
interface Props {
onValueChange: (value: ExpressionAstExpression) => void;
argValue: ExpressionAstExpression;
renderError: () => void;
argId?: string;
}

export const PaletteArgInput: FC<Props> = ({ onValueChange, argId, argValue, renderError }) => {
// TODO: This is weird, its basically a reimplementation of what the interpretter would return.
// Probably a better way todo this, and maybe a better way to handle template stype objects in general?
function astToPalette({ chain }) {
// Probably a better way todo this, and maybe a better way to handle template type objects in general?
const astToPalette = ({ chain }: { chain: ExpressionAstFunction[] }): ColorPalette | null => {
if (chain.length !== 1 || chain[0].function !== 'palette') {
throwNotParsed();
renderError();
return null;
}

try {
const colors = chain[0].arguments._.map((astObj) => {
if (getType(astObj) !== 'string') {
throwNotParsed();
renderError();
}
return astObj;
});
}) as string[];

const gradient = get(chain[0].arguments.gradient, '[0]');
const gradient = get<boolean>(chain[0].arguments.gradient, '[0]');
const palette = identifyPalette({ colors, gradient });

return { colors, gradient };
if (palette) {
return palette;
}

return ({
id: 'custom',
label: strings.getCustomPaletteLabel(),
colors,
gradient,
} as any) as ColorPalette;
} catch (e) {
throwNotParsed();
renderError();
}
}
return null;
};

function handleChange(palette) {
const astObj = {
const handleChange = (palette: ColorPalette): void => {
const astObj: ExpressionAstExpression = {
type: 'expression',
chain: [
{
Expand All @@ -57,16 +75,20 @@ const PaletteArgInput = ({ onValueChange, argValue, renderError }) => {
};

onValueChange(astObj);
}
};

const palette = astToPalette(argValue);

return (
<PalettePicker value={palette} onChange={handleChange} ariaLabel={strings.getDisplayName()} />
);
if (!palette) {
renderError();
return null;
}

return <PalettePicker id={argId} palette={palette} onChange={handleChange} />;
};

PaletteArgInput.propTypes = {
argId: PropTypes.string,
onValueChange: PropTypes.func.isRequired,
argValue: PropTypes.any.isRequired,
renderError: PropTypes.func,
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/canvas/common/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export * from './hex_to_rgb';
export * from './httpurl';
// @ts-expect-error missing local definition
export * from './missing_asset';
// @ts-expect-error missing local definition
export * from './palettes';
export * from './pivot_object_array';
// @ts-expect-error missing local definition
Expand Down
Loading

0 comments on commit c238e60

Please sign in to comment.