2023-10-13 13:01:12 +00:00
|
|
|
import {allCommands} from 'app/client/components/commands';
|
2023-10-17 18:14:54 +00:00
|
|
|
import GridView from 'app/client/components/GridView';
|
2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2023-11-22 10:02:04 +00:00
|
|
|
import {ColumnRec} from "app/client/models/entities/ColumnRec";
|
2023-10-13 13:01:12 +00:00
|
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
2023-10-23 05:51:08 +00:00
|
|
|
import {GristTooltips} from 'app/client/ui/GristTooltips';
|
|
|
|
import {withInfoTooltip} from 'app/client/ui/tooltips';
|
2023-10-20 11:05:29 +00:00
|
|
|
import {testId, theme, vars} from 'app/client/ui2018/cssVars';
|
2023-11-22 10:02:04 +00:00
|
|
|
import {IconName} from "app/client/ui2018/IconList";
|
2023-10-13 13:01:12 +00:00
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
|
|
|
import {
|
2023-11-22 10:02:04 +00:00
|
|
|
menuCssClass,
|
2023-10-13 13:01:12 +00:00
|
|
|
menuDivider,
|
2023-10-17 18:14:54 +00:00
|
|
|
menuIcon,
|
2023-10-13 13:01:12 +00:00
|
|
|
menuItem,
|
|
|
|
menuItemCmd,
|
|
|
|
menuItemSubmenu,
|
2023-10-20 11:05:29 +00:00
|
|
|
menuItemTrimmed,
|
2023-10-13 13:01:12 +00:00
|
|
|
menuSubHeader,
|
2023-10-20 11:05:29 +00:00
|
|
|
menuSubHeaderMenu,
|
2023-10-17 18:14:54 +00:00
|
|
|
menuText,
|
|
|
|
searchableMenu,
|
2023-10-20 11:05:29 +00:00
|
|
|
SearchableMenuItem,
|
2023-10-13 13:01:12 +00:00
|
|
|
} from 'app/client/ui2018/menus';
|
2023-11-22 10:02:04 +00:00
|
|
|
import * as UserType from "app/client/widgets/UserType";
|
|
|
|
import {isListType, RecalcWhen} from "app/common/gristTypes";
|
2023-10-13 13:01:12 +00:00
|
|
|
import {Sort} from 'app/common/SortSpec';
|
2023-10-17 18:14:54 +00:00
|
|
|
import {dom, DomElementArg, styled} from 'grainjs';
|
2023-10-20 11:05:29 +00:00
|
|
|
import * as weasel from 'popweasel';
|
2023-11-22 10:02:04 +00:00
|
|
|
import * as commands from "../components/commands";
|
2020-10-02 15:10:00 +00:00
|
|
|
import isEqual = require('lodash/isEqual');
|
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('GridViewMenus');
|
|
|
|
|
2023-10-17 18:14:54 +00:00
|
|
|
export function buildAddColumnMenu(gridView: GridView, index?: number) {
|
2023-10-24 18:32:33 +00:00
|
|
|
const isSummaryTable = Boolean(gridView.viewSection.table().summarySourceTable());
|
2023-10-17 18:14:54 +00:00
|
|
|
return [
|
2023-11-22 10:02:04 +00:00
|
|
|
buildAddNewColumMenuSection(gridView, index),
|
2023-10-17 18:14:54 +00:00
|
|
|
buildHiddenColumnsMenuItems(gridView, index),
|
2023-10-24 18:32:33 +00:00
|
|
|
isSummaryTable ? null : [
|
|
|
|
buildLookupSection(gridView, index),
|
|
|
|
buildShortcutsMenuItems(gridView, index),
|
|
|
|
],
|
2023-10-17 18:14:54 +00:00
|
|
|
];
|
2023-10-13 13:01:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 10:02:04 +00:00
|
|
|
function buildAddNewColumMenuSection(gridView: GridView, index?: number): DomElementArg[] {
|
|
|
|
function buildEmptyNewColumMenuItem() {
|
|
|
|
return menuItem(
|
|
|
|
async () => {
|
|
|
|
await gridView.insertColumn(null, {index});
|
|
|
|
},
|
|
|
|
t("Add Column"),
|
|
|
|
testId('new-columns-menu-add-new'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function BuildNewColumnWithTypeSubmenu() {
|
|
|
|
const columnTypes = [
|
|
|
|
"Text",
|
|
|
|
"Numeric",
|
|
|
|
"Int",
|
|
|
|
"Bool",
|
|
|
|
"Date",
|
|
|
|
`DateTime:${gridView.gristDoc.docModel.docInfoRow.timezone()}`,
|
|
|
|
"Choice",
|
|
|
|
"ChoiceList",
|
|
|
|
`Ref:${gridView.tableModel.tableMetaRow.tableId()}`,
|
|
|
|
`RefList:${gridView.tableModel.tableMetaRow.tableId()}`,
|
|
|
|
"Attachments"].map(type => ({type, obj: UserType.typeDefs[type.split(':')[0]]}))
|
|
|
|
.map((ct): { displayName: string, colType: string, testIdName: string, icon: IconName | undefined } => ({
|
|
|
|
displayName: t(ct.obj.label),
|
|
|
|
colType: ct.type,
|
|
|
|
testIdName: ct.obj.label.toLowerCase().replace(' ', '-'),
|
|
|
|
icon: ct.obj.icon
|
|
|
|
}));
|
|
|
|
|
|
|
|
return menuItemSubmenu(
|
|
|
|
(ctl) => [
|
|
|
|
...columnTypes.map((colType) =>
|
|
|
|
menuItem(
|
|
|
|
async () => {
|
|
|
|
await gridView.insertColumn(null, {index, colInfo: {type: colType.colType}});
|
|
|
|
},
|
|
|
|
menuIcon(colType.icon as IconName),
|
|
|
|
colType.displayName === 'Reference'?
|
|
|
|
gridView.gristDoc.behavioralPromptsManager.attachTip('referenceColumns', {
|
|
|
|
popupOptions: {
|
|
|
|
attach: `.${menuCssClass}`,
|
|
|
|
placement: 'left-start',
|
|
|
|
}
|
|
|
|
}):null,
|
|
|
|
colType.displayName,
|
|
|
|
testId(`new-columns-menu-add-${colType.testIdName}`)),
|
|
|
|
),
|
|
|
|
testId('new-columns-menu-add-with-type-submenu'),
|
|
|
|
],
|
|
|
|
{allowNothingSelected: false},
|
|
|
|
t('Add column with type'),
|
|
|
|
testId('new-columns-menu-add-with-type')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildNewFunctionColumnMenuItem() {
|
|
|
|
return menuItem(
|
|
|
|
async () => {
|
|
|
|
await gridView.insertColumn(null, {index, skipPopup: true, colInfo: {isFormula: true}});
|
|
|
|
gridView.activateEditorAtCursor();
|
|
|
|
commands.allCommands.makeFormula.run();
|
|
|
|
commands.allCommands.detachEditor.run();
|
|
|
|
},
|
|
|
|
withInfoTooltip(
|
|
|
|
t('Add formula column'),
|
|
|
|
GristTooltips.formulaColumn(),
|
|
|
|
{variant: 'hover'}
|
|
|
|
),
|
|
|
|
testId('new-columns-menu-add-formula'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
buildEmptyNewColumMenuItem(),
|
|
|
|
BuildNewColumnWithTypeSubmenu(),
|
|
|
|
buildNewFunctionColumnMenuItem()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-10-17 18:14:54 +00:00
|
|
|
function buildHiddenColumnsMenuItems(gridView: GridView, index?: number) {
|
|
|
|
const {viewSection} = gridView;
|
|
|
|
const hiddenColumns = viewSection.hiddenColumns();
|
|
|
|
if (hiddenColumns.length === 0) { return null; }
|
2023-10-13 13:01:12 +00:00
|
|
|
|
2023-10-20 11:05:29 +00:00
|
|
|
if (hiddenColumns.length <= 5) {
|
|
|
|
return [
|
|
|
|
menuDivider(),
|
2023-11-06 15:42:04 +00:00
|
|
|
menuSubHeader(t('Hidden Columns'), testId('new-columns-menu-hidden-columns-header')),
|
2023-10-20 11:05:29 +00:00
|
|
|
hiddenColumns.map((col: ColumnRec) =>
|
2023-10-17 18:14:54 +00:00
|
|
|
menuItem(
|
|
|
|
async () => {
|
|
|
|
await gridView.showColumn(col.id(), index);
|
2023-10-13 13:01:12 +00:00
|
|
|
},
|
2023-10-17 18:14:54 +00:00
|
|
|
col.label(),
|
2023-11-06 15:42:04 +00:00
|
|
|
testId('new-columns-menu-hidden-column-inlined'),
|
2023-10-13 13:01:12 +00:00
|
|
|
)
|
2023-10-17 18:14:54 +00:00
|
|
|
),
|
2023-10-20 11:05:29 +00:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return [
|
|
|
|
menuDivider(),
|
|
|
|
menuSubHeaderMenu(
|
|
|
|
() => {
|
|
|
|
return searchableMenu(
|
|
|
|
hiddenColumns.map((col) => ({
|
|
|
|
cleanText: col.label().trim().toLowerCase(),
|
2023-11-06 15:42:04 +00:00
|
|
|
builder: () => menuItemTrimmed(
|
|
|
|
() => gridView.showColumn(col.id(), index),
|
|
|
|
col.label(),
|
|
|
|
testId('new-columns-menu-hidden-column-collapsed'),
|
|
|
|
)
|
2023-10-20 11:05:29 +00:00
|
|
|
})),
|
|
|
|
{searchInputPlaceholder: t('Search columns')}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
{allowNothingSelected: true},
|
|
|
|
t('Hidden Columns'),
|
2023-11-06 15:42:04 +00:00
|
|
|
testId('new-columns-menu-hidden-columns-menu')
|
2023-10-20 11:05:29 +00:00
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
2023-10-13 13:01:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 18:14:54 +00:00
|
|
|
function buildShortcutsMenuItems(gridView: GridView, index?: number) {
|
2023-10-13 13:01:12 +00:00
|
|
|
return [
|
|
|
|
menuDivider(),
|
|
|
|
menuSubHeader(t("Shortcuts"), testId('new-columns-menu-shortcuts')),
|
2023-10-17 18:14:54 +00:00
|
|
|
buildTimestampMenuItems(gridView, index),
|
|
|
|
buildAuthorshipMenuItems(gridView, index),
|
|
|
|
buildDetectDuplicatesMenuItems(gridView, index),
|
|
|
|
buildUUIDMenuItem(gridView, index),
|
2023-10-13 13:01:12 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-10-17 18:14:54 +00:00
|
|
|
function buildTimestampMenuItems(gridView: GridView, index?: number) {
|
|
|
|
return menuItemSubmenu(() => [
|
|
|
|
menuItem(
|
|
|
|
async () => {
|
|
|
|
await gridView.insertColumn(t('Created At'), {
|
|
|
|
colInfo: {
|
|
|
|
label: t('Created At'),
|
2023-10-23 08:24:21 +00:00
|
|
|
type: `DateTime:${gridView.gristDoc.docModel.docInfoRow.timezone()}`,
|
2023-10-17 18:14:54 +00:00
|
|
|
isFormula: false,
|
|
|
|
formula: 'NOW()',
|
|
|
|
recalcWhen: RecalcWhen.DEFAULT,
|
|
|
|
recalcDeps: null,
|
|
|
|
},
|
|
|
|
index,
|
|
|
|
skipPopup: true,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
t("Apply to new records"),
|
|
|
|
testId('new-columns-menu-shortcuts-timestamp-new'),
|
|
|
|
),
|
|
|
|
menuItem(
|
|
|
|
async () => {
|
|
|
|
await gridView.insertColumn(t('Last Updated At'), {
|
|
|
|
colInfo: {
|
|
|
|
label: t('Last Updated At'),
|
2023-10-23 08:24:21 +00:00
|
|
|
type: `DateTime:${gridView.gristDoc.docModel.docInfoRow.timezone()}`,
|
2023-10-17 18:14:54 +00:00
|
|
|
isFormula: false,
|
|
|
|
formula: 'NOW()',
|
|
|
|
recalcWhen: RecalcWhen.MANUAL_UPDATES,
|
|
|
|
recalcDeps: null,
|
|
|
|
},
|
|
|
|
index,
|
|
|
|
skipPopup: true,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
t("Apply on record changes"),
|
|
|
|
testId('new-columns-menu-shortcuts-timestamp-change'),
|
|
|
|
),
|
2023-11-06 15:42:04 +00:00
|
|
|
], {},
|
|
|
|
t("Timestamp"),
|
|
|
|
testId('new-columns-menu-shortcuts-timestamp')
|
|
|
|
);
|
2023-10-13 13:01:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 18:14:54 +00:00
|
|
|
function buildAuthorshipMenuItems(gridView: GridView, index?: number) {
|
|
|
|
return menuItemSubmenu(() => [
|
|
|
|
menuItem(
|
|
|
|
async () => {
|
|
|
|
await gridView.insertColumn(t('Created By'), {
|
|
|
|
colInfo: {
|
|
|
|
label: t('Created By'),
|
|
|
|
type: 'Text',
|
|
|
|
isFormula: false,
|
|
|
|
formula: 'user.Name',
|
|
|
|
recalcWhen: RecalcWhen.DEFAULT,
|
|
|
|
recalcDeps: null,
|
|
|
|
},
|
|
|
|
index,
|
|
|
|
skipPopup: true,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
t("Apply to new records"),
|
|
|
|
testId('new-columns-menu-shortcuts-author-new')
|
|
|
|
),
|
|
|
|
menuItem(
|
|
|
|
async () => {
|
|
|
|
await gridView.insertColumn(t('Last Updated By'), {
|
|
|
|
colInfo: {
|
|
|
|
label: t('Last Updated By'),
|
|
|
|
type: 'Text',
|
|
|
|
isFormula: false,
|
|
|
|
formula: 'user.Name',
|
|
|
|
recalcWhen: RecalcWhen.MANUAL_UPDATES,
|
|
|
|
recalcDeps: null,
|
|
|
|
},
|
|
|
|
index,
|
|
|
|
skipPopup: true,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
t("Apply on record changes"),
|
|
|
|
testId('new-columns-menu-shortcuts-author-change')
|
|
|
|
),
|
|
|
|
], {}, t("Authorship"), testId('new-columns-menu-shortcuts-author'));
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2023-10-13 13:01:12 +00:00
|
|
|
|
2023-10-17 18:14:54 +00:00
|
|
|
function buildDetectDuplicatesMenuItems(gridView: GridView, index?: number) {
|
|
|
|
const {viewSection} = gridView;
|
|
|
|
return menuItemSubmenu(
|
|
|
|
() => searchableMenu(
|
2023-10-24 18:32:33 +00:00
|
|
|
viewSection.columns().map((col) => {
|
|
|
|
function buildFormula() {
|
|
|
|
if (isListType(col.type())) {
|
|
|
|
return `any([len(${col.table().tableId()}.lookupRecords(${col.colId()}` +
|
|
|
|
`=CONTAINS(x))) > 1 for x in $${col.colId()}])`;
|
|
|
|
} else {
|
|
|
|
return `$${col.colId()} != "" and $${col.colId()} is not None and ` +
|
|
|
|
`len(${col.table().tableId()}.lookupRecords(` +
|
|
|
|
`${col.colId()}=$${col.colId()})) > 1`;
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 18:14:54 +00:00
|
|
|
|
2023-10-24 18:32:33 +00:00
|
|
|
return {
|
|
|
|
cleanText: col.label().trim().toLowerCase(),
|
|
|
|
label: col.label(),
|
|
|
|
action: async () => {
|
|
|
|
await gridView.gristDoc.docData.bundleActions(t('Adding duplicates column'), async () => {
|
|
|
|
const newColInfo = await gridView.insertColumn(
|
|
|
|
t('Duplicate in {{- label}}', {label: col.label()}),
|
|
|
|
{
|
|
|
|
colInfo: {
|
|
|
|
label: t('Duplicate in {{- label}}', {label: col.label()}),
|
|
|
|
type: 'Bool',
|
|
|
|
isFormula: true,
|
|
|
|
formula: buildFormula(),
|
|
|
|
recalcWhen: RecalcWhen.DEFAULT,
|
|
|
|
recalcDeps: null,
|
|
|
|
widgetOptions: JSON.stringify({
|
|
|
|
rulesOptions: [{
|
|
|
|
fillColor: '#ffc23d',
|
|
|
|
textColor: '#262633',
|
|
|
|
}],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
index,
|
|
|
|
skipPopup: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: do the steps below as part of the AddColumn action.
|
|
|
|
const newField = viewSection.viewFields().all()
|
|
|
|
.find(field => field.colId() === newColInfo.colId);
|
|
|
|
if (!newField) {
|
|
|
|
throw new Error(`Unable to find field for column ${newColInfo.colId}`);
|
|
|
|
}
|
2023-10-17 18:14:54 +00:00
|
|
|
|
2023-10-24 18:32:33 +00:00
|
|
|
await newField.addEmptyRule();
|
|
|
|
const newRule = newField.rulesCols()[0];
|
|
|
|
if (!newRule) {
|
|
|
|
throw new Error(`Unable to find conditional rule for field ${newField.label()}`);
|
|
|
|
}
|
2023-10-17 18:14:54 +00:00
|
|
|
|
2023-10-24 18:32:33 +00:00
|
|
|
await newRule.formula.setAndSave(`$${newColInfo.colId}`);
|
|
|
|
}, {nestInActiveBundle: true});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}),
|
2023-10-17 18:14:54 +00:00
|
|
|
{searchInputPlaceholder: t('Search columns')}
|
2023-10-13 13:01:12 +00:00
|
|
|
),
|
2023-10-17 18:14:54 +00:00
|
|
|
{allowNothingSelected: true},
|
|
|
|
t('Detect Duplicates in...'),
|
|
|
|
testId('new-columns-menu-shortcuts-duplicates'),
|
|
|
|
);
|
2023-10-13 13:01:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 18:14:54 +00:00
|
|
|
function buildUUIDMenuItem(gridView: GridView, index?: number) {
|
|
|
|
return menuItem(
|
|
|
|
async () => {
|
|
|
|
await gridView.gristDoc.docData.bundleActions(t('Adding UUID column'), async () => {
|
|
|
|
// First create a formula column so that UUIDs are computed for existing cells.
|
|
|
|
const {colRef} = await gridView.insertColumn(t('UUID'), {
|
|
|
|
colInfo: {
|
|
|
|
label: t('UUID'),
|
|
|
|
type: 'Text',
|
|
|
|
isFormula: true,
|
|
|
|
formula: 'UUID()',
|
|
|
|
recalcWhen: RecalcWhen.DEFAULT,
|
|
|
|
recalcDeps: null,
|
|
|
|
},
|
|
|
|
index,
|
|
|
|
skipPopup: true,
|
|
|
|
});
|
2023-10-13 13:01:12 +00:00
|
|
|
|
2023-10-17 18:14:54 +00:00
|
|
|
// Then convert it to a trigger formula, so that UUIDs aren't re-computed.
|
|
|
|
//
|
|
|
|
// TODO: remove this step and do it as part of the AddColumn action.
|
|
|
|
await gridView.gristDoc.convertToTrigger(colRef, 'UUID()');
|
|
|
|
}, {nestInActiveBundle: true});
|
|
|
|
},
|
2023-10-23 05:51:08 +00:00
|
|
|
withInfoTooltip(
|
|
|
|
t('UUID'),
|
|
|
|
GristTooltips.uuid(),
|
|
|
|
{variant: 'hover'}
|
|
|
|
),
|
2023-10-17 18:14:54 +00:00
|
|
|
testId('new-columns-menu-shortcuts-uuid'),
|
|
|
|
);
|
|
|
|
}
|
2023-10-13 13:01:12 +00:00
|
|
|
|
2023-11-06 15:42:04 +00:00
|
|
|
function menuLabelWithBadge(label: string, toast: string) {
|
2023-10-20 11:05:29 +00:00
|
|
|
return cssListLabel(
|
|
|
|
cssListCol(label),
|
|
|
|
cssListFun(toast));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildLookupSection(gridView: GridView, index?: number){
|
|
|
|
function suggestAggregation(col: ColumnRec) {
|
|
|
|
if (col.pureType() === 'Int' || col.pureType() === 'Numeric') {
|
|
|
|
return [
|
|
|
|
'sum', 'average', 'min', 'max',
|
|
|
|
];
|
|
|
|
} else if (col.pureType() === 'Bool') {
|
|
|
|
return [
|
|
|
|
'count', 'percent'
|
|
|
|
];
|
|
|
|
} else if (col.pureType() === 'Date' || col.pureType() === 'DateTime') {
|
|
|
|
return [
|
|
|
|
'list', 'min', 'max',
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return [
|
|
|
|
'list'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//colTypeOverload allow to change created column type if default is wrong.
|
|
|
|
function buildColumnInfo(
|
|
|
|
fun: string,
|
|
|
|
referenceToSource: string,
|
|
|
|
col: ColumnRec) {
|
|
|
|
function formula() {
|
|
|
|
switch(fun) {
|
|
|
|
case 'list': return `${referenceToSource}.${col.colId()}`;
|
|
|
|
case 'average': return `AVERAGE(${referenceToSource}.${col.colId()})`;
|
|
|
|
case 'min': return `MIN(${referenceToSource}.${col.colId()})`;
|
|
|
|
case 'max': return `MAX(${referenceToSource}.${col.colId()})`;
|
|
|
|
case 'count':
|
|
|
|
case 'sum': return `SUM(${referenceToSource}.${col.colId()})`;
|
|
|
|
case 'percent':
|
2023-11-06 15:42:04 +00:00
|
|
|
return `AVERAGE(map(int, ${referenceToSource}.${col.colId()})) if ${referenceToSource} else None`;
|
2023-10-20 11:05:29 +00:00
|
|
|
default: return `${referenceToSource}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function type() {
|
|
|
|
switch(fun) {
|
|
|
|
case 'average': return 'Numeric';
|
|
|
|
case 'min': return col.type();
|
|
|
|
case 'max': return col.type();
|
|
|
|
case 'count': return 'Int';
|
|
|
|
case 'sum': return col.type();
|
|
|
|
case 'percent': return 'Numeric';
|
|
|
|
case 'list': return 'Any';
|
|
|
|
default: return 'Any';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function widgetOptions() {
|
|
|
|
switch(fun) {
|
|
|
|
case 'percent': return {numMode: 'percent'};
|
|
|
|
default: return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
formula: formula(),
|
|
|
|
type: type(),
|
|
|
|
widgetOptions: JSON.stringify(widgetOptions()),
|
|
|
|
isFormula: true,
|
|
|
|
};
|
|
|
|
}
|
2023-10-13 13:01:12 +00:00
|
|
|
|
2023-10-20 11:05:29 +00:00
|
|
|
function buildLookupsMenuItems() {
|
|
|
|
// Function that builds a menu for one of our Ref columns, we will show all columns
|
2023-10-24 18:32:33 +00:00
|
|
|
// from the referenced table and offer to create a formula column with aggregation in case
|
|
|
|
// our column is RefList.
|
|
|
|
function buildRefColMenu(ref: ColumnRec, col: ColumnRec): SearchableMenuItem {
|
2023-10-20 11:05:29 +00:00
|
|
|
// Helper for searching for this entry.
|
|
|
|
const cleanText = col.label().trim().toLowerCase();
|
|
|
|
|
|
|
|
// Next the label we will show.
|
|
|
|
let label: string|HTMLElement;
|
|
|
|
// For Ref column we will just show the column name.
|
|
|
|
if (ref.pureType() === 'Ref') {
|
|
|
|
label = col.label();
|
|
|
|
} else {
|
|
|
|
// For RefList column we will show the column name and the aggregation function which is the first
|
|
|
|
// on of suggested action (and a default action).
|
2023-11-06 15:42:04 +00:00
|
|
|
label = menuLabelWithBadge(col.label(), suggestAggregation(col)[0]);
|
2023-10-20 11:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
cleanText,
|
|
|
|
builder: buildItem
|
|
|
|
};
|
|
|
|
|
|
|
|
function buildItem() {
|
|
|
|
if (ref.pureType() === 'Ref') {
|
|
|
|
// Just insert a plain menu item that will insert a formula column with lookup.
|
2023-11-06 15:42:04 +00:00
|
|
|
return menuItemTrimmed(
|
|
|
|
() => insertPlainLookup(), col.label(),
|
|
|
|
testId(`new-columns-menu-lookup-column`),
|
|
|
|
testId(`new-columns-menu-lookup-column-${col.colId()}`),
|
2023-10-20 11:05:29 +00:00
|
|
|
);
|
2023-11-06 15:42:04 +00:00
|
|
|
} else {
|
|
|
|
// Depending on the number of aggregation functions we will either create a plain menu item
|
|
|
|
// or submenu with all the functions.
|
|
|
|
const functions = suggestAggregation(col);
|
|
|
|
if (functions.length === 1) {
|
|
|
|
const action = () => insertAggLookup(functions[0]);
|
|
|
|
return menuItem(action, label,
|
|
|
|
testId(`new-columns-menu-lookup-column`),
|
|
|
|
testId(`new-columns-menu-lookup-column-${col.colId()}`)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return menuItemSubmenu(
|
|
|
|
() => functions.map((fun) => menuItem(
|
|
|
|
() => insertAggLookup(fun), fun,
|
|
|
|
testId(`new-columns-menu-lookup-submenu-function`),
|
|
|
|
testId(`new-columns-menu-lookup-submenu-function-${fun}`),
|
|
|
|
)),
|
|
|
|
{
|
|
|
|
action: () => insertAggLookup(suggestAggregation(col)[0]),
|
|
|
|
},
|
|
|
|
label,
|
|
|
|
testId(`new-columns-menu-lookup-submenu`),
|
|
|
|
testId(`new-columns-menu-lookup-submenu-${col.colId()}`),
|
|
|
|
);
|
|
|
|
}
|
2023-10-20 11:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertAggLookup(fun: string) {
|
|
|
|
return gridView.insertColumn(`${ref.label()}_${col.label()}`, {
|
|
|
|
colInfo: {
|
|
|
|
label: `${ref.label()}_${col.label()}`,
|
|
|
|
...buildColumnInfo(
|
|
|
|
fun,
|
|
|
|
`$${ref.colId()}`,
|
|
|
|
col,
|
|
|
|
),
|
|
|
|
recalcDeps: null,
|
|
|
|
},
|
|
|
|
index,
|
|
|
|
skipPopup: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertPlainLookup() {
|
|
|
|
return gridView.insertColumn(`${ref.label()}_${col.label()}`, {
|
|
|
|
colInfo: {
|
|
|
|
label: `${ref.label()}_${col.label()}`,
|
|
|
|
isFormula: true,
|
|
|
|
formula: `$${ref.colId()}.${col.colId()}`,
|
|
|
|
recalcDeps: null,
|
|
|
|
type: col.type(),
|
|
|
|
widgetOptions: col.cleanWidgetOptionsJson()
|
|
|
|
},
|
|
|
|
index,
|
|
|
|
skipPopup: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-10-24 18:32:33 +00:00
|
|
|
|
2023-10-20 11:05:29 +00:00
|
|
|
const {viewSection} = gridView;
|
|
|
|
const columns = viewSection.columns();
|
|
|
|
const onlyRefOrRefList = (c: ColumnRec) => c.pureType() === 'Ref' || c.pureType() === 'RefList';
|
|
|
|
const references = columns.filter(onlyRefOrRefList);
|
|
|
|
|
|
|
|
return references.map((ref) => menuItemSubmenu(
|
2023-10-24 18:32:33 +00:00
|
|
|
() => searchableMenu(
|
|
|
|
ref.refTable()?.visibleColumns().map(buildRefColMenu.bind(null, ref)) ?? [],
|
|
|
|
{
|
|
|
|
searchInputPlaceholder: t('Search columns')
|
|
|
|
}
|
|
|
|
),
|
|
|
|
{allowNothingSelected: true},
|
|
|
|
`${ref.refTable()?.tableNameDef()} [${ref.label()}]`,
|
2023-11-06 15:42:04 +00:00
|
|
|
testId(`new-columns-menu-lookup-${ref.colId()}`),
|
|
|
|
testId(`new-columns-menu-lookup`),
|
2023-10-24 18:32:33 +00:00
|
|
|
));
|
2023-10-20 11:05:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 18:32:33 +00:00
|
|
|
interface RefTable {
|
|
|
|
tableId: string,
|
|
|
|
tableName: string,
|
|
|
|
columns: ColumnRec[],
|
|
|
|
referenceFields: ColumnRec[]
|
|
|
|
}
|
2023-10-20 11:05:29 +00:00
|
|
|
|
|
|
|
function buildReverseLookupsMenuItems() {
|
2023-11-06 15:42:04 +00:00
|
|
|
|
2023-10-24 18:32:33 +00:00
|
|
|
const getReferencesToThisTable = (): RefTable[] => {
|
2023-10-20 11:05:29 +00:00
|
|
|
const {viewSection} = gridView;
|
2023-10-24 18:32:33 +00:00
|
|
|
const otherTables = gridView.gristDoc.docModel.allTables.all().filter((tab) =>
|
|
|
|
tab.summarySourceTable() === 0 && tab.tableId.peek() !== viewSection.tableId());
|
2023-10-20 11:05:29 +00:00
|
|
|
return otherTables.map((tab) => {
|
|
|
|
return {
|
|
|
|
tableId: tab.tableId(),
|
2023-10-24 18:32:33 +00:00
|
|
|
tableName: tab.tableNameDef(),
|
2023-10-20 11:05:29 +00:00
|
|
|
columns: tab.visibleColumns(),
|
|
|
|
referenceFields:
|
2023-11-06 15:42:04 +00:00
|
|
|
tab.visibleColumns.peek().filter((c) => (c.pureType() === 'Ref' || c.pureType() == 'RefList') &&
|
2023-10-20 11:05:29 +00:00
|
|
|
c.refTable()?.tableId() === viewSection.tableId())
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.filter((tab) => tab.referenceFields.length > 0);
|
|
|
|
};
|
|
|
|
|
2023-11-06 15:42:04 +00:00
|
|
|
const insertColumn = async (tab: RefTable, col: ColumnRec, refCol: ColumnRec, aggregate: string) => {
|
|
|
|
const formula =
|
|
|
|
`${tab.tableId}.lookupRecords(${refCol.colId()}=${refCol.pureType() == 'RefList' ? 'CONTAINS($id)' : '$id'})`;
|
2023-10-20 11:05:29 +00:00
|
|
|
await gridView.insertColumn(`${tab.tableId}_${col.label()}`, {
|
|
|
|
colInfo: {
|
|
|
|
label: `${tab.tableId}_${col.label()}`,
|
|
|
|
...buildColumnInfo(aggregate,
|
|
|
|
formula,
|
|
|
|
col)
|
|
|
|
},
|
|
|
|
index,
|
|
|
|
skipPopup: true
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-11-06 15:42:04 +00:00
|
|
|
const tablesWithAnyRefColumn = getReferencesToThisTable();
|
|
|
|
return tablesWithAnyRefColumn.map((tab: RefTable) => tab.referenceFields.map((refCol) => {
|
2023-10-20 11:05:29 +00:00
|
|
|
const buildSubmenuForRevLookupMenuItem = (col: ColumnRec): SearchableMenuItem => {
|
2023-11-06 15:42:04 +00:00
|
|
|
const aggregationList = suggestAggregation(col);
|
|
|
|
const firstAggregation = aggregationList[0];
|
|
|
|
if (!firstAggregation) {
|
|
|
|
throw new Error(`No aggregation suggested for column ${col.label()}`);
|
|
|
|
}
|
2023-10-20 11:05:29 +00:00
|
|
|
return {
|
|
|
|
cleanText: col.label().trim().toLowerCase(),
|
|
|
|
builder: () => {
|
2023-11-06 15:42:04 +00:00
|
|
|
const content = menuLabelWithBadge(col.label(), firstAggregation);
|
|
|
|
// In case we have only one suggested column we will just insert it, and there is no,
|
|
|
|
// need for submenu.
|
|
|
|
if (aggregationList.length === 1) {
|
|
|
|
const action = () => insertColumn(tab, col, refCol, firstAggregation);
|
|
|
|
return menuItem(action, content, testId('new-columns-menu-revlookup-column'));
|
2023-10-20 11:05:29 +00:00
|
|
|
} else {
|
2023-11-06 15:42:04 +00:00
|
|
|
// We have some other suggested columns, we will build submenu for them.
|
|
|
|
const submenu = () => {
|
|
|
|
const items = aggregationList.map((fun) => {
|
|
|
|
const action = () => insertColumn(tab, col, refCol, fun);
|
|
|
|
return menuItem(action, fun, testId('new-columns-menu-revlookup-column-function'));
|
|
|
|
});
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
const options = {};
|
|
|
|
return menuItemSubmenu(
|
|
|
|
submenu,
|
|
|
|
options,
|
|
|
|
content,
|
|
|
|
testId('new-columns-menu-revlookup-submenu'),
|
|
|
|
);
|
2023-10-20 11:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2023-11-06 15:42:04 +00:00
|
|
|
const label = `${tab.tableName} [← ${refCol.label()}]`;
|
|
|
|
const options = {allowNothingSelected: true};
|
|
|
|
const submenu = () => {
|
|
|
|
const subItems = tab.columns.map(buildSubmenuForRevLookupMenuItem);
|
|
|
|
return searchableMenu(subItems, {searchInputPlaceholder: t('Search columns')});
|
|
|
|
};
|
|
|
|
return menuItemSubmenu(submenu, options, label, testId('new-columns-menu-revlookup'));
|
|
|
|
}));
|
2023-10-20 11:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const lookupMenu = buildLookupsMenuItems();
|
|
|
|
const reverseLookupMenu = buildReverseLookupsMenuItems();
|
|
|
|
|
|
|
|
const menuContent = (lookupMenu.length === 0 && reverseLookupMenu.length === 0)
|
|
|
|
? [ menuText(
|
|
|
|
t('No reference columns.'),
|
|
|
|
testId('new-columns-menu-lookups-none'),
|
|
|
|
)]
|
|
|
|
: [lookupMenu, reverseLookupMenu];
|
|
|
|
|
|
|
|
return [
|
2023-10-23 05:51:08 +00:00
|
|
|
menuDivider(),
|
|
|
|
menuSubHeader(
|
|
|
|
withInfoTooltip(
|
|
|
|
t('Lookups'),
|
|
|
|
GristTooltips.lookups(),
|
|
|
|
{variant: 'hover'}
|
|
|
|
),
|
|
|
|
testId('new-columns-menu-lookups'),
|
|
|
|
),
|
2023-10-20 11:05:29 +00:00
|
|
|
...menuContent
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-02-07 14:09:23 +00:00
|
|
|
export interface IMultiColumnContextMenu {
|
2021-03-05 15:17:07 +00:00
|
|
|
// For multiple selection, true/false means the value applies to all columns, 'mixed' means it's
|
|
|
|
// true for some columns, but not all.
|
|
|
|
numColumns: number;
|
2021-06-18 09:22:27 +00:00
|
|
|
numFrozen: number;
|
2022-10-17 09:47:16 +00:00
|
|
|
disableModify: boolean|'mixed'; // If the columns are read-only. Mixed for multiple columns where some are read-only.
|
2021-03-05 15:17:07 +00:00
|
|
|
isReadonly: boolean;
|
2022-02-07 14:02:26 +00:00
|
|
|
isRaw: boolean;
|
2021-03-05 15:17:07 +00:00
|
|
|
isFiltered: boolean; // If this view shows a proper subset of all rows in the table.
|
|
|
|
isFormula: boolean|'mixed';
|
2021-06-18 09:22:27 +00:00
|
|
|
columnIndices: number[];
|
|
|
|
totalColumnCount: number;
|
|
|
|
disableFrozenMenu: boolean;
|
2021-03-05 15:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IColumnContextMenu extends IMultiColumnContextMenu {
|
2020-10-02 15:10:00 +00:00
|
|
|
filterOpenFunc: () => void;
|
2021-11-03 11:44:28 +00:00
|
|
|
sortSpec: Sort.SortSpec;
|
2020-10-02 15:10:00 +00:00
|
|
|
colId: number;
|
2021-03-05 15:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function calcFieldsCondition(fields: ViewFieldRec[], condition: (f: ViewFieldRec) => boolean): boolean|"mixed" {
|
|
|
|
return fields.every(condition) ? true : (fields.some(condition) ? "mixed" : false);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 18:14:54 +00:00
|
|
|
export function buildColumnContextMenu(options: IColumnContextMenu) {
|
2022-09-30 15:34:53 +00:00
|
|
|
const { disableModify, filterOpenFunc, colId, sortSpec, isReadonly } = options;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-03-05 15:17:07 +00:00
|
|
|
const disableForReadonlyColumn = dom.cls('disabled', Boolean(disableModify) || isReadonly);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-03-05 15:17:07 +00:00
|
|
|
const addToSortLabel = getAddToSortLabel(sortSpec, colId);
|
2021-06-18 09:22:27 +00:00
|
|
|
|
2021-03-05 15:17:07 +00:00
|
|
|
return [
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.fieldTabOpen, t("Column Options")),
|
|
|
|
menuItem(filterOpenFunc, t("Filter Data")),
|
2021-03-05 15:17:07 +00:00
|
|
|
menuDivider({style: 'margin-bottom: 0;'}),
|
|
|
|
cssRowMenuItem(
|
|
|
|
customMenuItem(
|
|
|
|
allCommands.sortAsc.run,
|
2022-12-06 13:57:29 +00:00
|
|
|
dom('span', t("Sort"), {style: 'flex: 1 0 auto; margin-right: 8px;'},
|
2021-03-05 15:17:07 +00:00
|
|
|
testId('sort-label')),
|
|
|
|
icon('Sort', dom.style('transform', 'scaley(-1)')),
|
|
|
|
'A-Z',
|
|
|
|
dom.style('flex', ''),
|
2021-11-03 11:44:28 +00:00
|
|
|
cssCustomMenuItem.cls('-selected', Sort.containsOnly(sortSpec, colId, Sort.ASC)),
|
2021-03-05 15:17:07 +00:00
|
|
|
testId('sort-asc'),
|
|
|
|
),
|
|
|
|
customMenuItem(
|
|
|
|
allCommands.sortDesc.run,
|
|
|
|
icon('Sort'),
|
|
|
|
'Z-A',
|
2021-11-03 11:44:28 +00:00
|
|
|
cssCustomMenuItem.cls('-selected', Sort.containsOnly(sortSpec, colId, Sort.DESC)),
|
2021-03-05 15:17:07 +00:00
|
|
|
testId('sort-dsc'),
|
|
|
|
),
|
|
|
|
testId('sort'),
|
|
|
|
),
|
|
|
|
addToSortLabel ? [
|
2020-10-02 15:10:00 +00:00
|
|
|
cssRowMenuItem(
|
|
|
|
customMenuItem(
|
2021-03-05 15:17:07 +00:00
|
|
|
allCommands.addSortAsc.run,
|
|
|
|
cssRowMenuLabel(addToSortLabel, testId('add-to-sort-label')),
|
2020-10-02 15:10:00 +00:00
|
|
|
icon('Sort', dom.style('transform', 'scaley(-1)')),
|
|
|
|
'A-Z',
|
2021-11-03 11:44:28 +00:00
|
|
|
cssCustomMenuItem.cls('-selected', Sort.contains(sortSpec, colId, Sort.ASC)),
|
2021-03-05 15:17:07 +00:00
|
|
|
testId('add-to-sort-asc'),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
customMenuItem(
|
2021-03-05 15:17:07 +00:00
|
|
|
allCommands.addSortDesc.run,
|
2020-10-02 15:10:00 +00:00
|
|
|
icon('Sort'),
|
|
|
|
'Z-A',
|
2021-11-03 11:44:28 +00:00
|
|
|
cssCustomMenuItem.cls('-selected', Sort.contains(sortSpec, colId, Sort.DESC)),
|
2021-03-05 15:17:07 +00:00
|
|
|
testId('add-to-sort-dsc'),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
2021-03-05 15:17:07 +00:00
|
|
|
testId('add-to-sort'),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
2021-03-05 15:17:07 +00:00
|
|
|
] : null,
|
2021-11-03 11:44:28 +00:00
|
|
|
menuDivider({style: 'margin-bottom: 0; margin-top: 0;'}),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItem(allCommands.sortFilterTabOpen.run, t("More sort options ..."), testId('more-sort-options')),
|
2021-11-03 11:44:28 +00:00
|
|
|
menuDivider({style: 'margin-top: 0;'}),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.renameField, t("Rename column"), disableForReadonlyColumn),
|
2021-06-18 09:22:27 +00:00
|
|
|
freezeMenuItemCmd(options),
|
2021-03-05 15:17:07 +00:00
|
|
|
menuDivider(),
|
2023-10-17 18:14:54 +00:00
|
|
|
buildMultiColumnMenu((options.disableFrozenMenu = true, options)),
|
2021-03-05 15:17:07 +00:00
|
|
|
testId('column-menu'),
|
|
|
|
];
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 15:17:07 +00:00
|
|
|
/**
|
|
|
|
* Note about available options. There is a difference between clearing values (writing empty
|
|
|
|
* string, which makes cells blank, including Numeric cells) and converting a column to an empty
|
|
|
|
* column (i.e. column with empty formula; in this case a Numeric column becomes all 0s today).
|
|
|
|
*
|
|
|
|
* We offer both options if data columns are selected. If only formulas, only the second option
|
|
|
|
* makes sense.
|
|
|
|
*/
|
2023-10-17 18:14:54 +00:00
|
|
|
export function buildMultiColumnMenu(options: IMultiColumnContextMenu) {
|
2021-03-05 15:17:07 +00:00
|
|
|
const disableForReadonlyColumn = dom.cls('disabled', Boolean(options.disableModify) || options.isReadonly);
|
|
|
|
const disableForReadonlyView = dom.cls('disabled', options.isReadonly);
|
|
|
|
const num: number = options.numColumns;
|
|
|
|
const nameClearColumns = options.isFiltered ?
|
2022-12-13 16:26:42 +00:00
|
|
|
t('Reset {{count}} entire columns', {count: num}) :
|
|
|
|
t('Reset {{count}} columns', {count: num});
|
|
|
|
const nameDeleteColumns = t('Delete {{count}} columns', {count: num});
|
|
|
|
const nameHideColumns = t('Hide {{count}} columns', {count: num});
|
2021-06-18 09:22:27 +00:00
|
|
|
const frozenMenu = options.disableFrozenMenu ? null : freezeMenuItemCmd(options);
|
2020-10-02 15:10:00 +00:00
|
|
|
return [
|
2021-06-18 09:22:27 +00:00
|
|
|
frozenMenu ? [frozenMenu, menuDivider()]: null,
|
2021-03-05 15:17:07 +00:00
|
|
|
// Offered only when selection includes formula columns, and converts only those.
|
|
|
|
(options.isFormula ?
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.convertFormulasToData, t("Convert formula to data"),
|
2021-03-05 15:17:07 +00:00
|
|
|
disableForReadonlyColumn) : null),
|
|
|
|
|
|
|
|
// With data columns selected, offer an additional option to clear out selected cells.
|
|
|
|
(options.isFormula !== true ?
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.clearValues, t("Clear values"), disableForReadonlyColumn) : null),
|
2021-03-05 15:17:07 +00:00
|
|
|
|
2022-09-30 15:34:53 +00:00
|
|
|
(!options.isRaw ? menuItemCmd(allCommands.hideFields, nameHideColumns, disableForReadonlyView) : null),
|
2021-03-05 15:17:07 +00:00
|
|
|
menuItemCmd(allCommands.clearColumns, nameClearColumns, disableForReadonlyColumn),
|
|
|
|
menuItemCmd(allCommands.deleteFields, nameDeleteColumns, disableForReadonlyColumn),
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
menuDivider(),
|
2023-03-24 09:16:27 +00:00
|
|
|
menuItemCmd(allCommands.insertFieldBefore, t("Insert column to the left"), disableForReadonlyView),
|
|
|
|
menuItemCmd(allCommands.insertFieldAfter, t("Insert column to the right"), disableForReadonlyView)
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-06-18 09:22:27 +00:00
|
|
|
export function freezeAction(options: IMultiColumnContextMenu): { text: string; numFrozen: number; } | null {
|
|
|
|
/**
|
|
|
|
* When user clicks last column - don't offer freezing
|
|
|
|
* When user clicks on a normal column - offer him to freeze all the columns to the
|
|
|
|
* left (inclusive).
|
|
|
|
* When user clicks on a frozen column - offer him to unfreeze all the columns to the
|
|
|
|
* right (inclusive)
|
|
|
|
* When user clicks on a set of columns then:
|
|
|
|
* - If the set of columns contains the last columns that are frozen - offer unfreezing only those columns
|
|
|
|
* - If the set of columns is right after the frozen columns or spans across - offer freezing only those columns
|
|
|
|
*
|
|
|
|
* All of the above are a single command - toggle freeze
|
|
|
|
*/
|
|
|
|
|
|
|
|
const length = options.numColumns;
|
|
|
|
|
|
|
|
// make some assertions - number of columns selected should always be > 0
|
|
|
|
if (length === 0) { return null; }
|
|
|
|
|
|
|
|
const indices = options.columnIndices;
|
|
|
|
const firstColumnIndex = indices[0];
|
|
|
|
const lastColumnIndex = indices[indices.length - 1];
|
|
|
|
const numFrozen = options.numFrozen;
|
|
|
|
|
|
|
|
// if set has last column in it - don't offer freezing
|
|
|
|
if (lastColumnIndex == options.totalColumnCount - 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isNormalColumn = length === 1 && (firstColumnIndex + 1) > numFrozen;
|
|
|
|
const isFrozenColumn = length === 1 && (firstColumnIndex+ 1) <= numFrozen;
|
|
|
|
const isSet = length > 1;
|
|
|
|
const isLastFrozenSet = isSet && lastColumnIndex + 1 === numFrozen;
|
|
|
|
const isFirstNormalSet = isSet && firstColumnIndex === numFrozen;
|
|
|
|
const isSpanSet = isSet && firstColumnIndex <= numFrozen && lastColumnIndex >= numFrozen;
|
|
|
|
|
|
|
|
let text = '';
|
|
|
|
|
|
|
|
if (!isSet) {
|
|
|
|
if (isNormalColumn) {
|
|
|
|
// text to show depends on what user selected and how far are we from
|
|
|
|
// last frozen column
|
|
|
|
|
|
|
|
// if user clicked the first column or a column just after frozen set
|
|
|
|
if (firstColumnIndex === 0 || firstColumnIndex === numFrozen) {
|
2022-12-06 16:23:29 +00:00
|
|
|
text = t('Freeze {{count}} columns', {count: 1});
|
2021-06-18 09:22:27 +00:00
|
|
|
} else {
|
|
|
|
// else user clicked any other column that is farther, offer to freeze
|
|
|
|
// proper number of column
|
|
|
|
const properNumber = firstColumnIndex - numFrozen + 1;
|
2022-12-06 16:23:29 +00:00
|
|
|
text = numFrozen ?
|
|
|
|
t('Freeze {{count}} more columns', {count: properNumber}) :
|
|
|
|
t('Freeze {{count}} columns', {count: properNumber});
|
2021-06-18 09:22:27 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
text,
|
|
|
|
numFrozen : firstColumnIndex + 1
|
|
|
|
};
|
|
|
|
} else if (isFrozenColumn) {
|
|
|
|
// when user clicked last column in frozen set - offer to unfreeze this column
|
|
|
|
if (firstColumnIndex + 1 === numFrozen) {
|
2022-12-06 16:23:29 +00:00
|
|
|
text = t('Unfreeze {{count}} columns', {count: 1});
|
2021-06-18 09:22:27 +00:00
|
|
|
} else {
|
|
|
|
// else user clicked column that is not the last in a frozen set
|
|
|
|
// offer to unfreeze proper number of columns
|
|
|
|
const properNumber = numFrozen - firstColumnIndex;
|
2022-12-06 16:23:29 +00:00
|
|
|
text = properNumber === numFrozen ?
|
|
|
|
t('Unfreeze all columns') :
|
2022-12-13 16:26:42 +00:00
|
|
|
t('Unfreeze {{count}} columns', {count: properNumber});
|
2021-06-18 09:22:27 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
text,
|
|
|
|
numFrozen : indices[0]
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isLastFrozenSet) {
|
2022-12-13 16:26:42 +00:00
|
|
|
text = t('Unfreeze {{count}} columns', {count: length});
|
2021-06-18 09:22:27 +00:00
|
|
|
return {
|
|
|
|
text,
|
|
|
|
numFrozen : numFrozen - length
|
|
|
|
};
|
|
|
|
} else if (isFirstNormalSet) {
|
2022-12-13 16:26:42 +00:00
|
|
|
text = t('Freeze {{count}} columns', {count: length});
|
2021-06-18 09:22:27 +00:00
|
|
|
return {
|
|
|
|
text,
|
|
|
|
numFrozen : numFrozen + length
|
|
|
|
};
|
|
|
|
} else if (isSpanSet) {
|
|
|
|
const toFreeze = lastColumnIndex + 1 - numFrozen;
|
2022-12-06 16:23:29 +00:00
|
|
|
text = t('Freeze {{count}} more columns', {count: toFreeze});
|
2021-06-18 09:22:27 +00:00
|
|
|
return {
|
|
|
|
text,
|
|
|
|
numFrozen : numFrozen + toFreeze
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function freezeMenuItemCmd(options: IMultiColumnContextMenu) {
|
|
|
|
// calculate action available for this options
|
|
|
|
const toggle = freezeAction(options);
|
|
|
|
// if we can't offer freezing - don't create a menu at all
|
|
|
|
// this shouldn't happen - as current design offers some action on every column
|
|
|
|
if (!toggle) { return null; }
|
|
|
|
// create menu item if we have something to offer
|
|
|
|
return menuItemCmd(allCommands.toggleFreeze, toggle.text);
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Returns 'Add to sort' is there are columns in the sort spec but colId is not part of it. Returns
|
|
|
|
// undefined if colId is the only column in the spec. Otherwise returns `Sorted (#N)` where #N is
|
|
|
|
// the position (1 based) of colId in the spec.
|
2021-11-03 11:44:28 +00:00
|
|
|
function getAddToSortLabel(sortSpec: Sort.SortSpec, colId: number): string|undefined {
|
|
|
|
const columnsInSpec = sortSpec.map((n) =>Sort.getColRef(n));
|
2020-10-02 15:10:00 +00:00
|
|
|
if (sortSpec.length !== 0 && !isEqual(columnsInSpec, [colId])) {
|
|
|
|
const index = columnsInSpec.indexOf(colId);
|
|
|
|
if (index > -1) {
|
2022-12-06 16:23:29 +00:00
|
|
|
return t("Sorted (#{{count}})", {count: index + 1});
|
2020-10-02 15:10:00 +00:00
|
|
|
} else {
|
2022-12-06 13:57:29 +00:00
|
|
|
return t("Add to sort");
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssRowMenuItem = styled((...args: DomElementArg[]) => dom('li', {tabindex: '-1'}, ...args), `
|
|
|
|
display: flex;
|
|
|
|
outline: none;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssRowMenuLabel = styled('div', `
|
|
|
|
margin-right: 8px;
|
|
|
|
flex: 1 0 auto;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssCustomMenuItem = styled('div', `
|
|
|
|
padding: 8px 8px;
|
|
|
|
display: flex;
|
|
|
|
&:not(:hover) {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuBg};
|
|
|
|
color: ${theme.menuItemFg};
|
|
|
|
--icon-color: ${theme.menuItemFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
&:last-of-type {
|
|
|
|
padding-right: 24px;
|
|
|
|
flex: 0 0 auto;
|
|
|
|
}
|
|
|
|
&:first-of-type {
|
|
|
|
padding-left: 24px;
|
|
|
|
flex: 1 0 auto;
|
|
|
|
}
|
|
|
|
&-selected, &-selected:not(:hover) {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuItemSelectedBg};
|
|
|
|
color: ${theme.menuItemSelectedFg};
|
|
|
|
--icon-color: ${theme.menuItemSelectedFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
function customMenuItem(action: () => void, ...args: DomElementArg[]) {
|
|
|
|
const element: HTMLElement = cssCustomMenuItem(
|
|
|
|
...args,
|
|
|
|
dom.on('click', () => action()),
|
|
|
|
);
|
|
|
|
return element;
|
|
|
|
}
|
2023-10-20 11:05:29 +00:00
|
|
|
|
|
|
|
const cssListLabel = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
align-items: baseline;
|
|
|
|
flex: 1;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssListCol = styled('div', `
|
|
|
|
flex: 1 0 auto;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssListFun = styled('div', `
|
|
|
|
flex: 0 0 auto;
|
|
|
|
margin-left: 8px;
|
|
|
|
text-transform: lowercase;
|
|
|
|
padding: 1px 4px;
|
|
|
|
border-radius: 3px;
|
|
|
|
background-color: ${theme.choiceTokenBg};
|
|
|
|
font-size: ${vars.xsmallFontSize};
|
|
|
|
min-width: 28px;
|
|
|
|
text-align: center;
|
|
|
|
.${weasel.cssMenuItem.className}-sel & {
|
|
|
|
color: ${theme.choiceTokenFg};
|
|
|
|
}
|
|
|
|
`);
|