|
| 1 | +// Copyright The Perses Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +export const legendPositions = ['bottom', 'right'] as const; |
| 15 | +export type LegendPositions = (typeof legendPositions)[number]; |
| 16 | + |
| 17 | +export const legendModes = ['list', 'table'] as const; |
| 18 | +export type LegendMode = (typeof legendModes)[number]; |
| 19 | + |
| 20 | +export const legendSizes = ['small', 'medium'] as const; |
| 21 | +export type LegendSize = (typeof legendSizes)[number]; |
| 22 | + |
| 23 | +// Common legend options used across some UI components and panel specifications |
| 24 | +export interface LegendOptionsBase { |
| 25 | + position: LegendPositions; |
| 26 | + mode?: LegendMode; |
| 27 | + size?: LegendSize; |
| 28 | +} |
| 29 | + |
| 30 | +export function isValidLegendPosition(position: LegendPositions): boolean { |
| 31 | + return (legendPositions as readonly string[]).includes(position); |
| 32 | +} |
| 33 | + |
| 34 | +export function isValidLegendMode(mode: LegendMode): boolean { |
| 35 | + return (legendModes as readonly string[]).includes(mode); |
| 36 | +} |
| 37 | + |
| 38 | +export function isValidLegendSize(size: LegendSize): boolean { |
| 39 | + return (legendSizes as readonly string[]).includes(size); |
| 40 | +} |
| 41 | + |
| 42 | +export const DEFAULT_LEGEND: Required<LegendOptionsBase> = { |
| 43 | + position: 'bottom', |
| 44 | + mode: 'list', |
| 45 | + size: 'medium', |
| 46 | +}; |
| 47 | + |
| 48 | +export function getLegendPosition(position?: LegendPositions): 'bottom' | 'right' { |
| 49 | + if (position === undefined) { |
| 50 | + return DEFAULT_LEGEND.position; |
| 51 | + } |
| 52 | + if (isValidLegendPosition(position)) { |
| 53 | + return position; |
| 54 | + } |
| 55 | + return DEFAULT_LEGEND.position; |
| 56 | +} |
| 57 | + |
| 58 | +export function getLegendMode(mode?: LegendMode): 'list' | 'table' { |
| 59 | + if (!mode || !isValidLegendMode(mode)) { |
| 60 | + return DEFAULT_LEGEND.mode; |
| 61 | + } |
| 62 | + |
| 63 | + return mode; |
| 64 | +} |
| 65 | + |
| 66 | +export function getLegendSize(size?: LegendSize): 'small' | 'medium' { |
| 67 | + if (!size || !isValidLegendSize(size)) { |
| 68 | + return DEFAULT_LEGEND.size; |
| 69 | + } |
| 70 | + |
| 71 | + return size; |
| 72 | +} |
0 commit comments