2020-10-02 15:10:00 +00:00
|
|
|
import {loadUserManager} from 'app/client/lib/imports';
|
|
|
|
import {AppModel, reportError} from 'app/client/models/AppModel';
|
|
|
|
import {DocInfo, DocPageModel} from 'app/client/models/DocPageModel';
|
2020-11-26 02:29:13 +00:00
|
|
|
import {docUrl, urlState} from 'app/client/models/gristUrlState';
|
2022-10-19 23:06:05 +00:00
|
|
|
import {GristTooltips} from 'app/client/ui/GristTooltips';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {makeCopy, replaceTrunkWithFork} from 'app/client/ui/MakeCopyMenu';
|
2021-07-21 08:46:03 +00:00
|
|
|
import {sendToDrive} from 'app/client/ui/sendToDrive';
|
2022-10-19 23:06:05 +00:00
|
|
|
import {hoverTooltip, withInfoTooltip} from 'app/client/ui/tooltips';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {cssHoverCircle, cssTopBarBtn} from 'app/client/ui/TopBarCss';
|
|
|
|
import {primaryButton} from 'app/client/ui2018/buttons';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {mediaXSmall, testId, theme} from 'app/client/ui2018/cssVars';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
2020-12-09 05:59:42 +00:00
|
|
|
import {menu, menuAnnotate, menuDivider, menuIcon, menuItem, menuItemLink, menuText} from 'app/client/ui2018/menus';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {buildUrlId, parseUrlId} from 'app/common/gristUrls';
|
|
|
|
import * as roles from 'app/common/roles';
|
|
|
|
import {Document} from 'app/common/UserAPI';
|
|
|
|
import {dom, DomContents, styled} from 'grainjs';
|
2020-12-09 05:59:42 +00:00
|
|
|
import {MenuCreateFunc} from 'popweasel';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
function buildOriginalUrlId(urlId: string, isSnapshot: boolean): string {
|
|
|
|
const parts = parseUrlId(urlId);
|
|
|
|
return isSnapshot ? buildUrlId({...parts, snapshotId: undefined}) : parts.trunkId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the content of the export menu. The menu button and contents render differently for
|
|
|
|
* different modes (normal, pre-fork, fork, snapshot).
|
|
|
|
*/
|
|
|
|
export function buildShareMenuButton(pageModel: DocPageModel): DomContents {
|
|
|
|
// The menu needs pageModel.currentDoc to render the button. It further needs pageModel.gristDoc
|
|
|
|
// to render its contents, but we handle by merely skipping such content if gristDoc is not yet
|
|
|
|
// available (a user quick enough to open the menu in this state would have to re-open it).
|
|
|
|
return dom.maybe(pageModel.currentDoc, (doc) => {
|
|
|
|
const appModel = pageModel.appModel;
|
|
|
|
const saveCopy = () => makeCopy(doc, appModel, 'Save Document').catch(reportError);
|
|
|
|
if (doc.idParts.snapshotId) {
|
|
|
|
const backToCurrent = () => urlState().pushUrl({doc: buildOriginalUrlId(doc.id, true)});
|
|
|
|
return shareButton('Back to Current', () => [
|
|
|
|
menuManageUsers(doc, pageModel),
|
|
|
|
menuSaveCopy('Save Copy', doc, appModel),
|
|
|
|
menuOriginal(doc, appModel, true),
|
|
|
|
menuExports(doc, pageModel),
|
|
|
|
], {buttonAction: backToCurrent});
|
|
|
|
} else if (doc.isPreFork || doc.isBareFork) {
|
|
|
|
// A new unsaved document, or a fiddle, or a public example.
|
|
|
|
const saveActionTitle = doc.isBareFork ? 'Save Document' : 'Save Copy';
|
|
|
|
return shareButton(saveActionTitle, () => [
|
|
|
|
menuManageUsers(doc, pageModel),
|
|
|
|
menuSaveCopy(saveActionTitle, doc, appModel),
|
|
|
|
menuExports(doc, pageModel),
|
|
|
|
], {buttonAction: saveCopy});
|
|
|
|
} else if (doc.isFork) {
|
|
|
|
// For forks, the main actions are "Replace Original" and "Save Copy". When "Replace
|
|
|
|
// Original" is unavailable (for samples, forks of public docs, etc), we'll consider "Save
|
|
|
|
// Copy" primary and keep it as an action button on top. Otherwise, show a tag without a
|
|
|
|
// default action; click opens the menu where the user can choose.
|
|
|
|
if (!roles.canEdit(doc.trunkAccess || null)) {
|
|
|
|
return shareButton('Save Copy', () => [
|
|
|
|
menuManageUsers(doc, pageModel),
|
|
|
|
menuSaveCopy('Save Copy', doc, appModel),
|
|
|
|
menuOriginal(doc, appModel, false),
|
|
|
|
menuExports(doc, pageModel),
|
|
|
|
], {buttonAction: saveCopy});
|
|
|
|
} else {
|
|
|
|
return shareButton('Unsaved', () => [
|
|
|
|
menuManageUsers(doc, pageModel),
|
|
|
|
menuSaveCopy('Save Copy', doc, appModel),
|
|
|
|
menuOriginal(doc, appModel, false),
|
|
|
|
menuExports(doc, pageModel),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return shareButton(null, () => [
|
|
|
|
menuManageUsers(doc, pageModel),
|
|
|
|
menuSaveCopy('Duplicate Document', doc, appModel),
|
|
|
|
menuWorkOnCopy(pageModel),
|
|
|
|
menuExports(doc, pageModel),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the share button, possibly as a text+icon pair when buttonText is not null. The text
|
|
|
|
* portion can be an independent action button (when buttonAction is given), or simply a more
|
|
|
|
* visible extension of the icon that opens the menu.
|
|
|
|
*/
|
|
|
|
function shareButton(buttonText: string|null, menuCreateFunc: MenuCreateFunc,
|
|
|
|
options: {buttonAction?: () => void} = {},
|
|
|
|
) {
|
|
|
|
if (!buttonText) {
|
|
|
|
// Regular circular button that opens a menu.
|
|
|
|
return cssHoverCircle({ style: `margin: 5px;` },
|
2021-07-19 08:49:44 +00:00
|
|
|
cssTopBarBtn('Share', dom.cls('tour-share-icon')),
|
2020-10-02 15:10:00 +00:00
|
|
|
menu(menuCreateFunc, {placement: 'bottom-end'}),
|
2022-10-19 23:06:05 +00:00
|
|
|
hoverTooltip('Share', {key: 'topBarBtnTooltip'}),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('tb-share'),
|
|
|
|
);
|
|
|
|
} else if (options.buttonAction) {
|
|
|
|
// Split button: the left text part calls `buttonAction`, and the circular icon opens menu.
|
|
|
|
return cssShareButton(
|
|
|
|
cssShareAction(buttonText,
|
|
|
|
dom.on('click', options.buttonAction),
|
|
|
|
testId('tb-share-action'),
|
|
|
|
),
|
|
|
|
cssShareCircle(
|
|
|
|
cssShareIcon('Share'),
|
|
|
|
menu(menuCreateFunc, {placement: 'bottom-end'}),
|
2022-10-19 23:06:05 +00:00
|
|
|
hoverTooltip('Share', {key: 'topBarBtnTooltip'}),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('tb-share'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Combined button: the left text part and circular icon open the menu as a single button.
|
|
|
|
return cssShareButton(
|
|
|
|
cssShareButton.cls('-combined'),
|
|
|
|
cssShareAction(buttonText),
|
|
|
|
cssShareCircle(
|
|
|
|
cssShareIcon('Share')
|
|
|
|
),
|
|
|
|
menu(menuCreateFunc, {placement: 'bottom-end'}),
|
2022-10-19 23:06:05 +00:00
|
|
|
hoverTooltip('Share', {key: 'topBarBtnTooltip'}),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('tb-share'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renders "Manage Users" menu item.
|
|
|
|
function menuManageUsers(doc: DocInfo, pageModel: DocPageModel) {
|
|
|
|
return [
|
2022-04-12 19:31:41 +00:00
|
|
|
menuItem(() => manageUsers(doc, pageModel),
|
|
|
|
roles.canEditAccess(doc.access) ? 'Manage Users' : 'Access Details',
|
|
|
|
dom.cls('disabled', doc.isFork),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('tb-share-option')
|
|
|
|
),
|
|
|
|
menuDivider(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renders "Return to Original" and "Replace Original" menu items. When used with snapshots, we
|
|
|
|
// say "Current Version" in place of the word "Original".
|
|
|
|
function menuOriginal(doc: Document, appModel: AppModel, isSnapshot: boolean) {
|
|
|
|
const termToUse = isSnapshot ? "Current Version" : "Original";
|
|
|
|
const origUrlId = buildOriginalUrlId(doc.id, isSnapshot);
|
|
|
|
const originalUrl = urlState().makeUrl({doc: origUrlId});
|
2020-12-09 05:59:42 +00:00
|
|
|
|
|
|
|
// When comparing forks, show changes from the original to the fork. When comparing a snapshot,
|
|
|
|
// show changes from the snapshot to the original, which seems more natural. The per-snapshot
|
|
|
|
// comparison links in DocHistory use the same order.
|
|
|
|
const [leftDocId, rightDocId] = isSnapshot ? [doc.id, origUrlId] : [origUrlId, doc.id];
|
|
|
|
|
|
|
|
// Preserve the current state in order to stay on the selected page. TODO: Should auto-switch to
|
|
|
|
// first page when the requested page is not in the document.
|
|
|
|
const compareHref = dom.attr('href', (use) => urlState().makeUrl({
|
|
|
|
...use(urlState().state), doc: leftDocId, params: {compare: rightDocId}}));
|
|
|
|
|
|
|
|
const compareUrlId = urlState().state.get().params?.compare;
|
|
|
|
const comparingSnapshots: boolean = isSnapshot && Boolean(compareUrlId && parseUrlId(compareUrlId).snapshotId);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
function replaceOriginal() {
|
|
|
|
const user = appModel.currentValidUser;
|
|
|
|
replaceTrunkWithFork(user, doc, appModel, origUrlId).catch(reportError);
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
cssMenuSplitLink({href: originalUrl},
|
|
|
|
cssMenuSplitLinkText(`Return to ${termToUse}`), testId('return-to-original'),
|
|
|
|
cssMenuIconLink({href: originalUrl, target: '_blank'}, testId('open-original'),
|
|
|
|
cssMenuIcon('FieldLink'),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
menuItem(replaceOriginal, `Replace ${termToUse}...`,
|
2020-12-09 05:59:42 +00:00
|
|
|
// Disable if original is not writable, and also when comparing snapshots (since it's
|
|
|
|
// unclear which of the versions to use).
|
|
|
|
dom.cls('disabled', !roles.canEdit(doc.trunkAccess || null) || comparingSnapshots),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('replace-original'),
|
|
|
|
),
|
2020-12-09 05:59:42 +00:00
|
|
|
menuItemLink(compareHref, {target: '_blank'}, `Compare to ${termToUse}`,
|
|
|
|
menuAnnotate('Beta'),
|
2020-11-11 20:25:37 +00:00
|
|
|
testId('compare-original'),
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renders "Save Copy..." and "Copy as Template..." menu items. The name of the first action is
|
|
|
|
// specified in saveActionTitle.
|
|
|
|
function menuSaveCopy(saveActionTitle: string, doc: Document, appModel: AppModel) {
|
|
|
|
const saveCopy = () => makeCopy(doc, appModel, saveActionTitle).catch(reportError);
|
|
|
|
return [
|
|
|
|
// TODO Disable these when user has no accessible destinations.
|
|
|
|
menuItem(saveCopy, `${saveActionTitle}...`, testId('save-copy')),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renders "Work on a Copy" menu item.
|
|
|
|
function menuWorkOnCopy(pageModel: DocPageModel) {
|
|
|
|
const gristDoc = pageModel.gristDoc.get();
|
|
|
|
if (!gristDoc) { return null; }
|
|
|
|
|
|
|
|
const makeUnsavedCopy = async function() {
|
|
|
|
const {urlId} = await gristDoc.docComm.fork();
|
|
|
|
await urlState().pushUrl({doc: urlId});
|
|
|
|
};
|
|
|
|
|
|
|
|
return [
|
|
|
|
menuItem(makeUnsavedCopy, 'Work on a Copy', testId('work-on-copy')),
|
2022-10-19 23:06:05 +00:00
|
|
|
menuText(
|
|
|
|
withInfoTooltip(
|
|
|
|
'Edit without affecting the original',
|
|
|
|
GristTooltips.workOnACopy(),
|
|
|
|
{tooltipMenuOptions: {attach: null}}
|
|
|
|
)
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The part of the menu with "Download" and "Export CSV" items.
|
|
|
|
*/
|
|
|
|
function menuExports(doc: Document, pageModel: DocPageModel) {
|
|
|
|
const isElectron = (window as any).isRunningUnderElectron;
|
|
|
|
const gristDoc = pageModel.gristDoc.get();
|
|
|
|
if (!gristDoc) { return null; }
|
|
|
|
|
|
|
|
// Note: This line adds the 'show in folder' option for electron and a download option for hosted.
|
|
|
|
return [
|
|
|
|
menuDivider(),
|
|
|
|
(isElectron ?
|
|
|
|
menuItem(() => gristDoc.app.comm.showItemInFolder(doc.name),
|
|
|
|
'Show in folder', testId('tb-share-option')) :
|
2020-10-19 16:13:20 +00:00
|
|
|
menuItemLink({
|
|
|
|
href: pageModel.appModel.api.getDocAPI(doc.id).getDownloadUrl(),
|
|
|
|
target: '_blank', download: ''
|
|
|
|
},
|
2020-10-02 15:10:00 +00:00
|
|
|
menuIcon('Download'), 'Download', testId('tb-share-option'))
|
|
|
|
),
|
|
|
|
menuItemLink({ href: gristDoc.getCsvLink(), target: '_blank', download: ''},
|
|
|
|
menuIcon('Download'), 'Export CSV', testId('tb-share-option')),
|
2021-09-01 21:07:53 +00:00
|
|
|
menuItemLink({
|
|
|
|
href: pageModel.appModel.api.getDocAPI(doc.id).getDownloadXlsxUrl(),
|
|
|
|
target: '_blank', download: ''
|
|
|
|
}, menuIcon('Download'), 'Export XLSX', testId('tb-share-option')),
|
2021-07-21 08:46:03 +00:00
|
|
|
menuItem(() => sendToDrive(doc, pageModel),
|
|
|
|
menuIcon('Download'), 'Send to Google Drive', testId('tb-share-option')),
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the user-manager for the doc.
|
|
|
|
*/
|
|
|
|
async function manageUsers(doc: DocInfo, docPageModel: DocPageModel) {
|
|
|
|
const appModel: AppModel = docPageModel.appModel;
|
|
|
|
const api = appModel.api;
|
|
|
|
const user = appModel.currentValidUser;
|
|
|
|
(await loadUserManager()).showUserManagerModal(api, {
|
|
|
|
permissionData: api.getDocAccess(doc.id),
|
2022-05-19 04:51:48 +00:00
|
|
|
activeUser: user,
|
2020-10-02 15:10:00 +00:00
|
|
|
resourceType: 'document',
|
|
|
|
resourceId: doc.id,
|
2022-04-12 19:31:41 +00:00
|
|
|
resource: doc,
|
2020-10-02 15:10:00 +00:00
|
|
|
docPageModel,
|
2021-10-25 16:54:04 +00:00
|
|
|
appModel: docPageModel.appModel,
|
2020-11-26 02:29:13 +00:00
|
|
|
linkToCopy: urlState().makeUrl(docUrl(doc)),
|
2020-10-02 15:10:00 +00:00
|
|
|
// On save, re-fetch the document info, to toggle the "Public Access" icon if it changed.
|
2022-04-12 19:31:41 +00:00
|
|
|
// Skip if personal, since personal cannot affect "Public Access", and the only
|
|
|
|
// change possible is to remove the user (which would make refreshCurrentDoc fail)
|
|
|
|
onSave: async (personal) => !personal && docPageModel.refreshCurrentDoc(doc),
|
2021-10-25 16:54:04 +00:00
|
|
|
reload: () => api.getDocAccess(doc.id),
|
2020-10-02 15:10:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssShareButton = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
position: relative;
|
|
|
|
z-index: 0;
|
|
|
|
margin: 5px;
|
|
|
|
white-space: nowrap;
|
|
|
|
|
2022-09-06 01:51:57 +00:00
|
|
|
--share-btn-bg: ${theme.controlPrimaryBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
&-combined:hover, &-combined.weasel-popup-open {
|
2022-09-06 01:51:57 +00:00
|
|
|
--share-btn-bg: ${theme.controlPrimaryHoverBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2021-02-28 05:27:34 +00:00
|
|
|
// Hide this on very small screens, since it takes up a lot of space and its action is also
|
|
|
|
// available in the associated menu.
|
2020-10-02 15:10:00 +00:00
|
|
|
const cssShareAction = styled(primaryButton, `
|
|
|
|
margin-right: -16px;
|
|
|
|
padding-right: 24px;
|
|
|
|
background-color: var(--share-btn-bg);
|
|
|
|
border-color: var(--share-btn-bg);
|
2021-02-28 05:27:34 +00:00
|
|
|
|
|
|
|
@media ${mediaXSmall} {
|
|
|
|
& {
|
|
|
|
display: none !important;
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssShareCircle = styled(cssHoverCircle, `
|
|
|
|
z-index: 1;
|
|
|
|
background-color: var(--share-btn-bg);
|
2022-09-06 01:51:57 +00:00
|
|
|
border: 1px solid ${theme.topHeaderBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
&:hover, &.weasel-popup-open {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.controlPrimaryHoverBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssShareIcon = styled(cssTopBarBtn, `
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.controlPrimaryFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
height: 30px;
|
|
|
|
width: 30px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuSplitLink = styled(menuItemLink, `
|
|
|
|
padding: 0;
|
|
|
|
align-items: stretch;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuSplitLinkText = styled('div', `
|
|
|
|
flex: auto;
|
|
|
|
padding: var(--weaseljs-menu-item-padding, 8px 24px);
|
|
|
|
&:not(:hover) {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuBg};
|
|
|
|
color: ${theme.menuItemFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuIconLink = styled('a', `
|
|
|
|
display: block;
|
|
|
|
flex: none;
|
|
|
|
padding: 8px 24px;
|
|
|
|
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuBg};
|
|
|
|
--icon-color: ${theme.menuItemLinkFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
&:hover {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.menuItemLinkselectedBg};
|
|
|
|
--icon-color: ${theme.menuItemLinkSelectedFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuIcon = styled(icon, `
|
|
|
|
display: block;
|
|
|
|
`);
|