context menu tags - can use forward ref

This commit is contained in:
Jan Prochazka
2021-04-08 09:11:07 +02:00
parent d3a019e8a3
commit bb3dad6e1c
2 changed files with 31 additions and 16 deletions

View File

@@ -51,6 +51,8 @@
loadData();
});
registerMenu({ placeTag: 'switch' });
const menu = getContextMenu();
$: grider = new ChangeSetGrider(loadedRows, changeSetState, dispatchChangeSet, display);
@@ -58,7 +60,7 @@
// $: console.log('GRIDER', grider);
</script>
<div class="flexcol flex1" use:contextMenu={[{ placeTag: 'switch' }, menu]}>
<div class="flexcol flex1" use:contextMenu={menu}>
<div class="toolbar">
<Pager bind:skip bind:limit on:load={() => display.reload()} />
</div>

View File

@@ -31,20 +31,31 @@ export default function contextMenu(node, items = []) {
};
}
function doExtractMenuItems(menu, res, tagged) {
function doExtractMenuItems(menu, res) {
if (_.isFunction(menu)) {
doExtractMenuItems(menu(), res, tagged);
doExtractMenuItems(menu(), res);
} else if (_.isArray(menu)) {
for (const item of menu) {
doExtractMenuItems(item, res, tagged);
doExtractMenuItems(item, res);
}
} else if (_.isPlainObject(menu)) {
if (menu.tag) {
tagged.push({
...menu,
tags: getAsArray(menu.tag),
});
} else if (menu.placeTag) {
res.push(menu);
}
}
function processTags(items) {
const res = [];
const tagged = [];
for (const menu of items.filter(x => x.tag)) {
tagged.push({
...menu,
tags: getAsArray(menu.tag),
});
}
for (const menu of items.filter(x => !x.tag)) {
if (menu.placeTag) {
const placeTags = getAsArray(menu.placeTag);
for (let index = 0; index < tagged.length; ) {
const current = tagged[index];
@@ -59,15 +70,17 @@ function doExtractMenuItems(menu, res, tagged) {
res.push(menu);
}
}
}
export function extractMenuItems(menu) {
const res = [];
const tagged = [];
doExtractMenuItems(menu, res, tagged);
// append tagged, which were not appended by placeTag
res.push(...tagged);
return res;
}
export function extractMenuItems(menu) {
let res = [];
doExtractMenuItems(menu, res);
res = processTags(res);
return res;
}