menu.placeTag

This commit is contained in:
Jan Prochazka 2021-04-08 06:55:14 +02:00
parent bcc1f91352
commit 2db17f9eca
4 changed files with 76 additions and 53 deletions

View File

@ -31,13 +31,19 @@ export interface GlobalCommand {
export default function registerCommand(command: GlobalCommand) {
const { testEnabled } = command;
commands.update(x => ({
commands.update(x => {
if (x[command.id]) {
console.error(`Command ${command.id} already registered`);
return x;
}
return {
...x,
[command.id]: {
text: `${command.category}: ${command.name}`,
...command,
enabled: !testEnabled,
},
}));
};
});
invalidateCommandDefinitions();
}

View File

@ -219,7 +219,7 @@
import DataGridRow from './DataGridRow.svelte';
import { getFilterType, getFilterValueExpression } from 'dbgate-filterparser';
import stableStringify from 'json-stable-stringify';
import contextMenu from '../utility/contextMenu';
import contextMenu, { getContextMenu, registerMenu } from '../utility/contextMenu';
import { tick } from 'svelte';
import {
cellIsSelected,
@ -303,10 +303,6 @@
display.reload();
}
export function getTabId() {
return tabid;
}
export function save() {
if (onSave) onSave();
}
@ -1015,8 +1011,7 @@
return ['filter', columnRealIndex];
}
function createMenu() {
return [
registerMenu(
{ command: 'dataGrid.refresh' },
{ command: 'dataGrid.copyToClipboard' },
{ command: 'dataGrid.export' },
@ -1039,9 +1034,10 @@
{ command: 'dataGrid.openQuery' },
{ command: 'dataGrid.openFreeTable' },
{ command: 'dataGrid.openChartFromSelection' },
{ command: 'dataGrid.openActiveChart', hideDisabled: true },
];
}
{ placeTag: 'chart' }
);
const menu = getContextMenu();
</script>
{#if !display || (!isDynamicStructure && (!columns || columns.length == 0))}
@ -1062,12 +1058,7 @@
{/each}
</div>
{:else}
<div
class="container"
bind:clientWidth={containerWidth}
bind:clientHeight={containerHeight}
use:contextMenu={createMenu}
>
<div class="container" bind:clientWidth={containerWidth} bind:clientHeight={containerHeight} use:contextMenu={menu}>
<input
type="text"
class="focus-field"

View File

@ -63,6 +63,7 @@
import { showModal } from '../modals/modalTools';
import axiosInstance from '../utility/axiosInstance';
import { registerMenu } from '../utility/contextMenu';
import createActivator, { getActiveComponent } from '../utility/createActivator';
import openNewTab from '../utility/openNewTab';
import ChangeSetGrider from './ChangeSetGrider';
@ -180,6 +181,8 @@
}
);
}
registerMenu({ command: 'dataGrid.openActiveChart', tag: 'chart' });
</script>
<LoadingDataGridCore

View File

@ -1,10 +1,11 @@
import _ from 'lodash';
import { getContext, setContext } from 'svelte';
import { currentDropDownMenu } from '../stores';
import getAsArray from './getAsArray';
export function registerMenu(items) {
export function registerMenu(...items) {
const parentMenu = getContext('componentContextMenu');
setContext('componentContextMenu', [parentMenu, items]);
setContext('componentContextMenu', [parentMenu, ...items]);
}
export default function contextMenu(node, items = []) {
@ -30,21 +31,43 @@ export default function contextMenu(node, items = []) {
};
}
function doExtractMenuItems(menu, res) {
function doExtractMenuItems(menu, res, tagged) {
if (_.isFunction(menu)) {
doExtractMenuItems(menu(), res);
doExtractMenuItems(menu(), res, tagged);
} else if (_.isArray(menu)) {
for (const item of menu) {
doExtractMenuItems(item, res);
doExtractMenuItems(item, res, tagged);
}
} else if (_.isPlainObject(menu)) {
if (menu.tag) {
tagged.push({
...menu,
tags: getAsArray(menu.tag),
});
} else if (menu.placeTag) {
const placeTags = getAsArray(menu.placeTag);
for (let index = 0; index < tagged.length; ) {
const current = tagged[index];
if (_.intersection(placeTags, current.tags).length > 0) {
tagged.splice(index, 1);
res.push(current);
} else {
index++;
}
}
} else {
res.push(menu);
}
}
}
export function extractMenuItems(menu) {
const res = [];
doExtractMenuItems(menu, res);
const tagged = [];
doExtractMenuItems(menu, res, tagged);
// append tagged, which were not appended by placeTag
res.push(...tagged);
return res;
}