|
|
|
@ -1,4 +1,5 @@ |
|
|
|
|
import { css } from '@emotion/css'; |
|
|
|
|
import { uniqueId } from 'lodash'; |
|
|
|
|
import React, { useEffect, useState } from 'react'; |
|
|
|
|
import { DragDropContext, Droppable, DropResult } from 'react-beautiful-dnd'; |
|
|
|
|
|
|
|
|
@ -54,18 +55,11 @@ export function ValueMappingsEditorModal({ value, onChange, onClose, showIconPic |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
const onAddValueMapping = (value: SelectableValue<MappingType>) => { |
|
|
|
|
updateRows([ |
|
|
|
|
...rows, |
|
|
|
|
{ |
|
|
|
|
type: value.value!, |
|
|
|
|
isNew: true, |
|
|
|
|
result: {}, |
|
|
|
|
}, |
|
|
|
|
]); |
|
|
|
|
updateRows([...rows, createRow({ type: value.value!, result: {}, isNew: true })]); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const onDuplicateMapping = (index: number) => { |
|
|
|
|
const sourceRow = rows[index]; |
|
|
|
|
const sourceRow = duplicateRow(rows[index]); |
|
|
|
|
const copy = [...rows]; |
|
|
|
|
copy.splice(index, 0, { ...sourceRow }); |
|
|
|
|
|
|
|
|
@ -111,7 +105,7 @@ export function ValueMappingsEditorModal({ value, onChange, onClose, showIconPic |
|
|
|
|
<tbody ref={provided.innerRef} {...provided.droppableProps}> |
|
|
|
|
{rows.map((row, index) => ( |
|
|
|
|
<ValueMappingEditRow |
|
|
|
|
key={index.toString()} |
|
|
|
|
key={row.id} |
|
|
|
|
mapping={row} |
|
|
|
|
index={index} |
|
|
|
|
onChange={onChangeMapping} |
|
|
|
@ -178,6 +172,27 @@ export const getStyles = (theme: GrafanaTheme2) => ({ |
|
|
|
|
}), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
function getRowUniqueId(): string { |
|
|
|
|
return uniqueId('mapping-'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function createRow(row: Partial<ValueMappingEditRowModel>): ValueMappingEditRowModel { |
|
|
|
|
return { |
|
|
|
|
type: MappingType.ValueToText, |
|
|
|
|
result: {}, |
|
|
|
|
id: getRowUniqueId(), |
|
|
|
|
...row, |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function duplicateRow(row: Partial<ValueMappingEditRowModel>): ValueMappingEditRowModel { |
|
|
|
|
return { |
|
|
|
|
...createRow(row), |
|
|
|
|
// provide a new unique id to the duplicated row, to preserve focus when dragging 2 duplicated rows
|
|
|
|
|
id: getRowUniqueId(), |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function editModelToSaveModel(rows: ValueMappingEditRowModel[]) { |
|
|
|
|
const mappings: ValueMapping[] = []; |
|
|
|
|
const valueMaps: ValueMapping = { |
|
|
|
@ -250,34 +265,42 @@ export function buildEditRowModels(value: ValueMapping[]) { |
|
|
|
|
switch (mapping.type) { |
|
|
|
|
case MappingType.ValueToText: |
|
|
|
|
for (const key of Object.keys(mapping.options)) { |
|
|
|
|
editRows.push({ |
|
|
|
|
type: mapping.type, |
|
|
|
|
result: mapping.options[key], |
|
|
|
|
key, |
|
|
|
|
}); |
|
|
|
|
editRows.push( |
|
|
|
|
createRow({ |
|
|
|
|
type: mapping.type, |
|
|
|
|
result: mapping.options[key], |
|
|
|
|
key, |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case MappingType.RangeToText: |
|
|
|
|
editRows.push({ |
|
|
|
|
type: mapping.type, |
|
|
|
|
result: mapping.options.result, |
|
|
|
|
from: mapping.options.from ?? 0, |
|
|
|
|
to: mapping.options.to ?? 0, |
|
|
|
|
}); |
|
|
|
|
editRows.push( |
|
|
|
|
createRow({ |
|
|
|
|
type: mapping.type, |
|
|
|
|
result: mapping.options.result, |
|
|
|
|
from: mapping.options.from ?? 0, |
|
|
|
|
to: mapping.options.to ?? 0, |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
break; |
|
|
|
|
case MappingType.RegexToText: |
|
|
|
|
editRows.push({ |
|
|
|
|
type: mapping.type, |
|
|
|
|
result: mapping.options.result, |
|
|
|
|
pattern: mapping.options.pattern, |
|
|
|
|
}); |
|
|
|
|
editRows.push( |
|
|
|
|
createRow({ |
|
|
|
|
type: mapping.type, |
|
|
|
|
result: mapping.options.result, |
|
|
|
|
pattern: mapping.options.pattern, |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
break; |
|
|
|
|
case MappingType.SpecialValue: |
|
|
|
|
editRows.push({ |
|
|
|
|
type: mapping.type, |
|
|
|
|
result: mapping.options.result, |
|
|
|
|
specialMatch: mapping.options.match ?? SpecialValueMatch.Null, |
|
|
|
|
}); |
|
|
|
|
editRows.push( |
|
|
|
|
createRow({ |
|
|
|
|
type: mapping.type, |
|
|
|
|
result: mapping.options.result, |
|
|
|
|
specialMatch: mapping.options.match ?? SpecialValueMatch.Null, |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|