1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-04-20 01:01:03 +00:00
rolens/frontend/src/components/objectgrid.svelte

82 lines
1.8 KiB
Svelte
Raw Normal View History

2023-01-10 17:28:27 +01:00
<script>
import Grid from './grid.svelte';
export let data = [];
export let key = '_id';
2023-01-18 16:31:13 +01:00
export let activePath = [];
export let hideObjectIndicators = false;
2023-01-10 20:22:57 +01:00
const columns = [
{ key: 'key', label: 'Key' },
{ key: 'value', label: 'Value' },
{ key: 'type', label: 'Type' },
];
2023-01-10 17:28:27 +01:00
let items = [];
$: if (data) {
items = [];
if (Array.isArray(data)) {
for (const item of data) {
2023-01-17 21:03:01 +01:00
const newItem = {};
2023-01-18 16:31:13 +01:00
newItem.key = item[key];
2023-01-18 14:15:54 +01:00
newItem.type = getType(item[key]);
2023-01-18 16:31:13 +01:00
newItem.children = dissectObject(item);
2023-01-17 21:03:01 +01:00
newItem.menu = item.menu;
items = [ ...items, newItem ];
2023-01-10 17:28:27 +01:00
}
}
else {
items = dissectObject(data);
}
}
function getType(value) {
if (Array.isArray(value)) {
return `array (${value.length} item${value.length === 1 ? '' : 's'})`;
}
2023-01-10 20:10:39 +01:00
else if (typeof value === 'number') {
if (value.toString().includes('.')) {
return 'double';
}
return 'integer';
}
2023-01-10 17:28:27 +01:00
else if (new Date(value).toString() !== 'Invalid Date') {
return 'date';
}
else if (typeof value === 'object') {
const keys = Object.keys(value);
return `object (${keys.length} item${keys.length === 1 ? '' : 's'})`;
}
else {
return typeof value;
}
}
2023-01-18 16:31:13 +01:00
function dissectObject(object) {
2023-01-13 16:56:48 +01:00
const entries = [ ...Array.isArray(object) ? object.entries() : Object.entries(object) ];
2023-01-18 16:31:13 +01:00
return entries.map(([ key, value ]) => {
2023-01-13 16:56:48 +01:00
key = key + '';
2023-01-10 17:28:27 +01:00
const type = getType(value);
2023-01-18 16:31:13 +01:00
const child = { key, value, type, menu: value?.menu };
2023-01-10 17:28:27 +01:00
2023-01-13 16:56:48 +01:00
if (type.startsWith('object') || type.startsWith('array')) {
2023-01-10 17:28:27 +01:00
child.children = dissectObject(value);
}
return child;
});
}
</script>
2023-01-15 12:02:17 +01:00
<Grid
key="key"
on:select
on:trigger
2023-01-18 16:31:13 +01:00
bind:activePath
2023-01-15 12:02:17 +01:00
{columns}
{items}
{hideObjectIndicators}
2023-01-15 12:02:17 +01:00
/>