2021-06-28 18:02:45 +00:00
|
|
|
import { allCommands } from 'app/client/components/commands';
|
2022-10-28 16:11:08 +00:00
|
|
|
import { makeT } from 'app/client/lib/localization';
|
2023-11-20 00:46:32 +00:00
|
|
|
import { RECORD_CARDS } from 'app/client/models/features';
|
|
|
|
import { menuDivider, menuIcon, menuItemCmd, menuItemCmdLabel } from 'app/client/ui2018/menus';
|
2021-06-28 18:02:45 +00:00
|
|
|
import { dom } from 'grainjs';
|
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('RowContextMenu');
|
|
|
|
|
2022-04-19 13:01:08 +00:00
|
|
|
export interface IRowContextMenu {
|
2021-06-28 18:02:45 +00:00
|
|
|
disableInsert: boolean;
|
|
|
|
disableDelete: boolean;
|
2023-11-20 00:46:32 +00:00
|
|
|
disableShowRecordCard: boolean;
|
2021-06-28 18:02:45 +00:00
|
|
|
isViewSorted: boolean;
|
2022-04-19 13:01:08 +00:00
|
|
|
numRows: number;
|
2021-06-28 18:02:45 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 00:46:32 +00:00
|
|
|
export function RowContextMenu({
|
|
|
|
disableInsert,
|
|
|
|
disableDelete,
|
|
|
|
disableShowRecordCard,
|
|
|
|
isViewSorted,
|
|
|
|
numRows
|
|
|
|
}: IRowContextMenu) {
|
2021-06-28 18:02:45 +00:00
|
|
|
const result: Element[] = [];
|
2023-11-20 00:46:32 +00:00
|
|
|
if (RECORD_CARDS() && numRows === 1) {
|
|
|
|
result.push(
|
|
|
|
menuItemCmd(
|
|
|
|
allCommands.viewAsCard,
|
|
|
|
() => menuItemCmdLabel(menuIcon('TypeCard'), t("View as card")),
|
|
|
|
dom.cls('disabled', disableShowRecordCard),
|
|
|
|
),
|
|
|
|
menuDivider(),
|
|
|
|
);
|
|
|
|
}
|
2021-06-28 18:02:45 +00:00
|
|
|
if (isViewSorted) {
|
|
|
|
// When the view is sorted, any newly added records get shifts instantly at the top or
|
|
|
|
// bottom. It could be very confusing for users who might expect the record to stay above or
|
|
|
|
// below the active row. Thus in this case we show a single `insert row` command.
|
|
|
|
result.push(
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.insertRecordAfter, t("Insert row"),
|
2021-06-28 18:02:45 +00:00
|
|
|
dom.cls('disabled', disableInsert)),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
result.push(
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.insertRecordBefore, t("Insert row above"),
|
2021-06-28 18:02:45 +00:00
|
|
|
dom.cls('disabled', disableInsert)),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.insertRecordAfter, t("Insert row below"),
|
2021-06-28 18:02:45 +00:00
|
|
|
dom.cls('disabled', disableInsert)),
|
|
|
|
);
|
|
|
|
}
|
2022-04-19 13:01:08 +00:00
|
|
|
result.push(
|
2022-12-06 16:43:35 +00:00
|
|
|
menuItemCmd(allCommands.duplicateRows, t('Duplicate rows', { count: numRows }),
|
2022-04-19 13:01:08 +00:00
|
|
|
dom.cls('disabled', disableInsert || numRows === 0)),
|
|
|
|
);
|
2021-06-28 18:02:45 +00:00
|
|
|
result.push(
|
|
|
|
menuDivider(),
|
2022-02-07 14:09:23 +00:00
|
|
|
// TODO: should show `Delete ${num} rows` when multiple are selected
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.deleteRecords, t("Delete"),
|
2021-06-28 18:02:45 +00:00
|
|
|
dom.cls('disabled', disableDelete)),
|
|
|
|
);
|
|
|
|
result.push(
|
|
|
|
menuDivider(),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.copyLink, t("Copy anchor link")));
|
2021-06-28 18:02:45 +00:00
|
|
|
return result;
|
|
|
|
}
|