refactor: CB-1548 add menuExtractActions function

This commit is contained in:
Wroud
2022-01-19 14:31:38 +03:00
parent 4bdbbf7129
commit f56723cadd
6 changed files with 73 additions and 49 deletions
@@ -9,7 +9,7 @@
import { Bootstrap, injectable } from '@cloudbeaver/core-di';
import { CommonDialogService, ConfirmationDialogDelete, DialogueStateResult, RenameDialog } from '@cloudbeaver/core-dialogs';
import { NotificationService } from '@cloudbeaver/core-events';
import { ActionService, ACTION_DELETE, ACTION_OPEN, ACTION_REFRESH, ACTION_RENAME, DATA_CONTEXT_MENU_NESTED, MenuSeparatorItem, MenuService } from '@cloudbeaver/core-view';
import { ActionService, ACTION_DELETE, ACTION_OPEN, ACTION_REFRESH, ACTION_RENAME, DATA_CONTEXT_MENU_NESTED, menuExtractActions, MenuSeparatorItem, MenuService } from '@cloudbeaver/core-view';
import { CoreSettingsService } from '../../CoreSettingsService';
import { DATA_CONTEXT_NAV_NODE_ACTIONS } from '../../NavigationTree/NavigationTreeNode/TreeNodeMenu/DATA_CONTEXT_NAV_NODE_ACTIONS';
@@ -152,40 +152,22 @@ export class NavNodeContextMenuService extends Bootstrap {
],
orderItems: (context, items) => {
const refreshIndex = items.indexOf(ACTION_REFRESH);
if (refreshIndex > -1) {
items.splice(refreshIndex, 1);
}
const actionsManage = menuExtractActions(items, [
ACTION_DELETE,
ACTION_RENAME,
]);
const renameIndex = items.indexOf(ACTION_RENAME);
if (renameIndex > -1) {
items.splice(renameIndex, 1);
}
const actionsRefresh = menuExtractActions(items, [
ACTION_REFRESH,
]);
const deleteIndex = items.indexOf(ACTION_DELETE);
if (deleteIndex > -1) {
items.splice(deleteIndex, 1);
}
items.push(...actionsManage);
if (deleteIndex > -1) {
// if (items.length > 0) {
// items.push(new MenuSeparatorItem());
// }
items.push(ACTION_DELETE);
}
if (renameIndex > -1) {
// if (items.length > 0 && deleteIndex === -1) {
// items.push(new MenuSeparatorItem());
// }
items.push(ACTION_RENAME);
}
if (refreshIndex > -1) {
if (items.length > 0) {
if (actionsRefresh.length > 0) {
if (items.length > 0){
items.push(new MenuSeparatorItem());
}
items.push(ACTION_REFRESH);
items.push(...actionsRefresh);
}
return items;
@@ -6,17 +6,17 @@
* you may not use this file except in compliance with the License.
*/
import { Bootstrap, injectable } from "@cloudbeaver/core-di";
import { ActionService, DATA_CONTEXT_MENU, MenuService } from "@cloudbeaver/core-view";
import { Bootstrap, injectable } from '@cloudbeaver/core-di';
import { ActionService, DATA_CONTEXT_MENU, menuExtractActions, MenuSeparatorItem, MenuService } from '@cloudbeaver/core-view';
import { ACTION_TAB_CLOSE } from "./Actions/ACTION_TAB_CLOSE";
import { ACTION_TAB_CLOSE_ALL } from "./Actions/ACTION_TAB_CLOSE_ALL";
import { ACTION_TAB_CLOSE_ALL_TO_THE_LEFT } from "./Actions/ACTION_TAB_CLOSE_ALL_TO_THE_LEFT";
import { ACTION_TAB_CLOSE_ALL_TO_THE_RIGHT } from "./Actions/ACTION_TAB_CLOSE_ALL_TO_THE_RIGHT";
import { ACTION_TAB_CLOSE_OTHERS } from "./Actions/ACTION_TAB_CLOSE_OTHERS";
import { DATA_CONTEXT_TAB_ID } from "./Tab/DATA_CONTEXT_TAB_ID";
import { DATA_CONTEXT_TABS_CONTEXT } from "./Tab/DATA_CONTEXT_TABS_CONTEXT";
import { MENU_TAB } from "./Tab/MENU_TAB";
import { ACTION_TAB_CLOSE } from './Actions/ACTION_TAB_CLOSE';
import { ACTION_TAB_CLOSE_ALL } from './Actions/ACTION_TAB_CLOSE_ALL';
import { ACTION_TAB_CLOSE_ALL_TO_THE_LEFT } from './Actions/ACTION_TAB_CLOSE_ALL_TO_THE_LEFT';
import { ACTION_TAB_CLOSE_ALL_TO_THE_RIGHT } from './Actions/ACTION_TAB_CLOSE_ALL_TO_THE_RIGHT';
import { ACTION_TAB_CLOSE_OTHERS } from './Actions/ACTION_TAB_CLOSE_OTHERS';
import { DATA_CONTEXT_TAB_ID } from './Tab/DATA_CONTEXT_TAB_ID';
import { DATA_CONTEXT_TABS_CONTEXT } from './Tab/DATA_CONTEXT_TABS_CONTEXT';
import { MENU_TAB } from './Tab/MENU_TAB';
@injectable()
export class TabsBootstrap extends Bootstrap {
@@ -24,7 +24,7 @@ export class TabsBootstrap extends Bootstrap {
private readonly actionService: ActionService,
private readonly menuService: MenuService
) {
super()
super();
}
register(): void | Promise<void> {
@@ -88,7 +88,7 @@ export class TabsBootstrap extends Bootstrap {
this.menuService.addCreator({
isApplicable: context => {
const state = context.tryGet(DATA_CONTEXT_TABS_CONTEXT);
return !!state?.enabledBaseActions && context.get(DATA_CONTEXT_MENU) === MENU_TAB
return !!state?.enabledBaseActions && context.get(DATA_CONTEXT_MENU) === MENU_TAB;
},
getItems: (context, items) => [
...items,
@@ -96,8 +96,26 @@ export class TabsBootstrap extends Bootstrap {
ACTION_TAB_CLOSE_ALL,
ACTION_TAB_CLOSE_OTHERS,
ACTION_TAB_CLOSE_ALL_TO_THE_LEFT,
ACTION_TAB_CLOSE_ALL_TO_THE_RIGHT
ACTION_TAB_CLOSE_ALL_TO_THE_RIGHT,
],
orderItems: (context, items) => {
const actions = menuExtractActions(items, [
ACTION_TAB_CLOSE,
ACTION_TAB_CLOSE_ALL,
ACTION_TAB_CLOSE_OTHERS,
ACTION_TAB_CLOSE_ALL_TO_THE_LEFT,
ACTION_TAB_CLOSE_ALL_TO_THE_RIGHT,
]);
if (actions.length > 0) {
if (items.length > 0){
items.push(new MenuSeparatorItem());
}
items.push(...actions);
}
return items;
},
});
}
@@ -0,0 +1,24 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2022 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import type { IAction } from '../Action/IAction';
import type { MenuCreatorItem } from './IMenuItemsCreator';
export function menuExtractActions(items: MenuCreatorItem[], actions: IAction[]): IAction[] {
const list: IAction[] = [];
for (const action of actions){
const index = items.indexOf(action);
if (index > -1) {
items.splice(index, 1);
}
list.push(action);
}
return list;
}
+2 -1
View File
@@ -32,9 +32,10 @@ export * from './Menu/MenuItem/MenuLazyItem';
export * from './Menu/MenuItem/MenuSeparatorItem';
export * from './Menu/MenuItem/MenuSubMenuItem';
export * from './Menu/createMenu';
export * from './Menu/IMenu';
export * from './Menu/DATA_CONTEXT_MENU_NESTED';
export * from './Menu/DATA_CONTEXT_MENU';
export * from './Menu/IMenu';
export * from './Menu/menuExtractActions';
export * from './Menu/MenuService';
export * from './Menu/useMenu';
export * from './Menu/useMenuContext';
@@ -20,9 +20,9 @@ import { DataExportDialog } from './Dialog/DataExportDialog';
@injectable()
export class DataExportMenuService {
constructor(
private commonDialogService: CommonDialogService,
private tableFooterMenuService: TableFooterMenuService,
private dataExportSettingsService: DataExportSettingsService,
private readonly commonDialogService: CommonDialogService,
private readonly tableFooterMenuService: TableFooterMenuService,
private readonly dataExportSettingsService: DataExportSettingsService,
private readonly actionService: ActionService,
private readonly menuService: MenuService,
) { }
@@ -9,7 +9,7 @@
import { injectable, Bootstrap } from '@cloudbeaver/core-di';
import { NotificationService } from '@cloudbeaver/core-events';
import { MENU_TAB } from '@cloudbeaver/core-ui';
import { ActionService, ACTION_OPEN_IN_TAB, DATA_CONTEXT_MENU, IDataContextProvider, KeyBindingService, KEY_BINDING_OPEN_IN_TAB, MenuSeparatorItem, MenuService, IAction } from '@cloudbeaver/core-view';
import { ActionService, ACTION_OPEN_IN_TAB, DATA_CONTEXT_MENU, IDataContextProvider, KeyBindingService, KEY_BINDING_OPEN_IN_TAB, MenuService, IAction } from '@cloudbeaver/core-view';
import { DATA_CONTEXT_SQL_EDITOR_STATE } from '@cloudbeaver/plugin-sql-editor';
import { DATA_CONTEXT_SQL_EDITOR_TAB } from '@cloudbeaver/plugin-sql-editor-navigation-tab';
@@ -53,7 +53,6 @@ export class PluginBootstrap extends Bootstrap {
),
getItems: (context, items) => [
ACTION_OPEN_IN_TAB,
...(items.length > 0 ? [ new MenuSeparatorItem()] : []),
...items,
],
});