2022-02-07 14:09:23 +00:00
|
|
|
import { allCommands } from 'app/client/components/commands';
|
2022-10-28 16:11:08 +00:00
|
|
|
import { makeT } from 'app/client/lib/localization';
|
2022-02-07 14:09:23 +00:00
|
|
|
import { menuDivider, menuItemCmd } from 'app/client/ui2018/menus';
|
|
|
|
import { IMultiColumnContextMenu } from 'app/client/ui/GridViewMenus';
|
2022-10-17 09:47:16 +00:00
|
|
|
import { COMMENTS } from 'app/client/models/features';
|
2022-04-19 13:01:08 +00:00
|
|
|
import { dom } from 'grainjs';
|
2022-02-07 14:09:23 +00:00
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('CellContextMenu');
|
|
|
|
|
2023-11-20 00:46:32 +00:00
|
|
|
export interface ICellContextMenu {
|
|
|
|
disableInsert: boolean;
|
|
|
|
disableDelete: boolean;
|
|
|
|
isViewSorted: boolean;
|
|
|
|
numRows: number;
|
|
|
|
}
|
2022-02-07 14:09:23 +00:00
|
|
|
|
2023-11-20 00:46:32 +00:00
|
|
|
export function CellContextMenu(cellOptions: ICellContextMenu, colOptions: IMultiColumnContextMenu) {
|
|
|
|
|
|
|
|
const { disableInsert, disableDelete, isViewSorted, numRows } = cellOptions;
|
|
|
|
const { numColumns, disableModify, isReadonly, isFiltered } = colOptions;
|
2022-02-07 14:09:23 +00:00
|
|
|
|
2022-10-17 09:47:16 +00:00
|
|
|
// disableModify is true if the column is a summary column or is being transformed.
|
|
|
|
// isReadonly is true for readonly mode.
|
2022-02-07 14:09:23 +00:00
|
|
|
const disableForReadonlyColumn = dom.cls('disabled', Boolean(disableModify) || isReadonly);
|
|
|
|
const disableForReadonlyView = dom.cls('disabled', isReadonly);
|
|
|
|
|
2023-11-20 00:46:32 +00:00
|
|
|
const nameClearColumns = isFiltered ?
|
|
|
|
t("Reset {{count}} entire columns", {count: numColumns}) :
|
|
|
|
t("Reset {{count}} columns", {count: numColumns});
|
|
|
|
const nameDeleteColumns = t("Delete {{count}} columns", {count: numColumns});
|
2022-02-07 14:09:23 +00:00
|
|
|
|
2022-12-06 16:43:35 +00:00
|
|
|
const nameDeleteRows = t("Delete {{count}} rows", {count: numRows});
|
2022-02-07 14:09:23 +00:00
|
|
|
|
2023-11-20 00:46:32 +00:00
|
|
|
const nameClearCells = (numRows > 1 || numColumns > 1) ? t("Clear values") : t("Clear cell");
|
2022-02-07 14:09:23 +00:00
|
|
|
|
|
|
|
const result: Array<Element|null> = [];
|
|
|
|
|
|
|
|
result.push(
|
2023-04-28 09:20:28 +00:00
|
|
|
menuItemCmd(allCommands.contextMenuCut, t('Cut'), disableForReadonlyColumn),
|
|
|
|
menuItemCmd(allCommands.contextMenuCopy, t('Copy')),
|
|
|
|
menuItemCmd(allCommands.contextMenuPaste, t('Paste'), disableForReadonlyColumn),
|
|
|
|
menuDivider(),
|
2022-02-07 14:09:23 +00:00
|
|
|
colOptions.isFormula ?
|
|
|
|
null :
|
|
|
|
menuItemCmd(allCommands.clearValues, nameClearCells, disableForReadonlyColumn),
|
2022-10-17 09:47:16 +00:00
|
|
|
menuItemCmd(allCommands.clearColumns, nameClearColumns, disableForReadonlyColumn),
|
2022-02-07 14:09:23 +00:00
|
|
|
|
|
|
|
...(
|
2023-11-20 00:46:32 +00:00
|
|
|
(numColumns > 1 || numRows > 1) ? [] : [
|
2022-02-07 14:09:23 +00:00
|
|
|
menuDivider(),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.copyLink, t("Copy anchor link")),
|
2022-04-18 14:40:29 +00:00
|
|
|
menuDivider(),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.filterByThisCellValue, t("Filter by this value")),
|
2023-04-28 09:20:28 +00:00
|
|
|
menuItemCmd(allCommands.openDiscussion, t('Comment'), dom.cls('disabled', (
|
2023-11-20 00:46:32 +00:00
|
|
|
isReadonly || numRows === 0 || numColumns === 0
|
2022-10-28 16:11:08 +00:00
|
|
|
)), dom.hide(use => !use(COMMENTS()))) //TODO: i18next
|
2022-02-07 14:09:23 +00:00
|
|
|
]
|
|
|
|
),
|
|
|
|
|
|
|
|
menuDivider(),
|
|
|
|
|
|
|
|
// inserts
|
|
|
|
...(
|
|
|
|
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.
|
2022-12-06 13:57:29 +00:00
|
|
|
[menuItemCmd(allCommands.insertRecordAfter, t("Insert row"),
|
2022-02-07 14:09:23 +00:00
|
|
|
dom.cls('disabled', disableInsert))] :
|
|
|
|
|
2022-12-06 13:57:29 +00:00
|
|
|
[menuItemCmd(allCommands.insertRecordBefore, t("Insert row above"),
|
2022-02-07 14:09:23 +00:00
|
|
|
dom.cls('disabled', disableInsert)),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.insertRecordAfter, t("Insert row below"),
|
2022-02-07 14:09:23 +00:00
|
|
|
dom.cls('disabled', disableInsert))]
|
|
|
|
),
|
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)),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.insertFieldBefore, t("Insert column to the left"),
|
2022-02-07 14:09:23 +00:00
|
|
|
disableForReadonlyView),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.insertFieldAfter, t("Insert column to the right"),
|
2022-02-07 14:09:23 +00:00
|
|
|
disableForReadonlyView),
|
|
|
|
|
|
|
|
|
|
|
|
menuDivider(),
|
|
|
|
|
|
|
|
// deletes
|
2022-10-17 09:47:16 +00:00
|
|
|
menuItemCmd(allCommands.deleteRecords, nameDeleteRows, dom.cls('disabled', disableDelete)),
|
2022-02-07 14:09:23 +00:00
|
|
|
|
|
|
|
menuItemCmd(allCommands.deleteFields, nameDeleteColumns, disableForReadonlyColumn),
|
|
|
|
|
|
|
|
// todo: add "hide N columns"
|
|
|
|
);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|