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(); loadData();
}); });
registerMenu({ placeTag: 'switch' });
const menu = getContextMenu(); const menu = getContextMenu();
$: grider = new ChangeSetGrider(loadedRows, changeSetState, dispatchChangeSet, display); $: grider = new ChangeSetGrider(loadedRows, changeSetState, dispatchChangeSet, display);
@@ -58,7 +60,7 @@
// $: console.log('GRIDER', grider); // $: console.log('GRIDER', grider);
</script> </script>
<div class="flexcol flex1" use:contextMenu={[{ placeTag: 'switch' }, menu]}> <div class="flexcol flex1" use:contextMenu={menu}>
<div class="toolbar"> <div class="toolbar">
<Pager bind:skip bind:limit on:load={() => display.reload()} /> <Pager bind:skip bind:limit on:load={() => display.reload()} />
</div> </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)) { if (_.isFunction(menu)) {
doExtractMenuItems(menu(), res, tagged); doExtractMenuItems(menu(), res);
} else if (_.isArray(menu)) { } else if (_.isArray(menu)) {
for (const item of menu) { for (const item of menu) {
doExtractMenuItems(item, res, tagged); doExtractMenuItems(item, res);
} }
} else if (_.isPlainObject(menu)) { } else if (_.isPlainObject(menu)) {
if (menu.tag) { res.push(menu);
tagged.push({ }
...menu, }
tags: getAsArray(menu.tag),
}); function processTags(items) {
} else if (menu.placeTag) { 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); const placeTags = getAsArray(menu.placeTag);
for (let index = 0; index < tagged.length; ) { for (let index = 0; index < tagged.length; ) {
const current = tagged[index]; const current = tagged[index];
@@ -59,15 +70,17 @@ function doExtractMenuItems(menu, res, tagged) {
res.push(menu); res.push(menu);
} }
} }
}
export function extractMenuItems(menu) {
const res = [];
const tagged = [];
doExtractMenuItems(menu, res, tagged);
// append tagged, which were not appended by placeTag // append tagged, which were not appended by placeTag
res.push(...tagged); res.push(...tagged);
return res;
}
export function extractMenuItems(menu) {
let res = [];
doExtractMenuItems(menu, res);
res = processTags(res);
return res; return res;
} }