|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import { i18n } from '@kbn/i18n'; |
| 10 | +import { ExpressionShapeFunction, Shape } from '../types'; |
| 11 | +import { SVG } from '../constants'; |
| 12 | +import { getAvailableShapes } from '../lib'; |
| 13 | + |
| 14 | +export const strings = { |
| 15 | + help: i18n.translate('expressionShape.functions.shapeHelpText', { |
| 16 | + defaultMessage: 'Creates a shape.', |
| 17 | + }), |
| 18 | + args: { |
| 19 | + shape: i18n.translate('expressionShape.functions.shape.args.shapeHelpText', { |
| 20 | + defaultMessage: 'Pick a shape.', |
| 21 | + }), |
| 22 | + border: i18n.translate('expressionShape.functions.shape.args.borderHelpText', { |
| 23 | + defaultMessage: 'An {SVG} color for the border outlining the shape.', |
| 24 | + values: { |
| 25 | + SVG, |
| 26 | + }, |
| 27 | + }), |
| 28 | + borderWidth: i18n.translate('expressionShape.functions.shape.args.borderWidthHelpText', { |
| 29 | + defaultMessage: 'The thickness of the border.', |
| 30 | + }), |
| 31 | + fill: i18n.translate('expressionShape.functions.shape.args.fillHelpText', { |
| 32 | + defaultMessage: 'An {SVG} color to fill the shape.', |
| 33 | + values: { |
| 34 | + SVG, |
| 35 | + }, |
| 36 | + }), |
| 37 | + maintainAspect: i18n.translate('expressionShape.functions.shape.args.maintainAspectHelpText', { |
| 38 | + defaultMessage: `Maintain the shape's original aspect ratio?`, |
| 39 | + }), |
| 40 | + }, |
| 41 | +}; |
| 42 | + |
| 43 | +export const errors = { |
| 44 | + invalidShape: (shape: string) => |
| 45 | + new Error( |
| 46 | + i18n.translate('expressionShape.functions.shape.invalidShapeErrorMessage', { |
| 47 | + defaultMessage: "Invalid value: '{shape}'. Such a shape doesn't exist.", |
| 48 | + values: { |
| 49 | + shape, |
| 50 | + }, |
| 51 | + }) |
| 52 | + ), |
| 53 | +}; |
| 54 | + |
| 55 | +export const shapeFunction: ExpressionShapeFunction = () => { |
| 56 | + const { help, args: argHelp } = strings; |
| 57 | + |
| 58 | + return { |
| 59 | + name: 'shape', |
| 60 | + aliases: [], |
| 61 | + inputTypes: ['null'], |
| 62 | + help, |
| 63 | + args: { |
| 64 | + shape: { |
| 65 | + types: ['string'], |
| 66 | + help: argHelp.shape, |
| 67 | + aliases: ['_'], |
| 68 | + default: 'square', |
| 69 | + options: Object.values(Shape), |
| 70 | + }, |
| 71 | + border: { |
| 72 | + types: ['string'], |
| 73 | + aliases: ['stroke'], |
| 74 | + help: argHelp.border, |
| 75 | + }, |
| 76 | + borderWidth: { |
| 77 | + types: ['number'], |
| 78 | + aliases: ['strokeWidth'], |
| 79 | + help: argHelp.borderWidth, |
| 80 | + default: 0, |
| 81 | + }, |
| 82 | + fill: { |
| 83 | + types: ['string'], |
| 84 | + help: argHelp.fill, |
| 85 | + default: 'black', |
| 86 | + }, |
| 87 | + maintainAspect: { |
| 88 | + types: ['boolean'], |
| 89 | + help: argHelp.maintainAspect, |
| 90 | + default: false, |
| 91 | + options: [true, false], |
| 92 | + }, |
| 93 | + }, |
| 94 | + fn: (input, args) => { |
| 95 | + const avaliableShapes = getAvailableShapes(); |
| 96 | + if (!avaliableShapes.includes(args.shape)) { |
| 97 | + throw errors.invalidShape(args.shape); |
| 98 | + } |
| 99 | + |
| 100 | + return { |
| 101 | + type: 'shape', |
| 102 | + ...args, |
| 103 | + }; |
| 104 | + }, |
| 105 | + }; |
| 106 | +}; |
0 commit comments