2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {allCommands} from 'app/client/components/commands';
|
2022-02-07 14:02:26 +00:00
|
|
|
import {ViewSectionRec} from 'app/client/models/DocModel';
|
2022-08-02 13:01:07 +00:00
|
|
|
import {urlState} from 'app/client/models/gristUrlState';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {testId} from 'app/client/ui2018/cssVars';
|
2021-05-27 11:06:26 +00:00
|
|
|
import {menuDivider, menuItemCmd, menuItemLink} from 'app/client/ui2018/menus';
|
2023-02-24 11:12:55 +00:00
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
|
|
|
import {dom, UseCB} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('ViewLayoutMenu');
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of menu items for a view section.
|
|
|
|
*/
|
2022-02-07 14:02:26 +00:00
|
|
|
export function makeViewLayoutMenu(viewSection: ViewSectionRec, isReadonly: boolean) {
|
2021-06-28 18:02:45 +00:00
|
|
|
const viewInstance = viewSection.viewInstance.peek()!;
|
|
|
|
const gristDoc = viewInstance.gristDoc;
|
|
|
|
|
|
|
|
// get current row index from cursor
|
|
|
|
const cursorRow = viewInstance.cursor.rowIndex.peek();
|
|
|
|
// get row id from current data
|
|
|
|
// rowId can be string - it is wrongly typed in cursor and in viewData
|
|
|
|
const rowId = (cursorRow !== null ? viewInstance.viewData.getRowId(cursorRow) : null) as string|null|number;
|
|
|
|
const isAddRow = rowId === 'new';
|
|
|
|
|
|
|
|
const contextMenu = [
|
|
|
|
menuItemCmd(allCommands.deleteRecords,
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Delete record"),
|
2021-06-28 18:02:45 +00:00
|
|
|
testId('section-delete-card'),
|
|
|
|
dom.cls('disabled', isReadonly || isAddRow)),
|
|
|
|
menuItemCmd(allCommands.copyLink,
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Copy anchor link"),
|
2021-06-28 18:02:45 +00:00
|
|
|
testId('section-card-link'),
|
|
|
|
),
|
|
|
|
menuDivider(),
|
|
|
|
];
|
|
|
|
|
2022-02-07 14:02:26 +00:00
|
|
|
const viewRec = viewSection.view();
|
2022-08-02 13:01:07 +00:00
|
|
|
const isLight = urlState().state.get().params?.style === 'light';
|
2023-01-18 19:33:45 +00:00
|
|
|
|
|
|
|
const sectionId = viewSection.table.peek().rawViewSectionRef.peek();
|
|
|
|
const anchorUrlState = viewInstance.getAnchorLinkForSection(sectionId);
|
|
|
|
anchorUrlState.hash!.popup = true;
|
|
|
|
const rawUrl = urlState().makeUrl(anchorUrlState);
|
|
|
|
|
2023-02-24 11:12:55 +00:00
|
|
|
// Count number of rendered sections on the viewLayout. Note that the layout might be detached or cleaned
|
|
|
|
// when we have an external section in the popup.
|
|
|
|
const expandedSectionCount = () => gristDoc.viewLayout?.layout.getAllLeafIds().length ?? 0 > 1;
|
|
|
|
|
|
|
|
const dontRemoveSection = () =>
|
|
|
|
!viewRec.getRowId() || viewRec.viewSections().peekLength <= 1 || isReadonly || expandedSectionCount() === 1;
|
|
|
|
|
|
|
|
const dontCollapseSection = () =>
|
|
|
|
dontRemoveSection() ||
|
|
|
|
(gristDoc.externalSectionId.get() === viewSection.getRowId()) ||
|
|
|
|
(gristDoc.maximizedSectionId.get() === viewSection.getRowId());
|
|
|
|
|
|
|
|
const showRawData = (use: UseCB) => {
|
|
|
|
return !use(viewSection.isRaw)// Don't show raw data if we're already in raw data.
|
|
|
|
&& !isLight // Don't show raw data in light mode.
|
|
|
|
;
|
|
|
|
};
|
2023-01-18 19:33:45 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
return [
|
2021-06-28 18:02:45 +00:00
|
|
|
dom.maybe((use) => ['single'].includes(use(viewSection.parentKey)), () => contextMenu),
|
2023-02-24 11:12:55 +00:00
|
|
|
dom.maybe(showRawData,
|
2023-01-18 19:33:45 +00:00
|
|
|
() => menuItemLink(
|
|
|
|
{ href: rawUrl}, t("Show raw data"), testId('show-raw-data'),
|
|
|
|
dom.on('click', (ev) => {
|
|
|
|
// Replace the current URL so that the back button works as expected (it navigates back from
|
|
|
|
// the current page).
|
|
|
|
ev.stopImmediatePropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
urlState().pushUrl(anchorUrlState, { replace: true }).catch(reportError);
|
|
|
|
})
|
|
|
|
)
|
2022-08-02 13:01:07 +00:00
|
|
|
),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.printSection, t("Print widget"), testId('print-section')),
|
2021-05-27 11:06:26 +00:00
|
|
|
menuItemLink({ href: gristDoc.getCsvLink(), target: '_blank', download: ''},
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Download as CSV"), testId('download-section')),
|
2022-09-14 18:55:44 +00:00
|
|
|
menuItemLink({ href: gristDoc.getXlsxActiveViewLink(), target: '_blank', download: ''},
|
2023-03-16 21:37:24 +00:00
|
|
|
t("Download as XLSX"), testId('download-section')),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.maybe((use) => ['detail', 'single'].includes(use(viewSection.parentKey)), () =>
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.editLayout, t("Edit Card Layout"),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.cls('disabled', isReadonly))),
|
|
|
|
|
2022-08-02 13:01:07 +00:00
|
|
|
dom.maybe(!isLight, () => [
|
|
|
|
menuDivider(),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.viewTabOpen, t("Widget options"), testId('widget-options')),
|
|
|
|
menuItemCmd(allCommands.sortFilterTabOpen, t("Advanced Sort & Filter")),
|
|
|
|
menuItemCmd(allCommands.dataSelectionTabOpen, t("Data selection")),
|
2022-08-02 13:01:07 +00:00
|
|
|
]),
|
2020-10-09 21:39:13 +00:00
|
|
|
|
|
|
|
menuDivider(),
|
2022-01-12 13:30:51 +00:00
|
|
|
dom.maybe((use) => use(viewSection.parentKey) === 'custom' && use(viewSection.hasCustomOptions), () =>
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.openWidgetConfiguration, t("Open configuration"),
|
2022-01-12 13:30:51 +00:00
|
|
|
testId('section-open-configuration')),
|
|
|
|
),
|
2023-02-24 11:12:55 +00:00
|
|
|
menuItemCmd(allCommands.collapseSection, t("Collapse widget"),
|
|
|
|
dom.cls('disabled', dontCollapseSection()),
|
|
|
|
testId('section-collapse')),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItemCmd(allCommands.deleteSection, t("Delete widget"),
|
2023-02-24 11:12:55 +00:00
|
|
|
dom.cls('disabled', dontRemoveSection()),
|
|
|
|
testId('section-delete')),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of menu items for a view section.
|
|
|
|
*/
|
|
|
|
export function makeCollapsedLayoutMenu(viewSection: ViewSectionRec, gristDoc: GristDoc) {
|
|
|
|
const isReadonly = gristDoc.isReadonly.get();
|
|
|
|
const isLight = urlState().state.get().params?.style === 'light';
|
|
|
|
const sectionId = viewSection.table.peek().rawViewSectionRef.peek();
|
|
|
|
const anchorUrlState = { hash: { sectionId, popup: true } };
|
|
|
|
const rawUrl = urlState().makeUrl(anchorUrlState);
|
|
|
|
return [
|
|
|
|
dom.maybe((use) => !use(viewSection.isRaw) && !isLight && !use(gristDoc.maximizedSectionId),
|
|
|
|
() => menuItemLink(
|
|
|
|
{ href: rawUrl}, t("Show raw data"), testId('show-raw-data'),
|
|
|
|
dom.on('click', (ev) => {
|
|
|
|
// Replace the current URL so that the back button works as expected (it navigates back from
|
|
|
|
// the current page).
|
|
|
|
ev.stopImmediatePropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
urlState().pushUrl(anchorUrlState, { replace: true }).catch(reportError);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
),
|
|
|
|
menuDivider(),
|
|
|
|
menuItemCmd(allCommands.expandSection, t("Add to page"),
|
|
|
|
dom.cls('disabled', isReadonly),
|
|
|
|
testId('section-expand')),
|
|
|
|
menuItemCmd(allCommands.deleteCollapsedSection, t("Delete widget"),
|
|
|
|
dom.cls('disabled', isReadonly),
|
2020-10-09 21:39:13 +00:00
|
|
|
testId('section-delete')),
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
}
|