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-04-19 13:01:08 +00:00
|
|
|
import { IRowContextMenu } from 'app/client/ui/RowContextMenu';
|
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');
|
|
|
|
|
2022-02-07 14:09:23 +00:00
|
|
|
export function CellContextMenu(rowOptions: IRowContextMenu, colOptions: IMultiColumnContextMenu) {
|
|
|
|
|
|
|
|
const { disableInsert, disableDelete, isViewSorted } = rowOptions;
|
|
|
|
const { disableModify, isReadonly } = colOptions;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
const numCols: number = colOptions.numColumns;
|
2022-10-28 16:11:08 +00:00
|
|
|
const nameClearColumns = colOptions.isFiltered ? t("ClearEntireColumns", {count: numCols}) : t("ClearColumns", {count: numCols})
|
|
|
|
const nameDeleteColumns = t("DeleteColumns", {count: numCols})
|
2022-02-07 14:09:23 +00:00
|
|
|
|
|
|
|
const numRows: number = rowOptions.numRows;
|
2022-10-28 16:11:08 +00:00
|
|
|
const nameDeleteRows = t("DeleteRows", {count: numRows})
|
2022-02-07 14:09:23 +00:00
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const nameClearCells = (numRows > 1 || numCols > 1) ? t('ClearValues') : t('ClearCell');
|
2022-02-07 14:09:23 +00:00
|
|
|
|
|
|
|
const result: Array<Element|null> = [];
|
|
|
|
|
|
|
|
result.push(
|
|
|
|
|
|
|
|
// TODO: implement copy/paste actions
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
...(
|
|
|
|
(numCols > 1 || numRows > 1) ? [] : [
|
|
|
|
menuDivider(),
|
2022-10-28 16:11:08 +00:00
|
|
|
menuItemCmd(allCommands.copyLink, t('CopyAnchorLink')),
|
2022-04-18 14:40:29 +00:00
|
|
|
menuDivider(),
|
2022-10-28 16:11:08 +00:00
|
|
|
menuItemCmd(allCommands.filterByThisCellValue, t("FilterByValue")),
|
2022-10-17 09:47:16 +00:00
|
|
|
menuItemCmd(allCommands.openDiscussion, 'Comment', dom.cls('disabled', (
|
|
|
|
isReadonly || numRows === 0 || numCols === 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-10-28 16:11:08 +00:00
|
|
|
[menuItemCmd(allCommands.insertRecordAfter, t("InsertRow"),
|
2022-02-07 14:09:23 +00:00
|
|
|
dom.cls('disabled', disableInsert))] :
|
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
[menuItemCmd(allCommands.insertRecordBefore, t("InsertRowAbove"),
|
2022-02-07 14:09:23 +00:00
|
|
|
dom.cls('disabled', disableInsert)),
|
2022-10-28 16:11:08 +00:00
|
|
|
menuItemCmd(allCommands.insertRecordAfter, t("InsertRowBelow"),
|
2022-02-07 14:09:23 +00:00
|
|
|
dom.cls('disabled', disableInsert))]
|
|
|
|
),
|
2022-10-28 16:11:08 +00:00
|
|
|
menuItemCmd(allCommands.duplicateRows, t("DuplicateRows", {count: numRows}),
|
2022-04-19 13:01:08 +00:00
|
|
|
dom.cls('disabled', disableInsert || numRows === 0)),
|
2022-10-28 16:11:08 +00:00
|
|
|
menuItemCmd(allCommands.insertFieldBefore, t("InsertColumnLeft"),
|
2022-02-07 14:09:23 +00:00
|
|
|
disableForReadonlyView),
|
2022-10-28 16:11:08 +00:00
|
|
|
menuItemCmd(allCommands.insertFieldAfter, t("InsertColumnRight"),
|
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;
|
|
|
|
}
|